CMakeLists.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
  2. project(JoltPhysics VERSION 5.4.1 LANGUAGES CXX)
  3. # When turning this option on, the library will be compiled using assertions. By default asserts are enabled in Debug build.
  4. option(USE_ASSERTS "Enable asserts" OFF)
  5. # When turning this option on, the library will be compiled using doubles for positions. This allows for much bigger worlds.
  6. option(DOUBLE_PRECISION "Use double precision math" OFF)
  7. # When turning this option on, the library will be compiled with debug symbols
  8. option(GENERATE_DEBUG_SYMBOLS "Generate debug symbols" ON)
  9. # Which type of debug symbols to generate, e.g. using source-map when compiling with emscripten makes compilation a lot faster
  10. set(JPH_DEBUG_SYMBOL_FORMAT "" CACHE STRING "Which type of debug symbols to generate")
  11. # When turning this option on, the library will override the default CMAKE_CXX_FLAGS_DEBUG/RELEASE values, otherwise they will use the platform defaults
  12. option(OVERRIDE_CXX_FLAGS "Override CMAKE_CXX_FLAGS_DEBUG/RELEASE" ON)
  13. # When turning this option on, the library will be compiled in such a way to attempt to keep the simulation deterministic across platforms
  14. option(CROSS_PLATFORM_DETERMINISTIC "Cross platform deterministic" OFF)
  15. # When turning this option on, the library will be compiled for ARM using the CROSS_COMPILE_ARM_TARGET architecture, requires compiling with clang
  16. option(CROSS_COMPILE_ARM "Cross compile to the CROSS_COMPILE_ARM_TARGET architecture" OFF)
  17. # When cross compiling to ARM this specifies which target to use. Can be 'aarch64-linux-gnu' for 64-bit or 'arm-linux-gnueabihf' for 32-bit
  18. set(CROSS_COMPILE_ARM_TARGET "aarch64-linux-gnu" CACHE STRING "The target to use")
  19. # When turning this option on, Jolt will be compiled as a shared library and public symbols will be exported.
  20. option(BUILD_SHARED_LIBS "Compile Jolt as a shared library" OFF)
  21. # When turning this option on, the library will be compiled with interprocedural optimizations enabled, also known as link-time optimizations or link-time code generation.
  22. # Note that if you turn this on you need to use SET_INTERPROCEDURAL_OPTIMIZATION() or set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) to enable LTO specifically for your own project as well.
  23. # If you don't do this you may get an error: /usr/bin/ld: libJolt.a: error adding symbols: file format not recognized
  24. option(INTERPROCEDURAL_OPTIMIZATION "Enable interprocedural optimizations" ON)
  25. # When turning this on, in Debug and Release mode, the library will emit extra code to ensure that the 4th component of a 3-vector is kept the same as the 3rd component
  26. # and will enable floating point exceptions during simulation to detect divisions by zero.
  27. # Note that this currently only works using MSVC. Clang turns Float2 into a SIMD vector sometimes causing floating point exceptions (the option is ignored).
  28. option(FLOATING_POINT_EXCEPTIONS_ENABLED "Enable floating point exceptions" ON)
  29. # When turning this on, the library will be compiled with C++ exceptions enabled.
  30. # This adds some overhead and Jolt doesn't use exceptions so by default it is off.
  31. option(CPP_EXCEPTIONS_ENABLED "Enable C++ exceptions" OFF)
  32. # When turning this on, the library will be compiled with C++ RTTI enabled.
  33. # This adds some overhead and Jolt doesn't use RTTI so by default it is off.
  34. option(CPP_RTTI_ENABLED "Enable C++ RTTI" OFF)
  35. # Number of bits to use in ObjectLayer. Can be 16 or 32.
  36. option(OBJECT_LAYER_BITS "Number of bits in ObjectLayer" 16)
  37. # Select X86 processor features to use (if everything is off it will be SSE2 compatible)
  38. option(USE_SSE4_1 "Enable SSE4.1" ON)
  39. option(USE_SSE4_2 "Enable SSE4.2" ON)
  40. option(USE_AVX "Enable AVX" ON)
  41. option(USE_AVX2 "Enable AVX2" ON)
  42. option(USE_AVX512 "Enable AVX512" OFF)
  43. option(USE_LZCNT "Enable LZCNT" ON)
  44. option(USE_TZCNT "Enable TZCNT" ON)
  45. option(USE_F16C "Enable F16C" ON)
  46. option(USE_FMADD "Enable FMADD" ON)
  47. # Enable SIMD for the WASM build. Note that this is currently off by default since not all browsers support this.
  48. # See: https://caniuse.com/?search=WebAssembly%20SIMD (Safari got support in March 2023 and was the last major browser to get support).
  49. option(USE_WASM_SIMD "Enable SIMD for WASM" OFF)
  50. # Enable 64 bit WASM instead of the default 32 bit WASM. Note that this currently requires special commandline flags in browsers and nodejs to enable.
  51. # E.g. use 'node --experimental-wasm-memory64 UnitTests.js' to run the unit tests in nodejs in 64 bit.
  52. option(JPH_USE_WASM64 "Enable 64 bit WASM" OFF)
  53. # Enable all warnings
  54. option(ENABLE_ALL_WARNINGS "Enable all warnings and warnings as errors" ON)
  55. # Setting to periodically trace broadphase stats to help determine if the broadphase layer configuration is optimal
  56. option(TRACK_BROADPHASE_STATS "Track Broadphase Stats" OFF)
  57. # Setting to periodically trace narrowphase stats to help determine which collision queries could be optimized
  58. option(TRACK_NARROWPHASE_STATS "Track Narrowphase Stats" OFF)
  59. # Setting to track simulation timings per body
  60. option(JPH_TRACK_SIMULATION_STATS "Track Simulation Stats" OFF)
  61. # Enable the debug renderer in the Debug and Release builds. Note that DEBUG_RENDERER_IN_DISTRIBUTION will override this setting.
  62. option(DEBUG_RENDERER_IN_DEBUG_AND_RELEASE "Enable debug renderer in Debug and Release builds" ON)
  63. # Setting to enable the debug renderer in all builds.
  64. # Note that enabling this reduces the performance of the library even if you're not drawing anything.
  65. option(DEBUG_RENDERER_IN_DISTRIBUTION "Enable debug renderer in all builds" OFF)
  66. # Enable the profiler in Debug and Release builds. Note that PROFILER_IN_DISTRIBUTION will override this setting.
  67. option(PROFILER_IN_DEBUG_AND_RELEASE "Enable the profiler in Debug and Release builds" ON)
  68. # Enable the profiler in all builds.
  69. # Note that enabling this reduces the performance of the library.
  70. option(PROFILER_IN_DISTRIBUTION "Enable the profiler in all builds" OFF)
  71. # Ability to use the external profiler using CMake config. Defines preprocessor JPH_EXTERNAL_PROFILE.
  72. # Use external profiler set using ProfileStartMeasurement and ProfileEndMeasurement to profile.
  73. # Option is available only when profiling is enabled using PROFILER_IN_DEBUG_AND_RELEASE or PROFILER_IN_DISTRIBUTION.
  74. option(JPH_USE_EXTERNAL_PROFILE "Use external profiler when profiling is enabled" OFF)
  75. # Setting this option will force the library to use malloc/free instead of allowing the user to override the memory allocator
  76. option(DISABLE_CUSTOM_ALLOCATOR "Disable support for a custom memory allocator" OFF)
  77. # Setting this option will force the library to use the STL vector instead of the custom Array class
  78. option(USE_STD_VECTOR "Use std::vector instead of own Array class" OFF)
  79. # Setting this option will compile the ObjectStream class and RTTI attribute information
  80. option(ENABLE_OBJECT_STREAM "Compile the ObjectStream class and RTTI attribute information" ON)
  81. # Enable installation
  82. option(ENABLE_INSTALL "Generate installation target" ON)
  83. include(CMakeDependentOption)
  84. # Ability to toggle between the static and DLL versions of the MSVC runtime library
  85. # Windows Store only supports the DLL version
  86. cmake_dependent_option(USE_STATIC_MSVC_RUNTIME_LIBRARY "Use the static MSVC runtime library" ON "MSVC;NOT WINDOWS_STORE" OFF)
  87. # Enable Vulkan instead of DirectX
  88. cmake_dependent_option(JPH_ENABLE_VULKAN "Enable Vulkan" ON "LINUX" OFF)
  89. # Determine which configurations exist
  90. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) # Only do this when we're at the top level, see: https://gitlab.kitware.com/cmake/cmake/-/issues/24181
  91. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  92. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
  93. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  94. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseASAN;ReleaseUBSAN;ReleaseTSAN;ReleaseCoverage;Distribution")
  95. endif()
  96. endif()
  97. if (MSVC)
  98. # Fill in the path to the asan libraries
  99. set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
  100. # 64 bit architecture
  101. set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
  102. # Set runtime library
  103. if (USE_STATIC_MSVC_RUNTIME_LIBRARY)
  104. set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  105. endif()
  106. # Set general compiler flags
  107. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")
  108. # Enable warnings
  109. if (ENABLE_ALL_WARNINGS)
  110. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /WX")
  111. endif()
  112. # Optionally generate debug symbols
  113. if (GENERATE_DEBUG_SYMBOLS)
  114. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
  115. endif()
  116. if (NOT CPP_RTTI_ENABLED)
  117. # Set compiler flag for disabling RTTI
  118. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  119. else()
  120. # Set compiler flag for enabling RTTI
  121. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR")
  122. endif()
  123. if (NOT CPP_EXCEPTIONS_ENABLED)
  124. # Remove any existing compiler flag that enables exceptions
  125. string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  126. # Disable warning about STL and compiler-generated types using noexcept when exceptions are disabled
  127. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4577")
  128. else()
  129. # Enable exceptions
  130. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  131. endif()
  132. # Set compiler flags for various configurations
  133. if (OVERRIDE_CXX_FLAGS)
  134. set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1")
  135. set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot")
  136. endif()
  137. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  138. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address /Od")
  139. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  140. set(CMAKE_CXX_FLAGS_RELEASETSAN "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=thread")
  141. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-fprofile-instr-generate -fcoverage-mapping")
  142. # Set linker flags
  143. set(CMAKE_EXE_LINKER_FLAGS "/SUBSYSTEM:WINDOWS /ignore:4221")
  144. if (GENERATE_DEBUG_SYMBOLS)
  145. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")
  146. endif()
  147. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  148. if (CROSS_PLATFORM_DETERMINISTIC)
  149. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
  150. else()
  151. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:fast") # Clang doesn't use fast math because it cannot be turned off inside a single compilation unit
  152. endif()
  153. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  154. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
  155. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
  156. set(CMAKE_EXE_LINKER_FLAGS_RELEASEASAN "/SUBSYSTEM:CONSOLE /LIBPATH:${CLANG_LIB_PATH} clang_rt.asan-x86_64.lib -wholearchive:clang_rt.asan-x86_64.lib clang_rt.asan_cxx-x86_64.lib -wholearchive:clang_rt.asan_cxx-x86_64.lib")
  157. set(CMAKE_EXE_LINKER_FLAGS_RELEASEUBSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  158. set(CMAKE_EXE_LINKER_FLAGS_RELEASETSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  159. set(CMAKE_EXE_LINKER_FLAGS_RELEASECOVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  160. endif()
  161. else()
  162. # Enable warnings
  163. if (ENABLE_ALL_WARNINGS)
  164. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
  165. endif()
  166. # Optionally generate debug symbols
  167. if (GENERATE_DEBUG_SYMBOLS)
  168. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g${JPH_DEBUG_SYMBOL_FORMAT}")
  169. endif()
  170. if (NOT CPP_RTTI_ENABLED)
  171. # Set compiler flag for disabling RTTI
  172. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
  173. else()
  174. # Set compiler flag for enabling RTTI
  175. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")
  176. endif()
  177. if (NOT CPP_EXCEPTIONS_ENABLED)
  178. # Set compiler flag for disabling exception-handling
  179. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
  180. else()
  181. # Set compiler flag for enabling exception-handling
  182. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
  183. endif()
  184. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  185. # Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
  186. # Also disable -Wno-psabi to avoid messages of the form note: parameter passing for argument of type '...' changed in GCC 7.1
  187. # Also turn off automatic fused multiply add contractions, there doesn't seem to be a way to do this selectively through the macro JPH_PRECISE_MATH_OFF
  188. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -Wno-psabi -ffp-contract=off")
  189. else()
  190. # Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
  191. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")
  192. # On clang 14 and later we can turn off float contraction through a pragma, older versions and deterministic versions need it off always, see Core.h
  193. # clang on LoongArch does not support such pragma, also turn off contraction for it.
  194. if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "loongarch")
  195. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
  196. # Clang 20 and later complain with: overriding '-ffp-model=precise' option with '-ffp-contract=off' [-Woverriding-option], but this is exactly what we want.
  197. if (CMAKE_CXX_COMPILER_VERSION GREATER 19)
  198. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-overriding-option")
  199. endif()
  200. endif()
  201. # Cross compiler flags
  202. if (CROSS_COMPILE_ARM AND NOT ("${CROSS_COMPILE_ARM_TARGET}" STREQUAL ""))
  203. set(CMAKE_CXX_FLAGS "--target=${CROSS_COMPILE_ARM_TARGET} ${CMAKE_CXX_FLAGS}")
  204. endif()
  205. endif()
  206. # See https://github.com/jrouwe/JoltPhysics/issues/922. When compiling with DOUBLE_PRECISION=YES and CMAKE_OSX_DEPLOYMENT_TARGET=10.12 clang triggers a warning that we silence here.
  207. if ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  208. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-allocation")
  209. endif()
  210. # Set compiler flags for various configurations
  211. if (OVERRIDE_CXX_FLAGS)
  212. set(CMAKE_CXX_FLAGS_DEBUG "")
  213. set(CMAKE_CXX_FLAGS_RELEASE "-O3")
  214. endif()
  215. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  216. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address")
  217. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  218. set(CMAKE_CXX_FLAGS_RELEASETSAN "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=thread")
  219. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-O0 -DJPH_NO_FORCE_INLINE -fprofile-instr-generate -fcoverage-mapping")
  220. endif()
  221. # Set linker flags
  222. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
  223. # Enable link time optimization in Release and Distribution mode if requested and available
  224. function(SET_INTERPROCEDURAL_OPTIMIZATION)
  225. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE OFF PARENT_SCOPE)
  226. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION OFF PARENT_SCOPE)
  227. # On ARM, whole program optimization triggers an internal compiler error during code gen, so we don't turn it on
  228. # When compiling as a shared lib with MinGW, turning on LTO causes errors of the form 'ld.exe: cannot export symbol X wrong type (4 vs 3)'
  229. if (INTERPROCEDURAL_OPTIMIZATION
  230. AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM64")
  231. AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
  232. AND (NOT CROSS_COMPILE_ARM OR ("${CROSS_COMPILE_ARM_TARGET}" STREQUAL "aarch64-linux-gnu"))
  233. AND NOT (MINGW AND BUILD_SHARED_LIBS))
  234. include(CheckIPOSupported)
  235. check_ipo_supported(RESULT IS_IPO_SUPPORTED OUTPUT IPO_CHECK_OUTPUT)
  236. if (IS_IPO_SUPPORTED)
  237. message("Interprocedural optimizations are turned on")
  238. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON PARENT_SCOPE)
  239. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION ON PARENT_SCOPE)
  240. else()
  241. message("Warning: Interprocedural optimizations are not supported for this target, turn off the option INTERPROCEDURAL_OPTIMIZATION to disable this warning")
  242. endif()
  243. endif()
  244. endfunction()
  245. SET_INTERPROCEDURAL_OPTIMIZATION()
  246. # Set repository root
  247. set(PHYSICS_REPO_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../)
  248. # Make Jolt Library
  249. include(${PHYSICS_REPO_ROOT}/Jolt/Jolt.cmake)
  250. if (XCODE)
  251. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  252. set_property(TARGET Jolt PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  253. endif()
  254. # Install Jolt library and includes
  255. if (ENABLE_INSTALL)
  256. include(GNUInstallDirs)
  257. install(TARGETS Jolt
  258. EXPORT JoltExport
  259. INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  260. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  261. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  262. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  263. foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
  264. string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
  265. get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
  266. if (NOT RELATIVE_SRC_FILE MATCHES "\.cpp")
  267. cmake_path(SET DST_FILE NORMALIZE "${CMAKE_INSTALL_INCLUDEDIR}/${DESTINATION_PATH}")
  268. install(FILES ${SRC_FILE} DESTINATION ${DST_FILE})
  269. endif()
  270. endforeach()
  271. # Export Jolt library
  272. export(TARGETS Jolt
  273. NAMESPACE Jolt::
  274. FILE JoltConfig.cmake)
  275. install(EXPORT JoltExport
  276. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Jolt/
  277. NAMESPACE Jolt::
  278. FILE JoltConfig.cmake)
  279. endif()
  280. # Check if we're the root CMakeLists.txt, if not we are included by another CMake file and we should disable everything except for the main library
  281. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  282. # Ability to turn ON/OFF individual applications
  283. option(TARGET_UNIT_TESTS "Build Unit Tests" ON)
  284. option(TARGET_HELLO_WORLD "Build Hello World" ON)
  285. option(TARGET_PERFORMANCE_TEST "Build Performance Test" ON)
  286. option(TARGET_SAMPLES "Build Samples" ON)
  287. option(TARGET_VIEWER "Build JoltViewer" ON)
  288. if (TARGET_UNIT_TESTS)
  289. # Create UnitTests executable
  290. include(${PHYSICS_REPO_ROOT}/UnitTests/UnitTests.cmake)
  291. add_executable(UnitTests ${UNIT_TESTS_SRC_FILES})
  292. target_include_directories(UnitTests PUBLIC ${UNIT_TESTS_ROOT})
  293. target_link_libraries(UnitTests LINK_PUBLIC Jolt)
  294. if (EMSCRIPTEN)
  295. target_link_options(UnitTests PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  296. endif()
  297. # Code coverage doesn't work when using precompiled headers
  298. if (CMAKE_GENERATOR STREQUAL "Ninja Multi-Config" AND MSVC)
  299. # The Ninja Multi-Config generator errors out when selectively disabling precompiled headers for certain configurations.
  300. # See: https://github.com/jrouwe/JoltPhysics/issues/1211
  301. target_precompile_headers(UnitTests PRIVATE "${JOLT_PHYSICS_ROOT}/Jolt.h")
  302. else()
  303. target_precompile_headers(UnitTests PRIVATE "$<$<NOT:$<CONFIG:ReleaseCoverage>>:${JOLT_PHYSICS_ROOT}/Jolt.h>")
  304. endif()
  305. if (MSVC)
  306. target_link_options(UnitTests PUBLIC "/SUBSYSTEM:CONSOLE")
  307. endif()
  308. if (IOS)
  309. # Set the bundle information
  310. set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
  311. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
  312. endif()
  313. if (XCODE)
  314. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  315. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  316. endif()
  317. # Register unit tests as a test so that it can be run with:
  318. # ctest --output-on-failure
  319. enable_testing()
  320. add_test(UnitTests UnitTests)
  321. endif()
  322. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  323. if (TARGET_HELLO_WORLD)
  324. # Example 'Hello World' application
  325. include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
  326. add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
  327. target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
  328. target_link_libraries(HelloWorld LINK_PUBLIC Jolt)
  329. if (MSVC)
  330. target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
  331. endif()
  332. if (EMSCRIPTEN)
  333. target_link_options(HelloWorld PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  334. endif()
  335. endif()
  336. if (TARGET_PERFORMANCE_TEST)
  337. # Performance Test application
  338. include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
  339. add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
  340. target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
  341. target_link_libraries(PerformanceTest LINK_PUBLIC Jolt)
  342. if (MSVC)
  343. target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
  344. endif()
  345. if (EMSCRIPTEN)
  346. # Embed the assets for the RagdollScene
  347. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/Human.tof@/Assets/Human.tof")
  348. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/Human/dead_pose1.tof@/Assets/Human/dead_pose1.tof")
  349. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/terrain2.bof@/Assets/terrain2.bof")
  350. target_link_options(PerformanceTest PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  351. endif()
  352. set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
  353. endif()
  354. endif()
  355. if ((WIN32 OR LINUX OR ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")) AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")) # ARM 32-bit is missing dinput8.lib
  356. # Windows only targets
  357. if (TARGET_SAMPLES OR TARGET_VIEWER)
  358. include(${PHYSICS_REPO_ROOT}/TestFramework/TestFramework.cmake)
  359. endif()
  360. if (TARGET_SAMPLES)
  361. if (TEST_FRAMEWORK_AVAILABLE)
  362. include(${PHYSICS_REPO_ROOT}/Samples/Samples.cmake)
  363. else()
  364. message("Cannot build Samples because Vulkan/DirectX SDK is not available!")
  365. endif()
  366. endif()
  367. if (TARGET_VIEWER)
  368. if (TEST_FRAMEWORK_AVAILABLE)
  369. include(${PHYSICS_REPO_ROOT}/JoltViewer/JoltViewer.cmake)
  370. else()
  371. message("Cannot build JoltViewer because Vulkan/DirectX SDK is not available!")
  372. endif()
  373. endif()
  374. endif()
  375. endif()