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.13) 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_EXPORT_COMPILE_COMMANDS ON) 30set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) 31set(CMAKE_POSITION_INDEPENDENT_CODE ON) 32 33include(CheckIncludeFile) 34include(GNUInstallDirs) 35 36option(AMBER_SKIP_TESTS 37 "Skip building tests along with the library" ${AMBER_SKIP_TESTS}) 38option(AMBER_SKIP_SPIRV_TOOLS 39 "Skip building spirv-tools into the library" ${AMBER_SKIP_SPIRV_TOOLS}) 40option(AMBER_SKIP_SHADERC 41 "Skip building Shaderc into the library" ${AMBER_SKIP_SHADERC}) 42option(AMBER_SKIP_SAMPLES 43 "Skip building sample application" ${AMBER_SKIP_SAMPLES}) 44option(AMBER_SKIP_LODEPNG 45 "Skip building lodepng into the library" ${AMBER_SKIP_LODEPNG}) 46option(AMBER_USE_DXC "Enable DXC integration" ${AMBER_USE_DXC}) 47option(AMBER_USE_LOCAL_VULKAN "Build with vulkan in third_party" OFF) 48option(AMBER_USE_CLSPV "Build with Clspv support" OFF) 49option(AMBER_ENABLE_SWIFTSHADER 50 "Build using SwiftShader" ${AMBER_ENABLE_SWIFTSHADER}) 51option(AMBER_ENABLE_RTTI 52 "Build with runtime type information" OFF) 53option(AMBER_DISABLE_WERROR "Build without the -Werror flag" ${AMBER_DISABLE_WERROR}) 54option(AMBER_DISABLE_WEVERYTHING "Build without the -Weverything flag" ${AMBER_DISABLE_WEVERYTHING}) 55 56if (${AMBER_ENABLE_VK_DEBUGGING}) 57 message(FATAL_ERROR "Amber no longer supports Vulkan debugging") 58endif() 59 60set(CMAKE_CXX_STANDARD 17) 61 62if(WIN32) 63 # On Windows, CMake by default compiles with the shared CRT. 64 # Default it to the static CRT. 65 option(AMBER_ENABLE_SHARED_CRT 66 "Amber: Use the shared CRT with MSVC instead of the static CRT" 67 ${AMBER_ENABLE_SHARED_CRT}) 68endif(WIN32) 69 70if (${AMBER_SKIP_SPIRV_TOOLS}) 71 set(AMBER_ENABLE_SPIRV_TOOLS FALSE) 72 set(AMBER_ENABLE_SHADERC FALSE) 73else() 74 set(AMBER_ENABLE_SPIRV_TOOLS TRUE) 75 76 if (${AMBER_SKIP_SHADERC}) 77 set(AMBER_ENABLE_SHADERC FALSE) 78 else() 79 set(AMBER_ENABLE_SHADERC TRUE) 80 endif() 81endif() 82 83if (${AMBER_SKIP_TESTS}) 84 set(AMBER_ENABLE_TESTS FALSE) 85else() 86 set(AMBER_ENABLE_TESTS TRUE) 87endif() 88 89if (${AMBER_SKIP_SAMPLES}) 90 set(AMBER_ENABLE_SAMPLES FALSE) 91else() 92 set(AMBER_ENABLE_SAMPLES TRUE) 93endif() 94 95if (${AMBER_SKIP_LODEPNG}) 96 set(AMBER_ENABLE_LODEPNG FALSE) 97else() 98 set(AMBER_ENABLE_LODEPNG TRUE) 99endif() 100 101if (${AMBER_ENABLE_SWIFTSHADER}) 102 # Swiftshader requires the loader to be built. 103 set(AMBER_USE_LOCAL_VULKAN TRUE) 104endif() 105 106if (${AMBER_USE_DXC}) 107 set(AMBER_ENABLE_DXC TRUE) 108else() 109 set(AMBER_ENABLE_DXC FALSE) 110endif() 111 112if (${AMBER_USE_CLSPV}) 113 set(AMBER_ENABLE_CLSPV TRUE) 114 set(AMBER_ENABLE_SPIRV_TOOLS TRUE) 115else() 116 set(AMBER_ENABLE_CLSPV FALSE) 117endif() 118 119if (${AMBER_USE_CLSPV} OR ${AMBER_ENABLE_SWIFTSHADER}) 120 enable_language(ASM) 121endif() 122 123message(STATUS "Using python3") 124find_package(Python3 REQUIRED) 125 126message(STATUS "Amber enable SPIRV-Tools: ${AMBER_ENABLE_SPIRV_TOOLS}") 127message(STATUS "Amber enable Shaderc: ${AMBER_ENABLE_SHADERC}") 128message(STATUS "Amber enable tests: ${AMBER_ENABLE_TESTS}") 129message(STATUS "Amber enable samples: ${AMBER_ENABLE_SAMPLES}") 130message(STATUS "Amber enable lodepng: ${AMBER_ENABLE_LODEPNG}") 131message(STATUS "Amber enable SwiftShader: ${AMBER_ENABLE_SWIFTSHADER}") 132message(STATUS "Amber enable DXC: ${AMBER_ENABLE_DXC}") 133message(STATUS "Amber enable Clspv: ${AMBER_ENABLE_CLSPV}") 134message(STATUS "Amber enable RTTI: ${AMBER_ENABLE_RTTI}") 135 136include_directories("${PROJECT_SOURCE_DIR}/include") 137include_directories("${PROJECT_SOURCE_DIR}") 138 139if (${AMBER_ENABLE_SPIRV_TOOLS}) 140 include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include") 141endif() 142 143if (NOT ANDROID) 144 include(src/dawn/find_dawn.cmake) 145endif() 146 147include(src/vulkan/find_vulkan.cmake) 148 149add_definitions(-DAMBER_CTS_VULKAN_HEADER=$<BOOL:${VULKAN_CTS_HEADER}>) 150add_definitions(-DAMBER_ENGINE_VULKAN=$<BOOL:${Vulkan_FOUND}>) 151add_definitions(-DAMBER_ENGINE_DAWN=$<BOOL:${Dawn_FOUND}>) 152add_definitions(-DAMBER_ENABLE_SPIRV_TOOLS=$<BOOL:${AMBER_ENABLE_SPIRV_TOOLS}>) 153add_definitions(-DAMBER_ENABLE_SHADERC=$<BOOL:${AMBER_ENABLE_SHADERC}>) 154add_definitions(-DAMBER_ENABLE_DXC=$<BOOL:${AMBER_ENABLE_DXC}>) 155add_definitions(-DAMBER_ENABLE_CLSPV=$<BOOL:${AMBER_ENABLE_CLSPV}>) 156add_definitions(-DAMBER_ENABLE_LODEPNG=$<BOOL:${AMBER_ENABLE_LODEPNG}>) 157add_definitions(-DAMBER_ENABLE_RTTI=$<BOOL:${AMBER_ENABLE_RTTI}>) 158 159set(CMAKE_DEBUG_POSTFIX "") 160 161# This has to be done very early so the link path will get set correctly for all 162# the various libraries and binaries. 163if (${AMBER_ENABLE_DXC}) 164 link_directories("${CMAKE_BINARY_DIR}/third_party/dxc/lib") 165 166 if (MSVC) 167 # DXC turns this off all over the place so we have to do the same. 168 add_definitions(/D_ITERATOR_DEBUG_LEVEL=0) 169 endif() 170endif() 171 172if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 173 message(STATUS "No build type selected, default to Debug") 174 set(CMAKE_BUILD_TYPE "Debug") 175endif() 176 177if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") OR 178 (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND 179 (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))) 180 set(COMPILER_IS_LIKE_GNU TRUE) 181endif() 182 183if(MSVC) 184 # We don't want to have to copy the C Runtime DLL everywhere the executable 185 # goes. So by default compile code to assume the CRT is statically linked, 186 # i.e. use /MT* options. For debug builds use /MTd, and for release builds 187 # use /MT. If AMBER_ENABLE_SHARED_CRT is ON, then use the shared C runtime. 188 # Modify the project-wide options variables. This is ugly, but seems to be 189 # the state of the art. 190 if(NOT ${AMBER_ENABLE_SHARED_CRT}) 191 message(STATUS "Amber: Static C runtime selected: replacing /MD* with /MT*") 192 foreach (flag_var 193 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 194 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO 195 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 196 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 197 string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 198 endforeach() 199 endif() 200endif() 201 202function(amber_default_compile_options TARGET) 203 if (${COMPILER_IS_LIKE_GNU}) 204 target_compile_options(${TARGET} PRIVATE 205 -fno-exceptions 206 -fvisibility=hidden 207 -Wall 208 -Wextra 209 -Wno-cast-function-type-strict 210 -Wno-padded 211 -Wno-switch-enum 212 -Wno-unknown-pragmas 213 -Wno-unsafe-buffer-usage 214 -pedantic-errors 215 ) 216 if (NOT ${AMBER_DISABLE_WERROR}) 217 target_compile_options(${TARGET} PRIVATE -Werror) 218 endif() 219 220 if(NOT ${AMBER_ENABLE_RTTI}) 221 target_compile_options(${TARGET} PRIVATE -fno-rtti) 222 endif() 223 224 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 225 target_compile_options(${TARGET} PRIVATE 226 -Wno-c++98-compat 227 -Wno-c++98-compat-pedantic 228 -Wno-format-pedantic 229 -Wno-unknown-warning-option 230 ) 231 if (NOT ${AMBER_DISABLE_WEVERYTHING}) 232 target_compile_options(${TARGET} PRIVATE -Weverything) 233 endif() 234 endif() 235 endif() 236 237 if (MSVC) 238 target_compile_options(${TARGET} PRIVATE 239 /bigobj 240 /EHsc 241 /W3 242 /WX 243 /wd4068 244 /wd4514 245 /wd4571 246 /wd4625 247 /wd4626 248 /wd4710 249 /wd4774 250 /wd4820 251 /wd5026 252 /wd5027 253 ) 254 endif() 255 256 if (NOT ${AMBER_ENABLE_SHARED_CRT}) 257 # For MinGW cross compile, statically link to the C++ runtime. 258 # But it still depends on MSVCRT.dll. 259 if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") 260 if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") 261 set_target_properties(${TARGET} PROPERTIES LINK_FLAGS 262 -static 263 -static-libgcc 264 -static-libstdc++) 265 endif() 266 endif() 267 endif() 268endfunction() 269 270add_subdirectory(third_party) 271add_subdirectory(src) 272 273if (${AMBER_ENABLE_SAMPLES}) 274 add_subdirectory(samples) 275endif() 276