From ab02bbcc37350baa7f083e35c7b68a8a460d939c Mon Sep 17 00:00:00 2001 From: Pavel Punegov Date: Mon, 9 Nov 2020 15:25:35 +0300 Subject: [PATCH] Fix clang invocation in the tests: use linker with the same options as the build. (#4499) Fix clang invocation in the tests: use linker with the same options as the build. --- .../kotlin/org/jetbrains/kotlin/ExecClang.kt | 19 ++++----- .../org/jetbrains/kotlin/KotlinNativeTest.kt | 41 ++++++++++++++----- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/ExecClang.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/ExecClang.kt index 4e4c3e259c0..bf5782f5a55 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/ExecClang.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/ExecClang.kt @@ -88,18 +88,15 @@ class ExecClang(private val project: Project) { } private fun execClang(defaultArgs: List, action: Action): ExecResult { - val extendedAction = object : Action { - override fun execute(execSpec: ExecSpec) { - action.execute(execSpec) + val extendedAction = Action { execSpec -> + action.execute(execSpec) + execSpec.apply { + executable = resolveExecutable(executable) - execSpec.apply { - executable = resolveExecutable(executable) - - val hostPlatform = project.findProperty("hostPlatform") as Platform - environment["PATH"] = project.files(hostPlatform.clang.clangPaths).asPath + - java.io.File.pathSeparator + environment["PATH"] - args(defaultArgs) - } + val hostPlatform = project.findProperty("hostPlatform") as Platform + environment["PATH"] = project.files(hostPlatform.clang.clangPaths).asPath + + java.io.File.pathSeparator + environment["PATH"] + args = args + defaultArgs } } return project.exec(extendedAction) diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/KotlinNativeTest.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/KotlinNativeTest.kt index 3c167c6d4ec..ee611106db7 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/KotlinNativeTest.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/KotlinNativeTest.kt @@ -14,13 +14,13 @@ import org.gradle.api.tasks.Input import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction import org.gradle.language.base.plugins.LifecycleBasePlugin -import org.gradle.process.ExecSpec import java.io.File import java.io.ByteArrayOutputStream import java.util.regex.Pattern import org.jetbrains.kotlin.konan.target.HostManager +import org.jetbrains.kotlin.konan.target.LinkerOutputKind abstract class KonanTest : DefaultTask(), KonanTestExecutable { enum class Logger { @@ -439,25 +439,46 @@ open class KonanDynamicTest : KonanStandaloneTest() { private fun clang() { val log = ByteArrayOutputStream() val plugin = project.convention.getPlugin(ExecClang::class.java) - val execResult = plugin.execKonanClang(project.testTarget, Action { + val artifactsDir = "$outputDirectory/${project.testTarget}" + + fun flagsContain(opt: String) = project.globalTestArgs.contains(opt) || flags.contains(opt) + val isOpt = flagsContain("-opt") + val isDebug = flagsContain("-g") + + val execResult = plugin.execKonanClang(project.testTarget) { it.workingDir = File(outputDirectory) it.executable = clangTool - val artifactsDir = "$outputDirectory/${project.testTarget}" it.args = listOf(processCSource(), - "-o", executable, - "-I", artifactsDir, - "-L", artifactsDir, - "-l", name, - "-Wl,-rpath,$artifactsDir") - + "-c", + "-o", "$executable.o", + "-I", artifactsDir + ) it.standardOutput = log it.errorOutput = log it.isIgnoreExitValue = true - }) + } log.toString("UTF-8").also { project.file("$executable.compilation.log").writeText(it) println(it) } execResult.assertNormalExitValue() + + val linker = project.platformManager.platform(project.testTarget).linker + val commands = linker.finalLinkCommands( + objectFiles = listOf("$executable.o"), + executable = executable, + libraries = listOf("-l$name"), + linkerArgs = listOf("-L", artifactsDir, "-rpath", artifactsDir), + optimize = isOpt, + debug = isDebug, + kind = LinkerOutputKind.EXECUTABLE, + outputDsymBundle = "", + needsProfileLibrary = false, + mimallocEnabled = false + ) + commands.forEach { + it.logWith { message -> project.file("$executable.compilation.log").appendText(message()) } + it.execute() + } } } \ No newline at end of file