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_VK_DEBUGGING
51  "Build with cppdap debugging support" ${AMBER_ENABLE_VK_DEBUGGING})
52option(AMBER_ENABLE_RTTI
53  "Build with runtime type information" OFF)
54
55if (${AMBER_USE_CLSPV} OR ${AMBER_ENABLE_SWIFTSHADER})
56  set(CMAKE_CXX_STANDARD 14)
57else()
58  set(CMAKE_CXX_STANDARD 11)
59endif()
60
61if(WIN32)
62  # On Windows, CMake by default compiles with the shared CRT.
63  # Default it to the static CRT.
64  option(AMBER_ENABLE_SHARED_CRT
65         "Amber: Use the shared CRT with MSVC instead of the static CRT"
66	 ${AMBER_ENABLE_SHARED_CRT})
67endif(WIN32)
68
69if (${AMBER_SKIP_SPIRV_TOOLS})
70  set(AMBER_ENABLE_SPIRV_TOOLS FALSE)
71  set(AMBER_ENABLE_SHADERC FALSE)
72else()
73  set(AMBER_ENABLE_SPIRV_TOOLS TRUE)
74
75  if (${AMBER_SKIP_SHADERC})
76    set(AMBER_ENABLE_SHADERC FALSE)
77  else()
78    set(AMBER_ENABLE_SHADERC TRUE)
79  endif()
80endif()
81
82if (${AMBER_SKIP_TESTS})
83  set(AMBER_ENABLE_TESTS FALSE)
84else()
85  set(AMBER_ENABLE_TESTS TRUE)
86endif()
87
88if (${AMBER_SKIP_SAMPLES})
89  set(AMBER_ENABLE_SAMPLES FALSE)
90else()
91  set(AMBER_ENABLE_SAMPLES TRUE)
92endif()
93
94if (${AMBER_SKIP_LODEPNG})
95  set(AMBER_ENABLE_LODEPNG FALSE)
96else()
97  set(AMBER_ENABLE_LODEPNG TRUE)
98endif()
99
100if (${AMBER_ENABLE_SWIFTSHADER})
101  # Swiftshader requires the loader to be built.
102  set(AMBER_USE_LOCAL_VULKAN TRUE)
103endif()
104
105if (${AMBER_USE_DXC})
106  set(AMBER_ENABLE_DXC TRUE)
107else()
108  set(AMBER_ENABLE_DXC FALSE)
109endif()
110
111if (${AMBER_USE_CLSPV})
112  set(AMBER_ENABLE_CLSPV TRUE)
113  set(AMBER_ENABLE_SPIRV_TOOLS TRUE)
114else()
115  set(AMBER_ENABLE_CLSPV FALSE)
116endif()
117
118if (${AMBER_USE_CLSPV} OR ${AMBER_ENABLE_SWIFTSHADER})
119  enable_language(ASM)
120endif()
121
122message(STATUS "Using python3")
123find_package(PythonInterp 3 REQUIRED)
124
125message(STATUS "Amber enable SPIRV-Tools: ${AMBER_ENABLE_SPIRV_TOOLS}")
126message(STATUS "Amber enable Shaderc: ${AMBER_ENABLE_SHADERC}")
127message(STATUS "Amber enable tests: ${AMBER_ENABLE_TESTS}")
128message(STATUS "Amber enable samples: ${AMBER_ENABLE_SAMPLES}")
129message(STATUS "Amber enable lodepng: ${AMBER_ENABLE_LODEPNG}")
130message(STATUS "Amber enable SwiftShader: ${AMBER_ENABLE_SWIFTSHADER}")
131message(STATUS "Amber enable DXC: ${AMBER_ENABLE_DXC}")
132message(STATUS "Amber enable Clspv: ${AMBER_ENABLE_CLSPV}")
133message(STATUS "Amber enable RTTI: ${AMBER_ENABLE_RTTI}")
134
135include_directories("${PROJECT_SOURCE_DIR}/include")
136include_directories("${PROJECT_SOURCE_DIR}")
137
138if (${AMBER_ENABLE_SPIRV_TOOLS})
139  include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
140endif()
141
142if (NOT ANDROID)
143  include(src/dawn/find_dawn.cmake)
144endif()
145
146include(src/vulkan/find_vulkan.cmake)
147
148add_definitions(-DAMBER_CTS_VULKAN_HEADER=$<BOOL:${VULKAN_CTS_HEADER}>)
149add_definitions(-DAMBER_ENGINE_VULKAN=$<BOOL:${Vulkan_FOUND}>)
150add_definitions(-DAMBER_ENGINE_DAWN=$<BOOL:${Dawn_FOUND}>)
151add_definitions(-DAMBER_ENABLE_SPIRV_TOOLS=$<BOOL:${AMBER_ENABLE_SPIRV_TOOLS}>)
152add_definitions(-DAMBER_ENABLE_SHADERC=$<BOOL:${AMBER_ENABLE_SHADERC}>)
153add_definitions(-DAMBER_ENABLE_DXC=$<BOOL:${AMBER_ENABLE_DXC}>)
154add_definitions(-DAMBER_ENABLE_CLSPV=$<BOOL:${AMBER_ENABLE_CLSPV}>)
155add_definitions(-DAMBER_ENABLE_LODEPNG=$<BOOL:${AMBER_ENABLE_LODEPNG}>)
156add_definitions(-DAMBER_ENABLE_VK_DEBUGGING=$<BOOL:${AMBER_ENABLE_VK_DEBUGGING}>)
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      -std=c++11
206      -fno-exceptions
207      -fvisibility=hidden
208      -Wall
209      -Werror
210      -Wextra
211      -Wno-padded
212      -Wno-switch-enum
213      -Wno-unknown-pragmas
214      -pedantic-errors
215    )
216
217    if(NOT ${AMBER_ENABLE_RTTI})
218      target_compile_options(${TARGET} PRIVATE -fno-rtti)
219    endif()
220
221    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
222      target_compile_options(${TARGET} PRIVATE
223        -Wno-c++98-compat
224        -Wno-c++98-compat-pedantic
225        -Wno-format-pedantic
226        -Wno-unknown-warning-option
227        -Weverything
228      )
229    endif()
230  endif()
231
232  if (MSVC)
233    # Specify /EHs for exception handling.
234    target_compile_options(${TARGET} PRIVATE
235      /bigobj
236      /EHsc
237      /W3
238      /WX
239      /wd4068
240      /wd4514
241      /wd4571
242      /wd4625
243      /wd4626
244      /wd4710
245      /wd4774
246      /wd4820
247      /wd5026
248      /wd5027
249    )
250  endif()
251
252  if (NOT ${AMBER_ENABLE_SHARED_CRT})
253    # For MinGW cross compile, statically link to the C++ runtime.
254    # But it still depends on MSVCRT.dll.
255    if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
256      if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
257	set_target_properties(${TARGET} PROPERTIES LINK_FLAGS
258	  -static
259	  -static-libgcc
260	  -static-libstdc++)
261      endif()
262    endif()
263  endif()
264endfunction()
265
266add_subdirectory(third_party)
267add_subdirectory(src)
268
269if (${AMBER_ENABLE_SAMPLES} AND NOT ANDROID)
270  add_subdirectory(samples)
271endif()
272