diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index 407a072495f..280be77d483 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -11,8 +11,7 @@ fun main(args: Array) { val ktGenRoot = args[0] val nativeLibsDir = args[1] - val defFile = args[2] - val otherArgs = args.drop(3) + val otherArgs = args.drop(2) // TODO: remove OSX defaults. val substitutions = mapOf( @@ -20,7 +19,7 @@ fun main(args: Array) { "os" to (System.getenv("TARGET_OS") ?: detectHost()) ) - processDefFile(File(defFile), ktGenRoot, nativeLibsDir, llvmInstallPath, substitutions, otherArgs) + processLib(ktGenRoot, nativeLibsDir, llvmInstallPath, substitutions, otherArgs) } private fun detectHost():String { @@ -56,11 +55,21 @@ private fun substitute(properties: Properties, substitutions: Map, - additionalArgs: List) { +private fun Properties.getSpaceSeparated(name: String): List { + return this.getProperty(name)?.split(' ') ?: emptyList() +} + +private fun processLib(ktGenRoot: String, + nativeLibsDir: String, + llvmInstallPath: String, + substitutions: Map, + additionalArgs: List) { + + val args = additionalArgs.groupBy ({ getArgPrefix(it)!! }, { dropPrefix(it)!! }) // TODO + + val defFile = args["-def"]?.single()?.let { File(it) } val config = Properties() - defFile.bufferedReader().use { reader -> + defFile?.bufferedReader()?.use { reader -> config.load(reader) } substitute(config, substitutions) - val additionalCompilerOpts = additionalArgs.mapNotNull { it.removePrefixOrNull("-copt:") } - val additionalLinkerOpts = additionalArgs.mapNotNull { it.removePrefixOrNull("-lopt:") } + val additionalHeaders = args["-h"].orEmpty() + val additionalCompilerOpts = args["-copt"].orEmpty() + val additionalLinkerOpts = args["-lopt"].orEmpty() - val headerFiles = config.getProperty("headers").split(' ') - val compilerOpts = config.getProperty("compilerOpts").split(' ') + additionalCompilerOpts - val compiler = config.getProperty("compiler") - val linkerOpts = config.getProperty("linkerOpts").split(' ').toTypedArray() + additionalLinkerOpts - val linker = config.getProperty("linker") - val excludedFunctions = config.getProperty("excludedFunctions")?.split(' ')?.toSet() ?: emptySet() + val headerFiles = config.getSpaceSeparated("headers") + additionalHeaders + 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 excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet() - val fqParts = defFile.name.split('.').reversed().drop(1) + val fqParts = args["-pkg"]?.singleOrNull()?.let { + it.split('.') + } ?: defFile!!.name.split('.').reversed().drop(1) val outKtFileName = fqParts.last() + ".kt" @@ -135,10 +154,10 @@ private fun processDefFile(defFile: File, *compilerArgsForJniIncludes, "-c", outCFile.path, "-o", outOFile.path) - val defFileDir = defFile.parentFile + val workDir = defFile?.parentFile ?: File(System.getProperty("java.io.tmpdir")) ProcessBuilder(*compilerCmd) - .directory(defFileDir) + .directory(workDir) .inheritIO() .runExpectingSuccess() @@ -150,7 +169,7 @@ private fun processDefFile(defFile: File, "-Wl,-flat_namespace,-undefined,dynamic_lookup") ProcessBuilder(*linkerCmd) - .directory(defFileDir) + .directory(workDir) .inheritIO() .runExpectingSuccess() diff --git a/backend.native/llvm.def b/backend.native/llvm.def index d55dd8dcecb..bcf757b61c5 100644 --- a/backend.native/llvm.def +++ b/backend.native/llvm.def @@ -1,7 +1,5 @@ headers = llvm-c/Core.h llvm-c/ExecutionEngine.h llvm-c/Target.h llvm-c/Analysis.h llvm-c/BitWriter.h llvm-c/BitReader.h -compiler = clang - compilerOpts = -std=c99 -fPIC \ -Wall -W -Wno-unused-parameter -Wwrite-strings -Wmissing-field-initializers \ -pedantic -Wno-long-long -Wcovered-switch-default -Wdelete-non-virtual-dtor \ diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy index 90599793962..1465a7964e7 100644 --- a/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy @@ -22,7 +22,10 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named { private String defFile + private String pkg; + private List compilerOpts = [] + private FileCollection headers; private List linkerOpts = [] private FileCollection linkFiles; private List linkTasks = [] @@ -32,10 +35,19 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named { genTask.inputs.file(project.file(defFile)) } + void pkg(String value) { + pkg = value + } + void compilerOpts(String... values) { compilerOpts.addAll(values) } + void headers(FileCollection files) { + dependsOnFiles(files) + headers = headers + files + } + void linkerOpts(String... values) { linkerOpts.addAll(values) } @@ -95,6 +107,7 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named { this.name = name this.project = project + this.headers = project.files() this.linkFiles = project.files() interopStubs = project.sourceSets.create(name + "InteropStubs") @@ -139,10 +152,21 @@ class NamedNativeInteropConfig extends AbstractFileCollection implements Named { linkerOpts += linkFiles.files - args = [generatedSrcDir, nativeLibsDir, project.file(defFile)] + args = [generatedSrcDir, nativeLibsDir] + if (defFile != null) { + args "-def:" + project.file(defFile) + } + + if (pkg != null) { + args "-pkg:" + pkg + } args compilerOpts.collect { "-copt:$it" } args linkerOpts.collect { "-lopt:$it" } + + headers.files.each { + args "-h:$it" + } } } }