CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 specificly 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. include(CMakeDependentOption)
  42. # Ability to toggle between the static and DLL versions of the MSVC runtime library
  43. # Windows Store only supports the DLL version
  44. cmake_dependent_option(USE_STATIC_MSVC_RUNTIME_LIBRARY "Use the static MSVC runtime library" ON "MSVC;NOT WINDOWS_STORE" OFF)
  45. # Determine which configurations exist
  46. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  47. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
  48. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  49. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseASAN;ReleaseUBSAN;ReleaseCoverage;Distribution")
  50. endif()
  51. if (("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND NOT MINGW)
  52. # Fill in the path to the asan libraries
  53. set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
  54. # 64 bit architecture
  55. set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
  56. # Set runtime library
  57. if (USE_STATIC_MSVC_RUNTIME_LIBRARY)
  58. set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  59. endif()
  60. # Set general compiler flags
  61. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")
  62. # Enable warnings
  63. if (ENABLE_ALL_WARNINGS)
  64. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /WX")
  65. endif()
  66. # Optionally generate debug symbols
  67. if (GENERATE_DEBUG_SYMBOLS)
  68. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
  69. endif()
  70. # Remove any existing compiler flag that enables RTTI
  71. string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  72. # Set compiler flag for disabling RTTI
  73. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  74. if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")
  75. # On ARM the exception handling flag is missing which causes warnings
  76. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  77. endif()
  78. # Set compiler flags for various configurations
  79. set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1")
  80. set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot")
  81. set(CMAKE_CXX_FLAGS_DISTRIBUTION "/GS- /Gy /O2 /Oi /Ot")
  82. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address /Od")
  83. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  84. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-fprofile-instr-generate -fcoverage-mapping")
  85. # Set linker flags
  86. set(CMAKE_EXE_LINKER_FLAGS "/SUBSYSTEM:WINDOWS /ignore:4221")
  87. if (GENERATE_DEBUG_SYMBOLS)
  88. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")
  89. endif()
  90. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  91. if (CROSS_PLATFORM_DETERMINISTIC)
  92. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
  93. else()
  94. 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
  95. endif()
  96. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  97. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
  98. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
  99. 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")
  100. set(CMAKE_EXE_LINKER_FLAGS_RELEASEUBSAN "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  101. set(CMAKE_EXE_LINKER_FLAGS_RELEASECOVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LIBPATH:${CLANG_LIB_PATH}")
  102. endif()
  103. elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "iOS" OR MINGW OR EMSCRIPTEN OR ANDROID)
  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. # Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
  114. # 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
  115. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -ffp-contract=off")
  116. else()
  117. # Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
  118. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")
  119. # 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
  120. if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC)
  121. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
  122. endif()
  123. endif()
  124. # Cross compiler flags
  125. if (CROSS_COMPILE_ARM)
  126. set(CMAKE_CXX_FLAGS "--target=aarch64-linux-gnu ${CMAKE_CXX_FLAGS}")
  127. endif()
  128. # Set compiler flags for various configurations
  129. if (OVERRIDE_CXX_FLAGS)
  130. set(CMAKE_CXX_FLAGS_DEBUG "")
  131. set(CMAKE_CXX_FLAGS_RELEASE "-O3")
  132. endif()
  133. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  134. set(CMAKE_CXX_FLAGS_RELEASEASAN "-fsanitize=address")
  135. set(CMAKE_CXX_FLAGS_RELEASEUBSAN "-fsanitize=undefined,implicit-conversion,float-divide-by-zero,local-bounds -fno-sanitize-recover=all")
  136. set(CMAKE_CXX_FLAGS_RELEASECOVERAGE "-O0 -DJPH_NO_FORCE_INLINE -fprofile-instr-generate -fcoverage-mapping")
  137. # Set linker flags
  138. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
  139. endif()
  140. # Set linker flags
  141. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
  142. # Enable link time optimization in Release and Distribution mode if requested and available
  143. function(SET_INTERPROCEDURAL_OPTIMIZATION)
  144. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE OFF PARENT_SCOPE)
  145. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION OFF PARENT_SCOPE)
  146. # On ARM, whole program optimization triggers an internal compiler error during code gen, so we don't turn it on
  147. if (INTERPROCEDURAL_OPTIMIZATION AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM64") AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM"))
  148. include(CheckIPOSupported)
  149. check_ipo_supported(RESULT IS_IPO_SUPPORTED OUTPUT IPO_CHECK_OUTPUT)
  150. if (IS_IPO_SUPPORTED)
  151. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON PARENT_SCOPE)
  152. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DISTRIBUTION ON PARENT_SCOPE)
  153. else()
  154. message(WARNING "Interprocedural optimizations are not supported: ${IPO_CHECK_OUTPUT}")
  155. endif()
  156. endif()
  157. endfunction()
  158. SET_INTERPROCEDURAL_OPTIMIZATION()
  159. # Set repository root
  160. set(PHYSICS_REPO_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../)
  161. # Make Jolt Library
  162. include(${PHYSICS_REPO_ROOT}/Jolt/Jolt.cmake)
  163. if (XCODE)
  164. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  165. set_property(TARGET Jolt PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  166. endif()
  167. # Install Jolt library and includes
  168. install(TARGETS Jolt DESTINATION lib)
  169. foreach(SRC_FILE ${JOLT_PHYSICS_SRC_FILES})
  170. string(REPLACE ${PHYSICS_REPO_ROOT} "" RELATIVE_SRC_FILE ${SRC_FILE})
  171. get_filename_component(DESTINATION_PATH ${RELATIVE_SRC_FILE} DIRECTORY)
  172. if (NOT RELATIVE_SRC_FILE MATCHES "\.cpp")
  173. install(FILES ${SRC_FILE} DESTINATION include/${DESTINATION_PATH})
  174. endif()
  175. endforeach()
  176. # 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
  177. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  178. # Ability to turn ON/OFF individual applications
  179. option(TARGET_UNIT_TESTS "Build Unit Tests" ON)
  180. option(TARGET_HELLO_WORLD "Build Hello World" ON)
  181. option(TARGET_PERFORMANCE_TEST "Build Performance Test" ON)
  182. option(TARGET_SAMPLES "Build Samples" ON)
  183. option(TARGET_VIEWER "Build JoltViewer" ON)
  184. if (TARGET_UNIT_TESTS)
  185. # Create UnitTests executable
  186. include(${PHYSICS_REPO_ROOT}/UnitTests/UnitTests.cmake)
  187. add_executable(UnitTests ${UNIT_TESTS_SRC_FILES})
  188. target_include_directories(UnitTests PUBLIC ${UNIT_TESTS_ROOT})
  189. target_link_libraries(UnitTests LINK_PUBLIC Jolt)
  190. target_precompile_headers(UnitTests PRIVATE ${JOLT_PHYSICS_ROOT}/Jolt.h)
  191. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND NOT MINGW)
  192. target_link_options(UnitTests PUBLIC "/SUBSYSTEM:CONSOLE")
  193. endif()
  194. if (IOS)
  195. # Set the bundle information
  196. set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
  197. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
  198. endif()
  199. if (XCODE)
  200. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  201. set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  202. endif()
  203. # Register unit tests as a test so that it can be run with:
  204. # ctest --output-on-failure
  205. enable_testing()
  206. add_test(UnitTests UnitTests)
  207. endif()
  208. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  209. if (TARGET_HELLO_WORLD)
  210. # Example 'Hello World' application
  211. include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
  212. add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
  213. target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
  214. target_link_libraries(HelloWorld LINK_PUBLIC Jolt)
  215. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND NOT MINGW)
  216. target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
  217. endif()
  218. endif()
  219. if (TARGET_PERFORMANCE_TEST)
  220. # Performance Test application
  221. include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
  222. add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
  223. target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
  224. target_link_libraries(PerformanceTest LINK_PUBLIC Jolt)
  225. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND NOT MINGW)
  226. target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
  227. endif()
  228. set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
  229. # Copy the assets folder
  230. add_custom_command(TARGET PerformanceTest PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${PHYSICS_REPO_ROOT}/Assets/ $<TARGET_FILE_DIR:PerformanceTest>/Assets/)
  231. endif()
  232. endif()
  233. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" AND NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM")) # ARM 32-bit is missing dinput8.lib
  234. # Windows only targets
  235. if (TARGET_SAMPLES OR TARGET_VIEWER)
  236. include(${PHYSICS_REPO_ROOT}/TestFramework/TestFramework.cmake)
  237. endif()
  238. if (TARGET_SAMPLES)
  239. include(${PHYSICS_REPO_ROOT}/Samples/Samples.cmake)
  240. endif()
  241. if (TARGET_VIEWER)
  242. include(${PHYSICS_REPO_ROOT}/JoltViewer/JoltViewer.cmake)
  243. endif()
  244. endif()
  245. endif()