|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file. |
| 4 | + */ |
| 5 | + |
| 6 | +package kotlinx.io.build |
| 7 | + |
| 8 | +import org.gradle.api.Project |
| 9 | +import org.gradle.api.file.FileCollection |
| 10 | +import org.gradle.api.tasks.SourceSetContainer |
| 11 | +import org.gradle.api.tasks.compile.JavaCompile |
| 12 | +import org.gradle.jvm.toolchain.JavaLanguageVersion |
| 13 | +import org.gradle.jvm.toolchain.JavaToolchainService |
| 14 | +import org.gradle.kotlin.dsl.get |
| 15 | +import org.gradle.kotlin.dsl.withType |
| 16 | +import org.gradle.process.CommandLineArgumentProvider |
| 17 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 18 | +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation |
| 19 | +import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile |
| 20 | +import java.util.* |
| 21 | + |
| 22 | +private val Project.sourceSets: SourceSetContainer |
| 23 | + get() = this.extensions.getByName("sourceSets") as SourceSetContainer |
| 24 | + |
| 25 | +private val Project.javaToolchains: JavaToolchainService |
| 26 | + get() = this.extensions.getByName("javaToolchains") as JavaToolchainService |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Setup tasks to compile `module-info.java` file for a module named [moduleName] |
| 31 | + * from a source set with name [sourceSetName] using Java toolchain with version [toolchainVersion]. |
| 32 | +
|
| 33 | + * It is assumed that source set with [sourceSetName] only extends main JVM source set. |
| 34 | + * [parentCompilation] represent a compilation corresponding to such a main source set. |
| 35 | + */ |
| 36 | +public fun Project.configureJava9ModuleInfoCompilation( |
| 37 | + sourceSetName: String, |
| 38 | + toolchainVersion: JavaLanguageVersion, |
| 39 | + parentCompilation: KotlinJvmCompilation, |
| 40 | + moduleName: String |
| 41 | +) { |
| 42 | + val moduleOutputs = listOf(parentCompilation.output.allOutputs) |
| 43 | + val compileClasspathConfiguration = parentCompilation.configurations.compileDependencyConfiguration |
| 44 | + val sourceSetNameCapitalized = sourceSetName.replaceFirstChar { it.titlecase(Locale.getDefault()) } |
| 45 | + val javaCompileClasspath = configurations["${sourceSetName}CompileClasspath"] |
| 46 | + javaCompileClasspath.extendsFrom(compileClasspathConfiguration) |
| 47 | + |
| 48 | + tasks.named("compile${sourceSetNameCapitalized}Java", JavaCompile::class.java) { |
| 49 | + dependsOn(moduleOutputs) |
| 50 | + |
| 51 | + targetCompatibility = "9" |
| 52 | + sourceCompatibility = "9" |
| 53 | + |
| 54 | + javaCompiler.set( |
| 55 | + javaToolchains.compilerFor { |
| 56 | + languageVersion.set(toolchainVersion) |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + val javaSourceSet = sourceSets[sourceSetName].java |
| 61 | + destinationDirectory.set( |
| 62 | + javaSourceSet.destinationDirectory.asFile.get() |
| 63 | + .resolve("META-INF/versions/9") |
| 64 | + ) |
| 65 | + options.sourcepath = files(javaSourceSet.srcDirs) |
| 66 | + val moduleFiles = objects.fileCollection().from(moduleOutputs) |
| 67 | + val modulePath = javaCompileClasspath.filter { it !in moduleFiles.files } |
| 68 | + dependsOn(modulePath) |
| 69 | + classpath = objects.fileCollection().from() |
| 70 | + options.compilerArgumentProviders.add( |
| 71 | + JigsawArgumentsProvider( |
| 72 | + moduleName, |
| 73 | + moduleFiles, |
| 74 | + modulePath |
| 75 | + ) |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private class JigsawArgumentsProvider( |
| 81 | + private val moduleName: String, |
| 82 | + private val moduleFiles: FileCollection, |
| 83 | + private val modulePath: FileCollection |
| 84 | +) : CommandLineArgumentProvider { |
| 85 | + override fun asArguments(): Iterable<String> = listOf( |
| 86 | + "--module-path", modulePath.asPath, |
| 87 | + "--patch-module", "$moduleName=${moduleFiles.asPath}", |
| 88 | + "--release", "9" |
| 89 | + ) |
| 90 | +} |
0 commit comments