Skip to content

Commit e5d8dba

Browse files
committed
Simplify and rework after review comments
1 parent 4bf87e1 commit e5d8dba

File tree

7 files changed

+84
-101
lines changed

7 files changed

+84
-101
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ git submodule init
362362
git submodule update
363363
```
364364

365-
The path to JDK 11 must be specified either with the environment variable `JDK_11` or
366-
with the gradle property `JDK_11`. For local builds, you can use a later version of JDK if you don't have that
365+
The path to JDK 8 must be specified either with the environment variable `JDK_8` or
366+
with the gradle property `JDK_8`. For local builds, you can use a later version of JDK if you don't have that
367367
version installed.
368368

369369
After that, the project can be opened in IDEA and built with Gradle.

build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ fun jdkPath(version: Int): String {
2222
error("Specify path to JDK $version in JDK_$version environment variable or Gradle property")
2323
}
2424
//val JDK_6 by ext(jdkPath(6))
25-
//val JDK_8 by ext(jdkPath(8))
26-
val JDK_11 by ext(jdkPath(11))
25+
val JDK_8 by ext(jdkPath(8))
2726

2827
allprojects {
2928
repositories {

buildSrc/build.gradle.kts

Lines changed: 0 additions & 10 deletions
This file was deleted.

buildSrc/src/main/kotlin/Java9Modularity.kt

Lines changed: 0 additions & 80 deletions
This file was deleted.

core/build.gradle.kts

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import kotlinx.team.infra.mavenPublicationsPom
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
23
import java.net.URL
34
import java.util.Locale
45
import javax.xml.parsers.DocumentBuilderFactory
@@ -18,8 +19,7 @@ base {
1819
}
1920

2021
//val JDK_6: String by project
21-
//val JDK_8: String by project
22-
val JDK_11: String by project
22+
val JDK_8: String by project
2323
val serializationVersion: String by project
2424

2525
kotlin {
@@ -54,7 +54,7 @@ kotlin {
5454
compilations.all {
5555
kotlinOptions {
5656
jvmTarget = "1.8"
57-
jdkHome = JDK_11
57+
jdkHome = JDK_8
5858
}
5959
}
6060

@@ -199,7 +199,7 @@ kotlin {
199199
dependencies {
200200
api("org.jetbrains.kotlin:kotlin-stdlib-js")
201201
api("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
202-
implementation(npm("@js-joda/core", "3.2.0"))
202+
implementation(npm("@js-joda/core", "3.2.0"))
203203
}
204204
}
205205

@@ -233,6 +233,81 @@ tasks {
233233
// maxHeapSize = "1024m"
234234
// executable = "$JDK_6/bin/java"
235235
}
236+
237+
create("compileJavaModuleInfo", JavaCompile::class) {
238+
val compileKotlinJvm = getByName<KotlinCompile>("compileKotlinJvm")
239+
val sourceDir = file("jvm/java9/")
240+
val targetFile = compileKotlinJvm.destinationDir.resolve("../module-info.class")
241+
242+
// Use a Java 11 compiler for the module info.
243+
javaCompiler.set(project.javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(11)) })
244+
245+
// Always compile kotlin classes before the module descriptor.
246+
dependsOn(compileKotlinJvm)
247+
248+
// Add the module-info source file.
249+
source(sourceDir)
250+
251+
// Also add the module-info.java source file to the Kotlin compile task.
252+
// The Kotlin compiler will parse and check module dependencies,
253+
// but it currently won't compile to a module-info.class file.
254+
// Note that module checking only works on JDK 9+,
255+
// because the JDK built-in base modules are not available in earlier versions.
256+
val javaVersion = compileKotlinJvm.kotlinJavaToolchain.javaVersion.orNull
257+
// Note: extra sanity check, because `jdkHome` might be overridden, this can be
258+
// removed once https://github.com/Kotlin/kotlinx-datetime/pull/155/files is merged.
259+
val isJava8 = compileKotlinJvm.kotlinOptions.jdkHome?.contains("1.8") == true
260+
if (!isJava8 && javaVersion?.isJava9Compatible == true) {
261+
logger.info("Module-info checking is enabled; $compileKotlinJvm is compiled using Java $javaVersion")
262+
compileKotlinJvm.source(sourceDir)
263+
} else {
264+
logger.info("Module-info checking is disabled")
265+
}
266+
267+
// This task outputs only the module-info class file.
268+
outputs.file(targetFile)
269+
270+
// Use the same destination dir to see classes compiled by compileKotlinJvm.
271+
destinationDir = compileKotlinJvm.destinationDir
272+
273+
// Configure JVM compatibility
274+
sourceCompatibility = JavaVersion.VERSION_1_9.toString()
275+
targetCompatibility = JavaVersion.VERSION_1_9.toString()
276+
277+
// Use an empty classpath, since we are using a module path instead.
278+
classpath = files()
279+
280+
doFirst {
281+
// Create the module path that will be passed to the compiler instead of a classpath.
282+
// The module path should be the same as the classpath of the compiler.
283+
val modulePath = compileKotlinJvm.classpath.asPath
284+
285+
options.compilerArgs = listOf(
286+
"--release", "9",
287+
"--module-path", modulePath,
288+
"-Xlint:-requires-transitive-automatic"
289+
)
290+
}
291+
292+
doLast {
293+
// Move the compiled file out of the Kotlin compile task's destination dir,
294+
// so it won't disturb Gradle's caching mechanisms.
295+
val compiledFile = destinationDir.resolve(targetFile.name)
296+
targetFile.parentFile.mkdirs()
297+
compiledFile.renameTo(targetFile)
298+
}
299+
300+
// Configure the JAR task so it will include the compile module-info class file.
301+
getByName<Jar>("jvmJar") {
302+
dependsOn(this@create)
303+
manifest {
304+
attributes("Multi-Release" to true)
305+
}
306+
from(targetFile) {
307+
into("META-INF/versions/9/")
308+
}
309+
}
310+
}
236311
}
237312

238313
task("downloadWindowsZonesMapping") {
File renamed without changes.

serialization/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ plugins {
55
kotlin("plugin.serialization")
66
}
77

8-
//val JDK_8: String by project
9-
val JDK_11: String by project
8+
val JDK_8: String by project
109
val serializationVersion: String by project
1110

1211
kotlin {
@@ -36,7 +35,7 @@ kotlin {
3635
compilations.all {
3736
kotlinOptions {
3837
jvmTarget = "1.8"
39-
jdkHome = JDK_11
38+
jdkHome = JDK_8
4039
}
4140
}
4241

0 commit comments

Comments
 (0)