Skip to content

Commit eb731e0

Browse files
committed
Enable configuration cache and consolidate generated-file tasks
1 parent e861250 commit eb731e0

6 files changed

Lines changed: 78 additions & 38 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sourcegraph.buildlogic
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.Task
5+
import org.gradle.api.tasks.TaskProvider
6+
7+
/**
8+
* Registers a task that writes [content] (plus a trailing newline) to
9+
* build/generated/[relativePath], tracking the content for up-to-date checks.
10+
*/
11+
fun Project.registerGeneratedFile(
12+
name: String,
13+
relativePath: String,
14+
content: String,
15+
): TaskProvider<Task> {
16+
val output = layout.buildDirectory.file("generated/$relativePath")
17+
return tasks.register(name) {
18+
inputs.property("content", content)
19+
outputs.file(output)
20+
doLast {
21+
val file = output.get().asFile
22+
file.parentFile.mkdirs()
23+
file.writeText("$content\n")
24+
}
25+
}
26+
}

build-logic/src/main/kotlin/com/sourcegraph/buildlogic/SharedArtifacts.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.sourcegraph.buildlogic
22

33
import org.gradle.api.Project
44
import org.gradle.api.artifacts.Configuration
5+
import org.gradle.api.file.FileSystemLocation
6+
import org.gradle.api.provider.Provider
57

68
/**
79
* Declares a resolvable view of another project's shaded jar, published by its `shadowJarElements`
@@ -20,3 +22,26 @@ fun Project.shadowJarArtifact(producerPath: String, name: String): Configuration
2022
)
2123
return configurations.resolvable(name) { extendsFrom(bucket.get()) }.get()
2224
}
25+
26+
/**
27+
* Builds the `kotlinc` arguments that load the scip-kotlinc compiler plugin from [pluginClasspath]
28+
* (the resolved shaded jar) and point it at [sourceroot]/[targetroot].
29+
*
30+
* The mapping lives here, in compiled build logic, rather than in a build script: a `.map {}`
31+
* lambda declared in a `.gradle.kts` file captures a hidden reference to the script object, which
32+
* the configuration cache cannot serialize.
33+
*/
34+
fun scipKotlincPluginArgs(
35+
pluginClasspath: Provider<Set<FileSystemLocation>>,
36+
sourceroot: String,
37+
targetroot: String,
38+
): Provider<List<String>> =
39+
pluginClasspath.map { locations ->
40+
listOf(
41+
"-Xplugin=${locations.single().asFile.absolutePath}",
42+
"-P",
43+
"plugin:scip-kotlinc:sourceroot=$sourceroot",
44+
"-P",
45+
"plugin:scip-kotlinc:targetroot=$targetroot",
46+
)
47+
}

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
2+
org.gradle.configuration-cache=true
3+
org.gradle.parallel=true
24
kotlin.code.style=official
35
kotlin.stdlib.default.dependency=false

scip-java/build.gradle.kts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.sourcegraph.buildlogic.JavacInternals
2+
import com.sourcegraph.buildlogic.registerGeneratedFile
23
import com.sourcegraph.buildlogic.shadowJarArtifact
34

45
plugins {
@@ -62,32 +63,18 @@ tasks.named<Jar>("jar") {
6263
}
6364
}
6465

65-
val generateDistributionVersion = tasks.register("generateDistributionVersion") {
66-
val output = layout.buildDirectory.file("generated/distribution/VERSION")
67-
val version = project.version.toString()
68-
inputs.property("version", version)
69-
outputs.file(output)
70-
doLast {
71-
val file = output.get().asFile
72-
file.parentFile.mkdirs()
73-
file.writeText("version:=$version\n")
74-
}
75-
}
66+
val generateDistributionVersion =
67+
registerGeneratedFile("generateDistributionVersion", "distribution/VERSION", "version:=${project.version}")
7668

