-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tasks | ||
|
||
import java.util.* | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.tasks.SourceSetContainer | ||
import org.gradle.api.tasks.compile.JavaCompile | ||
import org.gradle.jvm.toolchain.JavaLanguageVersion | ||
import org.gradle.jvm.toolchain.JavaToolchainService | ||
import org.gradle.kotlin.dsl.get | ||
import org.gradle.process.CommandLineArgumentProvider | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation | ||
|
||
private val Project.sourceSets: SourceSetContainer | ||
get() = this.extensions.getByName("sourceSets") as SourceSetContainer | ||
|
||
private val Project.javaToolchains: JavaToolchainService | ||
get() = this.extensions.getByName("javaToolchains") as JavaToolchainService | ||
|
||
/** | ||
* Setup tasks to compile `module-info.java` file for a module named [moduleName] from a source set | ||
* with name [sourceSetName] using Java toolchain with version [toolchainVersion]. | ||
* | ||
* It is assumed that source set with [sourceSetName] only extends main JVM source set. | ||
* [parentCompilation] represent a compilation corresponding to such a main source set. | ||
* | ||
* For more details, check this [kotlinx-io](https://github.com/Kotlin/kotlinx-io/pull/406/files) PR | ||
*/ | ||
public fun Project.configureJava9ModuleInfoCompilation( | ||
sourceSetName: String, | ||
toolchainVersion: JavaLanguageVersion, | ||
parentCompilation: KotlinJvmCompilation, | ||
moduleName: String | ||
) { | ||
val moduleOutputs = listOf(parentCompilation.output.allOutputs) | ||
val compileClasspathConfiguration = | ||
parentCompilation.configurations.compileDependencyConfiguration | ||
val sourceSetNameCapitalized = | ||
sourceSetName.replaceFirstChar { it.titlecase(Locale.getDefault()) } | ||
val javaCompileClasspath = configurations["${sourceSetName}CompileClasspath"] | ||
javaCompileClasspath.extendsFrom(compileClasspathConfiguration) | ||
|
||
tasks.named("compile${sourceSetNameCapitalized}Java", JavaCompile::class.java) { | ||
dependsOn(moduleOutputs) | ||
|
||
targetCompatibility = "9" | ||
sourceCompatibility = "9" | ||
|
||
javaCompiler.set(javaToolchains.compilerFor { languageVersion.set(toolchainVersion) }) | ||
|
||
val javaSourceSet = sourceSets[sourceSetName].java | ||
destinationDirectory.set( | ||
javaSourceSet.destinationDirectory.asFile.get().resolve("META-INF/versions/9")) | ||
options.sourcepath = files(javaSourceSet.srcDirs) | ||
val moduleFiles = objects.fileCollection().from(moduleOutputs) | ||
val modulePath = javaCompileClasspath.filter { it !in moduleFiles.files } | ||
dependsOn(modulePath) | ||
classpath = objects.fileCollection().from() | ||
options.compilerArgumentProviders.add( | ||
JigsawArgumentsProvider(moduleName, moduleFiles, modulePath)) | ||
} | ||
} | ||
|
||
private class JigsawArgumentsProvider( | ||
private val moduleName: String, | ||
private val moduleFiles: FileCollection, | ||
private val modulePath: FileCollection | ||
) : CommandLineArgumentProvider { | ||
override fun asArguments(): Iterable<String> = | ||
listOf( | ||
"--module-path", | ||
modulePath.asPath, | ||
"--patch-module", | ||
"$moduleName=${moduleFiles.asPath}", | ||
"--release", | ||
"9") | ||
} |