CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
  2. project(joltc CXX C)
  3. # Use solution folders to organize projects
  4. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  5. # Requires C++ 17
  6. set(CMAKE_CXX_STANDARD 17)
  7. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  8. set(CMAKE_CXX_EXTENSIONS OFF)
  9. set(CMAKE_INSTALL_MESSAGE LAZY)
  10. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  11. set(CMAKE_DEBUG_POSTFIX "d")
  12. set(CMAKE_OSX_DEPLOYMENT_TARGET "11" CACHE STRING "Minimum OS X deployment version")
  13. set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Build architectures for OS X")
  14. # Set default symbol visibility to hidden. This prevents all Jolt's symbols to be exported on macOS. This can cause crashes if a program loads multiple shared libraries with different versions of Jolt.
  15. set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  16. # Determine if engine is built as a subproject (using add_subdirectory)
  17. # or if it is the master project.
  18. if (NOT DEFINED JPH_MASTER_PROJECT)
  19. set(JPH_MASTER_PROJECT OFF)
  20. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  21. set(JPH_MASTER_PROJECT ON)
  22. message(STATUS "CMake version: ${CMAKE_VERSION}")
  23. endif ()
  24. endif ()
  25. if (JPH_MASTER_PROJECT)
  26. # Configure CMake global variables
  27. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  28. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  29. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  30. endif()
  31. if (MSVC AND JPH_MASTER_PROJECT)
  32. if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
  33. set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  34. endif ()
  35. endif ()
  36. include(GNUInstallDirs)
  37. include(FetchContent)
  38. # When turning this option on, the library will be compiled using assertions. By default asserts are enabled in Debug build.
  39. option(USE_ASSERTS "Enable asserts" OFF)
  40. # When turning this option on, the library will be compiled using doubles for positions. This allows for much bigger worlds.
  41. option(DOUBLE_PRECISION "Use double precision math" OFF)
  42. # When turning this option on, the library will be compiled with debug symbols
  43. option(GENERATE_DEBUG_SYMBOLS "Generate debug symbols" OFF)
  44. # When turning this option on, the library will override the default CMAKE_CXX_FLAGS_DEBUG/RELEASE values, otherwise they will use the platform defaults
  45. option(OVERRIDE_CXX_FLAGS "Override CMAKE_CXX_FLAGS_DEBUG/RELEASE" ON)
  46. # When turning this option on, the library will be compiled in such a way to attempt to keep the simulation deterministic across platforms
  47. option(CROSS_PLATFORM_DETERMINISTIC "Cross platform deterministic" OFF)
  48. # When turning this option on, the library will be compiled for ARM (aarch64-linux-gnu), requires compiling with clang
  49. option(CROSS_COMPILE_ARM "Cross compile to aarch64-linux-gnu" OFF)
  50. # 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
  51. # and will enable floating point exceptions during simulation to detect divisions by zero.
  52. # Note that this currently only works using MSVC. Clang turns Float2 into a SIMD vector sometimes causing floating point exceptions (the option is ignored).
  53. option(FLOATING_POINT_EXCEPTIONS_ENABLED "Enable floating point exceptions" ON)
  54. # When turning this on, the library will be compiled with C++ exceptions enabled.
  55. # This adds some overhead and Jolt doesn't use exceptions so by default it is off.
  56. option(CPP_EXCEPTIONS_ENABLED "Enable C++ exceptions" OFF)
  57. # When turning this on, the library will be compiled with C++ RTTI enabled.
  58. # This adds some overhead and Jolt doesn't use RTTI so by default it is off.
  59. option(CPP_RTTI_ENABLED "Enable C++ RTTI" OFF)
  60. # Use 32-bit object layers to support more bits in ObjectLayerPairFilterMask
  61. set(OBJECT_LAYER_BITS 32)
  62. # Select X86 processor features to use (if everything is off it will be SSE2 compatible)
  63. option(USE_SSE4_1 "Enable SSE4.1" ON)
  64. option(USE_SSE4_2 "Enable SSE4.2" ON)
  65. option(USE_AVX "Enable AVX" ON)
  66. option(USE_AVX2 "Enable AVX2" ON)
  67. option(USE_AVX512 "Enable AVX512" OFF)
  68. option(USE_LZCNT "Enable LZCNT" ON)
  69. option(USE_TZCNT "Enable TZCNT" ON)
  70. option(USE_F16C "Enable F16C" ON)
  71. option(USE_FMADD "Enable FMADD" ON)
  72. # Enable SIMD for the WASM build. Note that this is currently off by default since not all browsers support this.
  73. # See: https://caniuse.com/?search=WebAssembly%20SIMD (Safari got support in March 2023 and was the last major browser to get support).
  74. option(USE_WASM_SIMD "Enable SIMD for WASM" OFF)
  75. # Enable all warnings
  76. option(ENABLE_ALL_WARNINGS "Enable all warnings and warnings as errors" OFF)
  77. # Setting to periodically trace broadphase stats to help determine if the broadphase layer configuration is optimal
  78. option(TRACK_BROADPHASE_STATS "Track Broadphase Stats" OFF)
  79. # Setting to periodically trace narrowphase stats to help determine which collision queries could be optimized
  80. option(TRACK_NARROWPHASE_STATS "Track Narrowphase Stats" OFF)
  81. # Enable the debug renderer in the Debug and Release builds. Note that DEBUG_RENDERER_IN_DISTRIBUTION will override this setting.
  82. option(DEBUG_RENDERER_IN_DEBUG_AND_RELEASE "Enable debug renderer in Debug and Release builds" ON)
  83. # Setting to enable the debug renderer in all builds.
  84. # Note that enabling this reduces the performance of the library even if you're not drawing anything.
  85. option(DEBUG_RENDERER_IN_DISTRIBUTION "Enable debug renderer in all builds" ON)
  86. # Enable the profiler in Debug and Release builds. Note that PROFILER_IN_DISTRIBUTION will override this setting.
  87. option(PROFILER_IN_DEBUG_AND_RELEASE "Enable the profiler in Debug and Release builds" ON)
  88. # Enable the profiler in all builds.
  89. # Note that enabling this reduces the performance of the library.
  90. option(PROFILER_IN_DISTRIBUTION "Enable the profiler in all builds" OFF)
  91. # Setting this option will force the library to use malloc/free instead of allowing the user to override the memory allocator
  92. option(DISABLE_CUSTOM_ALLOCATOR "Disable support for a custom memory allocator" OFF)
  93. # Setting this option will force the library to use the STL vector instead of the custom Array class
  94. option(USE_STD_VECTOR "Use std::vector instead of own Array class" OFF)
  95. # Setting this option will compile the ObjectStream class and RTTI attribute information
  96. option(ENABLE_OBJECT_STREAM "Compile the ObjectStream class and RTTI attribute information" ON)
  97. option(JPH_INSTALL "Install target" ${JPH_MASTER_PROJECT})
  98. option(JPH_SAMPLES "Build samples" ${JPH_MASTER_PROJECT})
  99. # Define standard configurations (Debug, Release, Distribution)
  100. set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")
  101. # Set the default CMAKE_BUILD_TYPE to Release.
  102. # This should be done before the project command since the latter can set
  103. # CMAKE_BUILD_TYPE itself (it does so for nmake).
  104. if (JPH_MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
  105. set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "The default build type" FORCE)
  106. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES})
  107. endif ()
  108. # Search for local JoltPhysics first
  109. if(EXISTS "${CMAKE_SOURCE_DIR}/JoltPhysics/Build/CMakeLists.txt")
  110. message(STATUS "Using local JoltPhysics from ${CMAKE_SOURCE_DIR}/JoltPhysics")
  111. FetchContent_Declare(
  112. JoltPhysics
  113. SOURCE_DIR "${CMAKE_SOURCE_DIR}/JoltPhysics"
  114. SOURCE_SUBDIR "Build"
  115. )
  116. elseif(EXISTS "${CMAKE_SOURCE_DIR}/../JoltPhysics/Build/CMakeLists.txt")
  117. message(STATUS "Using local JoltPhysics from ${CMAKE_SOURCE_DIR}/../JoltPhysics")
  118. FetchContent_Declare(
  119. JoltPhysics
  120. SOURCE_DIR "${CMAKE_SOURCE_DIR}/../JoltPhysics"
  121. SOURCE_SUBDIR "Build"
  122. )
  123. else()
  124. message(STATUS "Local Jolt not found, download from remote")
  125. FetchContent_Declare(
  126. JoltPhysics
  127. GIT_REPOSITORY "https://github.com/jrouwe/JoltPhysics"
  128. GIT_TAG v5.5.0
  129. SOURCE_SUBDIR "Build"
  130. )
  131. endif()
  132. FetchContent_MakeAvailable(JoltPhysics)
  133. if (XCODE)
  134. # Ensure that we enable SSE4.2 for the x86_64 build, XCode builds multiple architectures
  135. set_property(TARGET Jolt PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
  136. endif()
  137. if (MSVC)
  138. # Fill in the path to the asan libraries
  139. set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
  140. # 64 bit architecture
  141. set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
  142. # Set general compiler flags
  143. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline")
  144. # Enable warnings
  145. if (ENABLE_ALL_WARNINGS)
  146. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall /WX")
  147. endif()
  148. # Set compiler flag for disabling RTTI
  149. if (NOT CPP_RTTI_ENABLED)
  150. # Set compiler flag for disabling RTTI
  151. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  152. else()
  153. # Set compiler flag for enabling RTTI
  154. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR")
  155. endif()
  156. if (NOT CPP_EXCEPTIONS_ENABLED)
  157. # Remove any existing compiler flag that enables exceptions
  158. string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  159. # Disable warning about STL and compiler-generated types using noexcept when exceptions are disabled
  160. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4577")
  161. else()
  162. # Enable exceptions
  163. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  164. endif()
  165. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  166. # Set linker flags
  167. set(CMAKE_EXE_LINKER_FLAGS "/SUBSYSTEM:WINDOWS /ignore:4221")
  168. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  169. if (CROSS_PLATFORM_DETERMINISTIC)
  170. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise")
  171. else()
  172. 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
  173. endif()
  174. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  175. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
  176. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL
  177. endif()
  178. else()
  179. # Enable warnings
  180. if (ENABLE_ALL_WARNINGS)
  181. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
  182. endif()
  183. # Set compiler flag for disabling RTTI
  184. if (NOT CPP_RTTI_ENABLED)
  185. # Set compiler flag for disabling RTTI
  186. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
  187. else()
  188. # Set compiler flag for enabling RTTI
  189. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti")
  190. endif()
  191. # Disable exception-handling
  192. if (NOT CPP_EXCEPTIONS_ENABLED)
  193. # Set compiler flag for disabling exception-handling
  194. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
  195. else()
  196. # Set compiler flag for enabling exception-handling
  197. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
  198. endif()
  199. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  200. # Also disable -Wstringop-overflow or it will generate false positives that can't be disabled from code when link-time optimizations are enabled
  201. # 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
  202. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -ffp-contract=off")
  203. else()
  204. # Do not use -ffast-math since it cannot be turned off in a single compilation unit under clang, see Core.h
  205. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-model=precise")
  206. # 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
  207. if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC)
  208. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off")
  209. endif()
  210. # Cross compiler flags
  211. if (CROSS_COMPILE_ARM)
  212. set(CMAKE_CXX_FLAGS "--target=aarch64-linux-gnu ${CMAKE_CXX_FLAGS}")
  213. endif()
  214. endif()
  215. # 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.
  216. if ("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
  217. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-allocation")
  218. endif()
  219. # Set compiler flags for various configurations
  220. if (OVERRIDE_CXX_FLAGS)
  221. set(CMAKE_CXX_FLAGS_DEBUG "")
  222. set(CMAKE_CXX_FLAGS_RELEASE "-O3")
  223. endif()
  224. set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELEASE}")
  225. # Set linker flags
  226. if (NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows"))
  227. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
  228. endif()
  229. endif()
  230. # Set linker flags
  231. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
  232. set(CMAKE_SHARED_LINKER_FLAGS_DISTRIBUTION "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
  233. SET_INTERPROCEDURAL_OPTIMIZATION()
  234. set(PHYSICS_REPO_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/src/joltc)
  235. set(BUILD_SHARED_LIBS OFF CACHE BOOL "Disable shared library when building Jolt" FORCE)
  236. # Options
  237. if (IOS OR EMSCRIPTEN)
  238. set(JPH_BUILD_SHARED OFF CACHE BOOL "Always Disable shared library on (IOS, WEB)" FORCE)
  239. else()
  240. option(JPH_BUILD_SHARED "Build a shared library" ${JPH_MASTER_PROJECT})
  241. endif()
  242. # Setup joltc library
  243. # Define target name
  244. if (DOUBLE_PRECISION)
  245. set (TARGET_NAME joltc_double)
  246. else()
  247. set (TARGET_NAME joltc)
  248. endif ()
  249. set(SOURCE_FILES
  250. ${CMAKE_CURRENT_SOURCE_DIR}/include/joltc.h
  251. ${CMAKE_CURRENT_SOURCE_DIR}/src/joltc.cpp
  252. ${CMAKE_CURRENT_SOURCE_DIR}/src/joltc_assert.cpp
  253. ${CMAKE_CURRENT_SOURCE_DIR}/src/joltc.c
  254. )
  255. if (JPH_BUILD_SHARED)
  256. add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
  257. else()
  258. add_library(${TARGET_NAME} ${SOURCE_FILES})
  259. endif()
  260. if(JPH_BUILD_SHARED)
  261. target_compile_definitions(${TARGET_NAME} PRIVATE JPH_SHARED_LIBRARY_BUILD=1)
  262. endif ()
  263. target_include_directories(${TARGET_NAME} PUBLIC
  264. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  265. ${JoltPhysics_SOURCE_DIR}/..
  266. )
  267. target_link_libraries(${TARGET_NAME} PRIVATE Jolt)
  268. if (MSVC)
  269. # Debug information
  270. target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:Debug>:/Zi>)
  271. # Enable full optimization in dev/release
  272. target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:Debug>:/Od> $<$<NOT:$<CONFIG:Debug>>:/Ox>)
  273. # Inline function expansion
  274. target_compile_options(${TARGET_NAME} PRIVATE /Ob2)
  275. # Enable intrinsic functions in dev/release
  276. target_compile_options(${TARGET_NAME} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/Oi>)
  277. # Favor fast code
  278. target_compile_options(${TARGET_NAME} PRIVATE /Ot)
  279. # Enable fiber-safe optimizations in dev/release
  280. target_compile_options(${TARGET_NAME} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GT>)
  281. # Enable string pooling
  282. target_compile_options(${TARGET_NAME} PRIVATE /GF)
  283. # Use security checks only in debug
  284. target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:/sdl> $<$<NOT:$<CONFIG:DEBUG>>:/sdl->)
  285. else()
  286. endif ()
  287. if (JPH_INSTALL)
  288. install(
  289. FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/joltc.h
  290. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Jolt
  291. )
  292. install(TARGETS ${TARGET_NAME}
  293. EXPORT ${TARGET_NAME}
  294. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  295. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  296. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  297. )
  298. if (MSVC)
  299. install(FILES $<TARGET_PDB_FILE:${TARGET_NAME}> DESTINATION bin OPTIONAL)
  300. endif ()
  301. endif()
  302. if (JPH_SAMPLES)
  303. add_subdirectory(samples)
  304. endif ()
  305. # Tests
  306. option(JPH_TESTS "Build tests" ${JPH_MASTER_PROJECT})
  307. if (JPH_TESTS)
  308. add_subdirectory(tests)
  309. endif ()
  310. if (JPH_BUILD_SHARED)
  311. message(STATUS " Library SHARED")
  312. else ()
  313. message(STATUS " Library STATIC")
  314. endif ()
  315. message(STATUS " Samples ${JPH_SAMPLES}")
  316. if (CMAKE_GENERATOR_PLATFORM)
  317. message(STATUS "CMAKE_GENERATOR_PLATFORM: ${CMAKE_GENERATOR_PLATFORM}")
  318. endif()
  319. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  320. if (CMAKE_OSX_ARCHITECTURES)
  321. message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}")
  322. endif ()