-
Notifications
You must be signed in to change notification settings - Fork 89
/
CMakeLists.txt
545 lines (453 loc) · 14.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
message("++ CMake version ${CMAKE_VERSION}")
include(ProcessorCount)
project(AnKi)
################################################################################
# Funcs #
################################################################################
function(anki_install_executable EXE)
if(NOT ANDROID)
add_custom_command(TARGET ${EXE} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/Binaries
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${EXE}> ${CMAKE_BINARY_DIR}/Binaries)
endif()
endfunction()
macro(anki_new_executable)
if(NOT ANDROID)
add_executable(${ARGV})
# Move the target to another dir
set_target_properties(${ARGV0} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Binaries)
# Move the target to another dir using a different way as well because cor some reason it doesn't work on windows
add_custom_command(TARGET ${ARGV0} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/Binaries
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${ARGV0}> ${CMAKE_BINARY_DIR}/Binaries/$<TARGET_FILE_NAME:${ARGV0}>)
else()
set(_SKIP TRUE)
foreach(ARG ${ARGV})
if(_SKIP)
set(_SKIP FALSE)
else()
list(APPEND _TMP_LIST ${ARG})
endif()
endforeach()
add_library(${ARGV0} SHARED ${_TMP_LIST})
endif()
endmacro()
################################################################################
# Determin the system to build for. Do that first #
################################################################################
if(WIN32)
if(NOT WINDOWS)
set(WINDOWS TRUE)
message("++ Building for windows")
endif()
elseif(UNIX AND NOT APPLE)
if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
set(LINUX TRUE)
message("++ Building for Linux")
elseif(ANDROID)
message("++ Building for Android")
else()
message(FATAL_ERROR "Unknown unix")
endif()
elseif(APPLE)
if(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
set(MACOS TRUE)
message("++ Building for MacOS")
else()
message(FATAL_ERROR "Unknown apple")
endif()
else()
message(FATAL_ERROR "Unknown system")
endif()
# Indentify compiler
set(GCC FALSE)
set(CLANG FALSE)
set(CLANG_WINDOWS FALSE)
if(${CMAKE_C_COMPILER_ID} MATCHES "GNU")
set(GCC TRUE)
message("++ Compiler identified as GCC")
endif()
if(${CMAKE_C_COMPILER_ID} MATCHES "Clang")
message("++ Compiler identified as Clang")
set(CLANG TRUE)
if(WINDOWS)
# It's clang for windows
message("++ Compiler identified as Clang for Windows")
set(CLANG_WINDOWS TRUE)
endif()
endif()
if(MSVC)
message("++ Compiler identified as MSVC")
endif()
# Identify the target system
set(X86 FALSE)
set(ARM FALSE)
if(GCC OR CLANG)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine OUTPUT_VARIABLE target_arch)
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch" OR ${target_arch} MATCHES "aarch")
set(ARM TRUE)
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86.*" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64.*")
set(X86 TRUE)
else()
message(FATAL_ERROR "Couldn't find the target architecture from: ${target_arch} or ${CMAKE_SYSTEM_PROCESSOR}")
endif()
elseif(MSVC)
set(X86 TRUE)
else()
message(FATAL_ERROR "Couldn't find the target architecture")
endif()
if(X86)
message("++ Target architecture is X86")
elseif(ARM)
message("++ Target architecture is ARM")
endif()
################################################################################
# Configuration #
################################################################################
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(ANKI_EXTRA_CHECKS "Debugging checks (assertions)" OFF)
option(ANKI_LTO "LTO compilation" OFF)
option(ANKI_BUILD_TOOLS "Build tools" ON)
option(ANKI_BUILD_TESTS "Build unit tests" OFF)
option(ANKI_BUILD_SANDBOX "Build sandbox application" ON)
option(ANKI_BUILD_SAMPLES "Build sample applications" ON)
option(ANKI_STRIP "Strip the symbols from the executables" OFF)
option(ANKI_TRACE "Enable performance tracing. Small overhead" OFF)
if(ANKI_TRACE)
set(_ANKI_TRACING_ENABLED 1)
else()
set(_ANKI_TRACING_ENABLED 0)
endif()
option(ANKI_STATS "Enable performance statistics. Small overhead" ON)
if(ANKI_STATS)
set(_ANKI_STATS_ENABLED 1)
else()
set(_ANKI_STATS_ENABLED 0)
endif()
option(ANKI_SIMD "Enable SIMD optimizations" ON)
option(ANKI_ADDRESS_SANITIZER "Enable address sanitizer (-fsanitize=address)" OFF)
option(ANKI_HEADLESS "Build a headless application" OFF)
option(ANKI_SHADER_FULL_PRECISION "Build shaders with full precision" OFF)
set(ANKI_OVERRIDE_SHADER_COMPILER "" CACHE FILEPATH "Set the ShaderCompiler to be used to compile all shaders")
option(ANKI_DLSS "Integrate DLSS if supported" OFF)
if(ANDROID)
option(ANKI_PLATFORM_MOBILE "Build for a mobile platform" ON)
else()
option(ANKI_PLATFORM_MOBILE "Build for a mobile platform" OFF)
endif()
# Take a wild guess on the windowing system
if(ANKI_HEADLESS)
set(SDL FALSE)
elseif(LINUX)
set(SDL TRUE)
elseif(WINDOWS)
set(SDL TRUE)
elseif(ANDROID)
set(SDL FALSE)
elseif(MACOS)
set(SDL TRUE)
else()
message(FATAL_ERROR "Couldn't determine the window backend. You need to specify it manually.")
endif()
set(ANKI_GR_BACKEND "VULKAN" CACHE STRING "The graphics API to use (VULKAN or DIRECTX)")
if(${ANKI_GR_BACKEND} STREQUAL "DIRECTX")
set(DIRECTX TRUE)
set(VULKAN FALSE)
elseif(${ANKI_GR_BACKEND} STREQUAL "VULKAN")
set(DIRECTX FALSE)
set(VULKAN TRUE)
set(VIDEO_VULKAN TRUE) # Set for the SDL2 to pick up
else()
message(FATAL_ERROR "Wrong ANKI_GR_BACKEND")
endif()
if(NOT DEFINED CMAKE_BUILD_TYPE)
message(FATAL_ERROR "You need to define CMAKE_BUILD_TYPE")
endif()
################################################################################
# Common compiler & linker flags #
################################################################################
if(MINGW)
add_compile_options(-mconsole)
endif()
add_definitions(
-DSPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
-DANKI_BUILD
-DIMGUI_USER_CONFIG=<AnKi/Ui/ImGuiConfig.h>)
if(NOT MSVC)
# When building AnKi define this special flag
if(NOT CLANG_WINDOWS)
add_compile_options(-fPIC)
endif()
add_compile_options(-fno-exceptions)
if(GCC)
add_compile_options(-static-libstdc++)
endif()
if(X86)
add_compile_options(-msse4)
endif()
if(ANKI_LTO)
add_compile_options(-flto)
set(LINKER_FLAGS "${LINKER_FLAGS} -flto ")
endif()
if(ANKI_STRIP)
set(LINKER_FLAGS "${LINKER_FLAGS} -s ")
add_compile_options(-s)
endif()
if(ANKI_ADDRESS_SANITIZER)
add_compile_options(-fsanitize=address)
endif()
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
add_compile_options(-O3)
add_definitions(-DNDEBUG)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
add_compile_options(-O3 -g3 -fno-omit-frame-pointer)
set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
add_compile_options(-O0 -g3)
set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
else()
message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()
# Set the flags to cmake now
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
else()
#ProcessorCount(PC)
add_compile_options(
/MP
/EHsc) # Disable exceptions
if(${CMAKE_BUILD_TYPE} STREQUAL "Release" OR ${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
#add_definitions(/Ox)
endif()
add_definitions(
-D_CRT_SECURE_NO_WARNINGS=1 # Disable some string function warnings
-D_ITERATOR_DEBUG_LEVEL=0
-D_HAS_EXCEPTIONS=0)
# Full paths in compiler diagnostics else you can't click on visual studio have it open the file+line
add_compile_options(/FC)
endif()
# Use LLD or gold linker
if(UNIX AND NOT APPLE)
execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=lld -Wl,--version ERROR_QUIET OUTPUT_VARIABLE ld_version)
if("${ld_version}" MATCHES "compatible with GNU linkers" AND "${ld_version}" MATCHES "LLD")
message("++ Will use LLD linker")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Wl,--disable-new-dtags")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Wl,--disable-new-dtags")
else()
execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE ld_version)
if("${ld_version}" MATCHES "GNU gold")
message("++ Will use gold linker")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
endif()
endif()
endif()
################################################################################
# Thirdparty #
################################################################################
set(ANKI_EXTERN_SUB_DIRS TinyXml2 Lua ZLib Bullet ImGui MeshOptimizer SpirvCross)
# Bullet config
option(BUILD_BULLET2_DEMOS OFF)
option(BUILD_BULLET3 OFF)
option(BUILD_CPU_DEMOS OFF)
option(BUILD_OPENGL3_DEMOS OFF)
option(BUILD_EXTRAS OFF)
option(BUILD_UNIT_TESTS OFF)
if((LINUX OR MACOS OR WINDOWS) AND GL)
set(ANKI_EXTERN_SUB_DIRS ${ANKI_EXTERN_SUB_DIRS} GLEW)
endif()
# SDL
if(SDL)
message("++ Configuring SDL2")
option(SDL_X11 OFF)
add_subdirectory(ThirdParty/Sdl2)
message("++ End configuring SDL2")
# Include first the build directory.
set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/ThirdParty/Sdl2/include"
"${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Sdl2/include")
endif()
# glslang
message("++ Configuring SPIRV-tools")
add_subdirectory(ThirdParty/SpirvTools)
message("++ End configuring SPIRV-tools")
if(LINUX OR WINDOWS)
message("++ Configuring reproc")
set(REPROC++ OFF)
add_subdirectory(ThirdParty/Reproc)
message("++ End configuring reproc")
endif()
foreach(TMP ${ANKI_EXTERN_SUB_DIRS})
add_subdirectory(ThirdParty/${TMP})
endforeach()
if(ANDROID)
add_library(AnKiAndroidNativeGlue ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
add_subdirectory(ThirdParty/HwcPipe)
add_subdirectory(ThirdParty/StreamlineAnnotate)
endif()
# DLSS
if(ANKI_DLSS)
add_subdirectory(ThirdParty/DlssSdk)
set(_ANKI_DLSS_ENABLED 1)
else()
set(_ANKI_DLSS_ENABLED 0)
endif()
################################################################################
# AnKi #
################################################################################
# Revision
find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND
"${GIT_EXECUTABLE}" log -1 --date=short --format=%h
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_COMMIT
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
set(ANKI_REVISION "\"${GIT_COMMIT}\"")
else()
set(ANKI_REVISION "\"unknown\"")
endif()
# Doxygen
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Docs/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
find_package(Doxygen)
if(DOXYGEN_FOUND)
message("++ Doxygen found")
add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM)
endif()
# Config.h
set(ANKI_VERSION_MAJOR 0)
set(ANKI_VERSION_MINOR 1)
message("++ AnKi version: ${ANKI_VERSION_MAJOR}.${ANKI_VERSION_MINOR}")
if(ANKI_EXTRA_CHECKS)
set(_ANKI_EXTRA_CHECKS 1)
else()
set(_ANKI_EXTRA_CHECKS 0)
endif()
if(ANKI_SIMD)
set(_ANKI_ENABLE_SIMD 1)
else()
set(_ANKI_ENABLE_SIMD 0)
endif()
if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(ANKI_DEBUG_SYMBOLS 1)
set(ANKI_OPTIMIZE 0)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(ANKI_DEBUG_SYMBOLS 0)
set(ANKI_OPTIMIZE 1)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
set(ANKI_DEBUG_SYMBOLS 1)
set(ANKI_OPTIMIZE 1)
else()
message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()
if(ANKI_BUILD_TESTS)
set(ANKI_TESTS 1)
else()
set(ANKI_TESTS 0)
endif()
if(ANKI_HEADLESS)
set(_ANKI_WINDOWING_SYSTEM 0)
elseif(SDL)
set(_ANKI_WINDOWING_SYSTEM 1)
else()
set(_ANKI_WINDOWING_SYSTEM 2)
endif()
if(ANKI_PLATFORM_MOBILE)
message("++ Compiling for a mobile platform")
set(_ANKI_PLATFORM_MOBILE 1)
else()
message("++ NOT compiling for a mobile platform")
set(_ANKI_PLATFORM_MOBILE 0)
endif()
if(VULKAN)
set(_ANKI_GR_BACKEND 0)
else()
set(_ANKI_GR_BACKEND 1)
endif()
configure_file("AnKi/Config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/AnKi/Config.h")
# Include & lib directories
include_directories(
"ThirdParty/Khronos"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Bullet/src"
"ThirdParty"
"ThirdParty/ZLib"
${CMAKE_CURRENT_SOURCE_DIR})
if(SDL2_INCLUDE_DIRS)
include_directories("${SDL2_INCLUDE_DIRS}")
endif()
if(LINUX OR MACOS OR WINDOWS)
include_directories("ThirdParty/GLEW/include")
else()
#include_directories("ThirdParty/GLES3/include")
endif()
if(ANDROID)
include_directories("${ANDROID_NDK}/sources/android/native_app_glue")
endif()
if(LINUX)
set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} pthread dl)
if(SDL)
#set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} X11-xcb)
endif()
endif()
if(ANKI_DLSS)
set(THIRD_PARTY_LIBS ${THIRD_PARTY_LIBS} AnKiNgx)
endif()
# AnKi compiler flags (Mainly warnings)
if(NOT MSVC)
add_compile_options(
-pedantic
-Wno-unknown-warning-option
-Wall
-W
-Wextra
-Wstrict-aliasing
-Wwrite-strings
-Wunused
-Wundef
-Wno-ignored-attributes
-Wno-implicit-fallthrough
-Wunused-result
-Wconversion
-Wno-sign-conversion
-Wno-keyword-macro
-Wno-string-conversion
-Wno-class-memaccess
-Wunused-variable)
set(CMAKE_CXX_STANDARD 20)
else()
string(REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
add_compile_options(
/std:c++20
/W4
/wd4324
/wd4456
/wd4127
/wd4457)
endif()
# Add AnKi sub libraries
add_subdirectory(AnKi)
if(MSVC)
add_library(AnKi INTERFACE AnKi.natvis)
else()
add_library(AnKi INTERFACE)
endif()
target_link_libraries(AnKi INTERFACE AnKiCore ${THIRD_PARTY_LIBS})
add_dependencies(AnKi AnKiShaders)
################################################################################
# AnKi extra #
################################################################################
if(ANKI_BUILD_TESTS)
add_subdirectory(Tests)
endif()
if(ANKI_BUILD_TOOLS)
add_subdirectory(Tools)
endif()
if(ANKI_BUILD_SANDBOX)
add_subdirectory(Sandbox)
endif()
if(ANKI_BUILD_SAMPLES)
add_subdirectory(Samples)
endif()