From 40f76419a275f1a47402db1fb6c724be2be77c83 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 12 Mar 2018 14:15:21 +0300 Subject: [PATCH] [gradle-plugin] Support KONAN_ENABLE_OPTIMIZATIONS env variable --- .../gradle/plugin/EnvironmentVariables.kt | 26 ++++++++++++++----- .../gradle/plugin/tasks/KonanCompileTask.kt | 2 +- .../test/EnvVariableSpecification.groovy | 18 +++++++------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/EnvironmentVariables.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/EnvironmentVariables.kt index 40398a86fda..40b0f70f9ac 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/EnvironmentVariables.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/EnvironmentVariables.kt @@ -23,13 +23,18 @@ import java.io.File /** * The plugin allows an IDE to specify some building parameters. These parameters * are passed to the plugin via environment variables. Two variables are supported: - * - CONFIGURATION_BUILD_DIR - an absolute path to a destination directory for all compilation tasks. - * The IDE should take care about specifying different directories - * for different targets. This setting has less priority than - * an explicitly specified destination directory in the build script. - * - DEBUGGING_SYMBOLS - If YES, the debug support will be enabled for all artifacts. This option has less - * priority than explicitly specified enableDebug option in the build script and - * enableDebug project property. + * - CONFIGURATION_BUILD_DIR - An absolute path to a destination directory for all compilation tasks. + * The IDE should take care about specifying different directories + * for different targets. This setting has less priority than + * an explicitly specified destination directory in the build script. + * + * - DEBUGGING_SYMBOLS - If YES, the debug support will be enabled for all artifacts. This option has less + * priority than explicitly specified enableDebug option in the build script and + * enableDebug project property. + * + * - KONAN_ENABLE_OPTIMIZATIONS - If YES, optimizations will be enabled for all artifacts by default. This option + * has less priority than explicitly specified enableOptimizations option in the + * build script. * * Support for environment variables should be explicitly enabled by setting a project property: * konan.useEnvironmentVariables = true. @@ -38,6 +43,7 @@ import java.io.File internal interface EnvironmentVariables { val configurationBuildDir: File? val debuggingSymbols: Boolean + val enableOptimizations: Boolean } internal class EnvironmentVariablesUnused: EnvironmentVariables { @@ -46,6 +52,9 @@ internal class EnvironmentVariablesUnused: EnvironmentVariables { override val debuggingSymbols: Boolean get() = false + + override val enableOptimizations: Boolean + get() = false } internal class EnvironmentVariablesImpl: EnvironmentVariables { @@ -56,6 +65,9 @@ internal class EnvironmentVariablesImpl: EnvironmentVariables { override val debuggingSymbols: Boolean get() = System.getenv("DEBUGGING_SYMBOLS")?.toUpperCase() == "YES" + + override val enableOptimizations: Boolean + get() = System.getenv("KONAN_ENABLE_OPTIMIZATIONS")?.toUpperCase() == "YES" } internal val Project.useEnvironmentVariables: Boolean diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/tasks/KonanCompileTask.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/tasks/KonanCompileTask.kt index e4d75de1093..6c00880b5ad 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/tasks/KonanCompileTask.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/tasks/KonanCompileTask.kt @@ -74,7 +74,7 @@ abstract class KonanCompileTask: KonanBuildingTask(), KonanCompileSpec { @Input var noStdLib = false @Input var noMain = false - @Input var enableOptimizations = false + @Input var enableOptimizations = project.environmentVariables.enableOptimizations @Input var enableAssertions = false @Optional @Input var entryPoint: String? = null diff --git a/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/EnvVariableSpecification.groovy b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/EnvVariableSpecification.groovy index 23362f3f358..0bd39c7e150 100644 --- a/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/EnvVariableSpecification.groovy +++ b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/EnvVariableSpecification.groovy @@ -53,7 +53,7 @@ class EnvVariableSpecification extends BaseKonanSpecification { // Gradle TestKit doesn't support setting environment variables for runners. // So we use the following hack: we create a gradle wrapper, start it as a separate - // process with custom environment variables and check its exit code. + // process with custom environment variables and check its exit code and output. runner.withArguments("wrapper").build() def classpath = runner.pluginClasspath.collect { "'${escapeBackSlashes(it.absolutePath)}'" }.join(", ") @@ -113,8 +113,8 @@ class EnvVariableSpecification extends BaseKonanSpecification { return "$prefix${baseName}.$suffix" } - @Unroll("Plugin should support #action debug via an env variable") - def 'Plugin should support enabling/disabling debug via an env variable'() { + @Unroll("Plugin should support #action via an env variable") + def 'Plugin should support enabling/disabling debug/opt via an env variable'() { when: def project = createProjectWithWrapper() project.buildFile.append("""\ @@ -126,12 +126,12 @@ class EnvVariableSpecification extends BaseKonanSpecification { task assertEnableDebug { doLast { konanArtifacts.main.forEach { - $check + if (!$assertion) throw new AssertionError("$message for \${it.name}") } } } """.stripIndent()) - def result = runWrapper(project,"assertEnableDebug", ["DEBUGGING_SYMBOLS": value]) + def result = runWrapper(project,"assertEnableDebug", [(variable): value]) .printStderr() .getExitValue() @@ -139,9 +139,11 @@ class EnvVariableSpecification extends BaseKonanSpecification { result == 0 where: - action |value |check - "enabling" |"YES" |"if (!it.enableDebug) throw new AssertionError(\"Debug should be enabled for \${it.name}\")" - "disabling" |"NO" |"if (it.enableDebug) throw new AssertionError(\"Debug should be disabled for \${it.name}\")" + action |variable |value |assertion |message + "enabling debug" |"DEBUGGING_SYMBOLS" |"YES" |"it.enableDebug" |"Debug should be enabled" + "disabling debug" |"DEBUGGING_SYMBOLS" |"NO" |"!it.enableDebug" |"Debug should be disabled" + "enabling opt" |"KONAN_ENABLE_OPTIMIZATIONS" |"YES" |"it.enableOptimizations" |"Opts should be enabled" + "disabling opt" |"KONAN_ENABLE_OPTIMIZATIONS" |"NO" |"!it.enableOptimizations" |"Opts should be disabled" } def 'Plugin should support setting destination directory via an env variable'() {