forked from stephengold/jolt-jni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
406 lines (358 loc) · 13.8 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Gradle script to build jolt-jni Maven artifacts and desktop native libraries
plugins {
id 'application' // to build JVM applications
id 'checkstyle' // to analyze Java sourcecode for style violations
id 'cpp' // to compile C++ code and link native libraries
id 'java-library' // to build JVM libraries
id 'maven-publish' // to publish artifacts to Maven repositories
id 'signing' // to sign artifacts for publication
alias(libs.plugins.analyze) // to catch undeclared/unused dependencies
alias(libs.plugins.download) // to retrieve files from URLs
}
ext {
group = 'com.github.stephengold'
version = '0.5.1-SNAPSHOT'
baseName = "${artifact}-${version}" // for artifacts
websiteUrl = 'https://github.com/stephengold/jolt-jni'
}
// Regenerate all JNI header files and unpack Jolt before compiling any C++ code.
tasks.withType(CppCompile) {
dependsOn('classes', 'compileTestJava', 'unpackJoltSource')
}
String javaHome = org.gradle.internal.jvm.Jvm.current().javaHome.absolutePath
model {
buildTypes {
Debug
Release
}
flavors {
Dp // double-precision arithmetic
Sp // single-precision arithmetic
}
platforms {
Linux64 {
architecture 'x86_64'
operatingSystem 'linux'
}
Linux_ARM32hf {
architecture 'armhf'
operatingSystem 'linux'
}
Linux_ARM64 {
architecture 'aarch64'
operatingSystem 'linux'
}
MacOSX64 {
architecture 'x86_64'
operatingSystem 'osx'
}
MacOSX_ARM64 {
architecture 'arm-v8'
operatingSystem 'osx'
}
Windows64 {
architecture 'x86_64'
operatingSystem 'windows'
}
}
toolChains { // prioritize among the native toolchains
visualCpp(VisualCpp) // used when compiling on Windows
//clang(Clang) // to prefer Clang over Gcc
gcc(Gcc) // used when compiling on Linux
clang(Clang) // used when compiling on macOS
gcc10Arm(Gcc) { // used at Travis CI and GitHub Actions
target('Linux_ARM64') {
cppCompiler.executable = 'aarch64-linux-gnu-g++-10'
linker.executable = 'aarch64-linux-gnu-g++-10'
}
}
gcc9Arm(Gcc) { // used at Travis CI and GitHub Actions
target('Linux_ARM32hf') {
cppCompiler.executable = 'arm-linux-gnueabihf-g++-9'
linker.executable = 'arm-linux-gnueabihf-g++-9'
}
}
}
components {
joltjni(NativeLibrarySpec) {
targetPlatform 'Linux64'
targetPlatform 'Linux_ARM32hf'
targetPlatform 'Linux_ARM64'
targetPlatform 'MacOSX64'
targetPlatform 'MacOSX_ARM64'
targetPlatform 'Windows64'
sources.cpp.source {
srcDir 'src/main/native/Jolt'
srcDir 'src/main/native/glue'
include '**/*.cpp'
}
binaries.withType(SharedLibraryBinarySpec) {
String pName = targetPlatform.name
//println " - $pName $buildType $flavor" // to debug this script
buildable = true
String os = targetPlatform.operatingSystem.name
if (buildable && project.hasProperty('bt')) {
// set in gradle.properties file or -Pbt= on the command line
String btArg = project.ext.bt
buildable = (buildType.name == btArg)
}
if (buildable && project.hasProperty('flavor')) {
// set in gradle.properties file or -Pflavor= on the command line
String flavorArg = project.ext.flavor
buildable = (flavor.name == flavorArg)
}
if (buildable && project.hasProperty('target')) {
// set in gradle.properties file or -Ptarget= on the command line
String targetArg = project.ext.target
buildable = (pName == targetArg)
}
// flavor-specific CPP defines:
Boolean isDebug = (buildType == buildTypes.Debug)
if (isDebug) {
cppCompiler.define '_DEBUG'
cppCompiler.define 'JPH_ENABLE_ASSERTS'
}
Boolean isDp = (flavor == flavors.Dp)
if (isDp) {
cppCompiler.define 'JPH_DOUBLE_PRECISION'
}
String q = pName + buildType.name + flavor.name
if (toolChain in VisualCpp) {
cppCompiler.define 'WIN32'
cppCompiler.args '/EHsc' // synchronous exceptions only
cppCompiler.args "/I$javaHome/include"
cppCompiler.args "/I$javaHome/include/win32"
cppCompiler.args "/I$projectDir/src/main/native"
cppCompiler.args '/std:c++17'
if (isDebug) {
cppCompiler.args '/MTd' // to use LIBCMTD
linker.args '/DEBUG'
} else { // buildType == Release
cppCompiler.args '/O2'
cppCompiler.args '/Ob3'
}
} else { // toolChain in Clang or Gcc
//cppCompiler.args '-v' // to log compiler details
//linker.args '-v' // to log linker details
cppCompiler.args '-I', "$javaHome/include"
cppCompiler.args '-I', "$projectDir/src/main/native"
cppCompiler.args '-std=c++17'
cppCompiler.args '-Werror=return-type'
if (isDebug) {
cppCompiler.args '-O0', '-g3'
} else { // buildType == Release
cppCompiler.args '-O3'
}
if (os == 'osx') {
cppCompiler.args '-I', "$javaHome/include/darwin"
} else if (os == 'linux') {
cppCompiler.args '-I', "$javaHome/include/linux"
cppCompiler.args '-fPIC'
cppCompiler.args '-fvisibility=hidden'
linker.args '-fvisibility=hidden'
} else {
buildable = false
}
}
if (buildable) {
println 'Build ' + q + ' using ' + toolChain
String replaceWith = targetPlatform.operatingSystem.name \
+ '/' + targetPlatform.architecture.name \
+ '/com/github/stephengold/$1'
project.tasks.register('nativesJar' + q, Jar) {
archiveBaseName = project.ext.baseName
archiveClassifier = buildType.name + flavor.name
dependsOn "joltjni${q}SharedLibrary"
description "Creates a JAR of the ${q} native library."
from sharedLibraryFile
rename '(.+)', replaceWith
}
Task njTask = project.tasks.named('nativesJar' + q, Jar).get()
project.artifacts {
archives njTask.archiveFile
}
project.publishing.publications.maven {
artifact njTask
}
} else {
//println 'Do not build ' + q // to debug this script
}
}
binaries.withType(StaticLibraryBinarySpec) {
buildable = false
}
}
}
}
dependencies {
testImplementation(libs.jsnaploader)
testImplementation(libs.junit4)
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType(JavaCompile) { // Java compile-time options:
options.compilerArgs << '-Xdiags:verbose'
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true // to provide detailed deprecation warnings
options.encoding = 'UTF-8'
options.headerOutputDirectory = new File('src/main/native/auto')
options.release = 11
}
application {
mainClass = 'testjoltjni.app.HelloWorld'
}
tasks.register('ConvexVsMeshScene', JavaExec) {
mainClass = 'testjoltjni.app.ConvexVsMeshScene'
}
tasks.register('HelloWorld', JavaExec) {
mainClass = 'testjoltjni.app.HelloWorld'
}
tasks.withType(JavaExec).configureEach { // Java runtime options:
classpath sourceSets.test.runtimeClasspath
enableAssertions true
}
run.dependsOn('assemble')
test.dependsOn('assemble')
checkstyle {
toolVersion libs.versions.checkstyle.get()
}
tasks.register('checkstyle') {
dependsOn 'checkstyleMain', 'checkstyleTest'
description 'Checks the style of all Java sourcecode.'
}
// Register publishing tasks:
tasks.register('install') {
dependsOn 'publishMavenPublicationToMavenLocal'
description 'Installs Maven artifacts to the local repository.'
}
tasks.register('release') {
dependsOn 'publishMavenPublicationToOSSRHRepository'
description 'Stages Maven artifacts to Sonatype OSSRH.'
}
jar {
archiveBaseName = project.ext.baseName
doLast {
println "using Java ${JavaVersion.current()} (${System.getProperty("java.vendor")})"
}
manifest {
attributes 'Created-By': "${JavaVersion.current()} (${System.getProperty("java.vendor")})"
}
}
javadoc.dependsOn('compileTestJava')
java.withJavadocJar()
javadocJar { archiveBaseName = project.ext.baseName }
tasks.register('sourcesJar', Jar) {
archiveBaseName = project.ext.baseName
archiveClassifier = 'sources'
description 'Creates a JAR of Java sourcecode.'
from 'src/main/java' // default is ".allSource", which includes resources
}
publishing {
publications {
maven(MavenPublication) {
artifact sourcesJar
artifactId artifact
from components.java
groupId project.ext.group
pom {
description = 'a JNI interface to Jolt Physics'
developers {
developer {
email = '[email protected]'
name = 'Stephen Gold'
}
}
inceptionYear = '2024'
licenses {
license {
distribution = 'repo'
name = 'MIT License'
url = 'https://opensource.org/licenses/MIT'
}
}
name = project.ext.group + ':' + artifact
scm {
connection = 'scm:git:git://github.com/stephengold/jolt-jni.git'
developerConnection = 'scm:git:ssh://github.com:stephengold/jolt-jni.git'
url = project.ext.websiteUrl + '/tree/master'
}
url = project.ext.websiteUrl
}
version project.ext.version
}
}
// Staging to OSSRH relies on the existence of 2 properties
// (ossrhUsername and ossrhPassword)
// which should be stored in ~/.gradle/gradle.properties
// or passed in the command line
repositories {
maven {
credentials {
username = project.hasProperty('ossrhUsername') ? ossrhUsername : 'Unknown user'
password = project.hasProperty('ossrhPassword') ? ossrhPassword : 'Unknown password'
}
name = 'OSSRH'
url = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2'
}
}
}
publishMavenPublicationToMavenLocal.dependsOn('assemble')
publishMavenPublicationToMavenLocal.doLast {
println 'installed locally as ' + project.ext.baseName
}
// Register signing tasks:
// Signing relies on the existence of 2 properties
// (signingKeyEncoded and signingPassword)
// which should be stored in ~/.gradle/gradle.properties
// or passed in the command line
signing {
def signingKey = base64Decode(findProperty('signingKeyEncoded'))
def signingPassword = findProperty('signingPassword')
useInMemoryPgpKeys(signingKey, signingPassword)
sign configurations.archives
sign publishing.publications.maven
}
tasks.withType(Sign) {
onlyIf { project.hasProperty('signingKeyEncoded') }
}
def base64Decode(encodedString){
if (encodedString != null) {
byte[] decoded = encodedString.decodeBase64()
String decode = new String(decoded)
return decode
}
return null;
}
// Download and extract archived JoltPhysics source code
tasks.register('downloadJoltSource', Download) {
src 'https://github.com/jrouwe/JoltPhysics/archive/refs/tags/v5.1.0.zip'
dest file('downloads/JoltPhysics-5.1.0.zip')
overwrite false
}
tasks.register('unpackJoltSource', Copy) {
dependsOn 'downloadJoltSource'
from (zipTree('downloads/JoltPhysics-5.1.0.zip')) {
include 'JoltPhysics-5.1.0/Jolt/**'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('src/main/native/Jolt')
}
// Register cleanup tasks:
clean.dependsOn('cleanAutoHeaders', 'cleanLogs')
tasks.register('cleanAutoHeaders', Delete) { // auto-generated JNI headers
delete 'src/main/native/auto'
}
tasks.register('cleanDownloads', Delete) { // downloaded files
delete 'downloads'
}
tasks.register('cleanJoltSource', Delete) { // unpacked Jolt Physics source code
delete 'src/main/native/Jolt'
}
tasks.register('cleanLogs', Delete) { // JVM crash logs
delete fileTree(dir: '.', include: 'hs_err_pid*.log')
}