7769
// Emit the javac --add-exports flags as a ready-to-use, space-separated string
7870
// so the Docker wrapper script can read them with a plain `cat`, keeping
7971
// gradle/javac-internals.properties the single source of truth.
80-
val generateJavacJvmOptions = tasks.register("generateJavacJvmOptions") {
81-
val output = layout.buildDirectory.file("generated/distribution/javac-jvm-options")
82-
val options = JavacInternals.jvmOptions(rootDir).joinToString(" ")
83-
inputs.property("options", options)
84-
outputs.file(output)
85-
doLast {
86-
val file = output.get().asFile
87-
file.parentFile.mkdirs()
88-
file.writeText("$options\n")
89-
}
90-
}
72+
val generateJavacJvmOptions =
73+
registerGeneratedFile(
74+
"generateJavacJvmOptions",
75+
"distribution/javac-jvm-options",
76+
JavacInternals.jvmOptions(rootDir).joinToString(" "),
77+
)
9178

9279
distributions {
9380
named("main") {

scip-maven-plugin/build.gradle.kts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.apache.tools.ant.filters.ReplaceTokens
2+
13
plugins {
24
id("scip.java-library")
35
id("scip.maven-publish")
@@ -11,12 +13,14 @@ dependencies {
1113
compileOnly(libs.maven.plugin.annotations)
1214
}
1315

16+
val mavenPluginVersion = project.version.toString()
17+
1418
tasks.named<ProcessResources>("processResources") {
1519
exclude("META-INF/maven/plugin.template.xml")
1620
from("src/main/resources/META-INF/maven/plugin.template.xml") {
1721
into("META-INF/maven")
1822
rename { "plugin.xml" }
19-
filter { line: String -> line.replace("@VERSION@", project.version.toString()) }
23+
filter(mapOf("tokens" to mapOf("VERSION" to mavenPluginVersion)), ReplaceTokens::class.java)
2024
}
21-
inputs.property("version", project.version.toString())
25+
inputs.property("version", mavenPluginVersion)
2226
}

scip-snapshots/cases/kotlin/common/build.gradle.kts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.sourcegraph.buildlogic.JavacInternals
2+
import com.sourcegraph.buildlogic.scipKotlincPluginArgs
23
import com.sourcegraph.buildlogic.shadowJarArtifact
34
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
45

@@ -16,24 +17,19 @@ dependencies {
1617

1718
val scipTargetroot = layout.buildDirectory.dir("scip-targetroot")
1819

19-
val kotlincPluginArgs = kotlincShadowJar.elements.map { locations ->
20-
val pluginPath = locations.single().asFile.absolutePath
21-
listOf(
22-
"-Xplugin=$pluginPath",
23-
"-P",
24-
"plugin:scip-kotlinc:sourceroot=${rootProject.rootDir.absolutePath}",
25-
"-P",
26-
"plugin:scip-kotlinc:targetroot=${scipTargetroot.get().asFile.absolutePath}",
27-
)
28-
}
20+
val sourceroot = rootProject.rootDir.absolutePath
21+
val targetroot = scipTargetroot.get().asFile.absolutePath
2922

3023
tasks.named<KotlinCompile>("compileKotlin") {
24+
// Bind to a local so the doFirst action captures the provider, not the
25+
// surrounding script object (which the configuration cache cannot serialize).
26+
val targetrootDir = scipTargetroot
3127
inputs.files(kotlincShadowJar)
32-
outputs.dir(scipTargetroot)
33-
compilerOptions.freeCompilerArgs.addAll(kotlincPluginArgs)
28+
outputs.dir(targetrootDir)
29+
compilerOptions.freeCompilerArgs.addAll(scipKotlincPluginArgs(kotlincShadowJar.elements, sourceroot, targetroot))
3430
doFirst {
35-
scipTargetroot.get().asFile.deleteRecursively()
36-
scipTargetroot.get().asFile.mkdirs()
31+
targetrootDir.get().asFile.deleteRecursively()
32+
targetrootDir.get().asFile.mkdirs()
3733
}
3834
}
3935

0 commit comments

Comments
 (0)