1# Copyright 2018 The Amber Authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15cmake_minimum_required(VERSION 3.0) 16if (POLICY CMP0048) 17 cmake_policy(SET CMP0048 NEW) 18endif() 19if (POLICY CMP0054) 20 # Avoid dereferencing variables or interpret keywords that have been 21 # quoted or bracketed. 22 # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html 23 cmake_policy(SET CMP0054 NEW) 24endif() 25 26project(amber) 27enable_testing() 28 29set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) 30set(CMAKE_POSITION_INDEPENDENT_CODE ON) 31if (${AMBER_USE_CLSPV}) 32 set(CMAKE_CXX_STANDARD 14) 33else() 34 set(CMAKE_CXX_STANDARD 11) 35endif() 36 37include(CheckIncludeFile) 38include(GNUInstallDirs) 39 40option(AMBER_SKIP_TESTS 41 "Skip building tests along with the library" ${AMBER_SKIP_TESTS}) 42option(AMBER_SKIP_SPIRV_TOOLS 43 "Skip building spirv-tools into the library" ${AMBER_SKIP_SPIRV_TOOLS}) 44option(AMBER_SKIP_SHADERC 45 "Skip building Shaderc into the library" ${AMBER_SKIP_SHADERC}) 46option(AMBER_SKIP_SAMPLES 47 "Skip building sample application" ${AMBER_SKIP_SAMPLES}) 48option(AMBER_SKIP_LODEPNG 49 "Skip building lodepng into the library" ${AMBER_SKIP_LODEPNG}) 50option(AMBER_USE_DXC "Enable DXC integration" ${AMBER_USE_DXC}) 51option(AMBER_USE_LOCAL_VULKAN "Build with vulkan in third_party" OFF) 52option(AMBER_USE_CLSPV "Build with Clspv support" OFF) 53option(AMBER_ENABLE_SWIFTSHADER 54 "Build using SwiftShader" ${AMBER_ENABLE_SWIFTSHADER}) 55 56if(WIN32) 57 # On Windows, CMake by default compiles with the shared CRT. 58 # Default it to the static CRT. 59 option(AMBER_ENABLE_SHARED_CRT 60 "Amber: Use the shared CRT with MSVC instead of the static CRT" 61 ${AMBER_ENABLE_SHARED_CRT}) 62endif(WIN32) 63 64if (${AMBER_SKIP_SPIRV_TOOLS}) 65 set(AMBER_ENABLE_SPIRV_TOOLS FALSE) 66 set(AMBER_ENABLE_SHADERC FALSE) 67else() 68 set(AMBER_ENABLE_SPIRV_TOOLS TRUE) 69 70 if (${AMBER_SKIP_SHADERC}) 71 set(AMBER_ENABLE_SHADERC FALSE) 72 else() 73 set(AMBER_ENABLE_SHADERC TRUE) 74 endif() 75endif() 76 77if (${AMBER_SKIP_TESTS}) 78 set(AMBER_ENABLE_TESTS FALSE) 79else() 80 set(AMBER_ENABLE_TESTS TRUE) 81endif() 82 83if (${AMBER_SKIP_SAMPLES}) 84 set(AMBER_ENABLE_SAMPLES FALSE) 85else() 86 set(AMBER_ENABLE_SAMPLES TRUE) 87endif() 88 89if (${AMBER_SKIP_LODEPNG}) 90 set(AMBER_ENABLE_LODEPNG FALSE) 91else() 92 set(AMBER_ENABLE_LODEPNG TRUE) 93endif() 94 95if (${AMBER_ENABLE_SWIFTSHADER}) 96 # Swiftshader requires the loader to be built. 97 set(AMBER_USE_LOCAL_VULKAN TRUE) 98endif() 99 100if (${AMBER_USE_DXC}) 101 set(AMBER_ENABLE_DXC TRUE) 102else() 103 set(AMBER_ENABLE_DXC FALSE) 104endif() 105 106if (${AMBER_USE_CLSPV}) 107 enable_language(ASM) 108 set(AMBER_ENABLE_CLSPV TRUE) 109 set(AMBER_ENABLE_SPIRV_TOOLS TRUE) 110else() 111 set(AMBER_ENABLE_CLSPV FALSE) 112endif() 113 114message(STATUS "Using python3") 115find_package(PythonInterp 3 REQUIRED) 116 117message(STATUS "Amber enable SPIRV-Tools: ${AMBER_ENABLE_SPIRV_TOOLS}") 118message(STATUS "Amber enable Shaderc: ${AMBER_ENABLE_SHADERC}") 119message(STATUS "Amber enable tests: ${AMBER_ENABLE_TESTS}") 120message(STATUS "Amber enable samples: ${AMBER_ENABLE_SAMPLES}") 121message(STATUS "Amber enable lodepng: ${AMBER_ENABLE_LODEPNG}") 122message(STATUS "Amber enable SwiftShader: ${AMBER_ENABLE_SWIFTSHADER}") 123message(STATUS "Amber enable DXC: ${AMBER_ENABLE_DXC}") 124message(STATUS "Amber enable Clspv: ${AMBER_ENABLE_CLSPV}") 125 126include_directories("${PROJECT_SOURCE_DIR}/include") 127include_directories("${PROJECT_SOURCE_DIR}") 128 129if (${AMBER_ENABLE_SPIRV_TOOLS}) 130 include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include") 131endif() 132 133if (NOT ANDROID) 134 include(src/dawn/find_dawn.cmake) 135endif() 136 137include(src/vulkan/find_vulkan.cmake) 138 139add_definitions(-DAMBER_CTS_VULKAN_HEADER=$<BOOL:${VULKAN_CTS_HEADER}>) 140add_definitions(-DAMBER_ENGINE_VULKAN=$<BOOL:${Vulkan_FOUND}>) 141add_definitions(-DAMBER_ENGINE_DAWN=$<BOOL:${Dawn_FOUND}>) 142add_definitions(-DAMBER_ENABLE_SPIRV_TOOLS=$<BOOL:${AMBER_ENABLE_SPIRV_TOOLS}>) 143add_definitions(-DAMBER_ENABLE_SHADERC=$<BOOL:${AMBER_ENABLE_SHADERC}>) 144add_definitions(-DAMBER_ENABLE_DXC=$<BOOL:${AMBER_ENABLE_DXC}>) 145add_definitions(-DAMBER_ENABLE_CLSPV=$<BOOL:${AMBER_ENABLE_CLSPV}>) 146add_definitions(-DAMBER_ENABLE_LODEPNG=$<BOOL:${AMBER_ENABLE_LODEPNG}>) 147 148set(CMAKE_DEBUG_POSTFIX "") 149 150# This has to be done very early so the link path will get set correctly for all 151# the various libraries and binaries. 152if (${AMBER_ENABLE_DXC}) 153 link_directories("${CMAKE_BINARY_DIR}/third_party/dxc/lib") 154 155 if (MSVC) 156 # DXC turns this off all over the place so we have to do the same. 157 add_definitions(/D_ITERATOR_DEBUG_LEVEL=0) 158 endif() 159endif() 160 161if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 162 message(STATUS "No build type selected, default to Debug") 163 set(CMAKE_BUILD_TYPE "Debug") 164endif() 165 166if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") OR 167 (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND 168 (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))) 169 set(COMPILER_IS_LIKE_GNU TRUE) 170endif() 171 172if(MSVC) 173 # We don't want to have to copy the C Runtime DLL everywhere the executable 174 # goes. So by default compile code to assume the CRT is statically linked, 175 # i.e. use /MT* options. For debug builds use /MTd, and for release builds 176 # use /MT. If AMBER_ENABLE_SHARED_CRT is ON, then use the shared C runtime. 177 # Modify the project-wide options variables. This is ugly, but seems to be 178 # the state of the art. 179 if(NOT ${AMBER_ENABLE_SHARED_CRT}) 180 message(STATUS "Amber: Static C runtime selected: replacing /MD* with /MT*") 181 foreach (flag_var 182 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 183 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO 184 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 185 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 186 string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 187 endforeach() 188 endif() 189endif() 190 191function(amber_default_compile_options TARGET) 192 if (${COMPILER_IS_LIKE_GNU}) 193 target_compile_options(${TARGET} PRIVATE 194 -std=c++11 195 -fno-exceptions 196 -fno-rtti 197 -fvisibility=hidden 198 -Wall 199 -Werror 200 -Wextra 201 -Wno-padded 202 -Wno-switch-enum 203 -Wno-unknown-pragmas 204 -pedantic-errors 205 ) 206 207 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 208 target_compile_options(${TARGET} PRIVATE 209 -Wno-c++98-compat 210 -Wno-c++98-compat-pedantic 211 -Wno-format-pedantic 212 -Wno-unknown-warning-option 213 -Weverything 214 ) 215 endif() 216 endif() 217 218 if (MSVC) 219 # Specify /EHs for exception handling. 220 target_compile_options(${TARGET} PRIVATE 221 /bigobj 222 /EHsc 223 /W3 224 /WX 225 /wd4068 226 /wd4514 227 /wd4571 228 /wd4625 229 /wd4626 230 /wd4710 231 /wd4774 232 /wd4820 233 /wd5026 234 /wd5027 235 ) 236 endif() 237 238 if (NOT ${AMBER_ENABLE_SHARED_CRT}) 239 # For MinGW cross compile, statically link to the C++ runtime. 240 # But it still depends on MSVCRT.dll. 241 if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") 242 if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") 243 set_target_properties(${TARGET} PROPERTIES LINK_FLAGS 244 -static 245 -static-libgcc 246 -static-libstdc++) 247 endif() 248 endif() 249 endif() 250endfunction() 251 252add_subdirectory(third_party) 253add_subdirectory(src) 254 255if (${AMBER_ENABLE_SAMPLES} AND NOT ANDROID) 256 add_subdirectory(samples) 257endif() 258