diff --git a/native/native.tests/build.gradle.kts b/native/native.tests/build.gradle.kts index cb5f5fb7417..d35803758e4 100644 --- a/native/native.tests/build.gradle.kts +++ b/native/native.tests/build.gradle.kts @@ -48,6 +48,7 @@ enum class TestProperty(shortName: String) { KOTLIN_NATIVE_HOME("nativeHome"), COMPILER_CLASSPATH("compilerClasspath"), TEST_MODE("mode"), + OPTIMIZATION_MODE("optimizationMode"), USE_CACHE("useCache"), EXECUTION_TIMEOUT("executionTimeout"); diff --git a/native/native.tests/testData/samples/regular_custom_args.kt b/native/native.tests/testData/samples/regular_custom_args.kt index fe02ce88aad..032d0878b2f 100644 --- a/native/native.tests/testData/samples/regular_custom_args.kt +++ b/native/native.tests/testData/samples/regular_custom_args.kt @@ -1,4 +1,4 @@ -// FREE_COMPILER_ARGS: -opt -verbose +// FREE_COMPILER_ARGS: -Xprint-files -verbose import kotlin.test.* diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt index a90a6032d35..7d454f907fc 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt @@ -57,6 +57,7 @@ class NativeBlackBoxTestSupport : BeforeEachCallback { computeNativeHome(), computeNativeClassLoader(), computeTestMode(), + computeOptimizationMode(), CacheKind::class to computeCacheKind(), computeBaseDirs(), computeTimeouts() @@ -81,55 +82,47 @@ class NativeBlackBoxTestSupport : BeforeEachCallback { } ) - private fun computeTestMode(): TestMode = systemProperty( - name = TEST_MODE, - transform = { testModeName -> - TestMode.values().firstOrNull { it.name == testModeName } ?: fail { - buildString { - appendLine("Unknown test mode name $testModeName.") - appendLine("One of the following test modes should be passed through $TEST_MODE system property:") - TestMode.values().forEach { testMode -> - appendLine("- ${testMode.name}: ${testMode.description}") - } - } - } + private fun computeTestMode(): TestMode = enumSystemProperty(TEST_MODE, TestMode.values(), default = TestMode.WITH_MODULES) - }, - default = TestMode.WITH_MODULES - ) + private fun computeOptimizationMode(): OptimizationMode = + enumSystemProperty(OPTIMIZATION_MODE, OptimizationMode.values(), default = OptimizationMode.DEBUG) private fun computeCacheKind(): CacheKind { - val useCache = systemProperty( - name = USE_CACHE, - transform = { useCacheValue -> - useCacheValue.toBooleanStrictOrNull() ?: fail { "Invalid value for $USE_CACHE system property: $useCacheValue" } - }, - default = true - ) - + val useCache = systemProperty(USE_CACHE, String::toBooleanStrictOrNull, default = true) return if (useCache) CacheKind.WithStaticCache else CacheKind.WithoutCache } private fun computeBaseDirs(): BaseDirs = BaseDirs(File(requiredEnvironmentVariable(PROJECT_BUILD_DIR))) private fun computeTimeouts(): Timeouts { - val executionTimeout = systemProperty( - name = EXECUTION_TIMEOUT, - transform = { executionTimeoutValue -> - executionTimeoutValue.toLongOrNull()?.milliseconds - ?: fail { "Invalid value for $EXECUTION_TIMEOUT system property: $executionTimeoutValue" } - }, - default = 10.seconds - ) - + val executionTimeout = systemProperty(EXECUTION_TIMEOUT, { it.toLongOrNull()?.milliseconds }, default = 10.seconds) return Timeouts(executionTimeout) } private fun requiredSystemProperty(name: String): String = System.getProperty(name) ?: fail { "Unspecified $name system property" } - private fun systemProperty(name: String, transform: (String) -> T, default: T): T = - System.getProperty(name)?.let(transform) ?: default + private fun systemProperty(propertyName: String, transform: (String) -> T?, default: T): T { + val propertyValue = System.getProperty(propertyName) + return if (propertyValue != null) { + transform(propertyValue) ?: fail { "Invalid value for $propertyName system property: $propertyValue" } + } else + default + } + + private inline fun > enumSystemProperty(propertyName: String, values: Array, default: E): E { + val optionName = System.getProperty(propertyName) + return if (optionName != null) { + values.firstOrNull { it.name == optionName } ?: fail { + buildString { + appendLine("Unknown ${E::class.java.simpleName} name $optionName.") + appendLine("One of the following ${E::class.java.simpleName} should be passed through $propertyName system property:") + values.forEach { value -> appendLine("- ${value.name}: $value") } + } + } + } else + default + } private fun requiredEnvironmentVariable(name: String): String = System.getenv(name) ?: fail { "Unspecified $name environment variable" } @@ -139,6 +132,7 @@ class NativeBlackBoxTestSupport : BeforeEachCallback { private const val KOTLIN_NATIVE_HOME = "kotlin.internal.native.test.nativeHome" private const val COMPILER_CLASSPATH = "kotlin.internal.native.test.compilerClasspath" private const val TEST_MODE = "kotlin.internal.native.test.mode" + private const val OPTIMIZATION_MODE = "kotlin.internal.native.test.optimizationMode" private const val USE_CACHE = "kotlin.internal.native.test.useCache" private const val EXECUTION_TIMEOUT = "kotlin.internal.native.test.executionTimeout" private const val PROJECT_BUILD_DIR = "PROJECT_BUILD_DIR" diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestCompiler.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestCompiler.kt index 708e6d68eb3..50ecd402327 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestCompiler.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestCompiler.kt @@ -50,6 +50,7 @@ internal class TestCompilationFactory { targets = settings.get(), home = settings.get(), classLoader = settings.get(), + optimizationMode = settings.get(), freeCompilerArgs = freeCompilerArgs, sourceModules = rootModules, dependencies = TestCompilationDependencies(libraries = libraries, friends = friends), @@ -67,7 +68,7 @@ internal class TestCompilationFactory { add(testRunnerArg) } } - settings.getRootCacheDirectory(debuggable = true)?.let { rootCacheDir -> + settings.getRootCacheDirectory()?.let { rootCacheDir -> add("-Xcache-directory=$rootCacheDir") } } @@ -91,6 +92,7 @@ internal class TestCompilationFactory { targets = settings.get(), home = settings.get(), classLoader = settings.get(), + optimizationMode = settings.get(), freeCompilerArgs = freeCompilerArgs, sourceModules = sourceModules, dependencies = TestCompilationDependencies(libraries = libraries, friends = friends), @@ -239,6 +241,7 @@ private class TestCompilationImpl( private val targets: KotlinNativeTargets, private val home: KotlinNativeHome, private val classLoader: KotlinNativeClassLoader, + private val optimizationMode: OptimizationMode, private val freeCompilerArgs: TestCompilerArgs, private val sourceModules: Collection, private val dependencies: TestCompilationDependencies, @@ -257,7 +260,6 @@ private class TestCompilationImpl( private fun ArgsBuilder.applyCommonArgs() { add( "-enable-assertions", - "-g", "-target", targets.testTarget.name, "-repo", home.dir.resolve("klib").path, "-output", expectedArtifactFile.path, @@ -265,6 +267,7 @@ private class TestCompilationImpl( "-Xverify-ir", "-Xbinary=runtimeAssertionsMode=panic" ) + optimizationMode.compilerFlag?.let { compilerFlag -> add(compilerFlag) } addFlattened(dependencies.libraries) { library -> listOf("-l", library.resultingArtifactPath) } dependencies.friends.takeIf(Collection<*>::isNotEmpty)?.let { friends -> diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt index 3b5951719da..41024c0f64c 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt @@ -113,11 +113,12 @@ internal class TestCompilerArgs(val compilerArgs: List) { "-trw", "-generate-worker-test-runner", "-nomain", "-output", - "-entry", + "-entry", "-e", "-produce", "-repo", "-target", - "-Xinclude" + "-Xinclude", + "-g", "-opt" ) } } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt index 7672361bbad..e0b81771214 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt @@ -30,7 +30,7 @@ internal class KotlinNativeClassLoader(private val lazyClassLoader: Lazy().safeAs()?.getRootCacheDirectory(get(), get(), debuggable) +internal fun Settings.getRootCacheDirectory(): File? = + get().safeAs()?.getRootCacheDirectory(get(), get(), get())