Skip to content

Commit 5683fdc

Browse files
committed
Use a CMake project for bindings instead of Gradle C++ (ew bad)
1 parent b4ea76d commit 5683fdc

18 files changed

Lines changed: 193 additions & 94 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
if: ${{ github.event_name != 'pull_request' || github.repository != github.event.pull_request.head.repo.full_name }}
1313
strategy:
1414
matrix:
15-
os: [ ubuntu-latest, windows-latest, macos-latest ]
15+
os: [ ubuntu-latest ] # TODO windows-latest, macos-latest
1616
include:
1717
- os: ubuntu-latest
1818
publish-api: true
@@ -30,7 +30,7 @@ jobs:
3030
- name: "Setup Gradle"
3131
uses: gradle/gradle-build-action@v2
3232
- name: "Run Gradle build"
33-
run: ./gradlew generateNatives build
33+
run: ./gradlew assembleNatives build
3434
# - name: "Get version type (sh)"
3535
# if: ${{ matrix.os != 'windows-latest' }}
3636
# run: |

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,6 @@ build
133133
**/resources/jolt/*.so
134134
**/resources/jolt/*.dll
135135
**/resources/jolt/*.dylib
136+
137+
# C++
138+
cmake-build-*/

‎.gitmodules‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎JoltPhysics‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ JoltEnvironment.registerTypes();
7373
git clone https://github.com/aecsocket/jolt-jni
7474
cd jolt-jni
7575
git submodule update --init
76-
./gradlew generateNatives build
76+
./gradlew build
7777
```

‎build-logic/src/main/kotlin/Globals.kt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ enum class JoltFlavor {
1111

1212
enum class JoltFeature {
1313
DOUBLE_PRECISION,
14-
IPO,
1514
USE_SSE4_1,
1615
USE_SSE4_2,
1716
USE_AVX,
@@ -22,7 +21,5 @@ enum class JoltFeature {
2221
USE_F16C,
2322
USE_FMADD;
2423

25-
fun macro() = "JPH_$name"
26-
2724
fun cmakeFlag() = "JOLT_JNI_$name"
2825
}

‎build-logic/src/main/kotlin/ext.kt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ val Project.flavor: JoltFlavor
1616
val Project.features: List<JoltFeature>
1717
get() = listOfNotNull(
1818
if (flavor == JoltFlavor.DP) JoltFeature.DOUBLE_PRECISION else null,
19-
// JoltFeature.IPO,
2019
JoltFeature.USE_SSE4_1,
2120
JoltFeature.USE_SSE4_2,
22-
// JoltFeature.USE_AVX,
23-
// JoltFeature.USE_AVX2,
21+
JoltFeature.USE_AVX,
22+
JoltFeature.USE_AVX2,
2423
// JoltFeature.USE_AVX512,
2524
JoltFeature.USE_LZCNT,
2625
JoltFeature.USE_TZCNT,

‎build-logic/src/main/kotlin/natives-conventions.gradle.kts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ afterEvaluate {
1818

1919
tasks {
2020
jar {
21-
bindings.tasks.withType<LinkSharedLibrary> {
22-
this@jar.dependsOn(this)
23-
}
24-
from("${bindings.buildDir}/lib/main/release/${nativesExt.bindingsFileName.get()}") {
21+
dependsOn(bindings.tasks["assembleNatives"])
22+
from("${bindings.buildDir}/${nativesExt.bindingsFileName.get()}") {
2523
into("jolt/${nativesExt.destInnerDir.get()}")
2624
}
2725
}

‎gradle.properties‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
buildType=distribution
2+
flavor=sp

‎jolt-jni-bindings/CMakeLists.txt‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

Comments
 (0)