-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.gradle
598 lines (531 loc) · 21.3 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// Gradle script to build/publish jolt-jni desktop artifacts
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 detect undeclared/unused dependencies
alias(libs.plugins.download) // to retrieve files from URLs
alias(libs.plugins.validate.poms) // to verify POMs provide all info required by Maven Central
}
// $artifact and $jjVersion are set in the gradle.properties file
// or by -P options on the Gradle command line.
ext {
downloadedJoltZip = 'downloads/JoltPhysics-sg250217.zip'
downloadedVhacdZip = 'downloads/v-hacd-4.1.0.zip'
groupID = 'com.github.stephengold'
baseName = "${artifact}-${jjVersion}" // for artifacts
websiteUrl = 'https://github.com/stephengold/jolt-jni'
}
// Generate all JNI header files and unpack Jolt before compiling any C++ code:
tasks.withType(CppCompile).configureEach {
dependsOn('classes', 'compileTestJava', \
'unpackJoltSource', 'unpackTestFramework', 'unpackVhacdSource')
}
String javaHome = org.gradle.internal.jvm.Jvm.current().javaHome.absolutePath
model {
buildTypes {
Debug // for development, debugging, and functional testing
Release // for performance testing and production
}
flavors {
Dp // double-precision locations
Sp // single-precision locations
}
platforms {
// To build native libraries for Android, use "android.gradle" instead.
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 the available native toolchains
if (project.hasProperty('tool')) {
// set in gradle.properties file or -Ptool= on the command line
if (project.ext.tool == 'clang') {
clang(Clang)
} else if (project.ext.tool == 'gcc') {
gcc(Gcc)
} else if (project.ext.tool == 'visualCpp') {
visualCpp(VisualCpp)
}
}
visualCpp(VisualCpp)
clang(Clang)
gcc(Gcc)
gcc10Arm(Gcc) { // used when cross-compiling on Linux
target('Linux_ARM64') {
cppCompiler.executable = 'aarch64-linux-gnu-g++-10'
linker.executable = 'aarch64-linux-gnu-g++-10'
}
}
gcc9Arm(Gcc) { // used when cross-compiling on Linux
target('Linux_ARM32hf') {
cppCompiler.executable = 'arm-linux-gnueabihf-g++-9'
linker.executable = 'arm-linux-gnueabihf-g++-9'
}
}
}
components {
// To build native libraries for Android, use "android.gradle" instead.
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/TestFramework/External'
srcDir 'src/main/native/glue'
include '**/*.cpp'
}
binaries.withType(SharedLibraryBinarySpec) {
String pName = targetPlatform.name
//println " - $pName $buildType $flavor" // to debug this script
buildable = true
// Decide whether to build the current type:
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)
}
// Decide whether to build the current flavor:
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)
}
// Decide whether to build for the target platform:
if (buildable && project.hasProperty('target')) {
// set in gradle.properties file or -Ptarget= on the command line
String targetArg = project.ext.target
buildable = (pName == targetArg)
}
Boolean isDp = (flavor == flavors.Dp)
String os = targetPlatform.operatingSystem.name
// always-on preprocessor defines:
cppCompiler.define 'JPH_OBJECT_STREAM'
// ISA extensions:
Boolean use_fma = false
if (targetPlatform.architecture.name == 'x86-64' && project.hasProperty('use_fma')) {
use_fma = true
cppCompiler.define 'JPH_USE_AVX2' // implies SSE4.1, SSE4.2, AVX, F16C, BMI/TZCNT, and LZCNT
cppCompiler.define 'JPH_USE_FMADD'
}
// buildtype-specific preprocessor defines:
Boolean isDebug = (buildType == buildTypes.Debug)
if (isDebug) {
cppCompiler.define 'JPH_DEBUG_RENDERER'
//cppCompiler.define 'JPH_DET_LOG' // to implement the determinism log
cppCompiler.define 'JPH_ENABLE_ASSERTS'
//cppCompiler.define 'JPH_TRACK_NARROWPHASE_STATS' // to collect statistics
} else {
cppCompiler.define 'JPH_NO_DEBUG'
}
// flavor-specific preprocessor defines:
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 "/I$projectDir/src/main/native/TestFramework" // for External/Perlin.h
cppCompiler.args '/std:c++17'
if (isDebug) {
cppCompiler.define 'JPH_FLOATING_POINT_EXCEPTIONS_ENABLED'
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 '-I', "$projectDir/src/main/native/TestFramework" // for External/Perlin.h
cppCompiler.args '-std=c++17'
cppCompiler.args '-Werror=return-type'
if (use_fma) {
cppCompiler.args '-msse4.1'
cppCompiler.args '-msse4.2'
cppCompiler.args '-mavx'
cppCompiler.args '-mf16c'
cppCompiler.args '-mavx2'
cppCompiler.args '-mbmi'
cppCompiler.args '-mlzcnt'
cppCompiler.args '-mfma'
}
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 if (os == 'windows') {
cppCompiler.args '-I', "$javaHome/include/win32"
} else {
buildable = false
}
}
if (buildable) {
if (use_fma) {
println 'Build ' + q + '+FMA using ' + toolChain
} else {
println 'Build ' + q + ' using ' + toolChain
}
// Generate resource path for native library, one of:
// linux/x86-64/com/github/stephengold/libjoltjni.so
// linux/armhf/com/github/stephengold/libjoltjni.so
// linux/aarch64/com/github/stephengold/libjoltjni.so
// osx/x86-64/com/github/stephengold/libjoltjni.dylib
// osx/aarch64/com/github/stephengold/libjoltjni.dylib
// windows/x86-64/com/github/stephengold/joltjni.dll
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 { // not buildable
//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).configureEach { // 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
}
// Register test-execution tasks:
application {
// default settings for the 'run' task:
mainClass = 'testjoltjni.app.helloworld.HelloWorld'
}
tasks.register('debugConvexVsMesh', JavaExec) {
args '-s=ConvexVsMesh'
debug = true
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('debugHelloWorld', JavaExec) {
debug = true
mainClass = 'testjoltjni.app.helloworld.HelloWorld'
}
tasks.register('debugPyramid', JavaExec) {
args '-s=Pyramid'
debug = true
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('debugRagdoll', JavaExec) {
args '-s=Ragdoll', '-r', '-rs'
debug = true
dependsOn('unpackJoltAssets')
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runCharacterVirtual', JavaExec) {
args '-s=CharacterVirtual'
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runConvexVsMesh', JavaExec) {
args '-s=ConvexVsMesh'
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runHelloWorld', JavaExec) {
mainClass = 'testjoltjni.app.helloworld.HelloWorld'
}
tasks.register('runLargeMesh', JavaExec) {
args '-s=LargeMesh'
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runPyramid', JavaExec) {
args '-s=Pyramid'
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runRagdoll', JavaExec) {
args '-s=Ragdoll'
dependsOn('unpackJoltAssets')
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runRagdollSinglePile', JavaExec) {
args '-s=RagdollSinglePile'
dependsOn('unpackJoltAssets')
enableAssertions = false
mainClass = 'testjoltjni.app.performancetest.PerformanceTest'
}
tasks.register('runSmokeTestAll', JavaExec) {
dependsOn('unpackJoltAssets')
mainClass = 'testjoltjni.app.samples.SmokeTestAll'
}
tasks.withType(JavaExec).configureEach { // Java runtime options:
classpath sourceSets.test.runtimeClasspath
dependsOn('assemble')
enableAssertions = true
}
test.dependsOn('assemble')
// Register style-checking tasks:
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 = 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 = baseName }
tasks.register('sourcesJar', Jar) {
archiveBaseName = 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 = groupID
pom {
description = 'a JNI interface to Jolt Physics'
developers {
developer {
email = '[email protected]'
id = 'stephengold'
name = 'Stephen Gold'
}
}
inceptionYear = '2024'
licenses {
license {
distribution = 'repo'
name = 'MIT License'
url = 'https://opensource.org/licenses/MIT'
}
}
name = groupID + ':' + artifact
scm {
connection = 'scm:git:git://github.com/stephengold/jolt-jni.git'
developerConnection = 'scm:git:ssh://github.com:stephengold/jolt-jni.git'
url = websiteUrl + '/tree/master'
}
url = websiteUrl
}
version = jjVersion
}
}
repositories {
maven { // the staging repo of Sonatype OSSRH
// Staging to OSSRH relies on the existence of 2 properties
// (ossrhUsername and ossrhPassword)
// which should be set in the ~/.gradle/gradle.properties file
// or by -P options on the command line.
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 ' + baseName
}
// Register tasks to sign artifacts for publication:
// Signing relies on the existence of 2 properties
// (signingKeyEncoded and signingPassword)
// which should be set in the ~/.gradle/gradle.properties file
// or by -P options on 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).configureEach {
onlyIf { project.hasProperty('signingKeyEncoded') }
}
static def base64Decode(encodedString) {
if (encodedString != null) {
byte[] decoded = encodedString.decodeBase64()
String decode = new String(decoded)
return decode
}
return null
}
// Register tasks to download ZIP archives:
tasks.register('downloadJoltZip', Download) {
src 'https://github.com/stephengold/JoltPhysics/archive/refs/tags/sg250217.zip'
dest file(downloadedJoltZip)
overwrite false
}
tasks.register('downloadVhacdZip', Download) {
src 'https://github.com/kmammou/v-hacd/archive/refs/tags/v4.1.0.zip'
dest file(downloadedVhacdZip)
overwrite false
}
// Register tasks to unpack assets and source code:
tasks.register('unpackJoltAssets', Copy) {
dependsOn 'downloadJoltZip'
from (zipTree(downloadedJoltZip)) {
include 'JoltPhysics-sg250217/Assets/**'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('Assets')
}
tasks.register('unpackJoltSource', Copy) {
dependsOn 'downloadJoltZip'
from (zipTree(downloadedJoltZip)) {
include 'JoltPhysics-sg250217/Jolt/**'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('src/main/native/Jolt')
}
tasks.register('unpackTestFramework', Copy) {
dependsOn 'downloadJoltZip'
from (zipTree(downloadedJoltZip)) {
include 'JoltPhysics-sg250217/TestFramework/**'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('src/main/native/TestFramework')
}
tasks.register('unpackVhacdSource', Copy) {
dependsOn 'downloadVhacdZip'
from (zipTree(downloadedVhacdZip)) {
include 'v-hacd-4.1.0/include/VHACD.h'
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(2))
}
includeEmptyDirs = false
}
into layout.projectDirectory.dir('src/main/native')
}
// Register cleanup tasks:
clean.dependsOn('cleanAutoHeaders', 'cleanLogs', 'cleanRecordings')
tasks.register('cleanAll') { // restore the project to a pristine state
dependsOn('clean', 'cleanDownloads', \
'cleanJoltAssets', 'cleanJoltSource', 'cleanTestFramework', 'cleanVhacdSource')
}
tasks.register('cleanAutoHeaders', Delete) { // auto-generated JNI headers
delete 'src/main/native/auto'
}
tasks.register('cleanDownloads', Delete) { // downloaded ZIP archives
delete 'downloads'
}
tasks.register('cleanJoltAssets', Delete) { // unpacked Jolt Physics assets
delete 'Assets'
}
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')
}
tasks.register('cleanRecordings', Delete) {
// DebugRenderer recordings, recorded state, and frame timing output:
delete fileTree(dir: '.', includes: ['*.jor', 'per_frame_*.csv', 'state_*.bin'])
}
tasks.register('cleanTestFramework', Delete) { // unpacked Jolt Physics TestFramework source code
delete 'src/main/native/TestFramework'
}
tasks.register('cleanVhacdSource', Delete) { // unpacked V-HACD source code
delete 'src/main/native/VHACD.h'
}