From 820a009163a3d31266cd62e7db802173a913a626 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Tue, 12 Sep 2017 12:36:55 +0300 Subject: [PATCH] Pass the list of static libraries from the cinterop tool to the compiler. --- .../kotlin/native/interop/gen/jvm/main.kt | 27 ++++++++++++------- .../kotlin/backend/konan/NativeFileType.kt | 27 +++++++++++++++++++ .../jetbrains/kotlin/cli/utilities/main.kt | 7 ++--- 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeFileType.kt 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 56e80431752..e3bb54423c0 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 @@ -23,14 +23,16 @@ import java.lang.IllegalArgumentException import java.util.* import kotlin.reflect.KFunction -fun main(args: Array) { +fun main(args: Array) = interop(args, null) + +fun interop(args: Array, argsToCompiler: MutableList? = null) { val konanHome = File(System.getProperty("konan.home")).absolutePath val substitutions = mapOf( "arch" to (System.getenv("TARGET_ARCH") ?: "x86-64"), "os" to (System.getenv("TARGET_OS") ?: host) ) - processLib(konanHome, substitutions, parseArgs(args)) + processLib(konanHome, substitutions, parseArgs(args), argsToCompiler) } // Options, whose values are space-separated and can be escaped. @@ -337,10 +339,8 @@ private fun selectNativeLanguage(config: Properties): Language { error("Unexpected language '$language'. Possible values are: ${languages.keys.joinToString { "'$it'" }}") } -private fun resolveLibraries(staticLibraries: List, libraryPaths: List) { +private fun resolveLibraries(staticLibraries: List, libraryPaths: List): List { val result = mutableListOf() - println("libs = $staticLibraries") - println("paths = $libraryPaths") staticLibraries.forEach { library -> libraryPaths.forEach forEachPath@ { path -> val combined = "$path/$library" @@ -350,12 +350,19 @@ private fun resolveLibraries(staticLibraries: List, libraryPaths: List, libraryPaths: List): List { + return resolveLibraries(staticLibraries, libraryPaths) + .map { it -> listOf("-includeBinary", it) } .flatten() +} + + private fun processLib(konanHome: String, substitutions: Map, - args: Map>) { + args: Map>, + argsToCompiler: MutableList?) { val userDir = System.getProperty("user.dir") val ktGenRoot = args["-generated"]?.single() ?: userDir @@ -418,9 +425,9 @@ private fun processLib(konanHome: String, config.getSpaceSeparated("linkerOpts").toTypedArray() + defaultOpts + additionalLinkerOpts val linker = args["-linker"]?.atMostOne() ?: config.getProperty("linker") ?: "clang" val excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet() - val staticLibraries = config.getSpaceSeparated("staticLibraries") + args["staticLibrary"].orEmpty() - val libraryPath = args["-libraryPath"] - val resolvedStaticLibraries = resolveLibraries(staticLibraries, libraryPath.orEmpty()) + val staticLibraries = config.getSpaceSeparated("staticLibraries") + args["-staticLibrary"].orEmpty() + val libraryPaths = args["-libraryPath"].orEmpty() + argsToCompiler ?. let { it.addAll(argsToCompiler(staticLibraries, libraryPaths)) } val fqParts = (args["-pkg"]?.atMostOne() ?: config.getProperty("package"))?.let { it.split('.') diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeFileType.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeFileType.kt new file mode 100644 index 00000000000..4629be89cdd --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/NativeFileType.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.backend.konan + + +val String.isJavaScript + get() = this.endsWith(".js") + +val String.isUnixStaticLib + get() = this.endsWith(".a") + +val String.isWindowsStaticLib + get() = this.endsWith(".lib") diff --git a/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/main.kt b/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/main.kt index aebe2f81636..fd32ca41d8a 100644 --- a/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/main.kt +++ b/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/main.kt @@ -2,7 +2,7 @@ package org.jetbrains.kotlin.cli.utilities import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.cli.bc.main as konancMain -import org.jetbrains.kotlin.native.interop.gen.jvm.main as cinteropMain +import org.jetbrains.kotlin.native.interop.gen.jvm.interop import org.jetbrains.kotlin.cli.klib.main as klibMain fun invokeCinterop(args: Array) { @@ -29,7 +29,8 @@ fun invokeCinterop(args: Array) { "-flavor", "native") val cinteropArgs = (additionalArgs + args.toList()).toTypedArray() - cinteropMain(cinteropArgs) + val cinteropArgsToCompiler = mutableListOf() + interop(cinteropArgs, cinteropArgsToCompiler) val konancArgs = arrayOf( generatedDir.path, @@ -38,7 +39,7 @@ fun invokeCinterop(args: Array) { "-o", outputFileName, "-target", target, "-manifest", manifest.path - ) + ) + cinteropArgsToCompiler konancMain(konancArgs) }