|
1 | 1 | package org.scip_code.scip_java.gradle; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
3 | 7 | import java.util.ArrayList; |
4 | 8 | import java.util.HashSet; |
5 | 9 | import java.util.List; |
|
9 | 13 | import org.gradle.api.Project; |
10 | 14 | import org.gradle.api.Task; |
11 | 15 | import org.gradle.api.artifacts.Configuration; |
| 16 | +import org.gradle.api.file.FileCollection; |
12 | 17 | import org.gradle.api.initialization.Settings; |
13 | 18 | import org.gradle.api.internal.GradleInternal; |
14 | 19 | import org.gradle.api.invocation.Gradle; |
@@ -130,15 +135,14 @@ private void configureProject(Project project) { |
130 | 135 | triggers.add("compileKotlin"); |
131 | 136 | triggers.add("compileTestKotlin"); |
132 | 137 | } |
133 | | - |
134 | | - // The CLI's init script provides the path of the embedded scip-kotlinc jar. |
135 | | - Object scipKotlinc = requiredExtra(extraProperties, "scipKotlincJar"); |
136 | | - project |
137 | | - .getTasks() |
138 | | - .configureEach( |
139 | | - task -> configureKotlinCompileTask(task, scipKotlinc, sourceRoot, targetRoot)); |
140 | 138 | } |
141 | 139 |
|
| 140 | + // Not limited to the plugin checks above: AGP 9's built-in Kotlin adds |
| 141 | + // Kotlin compile tasks to Android modules without applying a Kotlin plugin. |
| 142 | + project |
| 143 | + .getTasks() |
| 144 | + .configureEach(task -> configureKotlinCompileTask(task, sourceRoot, targetRoot)); |
| 145 | + |
142 | 146 | project.getTasks().create("scipCompileAll").dependsOn(triggers); |
143 | 147 |
|
144 | 148 | project.getTasks().create("scipPrintDependencies", WriteDependencies.class); |
@@ -232,39 +236,72 @@ private static Set<String> addJavacPlugin(Project project, Object javacPluginDep |
232 | 236 | return pluginTasks; |
233 | 237 | } |
234 | 238 |
|
235 | | - private static void configureKotlinCompileTask( |
236 | | - Task task, Object scipKotlinc, String sourceRoot, String targetRoot) { |
| 239 | + /** |
| 240 | + * Kotlin sources are not indexed inside kotlinc: after each Kotlin compile task runs, its sources |
| 241 | + * and classpath are dumped to {@code <targetroot>/kotlin-configs/<task>.txt} for the scip-java |
| 242 | + * CLI to index after the build, decoupling indexing from the build's Kotlin compiler version. |
| 243 | + */ |
| 244 | + private static void configureKotlinCompileTask(Task task, String sourceRoot, String targetRoot) { |
237 | 245 | if (!task.getClass().getSimpleName().contains("KotlinCompile")) { |
238 | 246 | return; |
239 | 247 | } |
240 | 248 |
|
241 | | - // Referring to KotlinCompile directly here triggers NoClassDefFoundError - |
242 | | - // the plugin classpath is murky and we deliberately don't bundle the Kotlin |
243 | | - // Gradle plugin. So we commit the sins of reflection for our limited needs. |
244 | | - try { |
245 | | - Object kotlinOptions = task.getClass().getMethod("getKotlinOptions").invoke(task); |
246 | | - |
247 | | - @SuppressWarnings("unchecked") |
248 | | - List<String> freeCompilerArgs = |
249 | | - (List<String>) |
250 | | - kotlinOptions.getClass().getMethod("getFreeCompilerArgs").invoke(kotlinOptions); |
251 | | - |
252 | | - List<String> newArgs = new ArrayList<>(freeCompilerArgs.size() + 5); |
253 | | - newArgs.addAll(freeCompilerArgs); |
254 | | - newArgs.add("-Xplugin=" + scipKotlinc); |
255 | | - newArgs.add("-P"); |
256 | | - newArgs.add("plugin:scip-kotlinc:sourceroot=" + sourceRoot); |
257 | | - newArgs.add("-P"); |
258 | | - newArgs.add("plugin:scip-kotlinc:targetroot=" + targetRoot); |
259 | | - |
260 | | - kotlinOptions |
261 | | - .getClass() |
262 | | - .getMethod("setFreeCompilerArgs", List.class) |
263 | | - .invoke(kotlinOptions, newArgs); |
264 | | - } catch (ReflectiveOperationException exc) { |
265 | | - throw new RuntimeException( |
266 | | - "scip-java: failed to configure Kotlin compile task '" + task.getName() + "'", exc); |
267 | | - } |
| 249 | + task.doLast( |
| 250 | + ignored -> { |
| 251 | + try { |
| 252 | + List<String> lines = new ArrayList<>(); |
| 253 | + lines.add("sourceroot " + sourceRoot); |
| 254 | + |
| 255 | + // Referring to KotlinCompile directly triggers NoClassDefFoundError - |
| 256 | + // the plugin classpath is murky and we deliberately don't bundle the |
| 257 | + // Kotlin Gradle plugin. So we commit the sins of reflection. |
| 258 | + FileCollection libraries; |
| 259 | + try { |
| 260 | + libraries = (FileCollection) task.getClass().getMethod("getLibraries").invoke(task); |
| 261 | + } catch (ReflectiveOperationException exc) { |
| 262 | + libraries = (FileCollection) task.getClass().getMethod("getClasspath").invoke(task); |
| 263 | + } |
| 264 | + for (File entry : libraries.getFiles()) { |
| 265 | + lines.add("classpath " + entry.getAbsolutePath()); |
| 266 | + } |
| 267 | + |
| 268 | + int sources = 0; |
| 269 | + for (File file : task.getInputs().getSourceFiles().getFiles()) { |
| 270 | + if (file.getName().endsWith(".kt") || file.getName().endsWith(".kts")) { |
| 271 | + lines.add("source " + file.getAbsolutePath()); |
| 272 | + sources++; |
| 273 | + } |
| 274 | + } |
| 275 | + // Java sources of the same module are listed too, so Kotlin |
| 276 | + // references to them resolve. The indexer only emits Kotlin files. |
| 277 | + try { |
| 278 | + FileCollection javaSources = |
| 279 | + (FileCollection) task.getClass().getMethod("getJavaSources").invoke(task); |
| 280 | + for (File file : javaSources.getFiles()) { |
| 281 | + if (file.getName().endsWith(".java")) { |
| 282 | + lines.add("source " + file.getAbsolutePath()); |
| 283 | + } |
| 284 | + } |
| 285 | + } catch (ReflectiveOperationException exc) { |
| 286 | + // Kotlin Gradle Plugin without javaSources (pre 1.7). |
| 287 | + } |
| 288 | + if (sources == 0) { |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + Path configDir = Paths.get(targetRoot, "kotlin-configs"); |
| 293 | + Files.createDirectories(configDir); |
| 294 | + String fileName = task.getPath().replaceAll("[^A-Za-z0-9]", "-") + ".txt"; |
| 295 | + Files.write(configDir.resolve(fileName), lines); |
| 296 | + } catch (Exception exc) { |
| 297 | + throw new RuntimeException( |
| 298 | + "scip-java: failed to extract Kotlin compile configuration for task '" |
| 299 | + + task.getName() |
| 300 | + + "': " |
| 301 | + + exc, |
| 302 | + exc); |
| 303 | + } |
| 304 | + }); |
268 | 305 | } |
269 | 306 |
|
270 | 307 | private static Object requiredExtra(Map<String, Object> extraProperties, String name) { |
|
0 commit comments