[Native][tests] Gradle property: OptimizationMode

This commit is contained in:
Dmitriy Dolovov
2021-12-20 16:56:21 +03:00
parent 46f2c6ee10
commit dbd7a05c7e
6 changed files with 54 additions and 45 deletions
+1
View File
@@ -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");
@@ -1,4 +1,4 @@
// FREE_COMPILER_ARGS: -opt -verbose
// FREE_COMPILER_ARGS: -Xprint-files -verbose
import kotlin.test.*
@@ -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 <T> systemProperty(name: String, transform: (String) -> T, default: T): T =
System.getProperty(name)?.let(transform) ?: default
private fun <T> 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 <reified E : Enum<E>> enumSystemProperty(propertyName: String, values: Array<out E>, 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"
@@ -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<TestModule>,
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 ->
@@ -113,11 +113,12 @@ internal class TestCompilerArgs(val compilerArgs: List<String>) {
"-trw", "-generate-worker-test-runner",
"-nomain",
"-output",
"-entry",
"-entry", "-e",
"-produce",
"-repo",
"-target",
"-Xinclude"
"-Xinclude",
"-g", "-opt"
)
}
}
@@ -30,7 +30,7 @@ internal class KotlinNativeClassLoader(private val lazyClassLoader: Lazy<ClassLo
}
// TODO: in fact, only WITH_MODULES mode is supported now
internal enum class TestMode(val description: String) {
internal enum class TestMode(private val description: String) {
ONE_STAGE(
description = "Compile test files altogether without producing intermediate KLIBs."
),
@@ -40,7 +40,17 @@ internal enum class TestMode(val description: String) {
WITH_MODULES(
description = "Compile each test file as one or many modules (depending on MODULE directives declared in the file)." +
" Then link the KLIBs into the single executable file."
)
);
override fun toString() = description
}
internal enum class OptimizationMode(private val description: String, val compilerFlag: String?) {
DEBUG("Build with debug information", "-g"),
OPT("Build with optimizations applied", "-opt"),
NO("Don't use any specific optimizations", null);
override fun toString() = description + if (compilerFlag == null) "" else " ($compilerFlag)"
}
/**
@@ -63,10 +73,10 @@ internal sealed interface CacheKind {
fun getRootCacheDirectory(
kotlinNativeHome: KotlinNativeHome,
kotlinNativeTargets: KotlinNativeTargets,
debuggable: Boolean
optimizationMode: OptimizationMode
): File? = kotlinNativeHome.dir
.resolve("klib/cache")
.resolve(computeCacheDirName(kotlinNativeTargets.testTarget, CACHE_KIND, debuggable))
.resolve(computeCacheDirName(kotlinNativeTargets.testTarget, CACHE_KIND, optimizationMode == OptimizationMode.DEBUG))
.takeIf { it.exists() }
private const val CACHE_KIND = "STATIC"
@@ -78,5 +88,5 @@ internal sealed interface CacheKind {
}
}
internal fun Settings.getRootCacheDirectory(debuggable: Boolean): File? =
get<CacheKind>().safeAs<CacheKind.WithStaticCache>()?.getRootCacheDirectory(get(), get(), debuggable)
internal fun Settings.getRootCacheDirectory(): File? =
get<CacheKind>().safeAs<CacheKind.WithStaticCache>()?.getRootCacheDirectory(get(), get(), get())