From 5ddd60a015a99cb94b2267f365a5ed5bd4d74364 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Thu, 2 Mar 2023 16:58:57 +0100 Subject: [PATCH] Add '-progressive' option into Gradle compiler options DSL ^KT-53923 Fixed --- .../arguments/CommonCompilerArguments.kt | 4 + .../arguments/GenerateGradleOptions.kt | 2 +- .../api/kotlin-gradle-plugin-api.api | 1 + .../gradle/dsl/KotlinCommonCompilerOptions.kt | 7 + .../kotlin/gradle/CompilerOptionsIT.kt | 134 ++++++++++++++++++ .../dsl/KotlinCommonCompilerOptionsDefault.kt | 5 + .../sources/DefaultLanguageSettingsBuilder.kt | 5 +- .../tasks/KotlinNativeCompilerArgBuilder.kt | 2 +- .../targets/native/tasks/KotlinNativeTasks.kt | 4 +- 9 files changed, 157 insertions(+), 7 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt index 1cf0b51aff4..67114033c6a 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt @@ -85,6 +85,10 @@ abstract class CommonCompilerArguments : CommonToolArguments() { field = if (value.isNullOrEmpty()) null else value } + @GradleOption( + value = DefaultValue.BOOLEAN_FALSE_DEFAULT, + gradleInputType = GradleInputTypes.INPUT + ) @Argument( value = "-progressive", deprecatedName = "-Xprogressive", diff --git a/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt b/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt index 6c507fe0eb8..fead6d77e49 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt @@ -753,7 +753,7 @@ private fun Printer.generateDoc(property: KProperty1<*, *>) { val defaultValue = property.gradleDefaultValue println("/**") - println(" * $description") + println(" * ${description.replace("\n", " ")}") if (possibleValues != null) { println(" * Possible values: ${possibleValues.joinToString()}") } diff --git a/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api b/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api index a912a6fe6e3..194fdc35c8b 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api +++ b/libraries/tools/kotlin-gradle-plugin-api/api/kotlin-gradle-plugin-api.api @@ -166,6 +166,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/dsl/KotlinCommonComp public abstract fun getApiVersion ()Lorg/gradle/api/provider/Property; public abstract fun getLanguageVersion ()Lorg/gradle/api/provider/Property; public abstract fun getOptIn ()Lorg/gradle/api/provider/ListProperty; + public abstract fun getProgressiveMode ()Lorg/gradle/api/provider/Property; public abstract fun getUseK2 ()Lorg/gradle/api/provider/Property; } diff --git a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptions.kt b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptions.kt index cec542f7403..e89a4a28d61 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptions.kt +++ b/libraries/tools/kotlin-gradle-plugin-api/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptions.kt @@ -32,6 +32,13 @@ interface KotlinCommonCompilerOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCo @get:org.gradle.api.tasks.Input val optIn: org.gradle.api.provider.ListProperty + /** + * Enable progressive compiler mode. In this mode, deprecations and bug fixes for unstable code take effect immediately, instead of going through a graceful migration cycle. Code written in the progressive mode is backward compatible; however, code written in non-progressive mode may cause compilation errors in the progressive mode. + * Default value: false + */ + @get:org.gradle.api.tasks.Input + val progressiveMode: org.gradle.api.provider.Property + /** * Compile using experimental K2. K2 is a new compiler pipeline, no compatibility guarantees are yet provided * Default value: false diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt index 0631342d6b2..bcd29e10c7d 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt @@ -299,4 +299,138 @@ internal class CompilerOptionsIT : KGPBaseTest() { } } } + + @DisplayName("Should pass -progressive from compiler options DSL") + @JvmGradlePluginTests + @GradleTest + fun passesProgressive(gradleVersion: GradleVersion) { + project( + projectName = "simpleProject", + gradleVersion = gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG) + ) { + buildGradle.appendText( + //language=Groovy + """ + | + |tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask.class).configureEach { + | compilerOptions.progressiveMode.set(true) + |} + """.trimMargin() + ) + + build("compileKotlin") { + val compilerArgs = output + .lineSequence() + .first { + it.contains("Kotlin compiler args:") + } + .substringAfter("Kotlin compiler args:") + + val expectedArg = "-progressive" + assert(compilerArgs.contains(expectedArg)) { + printBuildOutput() + "compiler arguments does not contain '$expectedArg' - actual value: $compilerArgs" + } + } + } + } + + @DisplayName("Should not pass -progressive by default from compiler options DSL") + @JvmGradlePluginTests + @GradleTest + fun notPassesDefaultProgressive(gradleVersion: GradleVersion) { + project( + projectName = "simpleProject", + gradleVersion = gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG) + ) { + build("compileKotlin") { + val compilerArgs = output + .lineSequence() + .first { + it.contains("Kotlin compiler args:") + } + .substringAfter("Kotlin compiler args:") + + val expectedArg = "-progressive" + assert(!compilerArgs.contains(expectedArg)) { + printBuildOutput() + "compiler arguments contains '$expectedArg' - actual value: $compilerArgs" + } + } + } + } + + @DisplayName("Should pass -progressive from languageSettings if compiler options DSL is not configured") + @JvmGradlePluginTests + @GradleTest + fun passesProgressiveFromLanguageSettings(gradleVersion: GradleVersion) { + project( + projectName = "simpleProject", + gradleVersion = gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG) + ) { + buildGradle.appendText( + //language=Groovy + """ + | + |kotlin.sourceSets.all { + | languageSettings { + | progressiveMode = true + | } + |} + """.trimMargin() + ) + + build("compileKotlin") { + val compilerArgs = output + .lineSequence() + .first { + it.contains("Kotlin compiler args:") + } + .substringAfter("Kotlin compiler args:") + + val expectedArg = "-progressive" + assert(compilerArgs.contains(expectedArg)) { + printBuildOutput() + "compiler arguments does not contain '$expectedArg' - actual value: $compilerArgs" + } + } + } + } + + @DisplayName("Should pass -progressive from compiler options DSL in native project") + @NativeGradlePluginTests + @GradleTest + fun passesProgressiveModeNative(gradleVersion: GradleVersion) { + nativeProject( + projectName = "native-link-simple", + gradleVersion = gradleVersion, + ) { + buildGradle.appendText( + //language=Groovy + """ + | + |tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask.class).configureEach { + | compilerOptions.progressiveMode.set(true) + |} + """.trimMargin() + ) + + build("compileKotlinHost", forceOutput = true) { + val expectedArg = "-progressive" + val compilerArgs = output + .substringAfter("Arguments = [") + .substringBefore("]") + .lines() + val progressiveArg = compilerArgs.find { it.trim() == expectedArg } + + assert(progressiveArg != null) { + printBuildOutput() + "compiler arguments does not contain '$expectedArg': ${compilerArgs.joinToString()}" + } + } + } + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptionsDefault.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptionsDefault.kt index 1c579655a97..b4143121616 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptionsDefault.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinCommonCompilerOptionsDefault.kt @@ -18,6 +18,9 @@ internal abstract class KotlinCommonCompilerOptionsDefault @javax.inject.Inject override val optIn: org.gradle.api.provider.ListProperty = objectFactory.listProperty(kotlin.String::class.java).convention(emptyList()) + override val progressiveMode: org.gradle.api.provider.Property = + objectFactory.property(kotlin.Boolean::class.java).convention(false) + @Deprecated(message = "Compiler flag -Xuse-k2 is deprecated; please use language version 2.0 instead", level = DeprecationLevel.WARNING) override val useK2: org.gradle.api.provider.Property = objectFactory.property(kotlin.Boolean::class.java).convention(false) @@ -27,6 +30,7 @@ internal abstract class KotlinCommonCompilerOptionsDefault @javax.inject.Inject args.apiVersion = apiVersion.orNull?.version args.languageVersion = languageVersion.orNull?.version args.optIn = optIn.get().toTypedArray() + args.progressiveMode = progressiveMode.get() args.useK2 = useK2.get() } @@ -35,6 +39,7 @@ internal abstract class KotlinCommonCompilerOptionsDefault @javax.inject.Inject args.apiVersion = null args.languageVersion = null args.optIn = emptyList().toTypedArray() + args.progressiveMode = false args.useK2 = false } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultLanguageSettingsBuilder.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultLanguageSettingsBuilder.kt index 163b06b2a85..841f25fc001 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultLanguageSettingsBuilder.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/sources/DefaultLanguageSettingsBuilder.kt @@ -106,13 +106,10 @@ internal fun applyLanguageSettingsToCompilerOptions( ) = with(compilerOptions) { languageVersion.convention(languageSettingsBuilder.languageVersion?.let { KotlinVersion.fromVersion(it) }) apiVersion.convention(languageSettingsBuilder.apiVersion?.let { KotlinVersion.fromVersion(it) }) + progressiveMode.convention(languageSettingsBuilder.progressiveMode) optIn.addAll(languageSettingsBuilder.optInAnnotationsInUse) val freeArgs = mutableListOf().apply { - if (languageSettingsBuilder.progressiveMode) { - add("-progressive") - } - languageSettingsBuilder.enabledLanguageFeatures.forEach { featureName -> add("-XXLanguage:+$featureName") } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeCompilerArgBuilder.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeCompilerArgBuilder.kt index fe1fa3f3d49..c4da95b0660 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeCompilerArgBuilder.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeCompilerArgBuilder.kt @@ -178,7 +178,6 @@ internal fun buildKotlinNativeCompileCommonArgs( } languageSettings.run { - addKey("-progressive", progressiveMode) enabledLanguageFeatures.forEach { add("-XXLanguage:+$it") } } @@ -187,6 +186,7 @@ internal fun buildKotlinNativeCompileCommonArgs( addKey("-Werror", compilerOptions.allWarningsAsErrors.get()) addKey("-nowarn", compilerOptions.suppressWarnings.get()) addKey("-verbose", compilerOptions.verbose.get()) + addKey("-progressive", compilerOptions.progressiveMode.get()) compilerOptions.optIn.get().forEach { add("-opt-in=$it") } addAll(compilerOptions.freeCompilerArgs.get()) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt index e7703f90fd4..5cc365533da 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt @@ -197,7 +197,9 @@ abstract class AbstractKotlinNativeCompile< compilation.languageSettings } - @get:Input + @Suppress("DeprecatedCallableAddReplaceWith") + @get:Deprecated("Replaced with 'compilerOptions.progressiveMode'") + @get:Internal val progressiveMode: Boolean get() = languageSettings.progressiveMode // endregion.