From c2457cae604aa83331cede1e5c17c8d462350f14 Mon Sep 17 00:00:00 2001 From: Vyacheslav Gerasimov Date: Wed, 12 Feb 2020 21:41:13 +0300 Subject: [PATCH] Read KotlinCompilerVersion from resource file Writing build number into a public constant field leads to poor gradle cache reuse between different builds. Public constant value is a part of public api and its changes affect inputs of dependent modules. Extracting build number to resource file allows to ignore it from runtime classpath which fixes same problem for KotlinCompile tasks --- build.gradle.kts | 1 + core/util.runtime/build.gradle.kts | 11 ++++++-- .../resources/META-INF/compiler.version | 1 + .../kotlin/config/KotlinCompilerVersion.java | 28 +++++++++++++++---- .../kotlin/compilerRunner/reportUtils.kt | 18 +++++++++--- prepare/build.version/build.gradle.kts | 14 +--------- 6 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 core/util.runtime/resources/META-INF/compiler.version diff --git a/build.gradle.kts b/build.gradle.kts index 9a3d83627b3..7e852a6a997 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -393,6 +393,7 @@ allprojects { normalization { runtimeClasspath { ignore("META-INF/MANIFEST.MF") + ignore("META-INF/compiler.version") } } diff --git a/core/util.runtime/build.gradle.kts b/core/util.runtime/build.gradle.kts index cf32b853d82..2af07e5767f 100644 --- a/core/util.runtime/build.gradle.kts +++ b/core/util.runtime/build.gradle.kts @@ -1,3 +1,4 @@ +import org.apache.tools.ant.filters.ReplaceTokens plugins { java @@ -8,6 +9,8 @@ plugins { jvmTarget = "1.6" javaHome = rootProject.extra["JDK_16"] as String +val kotlinVersion: String by rootProject.extra + dependencies { compileOnly(kotlinStdlib()) } @@ -22,5 +25,9 @@ tasks.withType { targetCompatibility = "1.6" } -if (project.hasProperty("teamcity")) -tasks["compileJava"].dependsOn(":prepare:build.version:writeCompilerVersion") \ No newline at end of file +tasks.named("processResources") { + inputs.property("compilerVersion", kotlinVersion) + filesMatching("META-INF/compiler.version") { + filter("tokens" to mapOf("snapshot" to kotlinVersion)) + } +} \ No newline at end of file diff --git a/core/util.runtime/resources/META-INF/compiler.version b/core/util.runtime/resources/META-INF/compiler.version new file mode 100644 index 00000000000..3291b664484 --- /dev/null +++ b/core/util.runtime/resources/META-INF/compiler.version @@ -0,0 +1 @@ +@snapshot@ \ No newline at end of file diff --git a/core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java b/core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java index 7b26d9e3bbe..3c72e919971 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java +++ b/core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java @@ -18,10 +18,13 @@ package org.jetbrains.kotlin.config; import org.jetbrains.annotations.Nullable; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + public class KotlinCompilerVersion { - // The value of this constant is generated by the build script - // DON'T MODIFY IT - public static final String VERSION = "@snapshot@"; + public static final String VERSION_FILE_PATH = "/META-INF/compiler.version"; + public static final String VERSION; // True if the latest stable language version supported by this compiler has not yet been released. // Binaries produced by this compiler with that language version (or any future language version) are going to be marked @@ -45,12 +48,27 @@ public class KotlinCompilerVersion { */ @Nullable public static String getVersion() { - //noinspection ConstantConditions return VERSION.equals("@snapshot@") ? null : VERSION; } + private static String loadKotlinCompilerVersion() throws IOException { + BufferedReader versionReader = new BufferedReader( + new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH))); + try { + return versionReader.readLine(); + } finally { + versionReader.close(); + } + } + static { - //noinspection ConstantConditions + try { + VERSION = loadKotlinCompilerVersion(); + } + catch (IOException e) { + throw new IllegalStateException("Failed to read compiler version from " + VERSION_FILE_PATH); + } + if (!VERSION.equals("@snapshot@") && !VERSION.contains("-") && IS_PRE_RELEASE) { throw new IllegalStateException( "IS_PRE_RELEASE cannot be true for a compiler without '-' in its version.\n" + diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/reportUtils.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/reportUtils.kt index bece3e0b5e8..7de9fbea8c0 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/reportUtils.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/reportUtils.kt @@ -52,12 +52,22 @@ internal fun loadCompilerVersion(compilerClasspath: List): String { for (cpFile in compilerClasspath) { if (cpFile.isFile && cpFile.extension.toLowerCase() == "jar") { ZipFile(cpFile).use { jar -> - val bytes = jar.getInputStream(jar.getEntry(versionClassFileName)).use { it.readBytes() } - checkVersion(bytes) + val versionFileEntry = jar.getEntry(KotlinCompilerVersion.VERSION_FILE_PATH) + if (versionFileEntry != null) { + result = jar.getInputStream(versionFileEntry).bufferedReader().use { it.readText() } + } else { + val bytes = jar.getInputStream(jar.getEntry(versionClassFileName)).use { it.readBytes() } + checkVersion(bytes) + } } } else if (cpFile.isDirectory) { - File(cpFile, versionClassFileName).takeIf { it.isFile }?.let { - checkVersion(it.readBytes()) + val versionFile = File(cpFile, KotlinCompilerVersion.VERSION_FILE_PATH) + if (versionFile.isFile) { + result = versionFile.readText() + } else { + File(cpFile, versionClassFileName).takeIf { it.isFile }?.let { + checkVersion(it.readBytes()) + } } } if (result != null) break diff --git a/prepare/build.version/build.gradle.kts b/prepare/build.version/build.gradle.kts index 0f428feb18c..6907a4cef1b 100644 --- a/prepare/build.version/build.gradle.kts +++ b/prepare/build.version/build.gradle.kts @@ -44,18 +44,6 @@ val writeStdlibVersion by tasks.registering { } } -val writeCompilerVersion by tasks.registering { - val versionFile = rootDir.resolve("core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java") - inputs.property("version", kotlinVersion) - outputs.file(versionFile) - doLast { - replaceVersion(versionFile, """public static final String VERSION = "([^"]+)"""") { - logger.lifecycle("Writing new compiler version: $kotlinVersion") - kotlinVersion - } - } -} - val writePluginVersion by tasks.registering { val versionFile = project(":idea").projectDir.resolve("resources/META-INF/plugin.xml") val pluginVersion = rootProject.findProperty("pluginVersion") as String? @@ -71,5 +59,5 @@ val writePluginVersion by tasks.registering { } val writeVersions by tasks.registering { - dependsOn(writeBuildNumber, writeStdlibVersion, writeCompilerVersion) + dependsOn(writeBuildNumber, writeStdlibVersion) }