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