runtime/build.gradle: extract bitcode compilation to custom task class
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package org.jetbrains.kotlin
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.InputDirectory
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
class CompileCppToBitcode extends DefaultTask {
|
||||
private String name = "main"
|
||||
private File srcRoot;
|
||||
|
||||
private List<String> compilerArgs = []
|
||||
private List<String> linkerArgs = []
|
||||
|
||||
@InputDirectory
|
||||
File getSrcRoot() {
|
||||
return srcRoot ?: project.file("src/$name")
|
||||
}
|
||||
|
||||
@OutputFile
|
||||
File getOutFile() {
|
||||
return new File(project.buildDir, "${name}.bc")
|
||||
}
|
||||
|
||||
private File getSrcDir() {
|
||||
return new File(this.getSrcRoot(), "cpp")
|
||||
}
|
||||
|
||||
private File getHeadersDir() {
|
||||
return new File(this.getSrcRoot(), "headers")
|
||||
}
|
||||
|
||||
private File getObjDir() {
|
||||
return new File(project.buildDir, name)
|
||||
}
|
||||
|
||||
void name(String value) {
|
||||
name = value
|
||||
}
|
||||
|
||||
void srcRoot(File value) {
|
||||
srcRoot = value
|
||||
}
|
||||
|
||||
private List<String> getCompilerArgs() {
|
||||
return compilerArgs
|
||||
}
|
||||
|
||||
private List<String> getLinkerArgs() {
|
||||
return linkerArgs
|
||||
}
|
||||
|
||||
void compilerArgs(String... args) {
|
||||
compilerArgs.addAll(args)
|
||||
}
|
||||
|
||||
void linkerArgs(String... args) {
|
||||
linkerArgs.addAll(args)
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
void compile() {
|
||||
// the strange code below seems to be required due to some Gradle (Groovy?) behaviour
|
||||
File headersDir = this.getHeadersDir()
|
||||
File srcDir = this.getSrcDir()
|
||||
List<String> compilerArgs = this.getCompilerArgs()
|
||||
List<String> linkerArgs = this.getLinkerArgs()
|
||||
File objDir = this.getObjDir()
|
||||
objDir.mkdirs()
|
||||
|
||||
project.exec {
|
||||
workingDir objDir
|
||||
executable "$project.llvmInstallPath/bin/clang++"
|
||||
args '-std=c++11'
|
||||
|
||||
args compilerArgs
|
||||
|
||||
args "-I$headersDir"
|
||||
|
||||
args '-c', '-emit-llvm'
|
||||
args project.fileTree(srcDir).include('**/*.cpp')
|
||||
}
|
||||
|
||||
project.exec {
|
||||
executable "$project.llvmInstallPath/bin/llvm-link"
|
||||
args project.fileTree(objDir).include('**/*.bc')
|
||||
|
||||
args linkerArgs
|
||||
|
||||
args '-o', outFile
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-33
@@ -1,39 +1,12 @@
|
||||
import org.jetbrains.kotlin.CompileCppToBitcode
|
||||
|
||||
// TODO: consider using some Gradle plugins to build and test
|
||||
|
||||
def srcDir = 'src/main/cpp'
|
||||
def objDir = "$buildDir/bitcode"
|
||||
def outFile = "$buildDir/runtime.bc"
|
||||
|
||||
task compile(type: Exec) {
|
||||
doFirst {
|
||||
new File(objDir).mkdirs()
|
||||
}
|
||||
task build(type: CompileCppToBitcode) {
|
||||
name 'runtime'
|
||||
srcRoot file('src/main')
|
||||
|
||||
inputs.dir srcDir
|
||||
outputs.dir objDir
|
||||
|
||||
workingDir objDir
|
||||
|
||||
executable "$llvmInstallPath/bin/clang"
|
||||
args '-std=c++11'
|
||||
|
||||
args '-c', '-emit-llvm'
|
||||
|
||||
args fileTree(srcDir).include('**/*.cpp')
|
||||
}
|
||||
|
||||
task build(type: Exec) {
|
||||
dependsOn compile
|
||||
|
||||
inputs.dir objDir
|
||||
outputs.file outFile
|
||||
|
||||
executable "$llvmInstallPath/bin/llvm-link"
|
||||
args '-o', outFile
|
||||
|
||||
doFirst {
|
||||
args fileTree(objDir).include('**/*.bc')
|
||||
}
|
||||
}
|
||||
|
||||
task test {
|
||||
@@ -44,7 +17,7 @@ task test {
|
||||
commandLine "$llvmInstallPath/bin/clang", 'src/test/c/main.c', '-c', '-emit-llvm', '-o', "$buildDir/main.bc"
|
||||
}
|
||||
exec {
|
||||
commandLine "$llvmInstallPath/bin/clang++", outFile, "$buildDir/main.bc", '-o', "$buildDir/main"
|
||||
commandLine "$llvmInstallPath/bin/clang++", build.outFile, "$buildDir/main.bc", '-o', "$buildDir/main"
|
||||
}
|
||||
exec {
|
||||
workingDir buildDir
|
||||
|
||||
Reference in New Issue
Block a user