CMakeLists.txt 13 KB

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