From a4bb7d97301bf025165ec59b559b17c5b6d900ec Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Tue, 21 Jul 2020 16:32:51 +0200 Subject: [PATCH] [test][dwarfdump] infrostructure to complex test cases. some time we need more complex tests then just one file compilation, e.g test with libraries different flags and so on. Presented infrostructure allows to evaluate cinterop, konanc and dwarfdump complex scenarious --- .../native/test/debugger/DistProperties.kt | 2 + .../kotlin/native/test/debugger/Driver.kt | 14 ++++++- .../kotlin/native/test/debugger/Matchers.kt | 40 ++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/DistProperties.kt b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/DistProperties.kt index f81dd8af802..030cb965b1c 100644 --- a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/DistProperties.kt +++ b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/DistProperties.kt @@ -13,7 +13,9 @@ import java.nio.file.Paths object DistProperties { private val dist: Path = Paths.get(requireProp("kotlin.native.home")) private val konancDriver = if (HostManager.host.family == Family.MINGW) "konanc.bat" else "konanc" + private val cinteropDriver = if (HostManager.host.family == Family.MINGW) "cinterop.bat" else "cinterop" val konanc: Path = dist.resolve("bin/$konancDriver") + val cinterop: Path = dist.resolve("bin/$cinteropDriver") val lldb: Path = Paths.get("lldb") val devToolsSecurity: Path = Paths.get("DevToolsSecurity") val dwarfDump: Path = Paths.get("dwarfdump") diff --git a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Driver.kt b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Driver.kt index cd557e86b9b..bc06b40c433 100644 --- a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Driver.kt +++ b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Driver.kt @@ -35,6 +35,16 @@ class ToolDriver( } } + fun cinterop(defFile:Path, output: Path, pkg: String, vararg args: String) { + val allArgs = listOf("-o", output.toString(), "-def", defFile.toString(), "-pkg", pkg, *args).toTypedArray() + + //TODO: do we need in process cinterop? + subprocess(DistProperties.cinterop, *allArgs).thrownIfFailed() + check(Files.exists(output)) { + "Compiler has not produced an output at $output" + } + } + fun runLldb(program: Path, commands: List): String { val args = listOf("-b", "-o", "command script import \"${DistProperties.lldbPrettyPrinters}\"") + commands.flatMap { listOf("-o", it) } @@ -43,8 +53,8 @@ class ToolDriver( .stdout } - fun runDwarfDump(program: Path, processor:List.()->Unit) { - val out = subprocess(DistProperties.dwarfDump, "${program}.dSYM/Contents/Resources/DWARF/${program.fileName}").takeIf { it.process.exitValue() == 0 }?.stdout ?: error("${program}.dSYM/Contents/Resources/DWARF/${program.fileName}") + fun runDwarfDump(program: Path, vararg args:String = emptyArray(), processor:List.()->Unit) { + val out = subprocess(DistProperties.dwarfDump, "${program}.dSYM/Contents/Resources/DWARF/${program.fileName}", *args).takeIf { it.process.exitValue() == 0 }?.stdout ?: error("${program}.dSYM/Contents/Resources/DWARF/${program.fileName}") DwarfUtilParser().parse(StringReader(out)).tags.toList().processor() } } diff --git a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Matchers.kt b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Matchers.kt index 62dbeed339b..18ff0b3aa5a 100644 --- a/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Matchers.kt +++ b/backend.native/tests/debugger/src/test/kotlin/org/jetbrains/kotlin/native/test/debugger/Matchers.kt @@ -9,6 +9,7 @@ import org.intellij.lang.annotations.Language import org.junit.Assert.fail import java.io.IOException import java.nio.file.Files +import java.nio.file.Path /** * An integration test for debug info. @@ -109,7 +110,44 @@ fun dwarfDumpTest(@Language("kotlin") programText: String, flags: List, val driver = ToolDriver() Files.write(source, programText.trimIndent().toByteArray()) driver.compile(source, output, "-g", *flags.toTypedArray()) - driver.runDwarfDump(output, test) + driver.runDwarfDump(output, processor = test) + } +} + +class ToolDriverHelper(private val driver: ToolDriver, private val root:Path) { + fun String.cinterop(pkg:String, output: String):Path { + val def = feedOutput("$output.def") + val lib = root.resolve("$output.klib") + driver.cinterop(def, lib, pkg) + return lib + } + + + fun String.library(output: String, vararg flags:String) = feedOutput("$output.kt").compile(root.resolve("$output.klib"), "-p", "library", *flags) + + fun String.binary(output: String, vararg flags:String)= feedOutput("$output.kt").compile(root.resolve("$output.kexe"), *flags) + + private fun Path.compile(output: Path, vararg flags:String) = output.also{ driver.compile(this, it, *flags) } + + fun Path.dwarfDumpLookup(address: Long, parser:List.() -> Unit) = driver.runDwarfDump(this, "-lookup", address.toString(), processor = parser) + + + private fun String.feedOutput(output: String) = root.resolve(output).also { + Files.write(it, this.trimIndent().toByteArray()) + } + +} + +fun dwarfDumpComplexTest(test:ToolDriverHelper.()->Unit) { + if (!haveDwarfDump) { + println("Skipping test: no dwarfdump") + return + } + + + with(Files.createTempDirectory("dwarfdump_test_complex")) { + toFile().deleteOnExit() + val driver = ToolDriverHelper(ToolDriver(), this).test() } }