JVM_IR: Add test for compiling against cross-platform Klib
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.ir
|
||||
|
||||
import org.jetbrains.kotlin.cli.AbstractCliTest
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import org.jetbrains.kotlin.cli.js.K2JSCompiler
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.Companion.createForTests
|
||||
import org.jetbrains.kotlin.codegen.AbstractBlackBoxCodegenTest
|
||||
import org.jetbrains.kotlin.codegen.CodegenTestCase
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.jetbrains.kotlin.utils.rethrow
|
||||
import java.io.File
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
abstract class AbstractCompileKotlinAgainstKlibTest : AbstractBlackBoxCodegenTest() {
|
||||
lateinit var klibName: String
|
||||
lateinit var outputDir: File
|
||||
|
||||
override fun getBackend() = TargetBackend.JVM_IR
|
||||
|
||||
override fun doMultiFileTest(wholeFile: File, files: List<TestFile>) {
|
||||
outputDir = javaSourcesOutputDirectory
|
||||
klibName = Paths.get(outputDir.toString(), wholeFile.name.toString().removeSuffix(".kt")).toString()
|
||||
|
||||
val classpath: MutableList<File> = ArrayList()
|
||||
classpath.add(KotlinTestUtils.getAnnotationsJar())
|
||||
val configuration = createConfiguration(
|
||||
configurationKind, CodegenTestCase.getJdkKind(files),
|
||||
classpath,
|
||||
listOf(outputDir),
|
||||
files
|
||||
)
|
||||
myEnvironment = createForTests(
|
||||
testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES
|
||||
)
|
||||
setupEnvironment(myEnvironment)
|
||||
|
||||
|
||||
// All files but last are Klib's sources.
|
||||
try {
|
||||
compileToKlib(files.dropLast(1))
|
||||
} catch (t: Throwable) {
|
||||
if (!isIgnoredTarget(wholeFile)) {
|
||||
throw t
|
||||
}
|
||||
}
|
||||
super.doMultiFileTest(wholeFile, listOf(files.last()))
|
||||
}
|
||||
|
||||
override fun updateConfiguration(configuration: CompilerConfiguration) {
|
||||
configuration.put(JVMConfigurationKeys.KLIB_PATHS, listOf(klibName + ".klib"))
|
||||
}
|
||||
|
||||
// We need real (as opposed to virtual) files in order to produce a Klib.
|
||||
private fun loadMultiFilesReal(files: List<TestFile>): List<String> {
|
||||
val dir = outputDir
|
||||
return files.map { testFile ->
|
||||
assert(testFile.name.endsWith(".kt"))
|
||||
val ktFile = File(Paths.get(dir.toString(), testFile.name).toString())
|
||||
ktFile.writeText(testFile.content)
|
||||
ktFile.toString()
|
||||
}
|
||||
}
|
||||
|
||||
// For now, while there is no common backend, we generate Klib using
|
||||
// the JS_IR compiler.
|
||||
private fun compileToKlib(files: List<TestFile>) {
|
||||
val sourceFiles = loadMultiFilesReal(files)
|
||||
val (output, exitCode) = AbstractCliTest.executeCompilerGrabOutput(
|
||||
K2JSCompiler(),
|
||||
listOf(
|
||||
"-output", klibName,
|
||||
"-Xir-produce-klib-file",
|
||||
"-libraries", "libraries/stdlib/js-ir/build/fullRuntime/klib/"
|
||||
) + sourceFiles
|
||||
)
|
||||
if (exitCode != ExitCode.OK) {
|
||||
throw Exception(output)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TIMEOUT_SECONDS = 10L
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.ir;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TargetBackend;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("compiler/testData/codegen/boxKlib")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class CompileKotlinAgainstKlibTestGenerated extends AbstractCompileKotlinAgainstKlibTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInBoxKlib() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/boxKlib"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("compiler/testData/codegen/boxKlib/simple.kt");
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.generators.tests
|
||||
|
||||
import org.jetbrains.kotlin.codegen.ir.*
|
||||
import org.jetbrains.kotlin.generators.tests.generator.testGroup
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
System.setProperty("java.awt.headless", "true")
|
||||
|
||||
testGroup("compiler/tests-against-klib/tests", "compiler/testData") {
|
||||
testClass<AbstractCompileKotlinAgainstKlibTest> {
|
||||
model("codegen/boxKlib", targetBackend = TargetBackend.JVM_IR)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user