diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy new file mode 100644 index 00000000000..2cc4f2ab0c7 --- /dev/null +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/CompileToBitcode.groovy @@ -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 compilerArgs = [] + private List 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 getCompilerArgs() { + return compilerArgs + } + + private List 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 compilerArgs = this.getCompilerArgs() + List 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 + } + } +} diff --git a/runtime/build.gradle b/runtime/build.gradle index 9ec91384345..2923fa46d46 100644 --- a/runtime/build.gradle +++ b/runtime/build.gradle @@ -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