|
| 1 | +package com.sourcegraph.buildlogic |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import org.gradle.api.Task |
| 5 | +import org.gradle.api.artifacts.Configuration |
| 6 | +import org.gradle.api.file.Directory |
| 7 | +import org.gradle.api.file.FileSystemLocation |
| 8 | +import org.gradle.api.provider.Provider |
| 9 | +import org.gradle.api.tasks.compile.JavaCompile |
| 10 | + |
| 11 | +/** |
| 12 | + * Wires this [JavaCompile] task to run the scip-javac compiler plugin: puts the shaded plugin jar |
| 13 | + * [javacShadowJar] on the compile classpath and forks javac with the internal-API flags from |
| 14 | + * gradle/javac-internals.properties, recording [targetroot] as an output directory. |
| 15 | + * |
| 16 | + * Callers still add the case-specific `-Xplugin:scip ...` arguments and annotation processors. |
| 17 | + */ |
| 18 | +fun JavaCompile.useScipJavac( |
| 19 | + rootDir: File, |
| 20 | + javacShadowJar: Configuration, |
| 21 | + targetroot: Provider<Directory>, |
| 22 | +) { |
| 23 | + classpath = classpath.plus(javacShadowJar) |
| 24 | + outputs.dir(targetroot) |
| 25 | + options.isFork = true |
| 26 | + options.forkOptions.jvmArgs = |
| 27 | + (options.forkOptions.jvmArgs ?: emptyList()) + JavacInternals.jvmOptions(rootDir) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Builds the `kotlinc` arguments that load the scip-kotlinc compiler plugin from [pluginClasspath] |
| 32 | + * (the resolved shaded jar) and point it at [sourceroot]/[targetroot]. |
| 33 | + * |
| 34 | + * The mapping lives here, in compiled build logic, rather than in a build script: a `.map {}` |
| 35 | + * lambda declared in a `.gradle.kts` file captures a hidden reference to the script object, which |
| 36 | + * the configuration cache cannot serialize. |
| 37 | + */ |
| 38 | +fun scipKotlincPluginArgs( |
| 39 | + pluginClasspath: Provider<Set<FileSystemLocation>>, |
| 40 | + sourceroot: String, |
| 41 | + targetroot: String, |
| 42 | +): Provider<List<String>> = |
| 43 | + pluginClasspath.map { locations -> |
| 44 | + listOf( |
| 45 | + "-Xplugin=${locations.single().asFile.absolutePath}", |
| 46 | + "-P", |
| 47 | + "plugin:scip-kotlinc:sourceroot=$sourceroot", |
| 48 | + "-P", |
| 49 | + "plugin:scip-kotlinc:targetroot=$targetroot", |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | +/** |
| 54 | + * Registers a `doFirst` action that empties [dir] (deletes then recreates it) before the task runs. |
| 55 | + * |
| 56 | + * The action lives here, in compiled build logic, rather than in a `.gradle.kts` script: a lambda |
| 57 | + * declared in a build script captures a hidden reference to the script object, which the |
| 58 | + * configuration cache cannot serialize. |
| 59 | + */ |
| 60 | +fun Task.cleanDirectoryBeforeRunning(dir: Provider<Directory>) { |
| 61 | + doFirst { |
| 62 | + val file = dir.get().asFile |
| 63 | + file.deleteRecursively() |
| 64 | + file.mkdirs() |
| 65 | + } |
| 66 | +} |
0 commit comments