diff --git a/InteropExample/build.gradle b/InteropExample/build.gradle index d66de1f9c51..86dbc1c0d11 100644 --- a/InteropExample/build.gradle +++ b/InteropExample/build.gradle @@ -20,7 +20,7 @@ kotlinNativeInterop { llvm { defFile '../backend.native/llvm.def' compilerOpts "-I$llvmDir/include" - linkerOpts "-L$llvmDir/lib", "-B$sysrootDir/usr/bin" + linkerOpts "-L$llvmDir/lib" } } diff --git a/backend.native/build.gradle b/backend.native/build.gradle index eab4f5fe8d0..53da5b416c9 100644 --- a/backend.native/build.gradle +++ b/backend.native/build.gradle @@ -105,20 +105,19 @@ String[] linkerOpts1() { + ["-L$libffiDir/lib", "-lffi"]) ArrayList strldflags = new ArrayList<>() ldflags.forEach{strldflags.add(it.toString())} - strldflags.add("-B$sysrootDir/usr/bin".toString()) return strldflags.toArray(new String[0]) } kotlinNativeInterop { llvm { defFile 'llvm.def' - compilerOpts "-fPIC", "-I$llvmDir/include", "-B$sysrootDir/usr/bin" + compilerOpts "-fPIC", "-I$llvmDir/include" linkerOpts linkerOpts1() } hash { // TODO: copy-pasted from ':common:compileHash' compilerOpts '-fPIC' - linkerOpts '-fPIC', "-B$sysrootDir/usr/bin" + linkerOpts '-fPIC' linker 'clang++' linkOutputs ':common:compileHash' diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 3a740dfe64e..4a4eb9ebb7b 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -11,8 +11,6 @@ abstract class KonanTest extends DefaultTask { def backendNative = project.project(":backend.native") def runtimeProject = project.project(":runtime") def llvmLlc = llvmTool("llc") - def llvmC = llvmTool("clang") - def llvmCAdditionalArgs = ["--sysroot=$project.sysrootDir"] def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc") def mainC = 'main.c' String goldValue = null @@ -100,22 +98,18 @@ abstract class KonanTest extends DefaultTask { class UnitKonanTest extends KonanTest { void compileTest(File sourceS, File runtimeS, File exe) { def testC = sourceS.absolutePath.replace(".kt.S", "-test.c") - project.exec { - commandLine "${llvmC}", "-B${project.sysrootDir}/usr/bin", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(), + project.execClang { + commandLine "clang", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(), "-o", "${exe.absolutePath}" - - args llvmCAdditionalArgs } } } class RunKonanTest extends KonanTest { void compileTest(File sourceS, File runtimeS, File exe) { - project.exec { - commandLine "${llvmC}", "-DRUN_TEST", "-B${project.sysrootDir}/usr/bin", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(), + project.execClang { + commandLine "clang", "-DRUN_TEST", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(), "-o", "${exe.absolutePath}" - - args llvmCAdditionalArgs } } } diff --git a/build.gradle b/build.gradle index 4aebce93210..c451fb464c3 100644 --- a/build.gradle +++ b/build.gradle @@ -11,15 +11,22 @@ allprojects { evaluationDependsOn(":dependencies") } + ext.clangArgs = ["--sysroot=$sysrootDir", "-B$sysrootDir/usr/bin"] + ext.clangPath = ["$llvmDir/bin"] + + convention.plugins.execClang = new org.jetbrains.kotlin.ExecClang(project) + plugins.withType(NativeComponentPlugin) { model { toolChains { clang(Clang) { - path "$llvmDir/bin" + clangPath.each { + path it + } + eachPlatform { // TODO: will not work when cross-compiling [cCompiler, cppCompiler, linker].each { - it.withArguments { it << "--sysroot=$sysrootDir"} - it.withArguments { it << "-B$sysrootDir/usr/bin"} + it.withArguments { it.addAll(clangArgs) } } } diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy index 57d189f5dcc..da6ab4b27a9 100644 --- a/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy @@ -68,13 +68,11 @@ class CompileCppToBitcode extends DefaultTask { File objDir = this.getObjDir() objDir.mkdirs() - project.exec { + project.execClang { workingDir objDir - executable "$project.llvmDir/bin/clang++" + executable "clang++" args '-std=c++11' - args "--sysroot=$project.sysrootDir" - args compilerArgs args "-I$headersDir" diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/ExecClang.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/ExecClang.groovy new file mode 100644 index 00000000000..b4e87ef4b29 --- /dev/null +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/ExecClang.groovy @@ -0,0 +1,50 @@ +package org.jetbrains.kotlin + +import org.gradle.api.Action +import org.gradle.api.GradleException +import org.gradle.api.Project +import org.gradle.process.ExecResult +import org.gradle.process.ExecSpec +import org.gradle.util.ConfigureUtil + +class ExecClang { + + private final Project project + + ExecClang(Project project) { + this.project = project + } + + public ExecResult execClang(Closure closure) { + return this.execClang(ConfigureUtil.configureUsing(closure)); + } + + public ExecResult execClang(Action action) { + Action extendedAction = new Action() { + @Override + void execute(ExecSpec execSpec) { + action.execute(execSpec) + + execSpec.with { + + if (executable == null) { + executable = 'clang' + } + + if (executable in ['clang', 'clang++']) { + executable = "${project.llvmDir}/bin/$executable" + } else { + throw new GradleException("unsupport clang executable: $executable") + } + + environment["PATH"] = project.files(project.clangPath).asPath + + File.pathSeparator + environment["PATH"] + + args project.clangArgs + } + + } + } + return project.exec(extendedAction) + } +} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy index f7e10ca6b59..52be1915162 100644 --- a/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy @@ -174,9 +174,12 @@ class NamedNativeInteropConfig implements Named { args "-linker:" + linker } - String sysrootArg = "--sysroot=$project.sysrootDir" - compilerOpts += sysrootArg - linkerOpts += sysrootArg + // TODO: the interop plugin should probably be reworked to execute clang from build scripts directly + environment['PATH'] = project.files(project.clangPath).asPath + + File.pathSeparator + environment['PATH'] + + compilerOpts += project.clangArgs + linkerOpts += project.clangArgs args compilerOpts.collect { "-copt:$it" } args linkerOpts.collect { "-lopt:$it" }