CMakeLists.txt 14 KB

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