|
| 1 | +cmake_minimum_required(VERSION 3.16 FATAL_ERROR) |
| 2 | + |
| 3 | +project(JoltJNI CXX) |
| 4 | + |
| 5 | +include(FetchContent) |
| 6 | + |
| 7 | +# The configurations we support |
| 8 | +set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution") |
| 9 | + |
| 10 | +# When turning this option on, the library will be compiled using doubles for positions. This allows for much bigger worlds. |
| 11 | +set(DOUBLE_PRECISION $ENV{JOLT_JNI_DOUBLE_PRECISION}) |
| 12 | + |
| 13 | +# When turning this option on, the library will be compiled with debug symbols |
| 14 | +set(GENERATE_DEBUG_SYMBOLS ON) |
| 15 | + |
| 16 | +# When turning this option on, the library will be compiled in such a way to attempt to keep the simulation deterministic across platforms |
| 17 | +set(CROSS_PLATFORM_DETERMINISTIC OFF) |
| 18 | + |
| 19 | +# 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. |
| 20 | +# 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. |
| 21 | +# If you don't do this you may get an error: /usr/bin/ld: libJolt.a: error adding symbols: file format not recognized |
| 22 | +set(INTERPROCEDURAL_OPTIMIZATION ON) |
| 23 | + |
| 24 | +# 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 |
| 25 | +# and will enable floating point exceptions during simulation to detect divisions by zero. |
| 26 | +# Note that this currently only works using MSVC. Clang turns Float2 into a SIMD vector sometimes causing floating point exceptions (the option is ignored). |
| 27 | +set(FLOATING_POINT_EXCEPTIONS_ENABLED OFF) |
| 28 | + |
| 29 | +# Select X86 processor features to use, by default the library compiles with AVX2, if everything is off it will be SSE2 compatible. |
| 30 | +set(USE_SSE4_1 $ENV{JOLT_JNI_USE_SSE4_1}) |
| 31 | +set(USE_SSE4_2 $ENV{JOLT_JNI_USE_SSE4_2}) |
| 32 | +set(USE_AVX $ENV{JOLT_JNI_USE_AVX}) |
| 33 | +set(USE_AVX2 $ENV{JOLT_JNI_USE_AVX2}) |
| 34 | +set(USE_AVX512 $ENV{JOLT_JNI_USE_AVX512}) |
| 35 | +set(USE_LZCNT $ENV{JOLT_JNI_USE_LZCNT}) |
| 36 | +set(USE_TZCNT $ENV{JOLT_JNI_USE_TZCNT}) |
| 37 | +set(USE_F16C $ENV{JOLT_JNI_USE_F16C}) |
| 38 | +set(USE_FMADD $ENV{JOLT_JNI_USE_FMADD}) |
| 39 | + |
| 40 | +# Enable position-independent code |
| 41 | +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") |
| 42 | + |
| 43 | +# Include Jolt |
| 44 | +FetchContent_Declare( |
| 45 | + JoltPhysics |
| 46 | + GIT_REPOSITORY "https://github.com/jrouwe/JoltPhysics" |
| 47 | + GIT_TAG "9b9ca187190c55a21825b90537191f4071b127bc" |
| 48 | + SOURCE_SUBDIR "Build" |
| 49 | +) |
| 50 | +FetchContent_MakeAvailable(JoltPhysics) |
| 51 | + |
| 52 | +# Requires C++ 17 |
| 53 | +set(CMAKE_CXX_STANDARD 17) |
| 54 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 55 | +set(CMAKE_CXX_EXTENSIONS OFF) |
| 56 | + |
| 57 | +if (("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore") AND NOT MINGW) |
| 58 | + # 64 bit architecture |
| 59 | + set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64") |
| 60 | + |
| 61 | + # Set runtime library |
| 62 | + if (USE_STATIC_MSVC_RUNTIME_LIBRARY) |
| 63 | + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") |
| 64 | + else() |
| 65 | + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL") |
| 66 | + endif() |
| 67 | + |
| 68 | + # Set general compiler flags |
| 69 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /Gm- /Wall /WX /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline") |
| 70 | + |
| 71 | + # Optionally generate debug symbols |
| 72 | + if (GENERATE_DEBUG_SYMBOLS) |
| 73 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi") |
| 74 | + endif() |
| 75 | + |
| 76 | + # Remove any existing compiler flag that enables RTTI |
| 77 | + string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) |
| 78 | + |
| 79 | + # Set compiler flag for disabling RTTI |
| 80 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") |
| 81 | + |
| 82 | + if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM") |
| 83 | + # On ARM the exception handling flag is missing which causes warnings |
| 84 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") |
| 85 | + endif() |
| 86 | + |
| 87 | + # Set compiler flags for various configurations |
| 88 | + set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1") |
| 89 | + set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot") |
| 90 | + set(CMAKE_CXX_FLAGS_DISTRIBUTION "/GS- /Gy /O2 /Oi /Ot") |
| 91 | + |
| 92 | + # Set linker flags |
| 93 | + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
| 94 | + if (CROSS_PLATFORM_DETERMINISTIC) |
| 95 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise") |
| 96 | + else() |
| 97 | + 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 |
| 98 | + endif() |
| 99 | + elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 100 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames") |
| 101 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits warnings about unused arguments such as /MP and /GL |
| 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) |
| 104 | + # Set general compiler flags |
| 105 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I. -Wall -Werror") |
| 106 | + |
| 107 | + # Optionally generate debug symbols |
| 108 | + if (GENERATE_DEBUG_SYMBOLS) |
| 109 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") |
| 110 | + endif() |
| 111 | + |
| 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 | + |
| 121 | + # 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 |
| 122 | + if (CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC) |
| 123 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off") |
| 124 | + endif() |
| 125 | + endif() |
| 126 | + |
| 127 | + # Set compiler flags for various configurations |
| 128 | + set(CMAKE_CXX_FLAGS_DEBUG "") |
| 129 | + set(CMAKE_CXX_FLAGS_RELEASE "-O3") |
| 130 | + set(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3") |
| 131 | + |
| 132 | + # Set linker flags |
| 133 | + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") |
| 134 | +endif() |
| 135 | + |
| 136 | +# Set linker flags |
| 137 | +set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") |
| 138 | + |
| 139 | +# Enable link time optimization in Release and Distribution mode if requested and available |
| 140 | +SET_INTERPROCEDURAL_OPTIMIZATION() |
| 141 | + |
| 142 | +# Compile the library from the generated Java source |
| 143 | +add_library(JoltJNI SHARED JoltJNI/JoltJNI.cpp) |
| 144 | +target_include_directories(JoltJNI PUBLIC ${JoltPhysics_SOURCE_DIR}/..) |
| 145 | +target_include_directories(JoltJNI PUBLIC ../jolt-jni/build/generated/sources/annotationProcessor/java/main/jolt) |
| 146 | +find_package(JNI REQUIRED) |
| 147 | +target_include_directories(JoltJNI PUBLIC ${JNI_INCLUDE_DIRS}) |
| 148 | +target_link_libraries(JoltJNI Jolt) |
0 commit comments