Debian Codegraph @

CMakeLists.txt

raw ◀ ▶
Source reconstructed at patch state 4 of 8 — not the installed (fully patched) source.
8 Debian patches touch this file
1 # ==================================================================================================
2 # CMake
3 # ==================================================================================================
4 cmake_minimum_required(VERSION 3.19)
5
6 # ==================================================================================================
7 # Project declaration
8 # ==================================================================================================
9 project(TNT)
10
11 # ==================================================================================================
12 # Options
13 # ==================================================================================================
14 option(FILAMENT_ENABLE_JAVA "Compile Java projects, requires a JDK and the JAVA_HOME env var" ON)
15
16 option(FILAMENT_USE_EXTERNAL_GLES3 "Experimental: Compile Filament against OpenGL ES 3" OFF)
17
18 option(FILAMENT_USE_SWIFTSHADER "Compile Filament against SwiftShader" OFF)
19
20 option(FILAMENT_ENABLE_LTO "Enable link-time optimizations if supported by the compiler" OFF)
21
22 option(FILAMENT_SKIP_SAMPLES "Don't build samples" OFF)
23
24 option(FILAMENT_SUPPORTS_XCB "Include XCB support in Linux builds" ON)
25
26 option(FILAMENT_SUPPORTS_XLIB "Include XLIB support in Linux builds" ON)
27
28 set(FILAMENT_PER_RENDER_PASS_ARENA_SIZE_IN_MB "2" CACHE STRING
29 "Per render pass arena size. Must be roughly 1 MB larger than FILAMENT_PER_FRAME_COMMANDS_SIZE_IN_MB, default 2."
30 )
31
32 set(FILAMENT_PER_FRAME_COMMANDS_SIZE_IN_MB "1" CACHE STRING
33 "Size of the high-level draw commands buffer. Rule of thumb, 1 MB less than FILAMENT_PER_RENDER_PASS_ARENA_SIZE_IN_MB, default 1."
34 )
35
36 set(FILAMENT_MIN_COMMAND_BUFFERS_SIZE_IN_MB "1" CACHE STRING
37 "Size of the command-stream buffer. As a rule of thumb use the same value as FILAMENT_PER_FRRAME_COMMANDS_SIZE_IN_MB, default 1."
38 )
39
40 set(FILAMENT_OPENGL_HANDLE_ARENA_SIZE_IN_MB "2" CACHE STRING
41 "Size of the OpenGL handle arena, default 2."
42 )
43
44 # ==================================================================================================
45 # CMake policies
46 # ==================================================================================================
47 if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.12")
48 cmake_policy(SET CMP0074 NEW)
49 endif()
50
51 # ==================================================================================================
52 # Support for ccache
53 # ==================================================================================================
54 find_program(CCACHE_PROGRAM ccache)
55 if (CCACHE_PROGRAM)
56 set(C_LAUNCHER "${CCACHE_PROGRAM}")
57 set(CXX_LAUNCHER "${CCACHE_PROGRAM}")
58
59 configure_file(build/launch-c.in launch-c)
60 configure_file(build/launch-cxx.in launch-cxx)
61
62 execute_process(COMMAND chmod a+rx
63 "${CMAKE_BINARY_DIR}/launch-c"
64 "${CMAKE_BINARY_DIR}/launch-cxx"
65 )
66
67 if (CMAKE_GENERATOR STREQUAL "Xcode")
68 set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/launch-c")
69 set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/launch-cxx")
70 set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/launch-c")
71 set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/launch-cxx")
72 else()
73 set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-c")
74 set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-cxx")
75 endif()
76 endif()
77
78 # ==================================================================================================
79 # Support Vim and Visual Studio Code by generating compile_commands.json
80 # ==================================================================================================
81 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
82
83 # ==================================================================================================
84 # OS specific
85 # ==================================================================================================
86 if (UNIX AND NOT APPLE AND NOT ANDROID AND NOT WEBGL)
87 set(LINUX TRUE)
88 endif()
89
90 if (LINUX)
91 if (FILAMENT_SUPPORTS_XCB)
92 add_definitions(-DFILAMENT_SUPPORTS_XCB)
93 endif()
94
95 if (FILAMENT_SUPPORTS_XLIB)
96 add_definitions(-DFILAMENT_SUPPORTS_XLIB)
97 endif()
98 endif()
99
100 if (ANDROID OR WEBGL OR IOS)
101 set(IS_MOBILE_TARGET TRUE)
102 endif()
103
104 if (NOT ANDROID AND NOT WEBGL AND NOT IOS)
105 set(IS_HOST_PLATFORM TRUE)
106 endif()
107
108 if (IOS)
109 # Remove the headerpad_max_install_names linker flag on iOS. It causes warnings when linking
110 # executables with bitcode.
111 string(REPLACE "-Wl,-headerpad_max_install_names" "" CMAKE_C_LINK_FLAGS ${CMAKE_C_LINK_FLAGS})
112 string(REPLACE "-Wl,-headerpad_max_install_names" "" CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS})
113 string(REPLACE "-Wl,-headerpad_max_install_names" "" CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
114 string(REPLACE "-Wl,-headerpad_max_install_names" "" CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS})
115 endif()
116
117 if (WIN32)
118 # Link statically against c/c++ lib to avoid missing redistriburable such as
119 # "VCRUNTIME140.dll not found. Try reinstalling the app.", but give users
120 # a choice to opt for the shared runtime if they want.
121 option(USE_STATIC_CRT "Link against the static runtime libraries." ON)
122
123 # On Windows we need to instruct cmake to generate the .def in order to get the .lib required
124 # when linking against dlls. CL.EXE will not generate .lib without .def file (or without pragma
125 # __declspec(dllexport) in front of each functions).
126 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
127
128 if (${USE_STATIC_CRT})
129 set(CRT_FLAGS_RELEASE "/MT")
130 set(CRT_FLAGS_DEBUG "/MTd")
131 else()
132 set(CRT_FLAGS_RELEASE "/MD")
133 set(CRT_FLAGS_DEBUG "/MDd")
134 endif()
135
136 # TODO: Figure out why pdb generation messes with incremental compilaton.
137 # IN RELEASE_WITH_DEBUG_INFO, generate debug info in .obj, no in pdb.
138 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CRT_FLAGS_RELEASE} /Z7")
139 set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${CRT_FLAGS_RELEASE} /Z7")
140
141 # In RELEASE, also generate PDBs.
142 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CRT_FLAGS_RELEASE} /Zi")
143 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${CRT_FLAGS_RELEASE} /Zi")
144
145 # In DEBUG, avoid generating a PDB file which seems to mess with incremental compilation.
146 # Instead generate debug info directly inside obj files.
147 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CRT_FLAGS_DEBUG} /Z7")
148 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${CRT_FLAGS_DEBUG} /Z7")
149
150 # Special settings when building on CI.
151 if (${FILAMENT_WINDOWS_CI_BUILD})
152 set(LinkerFlags
153 CMAKE_SHARED_LINKER_FLAGS_DEBUG
154 CMAKE_EXE_LINKER_FLAGS_DEBUG
155 CMAKE_MODULE_LINKER_FLAGS_DEBUG
156 )
157 foreach(LinkerFlag ${LinkerFlags})
158 # The /debug flag outputs .pdb files, which we don't need on CI.
159 string(REPLACE "/debug" "" ${LinkerFlag} ${${LinkerFlag}})
160
161 # The /INCREMENTAL flag outputs .ilk files for incremental linking. These are huge, and
162 # we don't need them on CI.
163 string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" ${LinkerFlag} ${${LinkerFlag}})
164 endforeach()
165 endif()
166 endif()
167
168 # ==================================================================================================
169 # Paths
170 # ==================================================================================================
171 # Where our external libs are
172 set(EXTERNAL ${CMAKE_CURRENT_SOURCE_DIR}/third_party)
173
174 # Where our libraries are
175 set(LIBRARIES ${CMAKE_CURRENT_SOURCE_DIR}/libs)
176
177 # Where our filament code is
178 set(FILAMENT ${CMAKE_CURRENT_SOURCE_DIR})
179
180 # Where our tools are
181 set(TOOLS ${CMAKE_CURRENT_SOURCE_DIR}/tools)
182
183 # ==================================================================================================
184 # Compiler check
185 # ==================================================================================================
186 set(MIN_CLANG_VERSION "6.0")
187
188 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
189 if (CMAKE_C_COMPILER_VERSION VERSION_LESS MIN_CLANG_VERSION)
190 message(FATAL_ERROR "Detected C compiler Clang ${CMAKE_C_COMPILER_VERSION} < ${MIN_CLANG_VERSION}")
191 endif()
192 elseif (NOT MSVC)
193 message(FATAL_ERROR "Detected C compiler ${CMAKE_C_COMPILER_ID} is unsupported")
194 endif()
195
196 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
197 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS MIN_CLANG_VERSION)
198 message(FATAL_ERROR "Detected CXX compiler Clang ${CMAKE_CXX_COMPILER_VERSION} < ${MIN_CLANG_VERSION}")
199 endif()
200 elseif (NOT MSVC)
201 message(FATAL_ERROR "Detected CXX compiler ${CMAKE_CXX_COMPILER_ID} is unsupported")
202 endif()
203
204 # Detect use of the clang-cl.exe frontend, which does not support all of clangs normal options
205 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
206 if ("${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
207 message(FATAL_ERROR "Building with Clang on Windows is no longer supported. Use MSVC 2019 instead.")
208 endif()
209 endif()
210
211 # ==================================================================================================
212 # Link time optimizations (LTO)
213 # ==================================================================================================
214 if (FILAMENT_ENABLE_LTO)
215 include(CheckIPOSupported)
216
217 check_ipo_supported(RESULT IPO_SUPPORT)
218
219 if (IPO_SUPPORT)
220 message(STATUS "LTO support is enabled")
221 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
222 endif()
223 endif()
224
225 # ==================================================================================================
226 # General compiler flags
227 # ==================================================================================================
228 set(CXX_STANDARD "-std=c++17")
229 if (WIN32)
230 set(CXX_STANDARD "/std:c++17")
231 endif()
232
233 if (MSVC)
234 set(CXX_STANDARD "/std:c++latest")
235 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_STANDARD} /W0 /Zc:__cplusplus")
236 else()
237 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_STANDARD} -fstrict-aliasing -Wno-unknown-pragmas -Wno-unused-function")
238 endif()
239
240 if (FILAMENT_USE_EXTERNAL_GLES3)
241 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_EXTERNAL_GLES3")
242 endif()
243
244 if (FILAMENT_USE_SWIFTSHADER)
245 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFILAMENT_USE_SWIFTSHADER")
246 endif()
247
248 if (WIN32)
249 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_USE_MATH_DEFINES=1")
250 endif()
251
252 if (LINUX)
253 option(USE_STATIC_LIBCXX "Link against the static runtime libraries." ON)
254 if (${USE_STATIC_LIBCXX})
255 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
256 link_libraries("-static-libgcc -static-libstdc++")
257 link_libraries(libc++.a)
258 link_libraries(libc++abi.a)
259 endif()
260
261 # Only linux, clang doesn't want to use a shared library that is not PIC.
262 # /usr/bin/ld: ../bluegl/libbluegl.a(BlueGL.cpp.o): relocation R_X86_64_32S
263 # against `.bss' can not be used when making a shared object; recompile with -fPIC
264 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
265 endif()
266
267 if (ANDROID)
268 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
269 endif()
270
271 if (CYGWIN)
272 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
273 endif()
274
275 if (MSVC)
276 # Since the "secure" replacements that MSVC suggests are not portable, disable
277 # the deprecation warnings. Also disable warnings about use of POSIX functions (i.e. "unlink").
278 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE")
279 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE")
280 endif()
281
282 # Add colors to ninja builds
283 if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
284 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics")
285 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
286 endif()
287
288 # ==================================================================================================
289 # Release compiler flags
290 # ==================================================================================================
291 if (NOT MSVC)
292 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fomit-frame-pointer")
293
294 # These aren't compatible with -fembed-bitcode (and seem to have no effect on Apple platforms anyway)
295 if (NOT IOS)
296 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ffunction-sections -fdata-sections")
297 endif()
298 endif()
299
300 # On Android RELEASE builds, we disable exceptions and RTTI to save some space (about 75 KiB
301 # saved by -fno-exception and 10 KiB saved by -fno-rtti).
302 if (ANDROID OR IOS OR WEBGL)
303 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti")
304 endif()
305
306 # With WebGL, we disable RTTI even for debug builds because we pass emscripten::val back and forth
307 # between C++ and JavaScript in order to efficiently access typed arrays, which are unbound.
308 # NOTE: This is not documented in emscripten so we should consider a different approach.
309 if (WEBGL)
310 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-rtti")
311 endif()
312
313 # ==================================================================================================
314 # Debug compiler flags
315 # ==================================================================================================
316 # ASAN is deactivated for now because:
317 # -fsanitize=undefined causes extremely long link times
318 # -fsanitize=address causes a crash with assimp, which we can't explain for now
319 #set(EXTRA_SANITIZE_OPTIONS "-fsanitize=undefined -fsanitize=address")
320 if (NOT MSVC AND NOT WEBGL)
321 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fstack-protector")
322 endif()
323 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${EXTRA_SANITIZE_OPTIONS}")
324
325 # Disable the stack check for macOS to workaround a known issue in clang 11.0.0.
326 # See: https://forums.developer.apple.com/thread/121887
327 if (APPLE)
328 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-stack-check")
329 endif()
330
331 # ==================================================================================================
332 # Linker flags
333 # ==================================================================================================
334 # Strip unused sections
335 if (NOT WEBGL)
336 set(GC_SECTIONS "-Wl,--gc-sections")
337 endif()
338 set(B_SYMBOLIC_FUNCTIONS "-Wl,-Bsymbolic-functions")
339
340 if (APPLE)
341 set(GC_SECTIONS "-Wl,-dead_strip")
342 set(B_SYMBOLIC_FUNCTIONS "")
343
344 # tell ranlib to ignore empty compilation units
345 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols <TARGET>")
346 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols <TARGET>")
347 # prevents ar from invoking ranlib, let CMake do it
348 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")
349 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc -S <TARGET> <LINK_FLAGS> <OBJECTS>")
350 endif()
351
352 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GC_SECTIONS}")
353 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${GC_SECTIONS} ${B_SYMBOLIC_FUNCTIONS}")
354
355 if (WEBGL)
356 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_WEBGL2=1")
357 endif()
358
359 # ==================================================================================================
360 # Project flags
361 # ==================================================================================================
362 # Debug modes only
363 if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
364 set(TNT_DEV true)
365 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTNT_DEV")
366 endif()
367
368 # By default, build with Vulkan support on desktop platforms, although clients must request to use
369 # it at run time.
370 if (WIN32 OR WEBGL OR IOS)
371 option(FILAMENT_SUPPORTS_VULKAN "Include the Vulkan backend" OFF)
372 else()
373 option(FILAMENT_SUPPORTS_VULKAN "Include the Vulkan backend" ON)
374 endif()
375 if (FILAMENT_SUPPORTS_VULKAN)
376 add_definitions(-DFILAMENT_DRIVER_SUPPORTS_VULKAN)
377 endif()
378
379 # Build with Metal support on non-WebGL Apple platforms.
380 if (APPLE AND NOT WEBGL)
381 option(FILAMENT_SUPPORTS_METAL "Include the Metal backend" ON)
382 else()
383 option(FILAMENT_SUPPORTS_METAL "Include the Metal backend" OFF)
384 endif()
385 if (FILAMENT_SUPPORTS_METAL)
386 add_definitions(-DFILAMENT_SUPPORTS_METAL)
387 endif()
388
389 # Building filamat increases build times and isn't required for web, so turn it off by default.
390 if (NOT WEBGL)
391 option(FILAMENT_BUILD_FILAMAT "Build filamat and JNI buildings" ON)
392 else()
393 option(FILAMENT_BUILD_FILAMAT "Build filamat and JNI buildings" OFF)
394 endif()
395
396 # By default, link in matdbg for Desktop + Debug only since it pulls in filamat and a web server.
397 if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND IS_HOST_PLATFORM)
398 option(FILAMENT_ENABLE_MATDBG "Enable the material debugger" ON)
399 else()
400 option(FILAMENT_ENABLE_MATDBG "Enable the material debugger" OFF)
401 endif()
402
403 # Only optimize materials in Release mode (so error message lines match the source code)
404 if (CMAKE_BUILD_TYPE MATCHES Release)
405 option(FILAMENT_DISABLE_MATOPT "Disable material optimizations" OFF)
406 else()
407 option(FILAMENT_DISABLE_MATOPT "Disable material optimizations" ON)
408 endif()
409
410 # ==================================================================================================
411 # Material compilation flags
412 # ==================================================================================================
413
414 # Target system.
415 if (IS_MOBILE_TARGET)
416 set(MATC_TARGET mobile)
417 else()
418 set(MATC_TARGET desktop)
419 endif()
420
421 set(MATC_API_FLAGS )
422
423 # TODO: Add a flag to build Filament without support for OpenGL.
424 set(MATC_API_FLAGS ${MATC_API_FLAGS} -a opengl)
425
426 if (FILAMENT_SUPPORTS_VULKAN)
427 set(MATC_API_FLAGS ${MATC_API_FLAGS} -a vulkan)
428 endif()
429 if (FILAMENT_SUPPORTS_METAL)
430 set(MATC_API_FLAGS ${MATC_API_FLAGS} -a metal)
431 endif()
432
433 if (FILAMENT_DISABLE_MATOPT)
434 set(MATC_OPT_FLAGS -g)
435 endif()
436
437 set(MATC_BASE_FLAGS ${MATC_API_FLAGS} -p ${MATC_TARGET} ${MATC_OPT_FLAGS})
438
439 # ==================================================================================================
440 # Distribution
441 # ==================================================================================================
442 # choose where to put the objects in the dist folder
443 if (NOT DIST_ARCH)
444 # On Apple silicon, the value of CMAKE_HOST_SYSTEM_PROCESSOR varies based on the CMake process's
445 # own architecture. Because of this, running a x86_64 CMake binary on Apple silicon will cause
446 # DIST_ARCH to be set incorrectly.
447 set(DIST_ARCH "${CMAKE_HOST_SYSTEM_PROCESSOR}")
448
449 if (CMAKE_OSX_ARCHITECTURES MATCHES ".*;.*")
450 set(DIST_ARCH "universal")
451 else()
452 if (NOT "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
453 set(DIST_ARCH "${CMAKE_OSX_ARCHITECTURES}")
454 endif()
455 endif()
456
457 endif()
458 # On Windows machines, the host processor is set to 'AMD64', which we'll interpret as x86_64.
459 string(TOLOWER "${DIST_ARCH}" DIST_ARCH)
460 string(REPLACE "amd64" "x86_64" DIST_ARCH "${DIST_ARCH}")
461 if (NOT DIST_DIR)
462 set(DIST_DIR "${DIST_ARCH}")
463 endif()
464
465 # ==================================================================================================
466 # Functions
467 # ==================================================================================================
468 ## The MSVC compiler has a limitation on literal string length which is reached when all the
469 ## licenses are concatenated together into a large string... so split them into multiple strings.
470 function(list_licenses OUTPUT MODULES)
471 set(STR_OPENER "R\"FILAMENT__(")
472 set(STR_CLOSER ")FILAMENT__\"")
473 set(CONTENT)
474 set(_MODULES ${MODULES} ${ARGN})
475 foreach(module ${_MODULES})
476 set(license_path "../../third_party/${module}/LICENSE")
477 get_filename_component(fullname "${license_path}" ABSOLUTE)
478 if(EXISTS ${license_path})
479 string(APPEND CONTENT "${STR_OPENER}License and copyrights for ${module}:\n${STR_CLOSER},\n")
480 file(READ ${license_path} license_long)
481 string(REPLACE "\n" "${STR_CLOSER},\n${STR_OPENER}" license ${license_long})
482 string(APPEND CONTENT ${STR_OPENER}${license}\n${STR_CLOSER},)
483 string(APPEND CONTENT "\n\n")
484 endif()
485 endforeach()
486 configure_file(${FILAMENT}/build/licenses.inc.in ${OUTPUT})
487 endfunction(list_licenses)
488
489 set(COMBINE_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/build/linux/combine-static-libs.sh")
490 if (WIN32)
491 set(COMBINE_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/build/windows/combine-static-libs.bat")
492 set(CMAKE_AR "lib.exe")
493 endif()
494
495 # Add a custom command to TARGET that combines the static libraries in DEPS into a single archive.
496 function(combine_static_libs TARGET OUTPUT DEPS)
497 # Loop through the dependent libraries and query their location on disk.
498 set(DEPS_FILES )
499 foreach(DEPENDENCY ${DEPS})
500 if(TARGET ${DEPENDENCY})
501 get_property(dep_type TARGET ${DEPENDENCY} PROPERTY TYPE)
502 if(dep_type STREQUAL "STATIC_LIBRARY")
503 list(APPEND DEPS_FILES "$<TARGET_FILE:${DEPENDENCY}>")
504 endif()
505 endif()
506 endforeach()
507
508 add_custom_command(
509 TARGET ${TARGET} POST_BUILD
510 COMMAND "${COMBINE_SCRIPT}" "${CMAKE_AR}" "${OUTPUT}" ${DEPS_FILES}
511 COMMENT "Combining ${target} dependencies into single shared library"
512 VERBATIM
513 )
514 endfunction()
515
516 # ==================================================================================================
517 # Configuration for CMAKE_CROSSCOMPILING.
518 # ==================================================================================================
519 if (WEBGL)
520 set(IMPORT_EXECUTABLES ${FILAMENT}/${IMPORT_EXECUTABLES_DIR}/ImportExecutables-Release.cmake)
521 else()
522 set(IMPORT_EXECUTABLES ${FILAMENT}/${IMPORT_EXECUTABLES_DIR}/ImportExecutables-${CMAKE_BUILD_TYPE}.cmake)
523 endif()
524
525 # ==================================================================================================
526 # Try to find Vulkan if the SDK is installed, otherwise fall back to the bundled version.
527 # This needs to stay in our top-level CMakeLists because it sets up variables that are used by the
528 # "bluevk" and "samples" targets.
529 # ==================================================================================================
530
531 if (FILAMENT_USE_SWIFTSHADER)
532 if (NOT FILAMENT_SUPPORTS_VULKAN)
533 message(ERROR "SwiftShader is only useful when Vulkan is enabled.")
534 endif()
535 find_library(SWIFTSHADER_VK NAMES vk_swiftshader HINTS "$ENV{SWIFTSHADER_LD_LIBRARY_PATH}")
536 message(STATUS "Found SwiftShader VK library in: ${SWIFTSHADER_VK}.")
537 add_definitions(-DFILAMENT_VKLIBRARY_PATH=\"${SWIFTSHADER_VK}\")
538 elseif (FILAMENT_SUPPORTS_VULKAN AND APPLE)
539 find_library(Vulkan_LIBRARY NAMES vulkan HINTS "$ENV{VULKAN_SDK}/lib" "$ENV{VULKAN_SDK}/macOS/lib")
540 if (Vulkan_LIBRARY)
541 set(Vulkan_FOUND ON)
542 message(STATUS "Found Vulkan library in SDK: ${Vulkan_LIBRARY}.")
543 add_definitions(-DFILAMENT_VKLIBRARY_PATH=\"${Vulkan_LIBRARY}\")
544 endif()
545 endif()
546
547 # ==================================================================================================
548 # Common Functions
549 # ==================================================================================================
550
551 # Sets the following variables: RESGEN_HEADER, RESGEN_SOURCE, RESGEN_FLAGS, RESGEN_SOURCE_FLAGS,
552 # and RESGEN_OUTPUTS. Please pass in an ARCHIVE_NAME that is unique to your project, otherwise the
553 # incbin directive will happily consume a blob from the wrong project without warnings or errors.
554 # Also be sure to include the ASM language in the CMake "project" directive for your project.
555 function(get_resgen_vars ARCHIVE_DIR ARCHIVE_NAME)
556 set(OUTPUTS
557 ${ARCHIVE_DIR}/${ARCHIVE_NAME}.bin
558 ${ARCHIVE_DIR}/${ARCHIVE_NAME}.S
559 ${ARCHIVE_DIR}/${ARCHIVE_NAME}.apple.S
560 ${ARCHIVE_DIR}/${ARCHIVE_NAME}.h
561 )
562 if (IOS)
563 set(ASM_ARCH_FLAG "-arch ${DIST_ARCH}")
564 endif()
565 if (APPLE)
566 set(ASM_SUFFIX ".apple")
567 endif()
568 set(RESGEN_HEADER "${ARCHIVE_DIR}/${ARCHIVE_NAME}.h" PARENT_SCOPE)
569 # Visual Studio makes it difficult to use assembly without using MASM. MASM doesn't support
570 # the equivalent of .incbin, so on Windows we'll just tell resgen to output a C file.
571 if (WEBGL OR WIN32 OR ANDROID_ON_WINDOWS)
572 set(RESGEN_OUTPUTS "${OUTPUTS};${ARCHIVE_DIR}/${ARCHIVE_NAME}.c" PARENT_SCOPE)
573 set(RESGEN_FLAGS -qcx ${ARCHIVE_DIR} -p ${ARCHIVE_NAME} PARENT_SCOPE)
574 set(RESGEN_SOURCE "${ARCHIVE_DIR}/${ARCHIVE_NAME}.c" PARENT_SCOPE)
575 else()
576 set(RESGEN_OUTPUTS "${OUTPUTS}" PARENT_SCOPE)
577 set(RESGEN_FLAGS -qx ${ARCHIVE_DIR} -p ${ARCHIVE_NAME} PARENT_SCOPE)
578 set(RESGEN_SOURCE "${ARCHIVE_DIR}/${ARCHIVE_NAME}${ASM_SUFFIX}.S" PARENT_SCOPE)
579 set(RESGEN_SOURCE_FLAGS "-I${ARCHIVE_DIR} ${ASM_ARCH_FLAG}" PARENT_SCOPE)
580 endif()
581 endfunction()
582
583 # ==================================================================================================
584 # External dependencies
585 # ==================================================================================================
586 find_package(GTest REQUIRED)
587 find_package(astcenc REQUIRED)
588 find_package(tsl-robin-map REQUIRED)
589 find_package(meshoptimizer REQUIRED)
590 find_package(draco REQUIRED)
591 set(THREADS_PREFER_PTHREAD_FLAG ON)
592 find_package(Threads REQUIRED)
593 if(IS_HOST_PLATFORM)
594 find_package(PNG REQUIRED)
595 find_package(SDL2 REQUIRED)
596 find_package(ZLIB REQUIRED)
597 find_package(tinyexr REQUIRED)
598 endif()
599 include(debian/cmake/imgui.cmake)
600 include(debian/cmake/stb.cmake)
601
602 # ==================================================================================================
603 # Sub-projects
604 # ==================================================================================================
605
606 # Common to all platforms
607 add_subdirectory(${LIBRARIES}/camutils)
608 add_subdirectory(${LIBRARIES}/filabridge)
609 add_subdirectory(${LIBRARIES}/filaflat)
610 add_subdirectory(${LIBRARIES}/filagui)
611 add_subdirectory(${LIBRARIES}/filameshio)
612 add_subdirectory(${LIBRARIES}/geometry)
613 add_subdirectory(${LIBRARIES}/gltfio)
614 add_subdirectory(${LIBRARIES}/ibl)
615 add_subdirectory(${LIBRARIES}/iblprefilter)
616 add_subdirectory(${LIBRARIES}/image)
617 add_subdirectory(${LIBRARIES}/math)
618 add_subdirectory(${LIBRARIES}/mathio)
619 add_subdirectory(${LIBRARIES}/utils)
620 add_subdirectory(${LIBRARIES}/viewer)
621 add_subdirectory(${FILAMENT}/filament)
622 add_subdirectory(${FILAMENT}/shaders)
623 add_subdirectory(${EXTERNAL}/civetweb/tnt)
624 add_subdirectory(${EXTERNAL}/hat-trie/tnt)
625 add_subdirectory(${EXTERNAL}/smol-v/tnt)
626 include(debian/cmake/benchmark.cmake)
627 add_subdirectory(${EXTERNAL}/cgltf/tnt)
628 include(debian/cmake/jsmn.cmake)
629 add_subdirectory(${EXTERNAL}/getopt)
630
631 if (FILAMENT_BUILD_FILAMAT OR IS_HOST_PLATFORM)
632 # spirv-tools must come before filamat, as filamat relies on the presence of the
633 # spirv-tools_SOURCE_DIR variable.
634 add_subdirectory(${EXTERNAL}/spirv-tools)
635 add_subdirectory(${EXTERNAL}/glslang/tnt)
636 add_subdirectory(${EXTERNAL}/spirv-cross/tnt)
637 add_subdirectory(${LIBRARIES}/filamat)
638
639 # the material debugger requires filamat
640 if (FILAMENT_ENABLE_MATDBG OR IS_HOST_PLATFORM)
641 add_subdirectory(${LIBRARIES}/matdbg)
642 endif()
643 endif()
644
645 if (FILAMENT_SUPPORTS_VULKAN)
646 add_subdirectory(${LIBRARIES}/bluevk)
647 add_subdirectory(${EXTERNAL}/vkmemalloc/tnt)
648 endif()
649
650 if (APPLE)
651 add_subdirectory(${EXTERNAL}/moltenvk/tnt)
652 endif()
653
654 set(FILAMENT_SAMPLES_BINARY_DIR ${PROJECT_BINARY_DIR}/samples)
655
656 if (WEBGL)
657 add_subdirectory(web/filament-js)
658 add_subdirectory(web/samples)
659 endif()
660
661 if (IS_HOST_PLATFORM)
662 add_subdirectory(${LIBRARIES}/bluegl)
663 add_subdirectory(${LIBRARIES}/filamentapp)
664 add_subdirectory(${LIBRARIES}/imageio)
665
666 add_subdirectory(${FILAMENT}/samples)
667
668 add_subdirectory(${EXTERNAL}/etc2comp)
669 include(debian/cmake/libassimp.cmake)
670
671 add_subdirectory(${TOOLS}/cmgen)
672 add_subdirectory(${TOOLS}/cso-lut)
673 add_subdirectory(${TOOLS}/filamesh)
674 add_subdirectory(${TOOLS}/glslminifier)
675 add_subdirectory(${TOOLS}/matc)
676 add_subdirectory(${TOOLS}/matinfo)
677 add_subdirectory(${TOOLS}/mipgen)
678 add_subdirectory(${TOOLS}/normal-blending)
679 add_subdirectory(${TOOLS}/resgen)
680 add_subdirectory(${TOOLS}/roughness-prefilter)
681 add_subdirectory(${TOOLS}/specular-color)
682 endif()
683
684 # Generate exported executables for cross-compiled builds (Android, WebGL, and iOS)
685 if (NOT CMAKE_CROSSCOMPILING)
686 export(TARGETS matc cmgen filamesh mipgen resgen glslminifier FILE ${IMPORT_EXECUTABLES})
687 endif()