CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
  2. project(JoltPhysics CXX)
  3. # When turning this option on, the library will be compiled using doubles for positions. This allows for much bigger worlds.
  4. option(DOUBLE_PRECISION "Use double precision math" OFF)
  5. # When turning this option on, the library will be compiled with debug symbols
  6. option(GENERATE_DEBUG_SYMBOLS "Generate debug symbols" ON)
  7. # When turning this option on, the library will override the default CMAKE_CXX_FLAGS_DEBUG/RELEASE values, otherwise they will use the platform defaults
  8. option(OVERRIDE_CXX_FLAGS "Override CMAKE_CXX_FLAGS_DEBUG/RELEASE" ON)
  9. # When turning this option on, the library will be compiled in such a way to attempt to keep the simulation deterministic across platforms
  10. option(CROSS_PLATFORM_DETERMINISTIC "Cross platform deterministic" OFF)
  11. # When turning this option on, the library will be compiled for ARM (aarch64-linux-gnu), requires compiling with clang
  12. option(CROSS_COMPILE_ARM "Cross compile to aarch64-linux-gnu" OFF)
  13. # When turning this option on, Jolt will be compiled as a shared library and public symbols will be exported.
  14. option(BUILD_SHARED_LIBS "Compile Jolt as a shared library" OFF)
  15. # 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.
  16. # 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.
  17. # If you don't do this you may get an error: /usr/bin/ld: libJolt.a: error adding symbols: file format not recognized
  18. option(INTERPROCEDURAL_OPTIMIZATION "Enable interprocedural optimizations" ON)
  19. # 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
  20. # and will enable floating point exceptions during simulation to detect divisions by zero.
  21. # Note that this currently only works using MSVC. Clang turns Float2 into a SIMD vector sometimes causing floating point exceptions (the option is ignored).
  22. option(FLOATING_POINT_EXCEPTIONS_ENABLED "Enable floating point exceptions" ON)
  23. # Number of bits to use in ObjectLayer. Can be 16 or 32.
  24. option(OBJECT_LAYER_BITS "Number of bits in ObjectLayer" 16)
  25. # Select X86 processor features to use (if everything is off it will be SSE2 compatible)
  26. option(USE_SSE4_1 "Enable SSE4.1" ON)
  27. option(USE_SSE4_2 "Enable SSE4.2" ON)
  28. option(USE_AVX "Enable AVX" ON)
  29. option(USE_AVX2 "Enable AVX2" ON)
  30. option(USE_AVX512 "Enable AVX512" OFF)
  31. option(USE_LZCNT "Enable LZCNT" ON)
  32. option(USE_TZCNT "Enable TZCNT" ON)
  33. option(USE_F16C "Enable F16C" ON)
  34. option(USE_FMADD "Enable FMADD" ON)
  35. # Enable all warnings
  36. option(ENABLE_ALL_WARNINGS "Enable all warnings and warnings as errors" ON)
  37. # Setting to periodically trace broadphase stats to help determine if the broadphase layer configuration is optimal
  38. option(TRACK_BROADPHASE_STATS "Track Broadphase Stats" OFF)
  39. # Setting to periodically trace narrowphase stats to help determine which collision queries could be optimized
  40. option(TRACK_NARROWPHASE_STATS "Track Narrowphase Stats" OFF)
  41. # Enable the debug renderer in the Debug and Release builds. Note that DEBUG_RENDERER_IN_DISTRIBUTION will override this setting.
  42. option(DEBUG_RENDERER_IN_DEBUG_AND_RELEASE "Enable debug renderer in Debug and Release builds" ON)
  43. # Setting to enable the debug renderer in all builds.
  44. # Note that enabling this reduces the performance of the library even if you're not drawing anything.
  45. option(DEBUG_RENDERER_IN_DISTRIBUTION "Enable debug renderer in all builds" OFF)
  46. # Enable the profiler in Debug and Release builds. Note that PROFILER_IN_DISTRIBUTION will override this setting.
  47. option(PROFILER_IN_DEBUG_AND_RELEASE "Enable the profiler in Debug and Release builds" ON)
  48. # Enable the profiler in all builds.
  49. # Note that enabling this reduces the performance of the library.
  50. option(PROFILER_IN_DISTRIBUTION "Enable the profiler in all builds" OFF)
  51. # Setting this option will force the library to use malloc/free instead of allowing the user to override the memory allocator
  52. option(DISABLE_CUSTOM_ALLOCATOR "Disable support for a custom memory allocator" OFF)
  53. include(CMakeDependentOption)
  54. # Ability to toggle between the static and DLL versions of the MSVC runtime library
  55. # Windows Store only supports the DLL version
  56. cmake_dependent_option(USE_STATIC_MSVC_RUNTIME_LIBRARY "Use the static MSVC runtime library" ON "MSVC;NOT WINDOWS_STORE" OFF)
  57. # Determine which configurations exist
  58. 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
  59. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  60. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
  61. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  62. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseASAN;ReleaseUBSAN;ReleaseCoverage;Distribution")
  63. endif()
  64. endif()
  65. if (MSVC)
  66. # Fill in the path to the asan libraries
  67. set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
  68. # 64 bit architecture
  69. set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
  70. # Set runtime library
  71. if (USE_STATIC_MSVC_RUNTIME_LIBRARY)
  72. set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  73. endif()
  74. # Set general compiler flags
  75. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")
  76. # Enable warnings
  77. if (ENABLE_ALL_WARNINGS)
  78. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /WX")
  79. endif()
  80. # Optionally generate debug symbols
  81. if (GENERATE_DEBUG_SYMBOLS)
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
  83. endif()
  84. # Remove any existing compiler flag that enables RTTI
  85. string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  86. # Set compiler flag for disabling RTTI
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  88. if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
  89. # On ARM the exception handling flag is missing which causes warnings
  90. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  91. endif()
  92. # Set compiler flags for various configurations
  93. set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1")
  94. set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot")
  95. set(CMAKE_CXX_FLAGS_DISTRIBUTION "/GS- /Gy /O2 /Oi /Ot")
  96. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address /Od")
  97. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  98. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-fprofile-instr-generate -fcoverage-mapping")
  99. # Set linker flags
  100. set(CMAKE_EXE_LINKER_FLAGS "/SUBSYSTEM:WINDOWS /ignore:4221")
  101. if (GENERATE_DEBUG_SYMBOLS)
  102. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")
  103. endif()
  104. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  105. if (CROSS_PLATFORM_DETERMINISTIC)
  106. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
  107. else()
  108. 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
  109. endif()
  110. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  111. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
  112. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
  113. 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")
  114. set(CMAKE_EXE_LINKER_FLAGS_RELEASEUBSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  115. set(CMAKE_EXE_LINKER_FLAGS_RELEASECOVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  116. endif()
  117. else()
  118. # Enable warnings
  119. if (ENABLE_ALL_WARNINGS)
  120. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
  121. endif()
  122. # Optionally generate debug symbols
  123. if (GENERATE_DEBUG_SYMBOLS)
  124. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
  125. endif()
  126. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  127. # Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
  128. # 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
  129. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -ffp-contract=off")
  130. else()
  131. # Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
  132. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")
  133. # 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
  134. if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC)
  135. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
  136. endif()
  137. endif()
  138. # 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.
  139. if ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  140. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-allocation")
  141. endif()
  142. # Cross compiler flags
  143. if (CROSS_COMPILE_ARM)
  144. set(CMAKE_CXX_FLAGS "--target=aarch64-linux-gnu ${CMAKE_CXX_FLAGS}")
  145. endif()
  146. # Set compiler flags for various configurations
  147. if (OVERRIDE_CXX_FLAGS)
  148. set(CMAKE_CXX_FLAGS_DEBUG "")
  149. set(CMAKE_CXX_FLAGS_RELEASE "-O3")
  150. endif()
  151. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  152. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address")
  153. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  154. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-O0 -DJPH_NO_FORCE_INLINE -fprofile-instr-generate -fcoverage-mapping")
  155. # Set linker flags
  156. if (NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows"))
  157. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
  158. endif()
  159. endif()
  160. # Set linker flags
  161. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
  162. # Enable link time optimization in Release and Distribution mode if requested and available
  163. function(SET_INTERPROCEDURAL_OPTIMIZATION)
  164. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE OFF PARENT_SCOPE)
  165. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION OFF PARENT_SCOPE)
  166. # On ARM, whole program optimization triggers an internal compiler error during code gen, so we don't turn it on
  167. if (INTERPROCEDURAL_OPTIMIZATION AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM64") AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM"))
  168. include(CheckIPOSupported)
  169. check_ipo_supported(RESULT IS_IPO_SUPPORTED OUTPUT IPO_CHECK_OUTPUT)
  170. if (IS_IPO_SUPPORTED)
  171. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON PARENT_SCOPE)
  172. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION ON PARENT_SCOPE)
  173. else()
  174. message(WARNING "Interprocedural optimizations are not supported: ${IPO_CHECK_OUTPUT}")
  175. endif()
  176. endif()
  177. endfunction()
  178. SET_INTERPROCEDURAL_OPTIMIZATION()
  179. # Set repository root
  180. set(PHYSICS_REPO_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../)
  181. # Make Jolt Library
  182. include(${PHYSICS_REPO_ROOT}/Jolt/Jolt.cmake)
  183. if (XCODE)
  184. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  185. set_property(TARGET Jolt PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  186. endif()
  187. # Install Jolt library and includes
  188. install(TARGETS Jolt DESTINATION lib)
  189. foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
  190. string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
  191. get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
  192. if (NOT RELATIVE_SRC_FILE MATCHES "\.cpp")
  193. install(FILES ${SRC_FILE} DESTINATION include/${DESTINATION_PATH})
  194. endif()
  195. endforeach()
  196. # 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
  197. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  198. # Ability to turn ON/OFF individual applications
  199. option(TARGET_UNIT_TESTS "Build Unit Tests" ON)
  200. option(TARGET_HELLO_WORLD "Build Hello World" ON)
  201. option(TARGET_PERFORMANCE_TEST "Build Performance Test" ON)
  202. option(TARGET_SAMPLES "Build Samples" ON)
  203. option(TARGET_VIEWER "Build JoltViewer" ON)
  204. if (TARGET_UNIT_TESTS)
  205. # Create UnitTests executable
  206. include(${PHYSICS_REPO_ROOT}/UnitTests/UnitTests.cmake)
  207. add_executable(UnitTests ${UNIT_TESTS_SRC_FILES})
  208. target_include_directories(UnitTests PUBLIC ${UNIT_TESTS_ROOT})
  209. target_link_libraries(UnitTests LINK_PUBLIC Jolt)
  210. target_precompile_headers(UnitTests PRIVATE ${JOLT_PHYSICS_ROOT}/Jolt.h)
  211. if (MSVC)
  212. target_link_options(UnitTests PUBLIC "/SUBSYSTEM:CONSOLE")
  213. endif()
  214. if (IOS)
  215. # Set the bundle information
  216. set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
  217. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
  218. endif()
  219. if (XCODE)
  220. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  221. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  222. endif()
  223. # Register unit tests as a test so that it can be run with:
  224. # ctest --output-on-failure
  225. enable_testing()
  226. add_test(UnitTests UnitTests)
  227. endif()
  228. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  229. if (TARGET_HELLO_WORLD)
  230. # Example 'Hello World' application
  231. include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
  232. add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
  233. target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
  234. target_link_libraries(HelloWorld LINK_PUBLIC Jolt)
  235. if (MSVC)
  236. target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
  237. endif()
  238. endif()
  239. if (TARGET_PERFORMANCE_TEST)
  240. # Performance Test application
  241. include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
  242. add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
  243. target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
  244. target_link_libraries(PerformanceTest LINK_PUBLIC Jolt)
  245. if (MSVC)
  246. target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
  247. endif()
  248. set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
  249. # Copy the assets folder
  250. add_custom_command(TARGET PerformanceTest PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${PHYSICS_REPO_ROOT}/Assets/ $<TARGET_FILE_DIR:PerformanceTest>/Assets/)
  251. endif()
  252. endif()
  253. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")) # ARM 32-bit is missing dinput8.lib
  254. # Windows only targets
  255. if (TARGET_SAMPLES OR TARGET_VIEWER)
  256. include(${PHYSICS_REPO_ROOT}/TestFramework/TestFramework.cmake)
  257. endif()
  258. if (TARGET_SAMPLES)
  259. include(${PHYSICS_REPO_ROOT}/Samples/Samples.cmake)
  260. endif()
  261. if (TARGET_VIEWER)
  262. include(${PHYSICS_REPO_ROOT}/JoltViewer/JoltViewer.cmake)
  263. endif()
  264. endif()
  265. endif()