|
| 1 | +import org.gradle.api.internal.file.DefaultSourceDirectorySet |
| 2 | + |
| 3 | +import static org.joor.Reflect.onClass |
| 4 | + |
| 5 | +pluginManager.apply(Kotlin12Plugin.class) |
| 6 | + |
| 7 | +// We cannot use the 1.2 Kotlin plugin as it only works with a very old version of Gradle, which itself will only work on Java 8. So we need |
| 8 | +// our own plugin which calls the 1.2 compiler directly. |
| 9 | +class Kotlin12Plugin implements Plugin<Project> { |
| 10 | + private static final KOTLIN_VERSION = "1.2.71" |
| 11 | + |
| 12 | + @Override |
| 13 | + void apply(Project project) { |
| 14 | + project.pluginManager.apply(JavaPlugin.class) |
| 15 | + |
| 16 | + project.extensions.add("kotlin_1_2_version", KOTLIN_VERSION) |
| 17 | + project.dependencies.add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION") |
| 18 | + |
| 19 | + def kotlinCompilerConfiguration = project.configurations.create("kotlinCompiler") |
| 20 | + project.dependencies.add("kotlinCompiler", "org.jetbrains.kotlin:kotlin-compiler:$KOTLIN_VERSION") |
| 21 | + |
| 22 | + project.extensions.getByType(JavaPluginExtension.class).sourceSets.configureEach { sourceSet -> |
| 23 | + // Create the "src/*/kotlin" SourceDirectorySet, alongside the "java" one |
| 24 | + def kotlinSourceDirectorySet = new DefaultSourceDirectorySet(project.objects.sourceDirectorySet("kotlin", "${sourceSet.displayName} Kotlin source")) |
| 25 | + sourceSet.extensions.add(SourceDirectorySet.class, "kotlin", kotlinSourceDirectorySet) |
| 26 | + kotlinSourceDirectorySet.filter.include("**/*.java", "**/*.kt") |
| 27 | + kotlinSourceDirectorySet.srcDir(project.file("src/${sourceSet.name}/kotlin")) |
| 28 | + |
| 29 | + def allKotlin = project.objects.sourceDirectorySet("allkotlin", "${sourceSet.displayName} Kotlin source") |
| 30 | + allKotlin.filter.include("**/*.kt") |
| 31 | + allKotlin.source(kotlinSourceDirectorySet) |
| 32 | + |
| 33 | + sourceSet.allJava.source(kotlinSourceDirectorySet) |
| 34 | + sourceSet.allSource.source(kotlinSourceDirectorySet) |
| 35 | + |
| 36 | + def kotlinBuildDir = project.layout.buildDirectory.dir("classes/kotlin/${sourceSet.name}") |
| 37 | + sourceSet.output.dir(kotlinBuildDir) |
| 38 | + |
| 39 | + def taskSourceSetName = isMain(sourceSet) ? "" : sourceSet.name.capitalize() |
| 40 | + |
| 41 | + def compileKotlin = project.tasks.register("compile${taskSourceSetName}Kotlin", KotlinCompile.class) { task -> |
| 42 | + // The 1.2 compiler needs to be laoded in a separate class loader, as the build classpath already contains its own version |
| 43 | + // of Kotlin. |
| 44 | + task.compilerClasspath.from(kotlinCompilerConfiguration) |
| 45 | + task.source(allKotlin) |
| 46 | + // Paradoxically, the Java sources are also required by the Kotlin compiler. This is actually so that it can correctly |
| 47 | + // resolve any references the Kotlin code makes to Java code. |
| 48 | + task.source(sourceSet.allJava) |
| 49 | + task.classpath = sourceSet.compileClasspath |
| 50 | + task.destinationDirectory = kotlinBuildDir |
| 51 | + } |
| 52 | + |
| 53 | + // Compiling the Java code needs the compiled Kotlin code first |
| 54 | + project.tasks.named("compile${taskSourceSetName}Java", JavaCompile.class) { task -> |
| 55 | + task.classpath += project.files(compileKotlin.map { it.destinationDirectory }) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +abstract class KotlinCompile extends AbstractCompile { |
| 62 | + @Classpath |
| 63 | + abstract ConfigurableFileCollection getCompilerClasspath() |
| 64 | + |
| 65 | + @TaskAction |
| 66 | + void compile() { |
| 67 | + def args = [ |
| 68 | + "-jvm-target", "1.8", |
| 69 | + "-language-version", "1.2", |
| 70 | + "-api-version", "1.2", |
| 71 | + "-java-parameters", |
| 72 | + "-Xjvm-default=compatibility", |
| 73 | + "-no-stdlib", |
| 74 | + "-Xallow-kotlin-package", // We may have copies of stdlib APIs (see `core-1.2`) |
| 75 | + "-cp", classpath.asPath, |
| 76 | + "-d", destinationDirectory.get().asFile.absolutePath |
| 77 | + ] |
| 78 | + args.addAll(source.collect { it.absolutePath }) |
| 79 | + |
| 80 | + logger.info("args: {}", args) |
| 81 | + |
| 82 | + def compilerClassLoader = new URLClassLoader(compilerClasspath.collect { it.toURI().toURL() } as URL[]) |
| 83 | + def exitCode = onClass("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler", compilerClassLoader) |
| 84 | + .create() |
| 85 | + .call("exec", System.err, args as String[]) |
| 86 | + .get() |
| 87 | + if (exitCode.toString() != "OK") { |
| 88 | + throw new GradleException("Compilation error. See log for more details") |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments