Skip to content

Commit

Permalink
Fix clang invocation in the tests: use linker with the same options a…
Browse files Browse the repository at this point in the history
…s the build. (JetBrains#4499)

Fix clang invocation in the tests: use linker with the same options as the build.
  • Loading branch information
PavelPunegov authored Nov 9, 2020
1 parent c53b277 commit ee9211f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
21 changes: 9 additions & 12 deletions build-tools/src/main/kotlin/org/jetbrains/kotlin/ExecClang.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,15 @@ class ExecClang(private val project: Project) {
}

private fun execClang(defaultArgs: List<String>, action: Action<in ExecSpec>): ExecResult {
val extendedAction = object : Action<ExecSpec> {
override fun execute(execSpec: ExecSpec) {
action.execute(execSpec)

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 extendedAction = Action<ExecSpec> { execSpec ->
action.execute(execSpec)
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 = args + defaultArgs
}
}
return project.exec(extendedAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ExecSpec> {
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()
}
}
}

0 comments on commit ee9211f

Please sign in to comment.