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