CMakeLists.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
  2. project(JoltPhysics VERSION 5.4.0 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. # Enable the debug renderer in the Debug and Release builds. Note that DEBUG_RENDERER_IN_DISTRIBUTION will override this setting.
  60. option(DEBUG_RENDERER_IN_DEBUG_AND_RELEASE "Enable debug renderer in Debug and Release builds" ON)
  61. # Setting to enable the debug renderer in all builds.
  62. # Note that enabling this reduces the performance of the library even if you're not drawing anything.
  63. option(DEBUG_RENDERER_IN_DISTRIBUTION "Enable debug renderer in all builds" OFF)
  64. # Enable the profiler in Debug and Release builds. Note that PROFILER_IN_DISTRIBUTION will override this setting.
  65. option(PROFILER_IN_DEBUG_AND_RELEASE "Enable the profiler in Debug and Release builds" ON)
  66. # Enable the profiler in all builds.
  67. # Note that enabling this reduces the performance of the library.
  68. option(PROFILER_IN_DISTRIBUTION "Enable the profiler in all builds" OFF)
  69. # Ability to use the external profiler using CMake config. Defines preprocessor JPH_EXTERNAL_PROFILE.
  70. # Use external profiler set using ProfileStartMeasurement and ProfileEndMeasurement to profile.
  71. # Option is available only when profiling is enabled using PROFILER_IN_DEBUG_AND_RELEASE or PROFILER_IN_DISTRIBUTION.
  72. option(JPH_USE_EXTERNAL_PROFILE "Use external profiler when profiling is enabled" OFF)
  73. # Setting this option will force the library to use malloc/free instead of allowing the user to override the memory allocator
  74. option(DISABLE_CUSTOM_ALLOCATOR "Disable support for a custom memory allocator" OFF)
  75. # Setting this option will force the library to use the STL vector instead of the custom Array class
  76. option(USE_STD_VECTOR "Use std::vector instead of own Array class" OFF)
  77. # Setting this option will compile the ObjectStream class and RTTI attribute information
  78. option(ENABLE_OBJECT_STREAM "Compile the ObjectStream class and RTTI attribute information" ON)
  79. # Enable installation
  80. option(ENABLE_INSTALL "Generate installation target" ON)
  81. include(CMakeDependentOption)
  82. # Ability to toggle between the static and DLL versions of the MSVC runtime library
  83. # Windows Store only supports the DLL version
  84. cmake_dependent_option(USE_STATIC_MSVC_RUNTIME_LIBRARY "Use the static MSVC runtime library" ON "MSVC;NOT WINDOWS_STORE" OFF)
  85. # Enable Vulkan instead of DirectX
  86. cmake_dependent_option(JPH_ENABLE_VULKAN "Enable Vulkan" ON "LINUX" OFF)
  87. # Determine which configurations exist
  88. 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
  89. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  90. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
  91. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  92. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseASAN;ReleaseUBSAN;ReleaseTSAN;ReleaseCoverage;Distribution")
  93. endif()
  94. endif()
  95. if (MSVC)
  96. # Fill in the path to the asan libraries
  97. set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
  98. # 64 bit architecture
  99. set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
  100. # Set runtime library
  101. if (USE_STATIC_MSVC_RUNTIME_LIBRARY)
  102. set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  103. endif()
  104. # Set general compiler flags
  105. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")
  106. # Enable warnings
  107. if (ENABLE_ALL_WARNINGS)
  108. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /WX")
  109. endif()
  110. # Optionally generate debug symbols
  111. if (GENERATE_DEBUG_SYMBOLS)
  112. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
  113. endif()
  114. if (NOT CPP_RTTI_ENABLED)
  115. # Set compiler flag for disabling RTTI
  116. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  117. else()
  118. # Set compiler flag for enabling RTTI
  119. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR")
  120. endif()
  121. if (NOT CPP_EXCEPTIONS_ENABLED)
  122. # Remove any existing compiler flag that enables exceptions
  123. string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  124. # Disable warning about STL and compiler-generated types using noexcept when exceptions are disabled
  125. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4577")
  126. else()
  127. # Enable exceptions
  128. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  129. endif()
  130. # Set compiler flags for various configurations
  131. if (OVERRIDE_CXX_FLAGS)
  132. set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1")
  133. set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot")
  134. endif()
  135. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  136. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address /Od")
  137. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  138. set(CMAKE_CXX_FLAGS_RELEASETSAN "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=thread")
  139. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-fprofile-instr-generate -fcoverage-mapping")
  140. # Set linker flags
  141. set(CMAKE_EXE_LINKER_FLAGS "/SUBSYSTEM:WINDOWS /ignore:4221")
  142. if (GENERATE_DEBUG_SYMBOLS)
  143. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")
  144. endif()
  145. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  146. if (CROSS_PLATFORM_DETERMINISTIC)
  147. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
  148. else()
  149. 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
  150. endif()
  151. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  152. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
  153. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
  154. 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")
  155. set(CMAKE_EXE_LINKER_FLAGS_RELEASEUBSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  156. set(CMAKE_EXE_LINKER_FLAGS_RELEASETSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  157. set(CMAKE_EXE_LINKER_FLAGS_RELEASECOVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  158. endif()
  159. else()
  160. # Enable warnings
  161. if (ENABLE_ALL_WARNINGS)
  162. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
  163. endif()
  164. # Optionally generate debug symbols
  165. if (GENERATE_DEBUG_SYMBOLS)
  166. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g${JPH_DEBUG_SYMBOL_FORMAT}")
  167. endif()
  168. if (NOT CPP_RTTI_ENABLED)
  169. # Set compiler flag for disabling RTTI
  170. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
  171. else()
  172. # Set compiler flag for enabling RTTI
  173. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")
  174. endif()
  175. if (NOT CPP_EXCEPTIONS_ENABLED)
  176. # Set compiler flag for disabling exception-handling
  177. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
  178. else()
  179. # Set compiler flag for enabling exception-handling
  180. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
  181. endif()
  182. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  183. # Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
  184. # Also disable -Wno-psabi to avoid messages of the form note: parameter passing for argument of type '...' changed in GCC 7.1
  185. # 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
  186. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -Wno-psabi -ffp-contract=off")
  187. else()
  188. # Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
  189. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")
  190. # 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
  191. # clang on LoongArch does not support such pragma, also turn off contraction for it.
  192. if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "loongarch")
  193. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
  194. # Clang 20 and later complain with: overriding '-ffp-model=precise' option with '-ffp-contract=off' [-Woverriding-option], but this is exactly what we want.
  195. if (CMAKE_CXX_COMPILER_VERSION GREATER 19)
  196. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-overriding-option")
  197. endif()
  198. endif()
  199. # Cross compiler flags
  200. if (CROSS_COMPILE_ARM AND NOT ("${CROSS_COMPILE_ARM_TARGET}" STREQUAL ""))
  201. set(CMAKE_CXX_FLAGS "--target=${CROSS_COMPILE_ARM_TARGET} ${CMAKE_CXX_FLAGS}")
  202. endif()
  203. endif()
  204. # 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.
  205. if ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  206. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-allocation")
  207. endif()
  208. # Set compiler flags for various configurations
  209. if (OVERRIDE_CXX_FLAGS)
  210. set(CMAKE_CXX_FLAGS_DEBUG "")
  211. set(CMAKE_CXX_FLAGS_RELEASE "-O3")
  212. endif()
  213. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  214. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address")
  215. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  216. set(CMAKE_CXX_FLAGS_RELEASETSAN "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=thread")
  217. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-O0 -DJPH_NO_FORCE_INLINE -fprofile-instr-generate -fcoverage-mapping")
  218. endif()
  219. # Set linker flags
  220. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
  221. # Enable link time optimization in Release and Distribution mode if requested and available
  222. function(SET_INTERPROCEDURAL_OPTIMIZATION)
  223. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE OFF PARENT_SCOPE)
  224. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION OFF PARENT_SCOPE)
  225. # On ARM, whole program optimization triggers an internal compiler error during code gen, so we don't turn it on
  226. # 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)'
  227. if (INTERPROCEDURAL_OPTIMIZATION
  228. AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM64")
  229. AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
  230. AND (NOT CROSS_COMPILE_ARM OR ("${CROSS_COMPILE_ARM_TARGET}" STREQUAL "aarch64-linux-gnu"))
  231. AND NOT (MINGW AND BUILD_SHARED_LIBS))
  232. include(CheckIPOSupported)
  233. check_ipo_supported(RESULT IS_IPO_SUPPORTED OUTPUT IPO_CHECK_OUTPUT)
  234. if (IS_IPO_SUPPORTED)
  235. message("Interprocedural optimizations are turned on")
  236. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON PARENT_SCOPE)
  237. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION ON PARENT_SCOPE)
  238. else()
  239. message("Warning: Interprocedural optimizations are not supported for this target, turn off the option INTERPROCEDURAL_OPTIMIZATION to disable this warning")
  240. endif()
  241. endif()
  242. endfunction()
  243. SET_INTERPROCEDURAL_OPTIMIZATION()
  244. # Set repository root
  245. set(PHYSICS_REPO_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../)
  246. # Make Jolt Library
  247. include(${PHYSICS_REPO_ROOT}/Jolt/Jolt.cmake)
  248. if (XCODE)
  249. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  250. set_property(TARGET Jolt PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  251. endif()
  252. # Install Jolt library and includes
  253. if (ENABLE_INSTALL)
  254. include(GNUInstallDirs)
  255. install(TARGETS Jolt
  256. EXPORT JoltExport
  257. INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  258. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  259. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  260. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  261. foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
  262. string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
  263. get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
  264. if (NOT RELATIVE_SRC_FILE MATCHES "\.cpp")
  265. cmake_path(SET DST_FILE NORMALIZE "${CMAKE_INSTALL_INCLUDEDIR}/${DESTINATION_PATH}")
  266. install(FILES ${SRC_FILE} DESTINATION ${DST_FILE})
  267. endif()
  268. endforeach()
  269. # Export Jolt library
  270. export(TARGETS Jolt
  271. NAMESPACE Jolt::
  272. FILE JoltConfig.cmake)
  273. install(EXPORT JoltExport
  274. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Jolt/
  275. NAMESPACE Jolt::
  276. FILE JoltConfig.cmake)
  277. endif()
  278. # 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
  279. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  280. # Ability to turn ON/OFF individual applications
  281. option(TARGET_UNIT_TESTS "Build Unit Tests" ON)
  282. option(TARGET_HELLO_WORLD "Build Hello World" ON)
  283. option(TARGET_PERFORMANCE_TEST "Build Performance Test" ON)
  284. option(TARGET_SAMPLES "Build Samples" ON)
  285. option(TARGET_VIEWER "Build JoltViewer" ON)
  286. if (TARGET_UNIT_TESTS)
  287. # Create UnitTests executable
  288. include(${PHYSICS_REPO_ROOT}/UnitTests/UnitTests.cmake)
  289. add_executable(UnitTests ${UNIT_TESTS_SRC_FILES})
  290. target_include_directories(UnitTests PUBLIC ${UNIT_TESTS_ROOT})
  291. target_link_libraries(UnitTests LINK_PUBLIC Jolt)
  292. if (EMSCRIPTEN)
  293. target_link_options(UnitTests PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  294. endif()
  295. # Code coverage doesn't work when using precompiled headers
  296. if (CMAKE_GENERATOR STREQUAL "Ninja Multi-Config" AND MSVC)
  297. # The Ninja Multi-Config generator errors out when selectively disabling precompiled headers for certain configurations.
  298. # See: https://github.com/jrouwe/JoltPhysics/issues/1211
  299. target_precompile_headers(UnitTests PRIVATE "${JOLT_PHYSICS_ROOT}/Jolt.h")
  300. else()
  301. target_precompile_headers(UnitTests PRIVATE "$<$<NOT:$<CONFIG:ReleaseCoverage>>:${JOLT_PHYSICS_ROOT}/Jolt.h>")
  302. endif()
  303. if (MSVC)
  304. target_link_options(UnitTests PUBLIC "/SUBSYSTEM:CONSOLE")
  305. endif()
  306. if (IOS)
  307. # Set the bundle information
  308. set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
  309. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
  310. endif()
  311. if (XCODE)
  312. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  313. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  314. endif()
  315. # Register unit tests as a test so that it can be run with:
  316. # ctest --output-on-failure
  317. enable_testing()
  318. add_test(UnitTests UnitTests)
  319. endif()
  320. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  321. if (TARGET_HELLO_WORLD)
  322. # Example 'Hello World' application
  323. include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
  324. add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
  325. target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
  326. target_link_libraries(HelloWorld LINK_PUBLIC Jolt)
  327. if (MSVC)
  328. target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
  329. endif()
  330. if (EMSCRIPTEN)
  331. target_link_options(HelloWorld PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  332. endif()
  333. endif()
  334. if (TARGET_PERFORMANCE_TEST)
  335. # Performance Test application
  336. include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
  337. add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
  338. target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
  339. target_link_libraries(PerformanceTest LINK_PUBLIC Jolt)
  340. if (MSVC)
  341. target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
  342. endif()
  343. if (EMSCRIPTEN)
  344. # Embed the assets for the RagdollScene
  345. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/Human.tof@/Assets/Human.tof")
  346. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/Human/dead_pose1.tof@/Assets/Human/dead_pose1.tof")
  347. target_link_options(PerformanceTest PUBLIC "SHELL:--preload-file ${PHYSICS_REPO_ROOT}/Assets/terrain2.bof@/Assets/terrain2.bof")
  348. target_link_options(PerformanceTest PUBLIC -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=134217728)
  349. endif()
  350. set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
  351. endif()
  352. endif()
  353. 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
  354. # Windows only targets
  355. if (TARGET_SAMPLES OR TARGET_VIEWER)
  356. include(${PHYSICS_REPO_ROOT}/TestFramework/TestFramework.cmake)
  357. endif()
  358. if (TARGET_SAMPLES)
  359. if (TEST_FRAMEWORK_AVAILABLE)
  360. include(${PHYSICS_REPO_ROOT}/Samples/Samples.cmake)
  361. else()
  362. message("Cannot build Samples because Vulkan/DirectX SDK is not available!")
  363. endif()
  364. endif()
  365. if (TARGET_VIEWER)
  366. if (TEST_FRAMEWORK_AVAILABLE)
  367. include(${PHYSICS_REPO_ROOT}/JoltViewer/JoltViewer.cmake)
  368. else()
  369. message("Cannot build JoltViewer because Vulkan/DirectX SDK is not available!")
  370. endif()
  371. endif()
  372. endif()
  373. endif()