| 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 |
string(APPEND CONTENT "${STR_OPENER}License and copyrights for ${module}:\n${STR_CLOSER},\n") |
| 479 |
file(READ ${license_path} license_long) |
| 480 |
string(REPLACE "\n" "${STR_CLOSER},\n${STR_OPENER}" license ${license_long}) |
| 481 |
string(APPEND CONTENT ${STR_OPENER}${license}\n${STR_CLOSER},) |
| 482 |
string(APPEND CONTENT "\n\n") |
| 483 |
endforeach() |
| 484 |
configure_file(${FILAMENT}/build/licenses.inc.in ${OUTPUT}) |
| 485 |
endfunction(list_licenses) |
| 486 |
|
| 487 |
set(COMBINE_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/build/linux/combine-static-libs.sh") |
| 488 |
if (WIN32) |
| 489 |
set(COMBINE_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/build/windows/combine-static-libs.bat") |
| 490 |
set(CMAKE_AR "lib.exe") |
| 491 |
endif() |
| 492 |
|
| 493 |
# Add a custom command to TARGET that combines the static libraries in DEPS into a single archive. |
| 494 |
function(combine_static_libs TARGET OUTPUT DEPS) |
| 495 |
# Loop through the dependent libraries and query their location on disk. |
| 496 |
set(DEPS_FILES ) |
| 497 |
foreach(DEPENDENCY ${DEPS}) |
| 498 |
list(APPEND DEPS_FILES "$<TARGET_FILE:${DEPENDENCY}>") |
| 499 |
endforeach() |
| 500 |
|
| 501 |
add_custom_command( |
| 502 |
TARGET ${TARGET} POST_BUILD |
| 503 |
COMMAND "${COMBINE_SCRIPT}" "${CMAKE_AR}" "${OUTPUT}" ${DEPS_FILES} |
| 504 |
COMMENT "Combining ${target} dependencies into single shared library" |
| 505 |
VERBATIM |
| 506 |
) |
| 507 |
endfunction() |
| 508 |
|
| 509 |
# ================================================================================================== |
| 510 |
# Configuration for CMAKE_CROSSCOMPILING. |
| 511 |
# ================================================================================================== |
| 512 |
if (WEBGL) |
| 513 |
set(IMPORT_EXECUTABLES ${FILAMENT}/${IMPORT_EXECUTABLES_DIR}/ImportExecutables-Release.cmake) |
| 514 |
else() |
| 515 |
set(IMPORT_EXECUTABLES ${FILAMENT}/${IMPORT_EXECUTABLES_DIR}/ImportExecutables-${CMAKE_BUILD_TYPE}.cmake) |
| 516 |
endif() |
| 517 |
|
| 518 |
# ================================================================================================== |
| 519 |
# Try to find Vulkan if the SDK is installed, otherwise fall back to the bundled version. |
| 520 |
# This needs to stay in our top-level CMakeLists because it sets up variables that are used by the |
| 521 |
# "bluevk" and "samples" targets. |
| 522 |
# ================================================================================================== |
| 523 |
|
| 524 |
if (FILAMENT_USE_SWIFTSHADER) |
| 525 |
if (NOT FILAMENT_SUPPORTS_VULKAN) |
| 526 |
message(ERROR "SwiftShader is only useful when Vulkan is enabled.") |
| 527 |
endif() |
| 528 |
find_library(SWIFTSHADER_VK NAMES vk_swiftshader HINTS "$ENV{SWIFTSHADER_LD_LIBRARY_PATH}") |
| 529 |
message(STATUS "Found SwiftShader VK library in: ${SWIFTSHADER_VK}.") |
| 530 |
add_definitions(-DFILAMENT_VKLIBRARY_PATH=\"${SWIFTSHADER_VK}\") |
| 531 |
elseif (FILAMENT_SUPPORTS_VULKAN AND APPLE) |
| 532 |
find_library(Vulkan_LIBRARY NAMES vulkan HINTS "$ENV{VULKAN_SDK}/lib" "$ENV{VULKAN_SDK}/macOS/lib") |
| 533 |
if (Vulkan_LIBRARY) |
| 534 |
set(Vulkan_FOUND ON) |
| 535 |
message(STATUS "Found Vulkan library in SDK: ${Vulkan_LIBRARY}.") |
| 536 |
add_definitions(-DFILAMENT_VKLIBRARY_PATH=\"${Vulkan_LIBRARY}\") |
| 537 |
endif() |
| 538 |
endif() |
| 539 |
|
| 540 |
# ================================================================================================== |
| 541 |
# Common Functions |
| 542 |
# ================================================================================================== |
| 543 |
|
| 544 |
# Sets the following variables: RESGEN_HEADER, RESGEN_SOURCE, RESGEN_FLAGS, RESGEN_SOURCE_FLAGS, |
| 545 |
# and RESGEN_OUTPUTS. Please pass in an ARCHIVE_NAME that is unique to your project, otherwise the |
| 546 |
# incbin directive will happily consume a blob from the wrong project without warnings or errors. |
| 547 |
# Also be sure to include the ASM language in the CMake "project" directive for your project. |
| 548 |
function(get_resgen_vars ARCHIVE_DIR ARCHIVE_NAME) |
| 549 |
set(OUTPUTS |
| 550 |
${ARCHIVE_DIR}/${ARCHIVE_NAME}.bin |
| 551 |
${ARCHIVE_DIR}/${ARCHIVE_NAME}.S |
| 552 |
${ARCHIVE_DIR}/${ARCHIVE_NAME}.apple.S |
| 553 |
${ARCHIVE_DIR}/${ARCHIVE_NAME}.h |
| 554 |
) |
| 555 |
if (IOS) |
| 556 |
set(ASM_ARCH_FLAG "-arch ${DIST_ARCH}") |
| 557 |
endif() |
| 558 |
if (APPLE) |
| 559 |
set(ASM_SUFFIX ".apple") |
| 560 |
endif() |
| 561 |
set(RESGEN_HEADER "${ARCHIVE_DIR}/${ARCHIVE_NAME}.h" PARENT_SCOPE) |
| 562 |
# Visual Studio makes it difficult to use assembly without using MASM. MASM doesn't support |
| 563 |
# the equivalent of .incbin, so on Windows we'll just tell resgen to output a C file. |
| 564 |
if (WEBGL OR WIN32 OR ANDROID_ON_WINDOWS) |
| 565 |
set(RESGEN_OUTPUTS "${OUTPUTS};${ARCHIVE_DIR}/${ARCHIVE_NAME}.c" PARENT_SCOPE) |
| 566 |
set(RESGEN_FLAGS -qcx ${ARCHIVE_DIR} -p ${ARCHIVE_NAME} PARENT_SCOPE) |
| 567 |
set(RESGEN_SOURCE "${ARCHIVE_DIR}/${ARCHIVE_NAME}.c" PARENT_SCOPE) |
| 568 |
else() |
| 569 |
set(RESGEN_OUTPUTS "${OUTPUTS}" PARENT_SCOPE) |
| 570 |
set(RESGEN_FLAGS -qx ${ARCHIVE_DIR} -p ${ARCHIVE_NAME} PARENT_SCOPE) |
| 571 |
set(RESGEN_SOURCE "${ARCHIVE_DIR}/${ARCHIVE_NAME}${ASM_SUFFIX}.S" PARENT_SCOPE) |
| 572 |
set(RESGEN_SOURCE_FLAGS "-I${ARCHIVE_DIR} ${ASM_ARCH_FLAG}" PARENT_SCOPE) |
| 573 |
endif() |
| 574 |
endfunction() |
| 575 |
|
| 576 |
# ================================================================================================== |
| 577 |
# External dependencies |
| 578 |
# ================================================================================================== |
| 579 |
find_package(GTest REQUIRED) |
| 580 |
find_package(astcenc REQUIRED) |
| 581 |
find_package(tsl-robin-map REQUIRED) |
| 582 |
find_package(meshoptimizer REQUIRED) |
| 583 |
find_package(draco REQUIRED) |
| 584 |
set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 585 |
find_package(Threads REQUIRED) |
| 586 |
if(IS_HOST_PLATFORM) |
| 587 |
find_package(PNG REQUIRED) |
| 588 |
find_package(SDL2 REQUIRED) |
| 589 |
find_package(ZLIB REQUIRED) |
| 590 |
find_package(tinyexr REQUIRED) |
| 591 |
endif() |
| 592 |
include(debian/cmake/imgui.cmake) |
| 593 |
include(debian/cmake/stb.cmake) |
| 594 |
|
| 595 |
# ================================================================================================== |
| 596 |
# Sub-projects |
| 597 |
# ================================================================================================== |
| 598 |
|
| 599 |
# Common to all platforms |
| 600 |
add_subdirectory(${LIBRARIES}/camutils) |
| 601 |
add_subdirectory(${LIBRARIES}/filabridge) |
| 602 |
add_subdirectory(${LIBRARIES}/filaflat) |
| 603 |
add_subdirectory(${LIBRARIES}/filagui) |
| 604 |
add_subdirectory(${LIBRARIES}/filameshio) |
| 605 |
add_subdirectory(${LIBRARIES}/geometry) |
| 606 |
add_subdirectory(${LIBRARIES}/gltfio) |
| 607 |
add_subdirectory(${LIBRARIES}/ibl) |
| 608 |
add_subdirectory(${LIBRARIES}/iblprefilter) |
| 609 |
add_subdirectory(${LIBRARIES}/image) |
| 610 |
add_subdirectory(${LIBRARIES}/math) |
| 611 |
add_subdirectory(${LIBRARIES}/mathio) |
| 612 |
add_subdirectory(${LIBRARIES}/utils) |
| 613 |
add_subdirectory(${LIBRARIES}/viewer) |
| 614 |
add_subdirectory(${FILAMENT}/filament) |
| 615 |
add_subdirectory(${FILAMENT}/shaders) |
| 616 |
add_subdirectory(${EXTERNAL}/civetweb/tnt) |
| 617 |
add_subdirectory(${EXTERNAL}/hat-trie/tnt) |
| 618 |
add_subdirectory(${EXTERNAL}/smol-v/tnt) |
| 619 |
include(debian/cmake/benchmark.cmake) |
| 620 |
add_subdirectory(${EXTERNAL}/cgltf/tnt) |
| 621 |
include(debian/cmake/jsmn.cmake) |
| 622 |
add_subdirectory(${EXTERNAL}/getopt) |
| 623 |
|
| 624 |
if (FILAMENT_BUILD_FILAMAT OR IS_HOST_PLATFORM) |
| 625 |
# spirv-tools must come before filamat, as filamat relies on the presence of the |
| 626 |
# spirv-tools_SOURCE_DIR variable. |
| 627 |
add_subdirectory(${EXTERNAL}/spirv-tools) |
| 628 |
add_subdirectory(${EXTERNAL}/glslang/tnt) |
| 629 |
add_subdirectory(${EXTERNAL}/spirv-cross/tnt) |
| 630 |
add_subdirectory(${LIBRARIES}/filamat) |
| 631 |
|
| 632 |
# the material debugger requires filamat |
| 633 |
if (FILAMENT_ENABLE_MATDBG OR IS_HOST_PLATFORM) |
| 634 |
add_subdirectory(${LIBRARIES}/matdbg) |
| 635 |
endif() |
| 636 |
endif() |
| 637 |
|
| 638 |
if (FILAMENT_SUPPORTS_VULKAN) |
| 639 |
add_subdirectory(${LIBRARIES}/bluevk) |
| 640 |
add_subdirectory(${EXTERNAL}/vkmemalloc/tnt) |
| 641 |
endif() |
| 642 |
|
| 643 |
if (APPLE) |
| 644 |
add_subdirectory(${EXTERNAL}/moltenvk/tnt) |
| 645 |
endif() |
| 646 |
|
| 647 |
set(FILAMENT_SAMPLES_BINARY_DIR ${PROJECT_BINARY_DIR}/samples) |
| 648 |
|
| 649 |
if (WEBGL) |
| 650 |
add_subdirectory(web/filament-js) |
| 651 |
add_subdirectory(web/samples) |
| 652 |
endif() |
| 653 |
|
| 654 |
if (IS_HOST_PLATFORM) |
| 655 |
add_subdirectory(${LIBRARIES}/bluegl) |
| 656 |
add_subdirectory(${LIBRARIES}/filamentapp) |
| 657 |
add_subdirectory(${LIBRARIES}/imageio) |
| 658 |
|
| 659 |
add_subdirectory(${FILAMENT}/samples) |
| 660 |
|
| 661 |
add_subdirectory(${EXTERNAL}/etc2comp) |
| 662 |
include(debian/cmake/libassimp.cmake) |
| 663 |
|
| 664 |
add_subdirectory(${TOOLS}/cmgen) |
| 665 |
add_subdirectory(${TOOLS}/cso-lut) |
| 666 |
add_subdirectory(${TOOLS}/filamesh) |
| 667 |
add_subdirectory(${TOOLS}/glslminifier) |
| 668 |
add_subdirectory(${TOOLS}/matc) |
| 669 |
add_subdirectory(${TOOLS}/matinfo) |
| 670 |
add_subdirectory(${TOOLS}/mipgen) |
| 671 |
add_subdirectory(${TOOLS}/normal-blending) |
| 672 |
add_subdirectory(${TOOLS}/resgen) |
| 673 |
add_subdirectory(${TOOLS}/roughness-prefilter) |
| 674 |
add_subdirectory(${TOOLS}/specular-color) |
| 675 |
endif() |
| 676 |
|
| 677 |
# Generate exported executables for cross-compiled builds (Android, WebGL, and iOS) |
| 678 |
if (NOT CMAKE_CROSSCOMPILING) |
| 679 |
export(TARGETS matc cmgen filamesh mipgen resgen glslminifier FILE ${IMPORT_EXECUTABLES}) |
| 680 |
endif() |