Skip to content

Commit f1648bb

Browse files
authored
Update gradle build script to support configuration cache (deepjavalibrary#3782)
* Update gradle build script to support configuration cache * Fix fasttext continuous build
1 parent de7289b commit f1648bb

File tree

18 files changed

+507
-380
lines changed

18 files changed

+507
-380
lines changed

.github/workflows/continuous.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ jobs:
6868
- 'extensions/fasttext/**'
6969
- name: Compile fastText JNI macOs
7070
if: ${{ steps.fasttext_changes.outputs.src == 'true' && runner.os == 'macOs' }}
71-
run: ./gradlew :extensions:fasttext:compileJNI
71+
run: |
72+
export CMAKE_POLICY_VERSION_MINIMUM=3.5
73+
./gradlew :extensions:fasttext:compileJNI
7274
- name: Compile fastText JNI linux
7375
if: ${{ steps.fasttext_changes.outputs.src == 'true' && runner.os == 'Linux' }}
7476
# fasttext on linux requires gcc12, but ubuntu-latest uses at least gcc13

api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tasks {
2222
compileJava { dependsOn(processResources) }
2323

2424
processResources {
25+
val version = project.version
2526
inputs.properties(mapOf("version" to version))
2627
filesMatching("**/api.properties") {
2728
expand(mapOf("version" to version))

buildSrc/src/main/kotlin/ai/djl/cppFormatter.gradle.kts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,28 @@ import os
55
import text
66
import url
77

8-
fun checkClang(clang: File) {
9-
val url = "https://djl-ai.s3.amazonaws.com/build-tools/osx/clang-format".url
10-
if (!clang.exists()) {
11-
// create the folder and download the executable
12-
clang.parentFile.mkdirs()
13-
// TODO original method was appending
14-
clang.writeBytes(url.openStream().use { it.readAllBytes() })
15-
clang.setExecutable(true)
8+
tasks {
9+
fun checkClang(clang: File) {
10+
val url = "https://djl-ai.s3.amazonaws.com/build-tools/osx/clang-format".url
11+
if (!clang.exists()) {
12+
// create the folder and download the executable
13+
clang.parentFile.mkdirs()
14+
// TODO original method was appending
15+
clang.writeBytes(url.openStream().use { it.readAllBytes() })
16+
clang.setExecutable(true)
17+
}
1618
}
17-
}
18-
19-
fun formatCpp(f: File, clang: File): String = when {
20-
!f.name.endsWith(".cc") && !f.name.endsWith(".cpp") && !f.name.endsWith(".h") -> ""
21-
else -> ProcessBuilder(
22-
clang.absolutePath,
23-
"-style={BasedOnStyle: Google, IndentWidth: 2, ColumnLimit: 120, AlignAfterOpenBracket: DontAlign, SpaceAfterCStyleCast: true}",
24-
f.absolutePath
25-
)
26-
.start().inputStream.use { it.readAllBytes().decodeToString() }
27-
}
2819

29-
interface FormatterConfig {
30-
val exclusions: ListProperty<String>
31-
}
32-
project.extensions.create<FormatterConfig>("formatCpp")
20+
fun formatCpp(f: File, clang: File): String = when {
21+
!f.name.endsWith(".cc") && !f.name.endsWith(".cpp") && !f.name.endsWith(".h") -> ""
22+
else -> ProcessBuilder(
23+
clang.absolutePath,
24+
"-style={BasedOnStyle: Google, IndentWidth: 2, ColumnLimit: 120, AlignAfterOpenBracket: DontAlign, SpaceAfterCStyleCast: true}",
25+
f.absolutePath
26+
)
27+
.start().inputStream.use { it.readAllBytes().decodeToString() }
28+
}
3329

34-
project.tasks {
3530
register("formatCpp") {
3631
val rootProject = project.rootProject
3732
val clang = rootProject.projectDir / ".clang/clang-format"
@@ -78,3 +73,9 @@ project.tasks {
7873
}
7974
}
8075
}
76+
77+
interface FormatterConfig {
78+
val exclusions: ListProperty<String>
79+
}
80+
project.extensions.create<FormatterConfig>("formatCpp")
81+

buildSrc/src/main/kotlin/ai/djl/javaFormatter.gradle.kts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ import java.io.PrintWriter
77

88
tasks {
99
register("formatJava") {
10-
val sourceSets = project.sourceSets
10+
val resultFilePath = "build/formatJava-result.txt"
11+
var files = project.sourceSets.flatMap { it.allSource }
12+
files = files.filter { it.name.endsWith(".java") && "generated-src" !in it.absolutePath }
13+
inputs.files(files)
14+
outputs.file(project.file(resultFilePath))
1115
doLast {
1216
val formatter = Main(PrintWriter(System.out, true), PrintWriter(System.err, true), System.`in`)
13-
for (item in sourceSets)
14-
for (file in item.allSource) {
15-
if (!file.name.endsWith(".java") || "generated-src" in file.absolutePath)
16-
continue
17-
if (formatter.format("-a", "-i", file.absolutePath) != 0)
18-
throw GradleException("Format java failed: " + file.absolutePath)
17+
for (f in files) {
18+
if (formatter.format("-a", "-i", f.absolutePath) != 0) {
19+
throw GradleException("Format java failed: " + f.absolutePath)
1920
}
21+
}
22+
File(resultFilePath).writeText("Success")
2023
}
2124
}
2225

2326
val verifyJava by registering {
2427
val resultFilePath = "build/verifyJava-result.txt"
25-
inputs.files(project.sourceSets.flatMap { it.allSource })
26-
inputs.files(project.fileTree("generated-src"))
28+
var files = project.sourceSets.flatMap { it.allSource }
29+
files = files.filter { it.name.endsWith(".java") && "generated-src" !in it.absolutePath }
30+
inputs.files(files)
2731
outputs.file(project.file(resultFilePath))
28-
29-
val proj = project
3032
doLast {
3133
val formatter = Main(PrintWriter(System.out, true), PrintWriter(System.err, true), System.`in`)
32-
for (item in proj.sourceSets)
33-
for (file in item.allSource) {
34-
if (!file.name.endsWith(".java") || "generated-src" in file.absolutePath)
35-
continue
36-
if (formatter.format("-a", "-n", "--set-exit-if-changed", file.absolutePath) != 0)
37-
throw GradleException(
38-
"File not formatted: " + file.absolutePath
39-
+ System.lineSeparator()
40-
+ "In order to reformat your code, run './gradlew formatJava' (or './gradlew fJ' for short)"
41-
+ System.lineSeparator()
42-
+ "See https://github.com/deepjavalibrary/djl/blob/master/docs/development/development_guideline.md#coding-conventions for more details"
43-
)
34+
for (f in files) {
35+
if (formatter.format("-a", "-n", "--set-exit-if-changed", f.absolutePath) != 0) {
36+
throw GradleException(
37+
"File not formatted: " + f.absolutePath
38+
+ System.lineSeparator()
39+
+ "In order to reformat your code, run './gradlew formatJava' (or './gradlew fJ' for short)"
40+
+ System.lineSeparator()
41+
+ "See https://github.com/deepjavalibrary/djl/blob/master/docs/development/development_guideline.md#coding-conventions for more details"
42+
)
4443
}
45-
proj.file(resultFilePath).writeText("Success")
44+
}
45+
File(resultFilePath).writeText("Success")
4646
}
4747
}
4848

buildSrc/src/main/kotlin/ai/djl/stats.gradle.kts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ai.djl
22

3-
import org.gradle.api.plugins.ExtraPropertiesExtension
43
import org.gradle.kotlin.dsl.support.serviceOf
54
import org.gradle.tooling.events.FinishEvent
65
import org.gradle.tooling.events.OperationCompletionListener
@@ -46,21 +45,20 @@ abstract class StatisticsService : BuildService<StatisticsService.Parameters>,
4645
}
4746

4847
tasks.test {
48+
@OptIn(ExperimentalTime::class)
49+
val timeSource = timeSource
50+
val projectName = project.name
51+
val demoListener = demoListener
52+
val ext = project.ext
4953
doFirst {
5054
@OptIn(ExperimentalTime::class)
51-
startTime = timeSource.markNow()
55+
ext.set("startTime", timeSource.markNow())
5256
}
5357
doLast {
5458
@OptIn(ExperimentalTime::class)
55-
if (state.didWork)
56-
demoListener.get().parameters.testsResults[timeSource.markNow() - startTime] = project.name
59+
if (state.didWork) {
60+
val t = ext.get("startTime") as TimeSource.Monotonic.ValueTimeMark
61+
demoListener.get().parameters.testsResults[timeSource.markNow() - t] = projectName
62+
}
5763
}
5864
}
59-
60-
@ExperimentalTime
61-
var Task.startTime: TimeSource.Monotonic.ValueTimeMark
62-
get() = ext.get("startTime") as TimeSource.Monotonic.ValueTimeMark
63-
set(value) = ext.set("startTime", value)
64-
65-
val Task.ext: ExtraPropertiesExtension
66-
get() = (this as ExtensionAware).extensions.getByName<ExtraPropertiesExtension>("ext")

engines/ml/xgboost/build.gradle.kts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ tasks {
5757
}
5858

5959
publishing {
60+
val rapisVersion = libs.versions.rapis.get()
61+
val projectName = project.name
62+
val xgbFlavor = xgbFlavor
63+
val isGpu = isGpu
64+
6065
publications {
6166
named<MavenPublication>("maven") {
62-
artifactId = "${project.name}$xgbFlavor"
67+
artifactId = "${projectName}$xgbFlavor"
6368
pom {
6469
name = "DJL Engine Adapter for XGBoost"
6570
description = "Deep Java Library (DJL) Engine Adapter for XGBoost"
66-
url = "https://djl.ai/engines/ml/${project.name}"
71+
url = "https://djl.ai/engines/ml/${projectName}"
6772

6873
withXml {
6974
val pomNode = asElement()
@@ -85,7 +90,7 @@ tasks {
8590
}
8691
"groupId" addWith "ai.rapids"
8792
"artifactId" addWith "cudf"
88-
"version" addWith libs.versions.rapis.get()
93+
"version" addWith rapisVersion
8994
"classifier" addWith "cuda11"
9095
"scope" addWith "compile"
9196
}

engines/mxnet/mxnet-engine/build.gradle.kts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ sourceSets.main {
2525

2626
tasks {
2727
processResources {
28-
inputs.properties(mapOf("djlVersion" to libs.versions.djl.get(), "mxnetVersion" to libs.versions.mxnet.get()))
28+
val djlVersion = libs.versions.djl.get()
29+
val mxnetVersion = libs.versions.mxnet.get()
30+
inputs.properties(mapOf("djlVersion" to djlVersion, "mxnetVersion" to mxnetVersion))
2931
filesMatching("**/mxnet-engine.properties") {
30-
expand(mapOf("djlVersion" to libs.versions.djl.get(), "mxnetVersion" to libs.versions.mxnet.get()))
32+
expand(mapOf("djlVersion" to djlVersion, "mxnetVersion" to mxnetVersion))
3133
}
3234
}
3335

@@ -42,11 +44,13 @@ tasks {
4244
.withPropertyName("jna/mapping.properties file")
4345
outputs.dir(buildDirectory / "generated-src")
4446
outputs.cacheIf { true }
47+
val jnaGenerator = jnaratorJar.get().outputs.files.singleFile
48+
val buildDir = buildDirectory
49+
val dir = projectDir
50+
val prov = providers
4551

46-
val dir = project.projectDir
4752
doLast {
48-
val jnaGenerator = jnaratorJar.get().outputs.files.singleFile
49-
providers.javaexec {
53+
prov.javaexec {
5054
workingDir = dir
5155
mainClass = "-jar"
5256
args(
@@ -56,7 +60,7 @@ tasks {
5660
"-p",
5761
"ai.djl.mxnet.jna",
5862
"-o",
59-
"$buildDirectory/generated-src",
63+
"$buildDir/generated-src",
6064
"-m",
6165
"${dir}/src/main/jna/mapping.properties",
6266
"-f",

engines/mxnet/native/build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ tasks {
2424
outputs.dir("build/libs")
2525

2626
var versionName = project.version.toString()
27+
if (!isRelease)
28+
versionName += "-$nowFormatted"
29+
2730
doFirst {
28-
if (!isRelease)
29-
versionName += "-$nowFormatted"
3031
val dir = placeholder / "native/lib"
3132
dir.mkdirs()
3233
val propFile = placeholder / "native/lib/mxnet.properties"
@@ -153,7 +154,7 @@ tasks {
153154
withType<PublishToMavenRepository> {
154155
for (flavor in flavorNames) {
155156
if (requireSigning) {
156-
dependsOn("sign${flavor.substring(0, 1).uppercase() + flavor.substring(1)}Publication")
157+
dependsOn("sign${flavor.take(1).uppercase() + flavor.substring(1)}Publication")
157158
}
158159

159160
val platformNames = (binaryRoot / flavor).list() ?: emptyArray()
@@ -245,7 +246,7 @@ if (project.hasProperty("staging")) {
245246
doLast {
246247
val conn = URL(url).openConnection() as HttpURLConnection
247248
conn.requestMethod = "POST"
248-
conn.setRequestProperty("Authorization", "Bearer ${token}")
249+
conn.setRequestProperty("Authorization", "Bearer $token")
249250
val status = conn.responseCode
250251
if (status != HttpURLConnection.HTTP_OK) {
251252
project.logger.error("Failed to POST '${url}'. Received status code ${status}: ${conn.responseMessage}")

engines/pytorch/pytorch-engine/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ tasks {
1919
compileJava { dependsOn(processResources) }
2020

2121
processResources {
22-
inputs.properties(mapOf("djlVersion" to libs.versions.djl.get(), "pytorchVersion" to libs.versions.pytorch.get()))
22+
val djlVersion = libs.versions.djl.get()
23+
val pytorchVersion = libs.versions.pytorch.get()
24+
inputs.properties(mapOf("djlVersion" to djlVersion, "pytorchVersion" to pytorchVersion))
2325
filesMatching("**/pytorch-engine.properties") {
24-
expand(mapOf("djlVersion" to libs.versions.djl.get(), "pytorchVersion" to libs.versions.pytorch.get()))
26+
expand(mapOf("djlVersion" to djlVersion, "pytorchVersion" to pytorchVersion))
2527
}
2628
}
2729

0 commit comments

Comments
 (0)