diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt index 943fbdbf95a..745bbe54532 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.konan.blackboxtest.support.settings.* import org.jetbrains.kotlin.konan.blackboxtest.support.util.ArgsBuilder import org.jetbrains.kotlin.konan.blackboxtest.support.util.buildArgs import org.jetbrains.kotlin.konan.blackboxtest.support.util.flatMapToSet +import org.jetbrains.kotlin.konan.blackboxtest.support.util.mapToSet import org.jetbrains.kotlin.konan.properties.resolvablePropertyList import org.jetbrains.kotlin.test.services.JUnit5Assertions.fail import java.io.File @@ -64,11 +65,7 @@ internal abstract class BasicCompilation( addFlattenedTwice(sourceModules, { it.files }) { it.location.path } } - protected open fun doBeforeCompile() = Unit - private fun doCompile(): TestCompilationResult.ImmediateResult { - doBeforeCompile() - val compilerArgs = buildArgs { applyCommonArgs() applySpecificArgs(this) @@ -101,7 +98,7 @@ internal abstract class BasicCompilation( loggedFailure to result } - getLogFile(expectedArtifact.file).writeText(loggedCompilerCall.toString()) + expectedArtifact.logFile.writeText(loggedCompilerCall.toString()) return result } @@ -129,10 +126,12 @@ internal abstract class BasicCompilation( } @JvmStatic - protected val Iterable>.cachedLibraries + protected val Iterable>.cachedLibraries: List get() = collectArtifacts() - private fun getLogFile(expectedArtifactFile: File): File = expectedArtifactFile.resolveSibling(expectedArtifactFile.name + ".log") + @JvmStatic + protected val Iterable.uniqueCacheDirs: Set + get() = mapToSet { (libraryCacheDir, _) -> libraryCacheDir } // Avoid repeating the same directory more than once. } } @@ -157,10 +156,7 @@ internal abstract class SourceBasedCompilation( expectedArtifact = expectedArtifact ) { override fun applySpecificArgs(argsBuilder: ArgsBuilder): Unit = with(argsBuilder) { - add( - "-repo", home.librariesDir.path, - "-output", expectedArtifact.path - ) + add("-repo", home.librariesDir.path) memoryModel.compilerFlags?.let { compilerFlags -> add(compilerFlags) } threadStateChecker.compilerFlag?.let { compilerFlag -> add(compilerFlag) } gcType.compilerFlag?.let { compilerFlag -> add(compilerFlag) } @@ -201,7 +197,10 @@ internal class LibraryCompilation( expectedArtifact = expectedArtifact ) { override fun applySpecificArgs(argsBuilder: ArgsBuilder) = with(argsBuilder) { - add("-produce", "library") + add( + "-produce", "library", + "-output", expectedArtifact.path + ) super.applySpecificArgs(argsBuilder) } } @@ -229,7 +228,10 @@ internal class ExecutableCompilation( private val cacheKind: CacheKind = settings.get() override fun applySpecificArgs(argsBuilder: ArgsBuilder): Unit = with(argsBuilder) { - add("-produce", "program") + add( + "-produce", "program", + "-output", expectedArtifact.path + ) when (extras) { is NoTestRunnerExtras -> add("-entry", extras.entryPoint) is WithTestRunnerExtras -> { @@ -247,7 +249,7 @@ internal class ExecutableCompilation( override fun applyDependencies(argsBuilder: ArgsBuilder): Unit = with(argsBuilder) { super.applyDependencies(argsBuilder) cacheKind.staticCacheRootDir?.let { cacheRootDir -> add("-Xcache-directory=$cacheRootDir") } - add(dependencies.cachedLibraries) { (libraryCacheDir, _) -> "-Xcache-directory=${libraryCacheDir.path}" } + add(dependencies.cachedLibraries.uniqueCacheDirs) { libraryCacheDir -> "-Xcache-directory=${libraryCacheDir.path}" } } } @@ -266,17 +268,11 @@ internal class StaticCacheCompilation( override val sourceModules get() = emptyList() override val freeCompilerArgs get() = TestCompilerArgs.EMPTY - private val cacheDir = expectedArtifact.file - private val cacheRootDir: File = run { val cacheKind = settings.get() cacheKind.staticCacheRootDir ?: fail { "No cache root directory found for cache kind $cacheKind" } } - override fun doBeforeCompile() { - cacheDir.mkdirs() // Make sure the directory to hold the cache exists. - } - override fun applySpecificArgs(argsBuilder: ArgsBuilder): Unit = with(argsBuilder) { add("-produce", "static_cache") @@ -284,18 +280,15 @@ internal class StaticCacheCompilation( add(home.properties.resolvablePropertyList("additionalCacheFlags", targets.testTarget.visibleName)) add( "-Xadd-cache=${dependencies.libraryToCache.path}", - "-Xcache-directory=${cacheDir.path}", + "-Xcache-directory=${expectedArtifact.cacheDir.path}", "-Xcache-directory=$cacheRootDir" ) } override fun applyDependencies(argsBuilder: ArgsBuilder): Unit = with(argsBuilder) { - addFlattened(dependencies.cachedLibraries) { (libraryCacheDir, library) -> - listOf( - "-l", library.path, - "-Xcache-directory=${libraryCacheDir.path}" - ) - } + val cachedLibraries = dependencies.cachedLibraries + addFlattened(cachedLibraries) { (_, library) -> listOf("-l", library.path) } + add(cachedLibraries.uniqueCacheDirs) { libraryCacheDir -> "-Xcache-directory=${libraryCacheDir.path}" } } companion object { diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationArtifact.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationArtifact.kt index e0d74cabb44..da253709982 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationArtifact.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationArtifact.kt @@ -8,10 +8,19 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.compilation import java.io.File internal sealed interface TestCompilationArtifact { - val file: File - val path: String get() = file.path + val logFile: File - data class KLIB(override val file: File) : TestCompilationArtifact - data class KLIBStaticCache(override val file: File, val klib: KLIB) : TestCompilationArtifact - data class Executable(override val file: File) : TestCompilationArtifact + data class KLIB(val klibFile: File) : TestCompilationArtifact { + val path: String get() = klibFile.path + override val logFile: File get() = klibFile.resolveSibling("${klibFile.name}.log") + } + + data class KLIBStaticCache(val cacheDir: File, val klib: KLIB) : TestCompilationArtifact { + override val logFile: File get() = cacheDir.resolve("${klib.klibFile.nameWithoutExtension}-cache.log") + } + + data class Executable(val executableFile: File) : TestCompilationArtifact { + val path: String get() = executableFile.path + override val logFile: File get() = executableFile.resolveSibling("${executableFile.name}.log") + } } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationFactory.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationFactory.kt index 5c19b3875cc..9ba814332e1 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationFactory.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationFactory.kt @@ -78,7 +78,7 @@ internal class TestCompilationFactory { val klibArtifact = KLIB(settings.artifactFileForKlib(sourceModule, freeCompilerArgs)) val staticCacheArtifact: KLIBStaticCache? = if (settings.get().staticCacheRequiredForEveryLibrary) - KLIBStaticCache(file = klibArtifact.artifactFileForStaticCache(), klib = klibArtifact) + KLIBStaticCache(cacheDir = klibArtifact.cacheDirForStaticCache(), klib = klibArtifact) else null // No artifact means no static cache should be compiled. @@ -148,7 +148,7 @@ internal class TestCompilationFactory { is TestModule.Shared -> get().sharedBinariesDir.resolve("${module.name}-${prettyHash(freeCompilerArgs.hashCode())}.klib") } - private fun KLIB.artifactFileForStaticCache() = file.resolveSibling("${file.name}-cache") + private fun KLIB.cacheDirForStaticCache(): File = klibFile.parentFile.resolve(STATIC_CACHE_DIR_NAME).apply { mkdirs() } private fun Settings.singleModuleArtifactFile(module: TestModule.Exclusive, extension: String): File { val artifactFileName = buildString { diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/runner/TestRunProvider.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/runner/TestRunProvider.kt index debf638e8ae..443491d798d 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/runner/TestRunProvider.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/runner/TestRunProvider.kt @@ -157,7 +157,7 @@ internal class TestRunProvider( } val (artifact, loggedCompilerCall) = testCompilation.result.assertSuccess() // <-- Compilation happens here. - val executable = TestExecutable(artifact.file, loggedCompilerCall) + val executable = TestExecutable(artifact.executableFile, loggedCompilerCall) return action(testCase, executable, cacheKey) } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Names.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Names.kt index 72431c0cd8e..f11ce9ccd05 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Names.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/util/Names.kt @@ -28,4 +28,6 @@ internal const val DEFAULT_MODULE_NAME = "default" internal const val SUPPORT_MODULE_NAME = "support" internal const val LAUNCHER_MODULE_NAME = "__launcher__" // Used only in KLIB tests. +internal const val STATIC_CACHE_DIR_NAME = "__static_cache__" + internal fun prettyHash(hash: Int): String = hash.toUInt().toString(16).padStart(8, '0')