build: simplify clang(++) configuration
also introduce 'execClang' block
This commit is contained in:
committed by
SvyatoslavScherbina
parent
4e5e7d37ee
commit
55f8257f92
@@ -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"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -105,20 +105,19 @@ String[] linkerOpts1() {
|
||||
+ ["-L$libffiDir/lib", "-lffi"])
|
||||
ArrayList<String> 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'
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-3
@@ -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) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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<? super ExecSpec> action) {
|
||||
Action<? super ExecSpec> extendedAction = new Action<ExecSpec>() {
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -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" }
|
||||
|
||||
Reference in New Issue
Block a user