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 bf5782f5a55..66b561814bc 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 @@ -50,6 +50,22 @@ class ExecClang(private val project: Project) { } } + fun resolveToolchainExecutable(target: KonanTarget, executableOrNull: String?): String { + val executable = executableOrNull ?: "clang" + + if (listOf("clang", "clang++").contains(executable)) { + // TODO: This is copied from `BitcodeCompiler`. Consider sharing the code instead. + val platform = platformManager.platform(target) + return if (target.family.isAppleFamily) { + "${platform.absoluteTargetToolchain}/usr/bin/$executable" + } else { + "${platform.absoluteTargetToolchain}/bin/$executable" + } + } else { + throw GradleException("unsupported clang executable: $executable") + } + } + // The bare ones invoke clang with system default sysroot. fun execBareClang(action: Action): ExecResult { @@ -81,6 +97,30 @@ class ExecClang(private val project: Project) { return this.execClang(konanArgs(target), closure) } + // The toolchain ones execute clang from the toolchain. + + fun execToolchainClang(target: String?, action: Action): ExecResult { + return this.execToolchainClang(platformManager.targetManager(target).target, action) + } + + fun execToolchainClang(target: String?, closure: Closure): ExecResult { + return this.execToolchainClang(platformManager.targetManager(target).target, ConfigureUtil.configureUsing(closure)) + } + + fun execToolchainClang(target: KonanTarget, action: Action): ExecResult { + val extendedAction = Action { execSpec -> + action.execute(execSpec) + execSpec.apply { + executable = resolveToolchainExecutable(target, executable) + } + } + return project.exec(extendedAction) + } + + fun execToolchainClang(target: KonanTarget, closure: Closure): ExecResult { + return this.execToolchainClang(target, ConfigureUtil.configureUsing(closure)) + } + // These ones are private, so one has to choose either Bare or Konan. private fun execClang(defaultArgs: List, closure: Closure): ExecResult { diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/testing/native/NativeTest.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/testing/native/NativeTest.kt index 880fb4cb971..017500775d9 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/testing/native/NativeTest.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/testing/native/NativeTest.kt @@ -17,10 +17,10 @@ import org.jetbrains.kotlin.konan.target.* open class CompileNativeTest @Inject constructor( @InputFile val inputFile: File, - @Input val target: String + @Input val target: KonanTarget, ) : DefaultTask() { @OutputFile - var outputFile = project.buildDir.resolve("bin/test/$target/${inputFile.nameWithoutExtension}.o") + var outputFile = project.buildDir.resolve("bin/test/${target.name}/${inputFile.nameWithoutExtension}.o") @Input val clangArgs = mutableListOf() @@ -28,9 +28,16 @@ open class CompileNativeTest @Inject constructor( @TaskAction fun compile() { val plugin = project.convention.getPlugin(ExecClang::class.java) - plugin.execBareClang { - it.executable = "clang++" - it.args = clangArgs + listOf(inputFile.absolutePath, "-o", outputFile.absolutePath) + if (target.family.isAppleFamily) { + plugin.execToolchainClang(target) { + it.executable = "clang++" + it.args = clangArgs + listOf(inputFile.absolutePath, "-o", outputFile.absolutePath) + } + } else { + plugin.execBareClang { + it.executable = "clang++" + it.args = clangArgs + listOf(inputFile.absolutePath, "-o", outputFile.absolutePath) + } } } } @@ -218,7 +225,7 @@ fun createTestTask( "${testTaskName}Compile", CompileNativeTest::class.java, llvmLinkTask.outputFile, - target + konanTarget, ).apply { dependsOn(llvmLinkTask) clangArgs.addAll(clangFlags.clangFlags)