Merge pull request #6 from JetBrains/runtime-code-in-compiler
Import some runtime code to compiler using interop
This commit is contained in:
+3
-1
@@ -279,8 +279,10 @@ fun buildNativeIndexImpl(headerFile: File, args: List<String>): NativeIndex {
|
||||
indexEntityReference.setStatic(null)
|
||||
}
|
||||
|
||||
val commandLineArgs = if (args1.size != 0) mallocNativeArrayOf(Int8Box.Companion, *args1)[0] else null
|
||||
|
||||
clang_indexSourceFile(indexAction, clientData, callbacks, IndexerCallbacks.size, 0, headerFile.path,
|
||||
mallocNativeArrayOf(Int8Box.Companion, *args1)[0], args1.size, null, 0, null, 0)
|
||||
commandLineArgs, args1.size, null, 0, null, 0)
|
||||
|
||||
|
||||
return res
|
||||
|
||||
+1
-1
@@ -110,7 +110,7 @@ private fun processLib(ktGenRoot: String,
|
||||
val compilerOpts = config.getSpaceSeparated("compilerOpts") + additionalCompilerOpts
|
||||
val compiler = config.getProperty("compiler") ?: "clang"
|
||||
val linkerOpts = config.getSpaceSeparated("linkerOpts").toTypedArray() + additionalLinkerOpts
|
||||
val linker = config.getProperty("linker") ?: "clang"
|
||||
val linker = args["-linker"]?.singleOrNull() ?: config.getProperty("linker") ?: "clang"
|
||||
val excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet()
|
||||
|
||||
val fqParts = args["-pkg"]?.singleOrNull()?.let {
|
||||
|
||||
@@ -39,11 +39,26 @@ kotlinNativeInterop {
|
||||
compilerOpts "-I$llvmInstallPath/include"
|
||||
linkerOpts "-L$llvmInstallPath/lib"
|
||||
}
|
||||
|
||||
hash { // TODO: copy-pasted from ':common:compileHash'
|
||||
compilerOpts '-fPIC'
|
||||
linkerOpts '-fPIC'
|
||||
linker 'clang++'
|
||||
linkOutputs ':common:compileHash'
|
||||
|
||||
headers fileTree('../common/src/hash/headers') {
|
||||
include '**/*.h'
|
||||
include '**/*.hpp'
|
||||
}
|
||||
|
||||
pkg 'org.jetbrains.kotlin.backend.native.hash'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compilerCompile deps
|
||||
compilerCompile kotlinNativeInterop['llvm']
|
||||
compilerCompile kotlinNativeInterop['hash']
|
||||
|
||||
cli_bcCompile deps
|
||||
cli_bcCompile sourceSets.compiler.output
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named {
|
||||
|
||||
private List<String> compilerOpts = []
|
||||
private FileCollection headers;
|
||||
private String linker
|
||||
private List<String> linkerOpts = []
|
||||
private FileCollection linkFiles;
|
||||
private List<String> linkTasks = []
|
||||
@@ -48,6 +49,10 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named {
|
||||
headers = headers + files
|
||||
}
|
||||
|
||||
void linker(String value) {
|
||||
linker = value
|
||||
}
|
||||
|
||||
void linkerOpts(String... values) {
|
||||
linkerOpts.addAll(values)
|
||||
}
|
||||
@@ -161,6 +166,10 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named {
|
||||
args "-pkg:" + pkg
|
||||
}
|
||||
|
||||
if (linker != null) {
|
||||
args "-linker:" + linker
|
||||
}
|
||||
|
||||
args compilerOpts.collect { "-copt:$it" }
|
||||
args linkerOpts.collect { "-lopt:$it" }
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import org.jetbrains.kotlin.CompileCppToBitcode
|
||||
|
||||
// TODO: consider using some Gradle plugins to build and test
|
||||
|
||||
|
||||
task compileHash(type: CompileCppToBitcode) {
|
||||
name 'hash'
|
||||
}
|
||||
|
||||
task build {
|
||||
dependsOn compileHash
|
||||
}
|
||||
|
||||
task clean << {
|
||||
delete buildDir
|
||||
}
|
||||
+9
-33
@@ -1,39 +1,15 @@
|
||||
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')
|
||||
}
|
||||
dependsOn ':common:compileHash'
|
||||
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
|
||||
linkerArgs project.file('../common/build/hash.bc').path
|
||||
}
|
||||
|
||||
task test {
|
||||
@@ -44,7 +20,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
|
||||
|
||||
@@ -4,3 +4,4 @@ include ':Interop:Runtime'
|
||||
include ':InteropExample'
|
||||
include ':backend.native'
|
||||
include ':runtime'
|
||||
include ':common'
|
||||
|
||||
Reference in New Issue
Block a user