From 33a09fbb7729540cd242ccfe69c6bf8b2cef8418 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Tue, 25 Jan 2022 10:59:52 +0300 Subject: [PATCH] [Native][tests] Support all cache modes in KLIB ABI tests ^KT-50775 --- .../blackboxtest/AbstractNativeKlibABITest.kt | 58 ++++++++++++++----- .../compilation/TestCompilationDependency.kt | 8 +-- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeKlibABITest.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeKlibABITest.kt index 3bef2c69627..ec19621803b 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeKlibABITest.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeKlibABITest.kt @@ -9,26 +9,23 @@ import com.intellij.testFramework.TestDataFile import org.jetbrains.kotlin.klib.AbstractKlibABITestCase import org.jetbrains.kotlin.konan.blackboxtest.support.* import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase.WithTestRunnerExtras -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExecutableCompilation -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExistingLibraryDependency -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.LibraryCompilation -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.Executable +import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.* +import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.* import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependencyType.Library import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Companion.assertSuccess import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestExecutable +import org.jetbrains.kotlin.konan.blackboxtest.support.settings.CacheKind import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeHome import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeTargets import org.jetbrains.kotlin.konan.blackboxtest.support.settings.SimpleTestDirectories -import org.jetbrains.kotlin.konan.blackboxtest.support.util.LAUNCHER_FILE_NAME -import org.jetbrains.kotlin.konan.blackboxtest.support.util.LAUNCHER_MODULE_NAME -import org.jetbrains.kotlin.konan.blackboxtest.support.util.generateBoxFunctionLauncher -import org.jetbrains.kotlin.konan.blackboxtest.support.util.getAbsoluteFile +import org.jetbrains.kotlin.konan.blackboxtest.support.util.* import org.junit.jupiter.api.Tag import java.io.File @Tag("klib") abstract class AbstractNativeKlibABITest : AbstractNativeSimpleTest() { + private val producedKlibs = linkedMapOf>() // IMPORTANT: The order makes sense! + protected fun runTest(@TestDataFile testPath: String): Unit = AbstractKlibABITestCase.doTest( testDir = getAbsoluteFile(testPath), buildDir = buildDir, @@ -46,18 +43,29 @@ abstract class AbstractNativeKlibABITest : AbstractNativeSimpleTest() { val testCase = createTestCase(module, COMPILER_ARGS_FOR_KLIB) + val klibArtifact = KLIB(klibFile) val compilation = LibraryCompilation( settings = testRunSettings, freeCompilerArgs = testCase.freeCompilerArgs, sourceModules = testCase.modules, - dependencies = createExistingDependencies(moduleDependencies), - expectedArtifact = TestCompilationArtifact.KLIB(klibFile), + dependencies = createLibraryDependencies(moduleDependencies), + expectedArtifact = klibArtifact ) compilation.result.assertSuccess() // <-- trigger compilation + + producedKlibs[klibArtifact] = moduleDependencies // Remember the artifact with its dependencies. } private fun buildBinaryAndRun(allDependencies: Collection) { + val cacheDependencies = if (staticCacheRequiredForEveryLibrary) { + producedKlibs.map { (klibArtifact, moduleDependencies) -> + buildCacheForKlib(moduleDependencies, klibArtifact) + klibArtifact.toStaticCacheArtifact().toDependency() + } + } else + emptyList() + val (sourceDir, outputDir) = AbstractKlibABITestCase.createModuleDirs(buildDir, LAUNCHER_MODULE_NAME) val executableFile = outputDir.resolve("app." + testRunSettings.get().testTarget.family.exeSuffix) @@ -75,7 +83,7 @@ abstract class AbstractNativeKlibABITest : AbstractNativeSimpleTest() { freeCompilerArgs = testCase.freeCompilerArgs, sourceModules = testCase.modules, extras = testCase.extras, - dependencies = createExistingDependencies(allDependencies), + dependencies = createLibraryDependencies(allDependencies) + cacheDependencies, expectedArtifact = Executable(executableFile) ) @@ -85,6 +93,16 @@ abstract class AbstractNativeKlibABITest : AbstractNativeSimpleTest() { runExecutableAndVerify(testCase, executable) // <-- run executable and verify } + private fun buildCacheForKlib(moduleDependencies: Collection, klibArtifact: KLIB) { + val compilation = StaticCacheCompilation( + settings = testRunSettings, + dependencies = createLibraryCacheDependencies(moduleDependencies) + klibArtifact.toDependency(), + expectedArtifact = klibArtifact.toStaticCacheArtifact() + ) + + compilation.result.assertSuccess() // <-- trigger compilation + } + private fun createModule(moduleName: String) = TestModule.Exclusive( name = moduleName, directDependencySymbols = emptySet(), /* Don't need to pass any dependency symbols here. @@ -104,11 +122,23 @@ abstract class AbstractNativeKlibABITest : AbstractNativeSimpleTest() { initialize(null) } - private fun createExistingDependencies(dependencies: Collection) = - dependencies.map { ExistingLibraryDependency(TestCompilationArtifact.KLIB(it), Library) } + private fun createLibraryDependencies(klibFiles: Iterable): Iterable> = + klibFiles.map { klibFile -> KLIB(klibFile).toDependency() } + + private fun createLibraryCacheDependencies(klibFiles: Iterable): Iterable> = + klibFiles.mapNotNull { klibFile -> if (klibFile != stdlibFile) KLIB(klibFile).toStaticCacheArtifact().toDependency() else null } + + private fun KLIB.toDependency() = ExistingDependency(this, Library) + private fun KLIBStaticCache.toDependency() = ExistingDependency(this, TestCompilationDependencyType.LibraryStaticCache) + + private fun KLIB.toStaticCacheArtifact() = KLIBStaticCache( + cacheDir = klibFile.parentFile.resolve(STATIC_CACHE_DIR_NAME).apply { mkdirs() }, + klib = this + ) private val buildDir: File get() = testRunSettings.get().testBuildDir private val stdlibFile: File get() = testRunSettings.get().stdlibFile + private val staticCacheRequiredForEveryLibrary: Boolean get() = testRunSettings.get().staticCacheRequiredForEveryLibrary companion object { private val COMPILER_ARGS_FOR_KLIB = TestCompilerArgs( diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationDependency.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationDependency.kt index 90c3231e811..68edf1744ac 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationDependency.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilationDependency.kt @@ -38,7 +38,7 @@ internal class CompiledDependency( override val artifact: A get() = compilation.result.assertSuccess().resultingArtifact } -internal class ExistingLibraryDependency( - override val artifact: KLIB, - override val type: TestCompilationDependencyType -) : TestCompilationDependency +internal class ExistingDependency( + override val artifact: A, + override val type: TestCompilationDependencyType +) : TestCompilationDependency