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 {
|
llvm {
|
||||||
defFile '../backend.native/llvm.def'
|
defFile '../backend.native/llvm.def'
|
||||||
compilerOpts "-I$llvmDir/include"
|
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"])
|
+ ["-L$libffiDir/lib", "-lffi"])
|
||||||
ArrayList<String> strldflags = new ArrayList<>()
|
ArrayList<String> strldflags = new ArrayList<>()
|
||||||
ldflags.forEach{strldflags.add(it.toString())}
|
ldflags.forEach{strldflags.add(it.toString())}
|
||||||
strldflags.add("-B$sysrootDir/usr/bin".toString())
|
|
||||||
return strldflags.toArray(new String[0])
|
return strldflags.toArray(new String[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlinNativeInterop {
|
kotlinNativeInterop {
|
||||||
llvm {
|
llvm {
|
||||||
defFile 'llvm.def'
|
defFile 'llvm.def'
|
||||||
compilerOpts "-fPIC", "-I$llvmDir/include", "-B$sysrootDir/usr/bin"
|
compilerOpts "-fPIC", "-I$llvmDir/include"
|
||||||
linkerOpts linkerOpts1()
|
linkerOpts linkerOpts1()
|
||||||
}
|
}
|
||||||
|
|
||||||
hash { // TODO: copy-pasted from ':common:compileHash'
|
hash { // TODO: copy-pasted from ':common:compileHash'
|
||||||
compilerOpts '-fPIC'
|
compilerOpts '-fPIC'
|
||||||
linkerOpts '-fPIC', "-B$sysrootDir/usr/bin"
|
linkerOpts '-fPIC'
|
||||||
linker 'clang++'
|
linker 'clang++'
|
||||||
linkOutputs ':common:compileHash'
|
linkOutputs ':common:compileHash'
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ abstract class KonanTest extends DefaultTask {
|
|||||||
def backendNative = project.project(":backend.native")
|
def backendNative = project.project(":backend.native")
|
||||||
def runtimeProject = project.project(":runtime")
|
def runtimeProject = project.project(":runtime")
|
||||||
def llvmLlc = llvmTool("llc")
|
def llvmLlc = llvmTool("llc")
|
||||||
def llvmC = llvmTool("clang")
|
|
||||||
def llvmCAdditionalArgs = ["--sysroot=$project.sysrootDir"]
|
|
||||||
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
||||||
def mainC = 'main.c'
|
def mainC = 'main.c'
|
||||||
String goldValue = null
|
String goldValue = null
|
||||||
@@ -100,22 +98,18 @@ abstract class KonanTest extends DefaultTask {
|
|||||||
class UnitKonanTest extends KonanTest {
|
class UnitKonanTest extends KonanTest {
|
||||||
void compileTest(File sourceS, File runtimeS, File exe) {
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
||||||
def testC = sourceS.absolutePath.replace(".kt.S", "-test.c")
|
def testC = sourceS.absolutePath.replace(".kt.S", "-test.c")
|
||||||
project.exec {
|
project.execClang {
|
||||||
commandLine "${llvmC}", "-B${project.sysrootDir}/usr/bin", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(),
|
commandLine "clang", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(),
|
||||||
"-o", "${exe.absolutePath}"
|
"-o", "${exe.absolutePath}"
|
||||||
|
|
||||||
args llvmCAdditionalArgs
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RunKonanTest extends KonanTest {
|
class RunKonanTest extends KonanTest {
|
||||||
void compileTest(File sourceS, File runtimeS, File exe) {
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
||||||
project.exec {
|
project.execClang {
|
||||||
commandLine "${llvmC}", "-DRUN_TEST", "-B${project.sysrootDir}/usr/bin", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(),
|
commandLine "clang", "-DRUN_TEST", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}", linkDl(),
|
||||||
"-o", "${exe.absolutePath}"
|
"-o", "${exe.absolutePath}"
|
||||||
|
|
||||||
args llvmCAdditionalArgs
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -11,15 +11,22 @@ allprojects {
|
|||||||
evaluationDependsOn(":dependencies")
|
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) {
|
plugins.withType(NativeComponentPlugin) {
|
||||||
model {
|
model {
|
||||||
toolChains {
|
toolChains {
|
||||||
clang(Clang) {
|
clang(Clang) {
|
||||||
path "$llvmDir/bin"
|
clangPath.each {
|
||||||
|
path it
|
||||||
|
}
|
||||||
|
|
||||||
eachPlatform { // TODO: will not work when cross-compiling
|
eachPlatform { // TODO: will not work when cross-compiling
|
||||||
[cCompiler, cppCompiler, linker].each {
|
[cCompiler, cppCompiler, linker].each {
|
||||||
it.withArguments { it << "--sysroot=$sysrootDir"}
|
it.withArguments { it.addAll(clangArgs) }
|
||||||
it.withArguments { it << "-B$sysrootDir/usr/bin"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,13 +68,11 @@ class CompileCppToBitcode extends DefaultTask {
|
|||||||
File objDir = this.getObjDir()
|
File objDir = this.getObjDir()
|
||||||
objDir.mkdirs()
|
objDir.mkdirs()
|
||||||
|
|
||||||
project.exec {
|
project.execClang {
|
||||||
workingDir objDir
|
workingDir objDir
|
||||||
executable "$project.llvmDir/bin/clang++"
|
executable "clang++"
|
||||||
args '-std=c++11'
|
args '-std=c++11'
|
||||||
|
|
||||||
args "--sysroot=$project.sysrootDir"
|
|
||||||
|
|
||||||
args compilerArgs
|
args compilerArgs
|
||||||
|
|
||||||
args "-I$headersDir"
|
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
|
args "-linker:" + linker
|
||||||
}
|
}
|
||||||
|
|
||||||
String sysrootArg = "--sysroot=$project.sysrootDir"
|
// TODO: the interop plugin should probably be reworked to execute clang from build scripts directly
|
||||||
compilerOpts += sysrootArg
|
environment['PATH'] = project.files(project.clangPath).asPath +
|
||||||
linkerOpts += sysrootArg
|
File.pathSeparator + environment['PATH']
|
||||||
|
|
||||||
|
compilerOpts += project.clangArgs
|
||||||
|
linkerOpts += project.clangArgs
|
||||||
|
|
||||||
args compilerOpts.collect { "-copt:$it" }
|
args compilerOpts.collect { "-copt:$it" }
|
||||||
args linkerOpts.collect { "-lopt:$it" }
|
args linkerOpts.collect { "-lopt:$it" }
|
||||||
|
|||||||
Reference in New Issue
Block a user