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 11c54b58b0f..befbd1eae00 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 @@ -34,6 +34,15 @@ fun main(args: Array) { processLib(konanHome, substitutions, parseArgs(args)) } +// Options, whose values are space-separated and can be escaped. +val escapedOptions = setOf("-compilerOpts", "-linkerOpts") + +private fun String.asArgList(key: String) = + if (escapedOptions.contains(key)) + this.split(Regex("(?): Map> { val commandLine = mutableMapOf>() for (index in 0..args.size - 1 step 2) { @@ -44,8 +53,8 @@ private fun parseArgs(args: Array): Map> { if (index + 1 == args.size) { throw IllegalArgumentException("Expected an value after $key") } - val value = args[index + 1] - commandLine[key]?.add(value) ?: commandLine.put(key, mutableListOf(value)) + val value = args[index + 1].asArgList(key) + commandLine[key]?.addAll(value) ?: commandLine.put(key, value.toMutableList()) } return commandLine } @@ -273,8 +282,8 @@ private fun usage() { Run interop tool with -def .def Following flags are supported: -def .def specifies library definition file - -copt specifies flags passed to clang - -lopt specifies flags passed to linker + -compilerOpts specifies flags passed to clang + -linkerOpts specifies flags passed to linker -verbose increases verbosity -shims adds generation of shims tracing native library calls -pkg place the resulting definitions into the package @@ -317,16 +326,14 @@ private fun processLib(konanHome: String, val llvmHome = konanProperties.getHostSpecific("llvmHome")!! val llvmInstallPath = "$dependencies/$llvmHome" val additionalHeaders = args["-h"].orEmpty() - val additionalCompilerOpts = args["-copt"].orEmpty() - val additionalLinkerOpts = args["-lopt"].orEmpty() + val additionalCompilerOpts = args["-copt"].orEmpty()+ args["-compilerOpts"].orEmpty() + val additionalLinkerOpts = args["-lopt"].orEmpty() + args["-linkerOpts"].orEmpty() val generateShims = args["-shims"].isTrue() val verbose = args["-verbose"].isTrue() val defaultOpts = konanProperties.defaultCompilerOpts(target, dependencies) val headerFiles = config.getSpaceSeparated("headers") + additionalHeaders - val compilerOpts = - config.getSpaceSeparated("compilerOpts") + - defaultOpts + additionalCompilerOpts + val compilerOpts = config.getSpaceSeparated("compilerOpts") + defaultOpts + additionalCompilerOpts val compiler = "clang" val language = Language.C val excludeSystemLibs = config.getProperty("excludeSystemLibs")?.toBoolean() ?: false @@ -334,8 +341,7 @@ private fun processLib(konanHome: String, val entryPoint = config.getSpaceSeparated("entryPoint").atMostOne() val linkerOpts = - config.getSpaceSeparated("linkerOpts").toTypedArray() + - defaultOpts + additionalLinkerOpts + config.getSpaceSeparated("linkerOpts").toTypedArray() + defaultOpts + additionalLinkerOpts val linker = args["-linker"]?.atMostOne() ?: config.getProperty("linker") ?: "clang" val excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet() diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java index 193df0f347a..4e07d4ed12a 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java @@ -1,3 +1,4 @@ + /* * Copyright 2010-2017 JetBrains s.r.o. * @@ -48,6 +49,9 @@ public class K2NativeCompilerArguments extends CommonCompilerArguments { @Argument(value = "-nopack", description = "Don't pack the library into a klib file") public boolean nopack; + @Argument(value = "-linkerOpts", valueDescription = "", description = "Pass arguments to linker", delimiter = " ") + public String[] linkerArguments; + @Argument(value = "-nostdlib", description = "Don't link with stdlib") public boolean nostdlib; diff --git a/buildSrc/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy b/buildSrc/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy index df511ad8503..adf8f9dc39a 100644 --- a/buildSrc/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy +++ b/buildSrc/src/main/groovy/org/jetbrains/kotlin/KonanTest.groovy @@ -315,7 +315,7 @@ class RunInteropKonanTest extends KonanTest { List linkerArguments = interopConf.linkerOpts // TODO: add arguments from .def file List compilerArguments = ["-library", interopBc, "-nativelibrary", interopStubsBc] + - linkerArguments.collectMany { ["-linkerArgs", it] } + linkerArguments.collectMany { ["-linkerOpts", it] } runCompiler(filesToCompile, exe, compilerArguments) } diff --git a/samples/concurrent/build.sh b/samples/concurrent/build.sh index 9bf3dbfc95b..271d65e580d 100755 --- a/samples/concurrent/build.sh +++ b/samples/concurrent/build.sh @@ -25,6 +25,6 @@ var=CLANG_${TARGET} CLANG=${!var} $CLANG -std=c++11 -c $DIR/MessageChannel.cpp -o $DIR/MessageChannel.bc -emit-llvm || exit 1 -cinterop -def $DIR/MessageChannel.def -copt "-I$DIR" -target $TARGET -o $DIR/MessageChannel || exit 1 +cinterop -def $DIR/MessageChannel.def -compilerOpts "-I$DIR" -target $TARGET -o $DIR/MessageChannel || exit 1 konanc $DIR/Concurrent.kt -library $DIR/MessageChannel \ -nativelibrary $DIR/MessageChannel.bc -o Concurrent || exit 1 diff --git a/samples/csvparser/build.sh b/samples/csvparser/build.sh index 7c9d550acab..c49572d47e7 100755 --- a/samples/csvparser/build.sh +++ b/samples/csvparser/build.sh @@ -18,5 +18,5 @@ LINKER_ARGS=${!var} var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. -cinterop -def $DIR/stdio.def -copt "$CFLAGS" -target $TARGET -o stdio || exit 1 +cinterop -def $DIR/stdio.def -compilerOpts "$CFLAGS" -target $TARGET -o stdio || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/CsvParser.kt -library stdio -o CsvParser || exit 1 diff --git a/samples/gitchurn/build.sh b/samples/gitchurn/build.sh index d6c78a07e12..12fedbabe89 100755 --- a/samples/gitchurn/build.sh +++ b/samples/gitchurn/build.sh @@ -26,5 +26,5 @@ LINKER_ARGS=${!var} var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. -cinterop -copt $CFLAGS -def $DIR/libgit2.def -target $TARGET -o libgit2 || exit 1 +cinterop -compilerOpts $CFLAGS -def $DIR/libgit2.def -target $TARGET -o libgit2 || exit 1 konanc -target $TARGET src -library libgit2 -linkerArgs "$LINKER_ARGS" -o GitChurn || exit 1 diff --git a/samples/gtk/build.sh b/samples/gtk/build.sh index 492bcf1d7d0..ee89162c940 100755 --- a/samples/gtk/build.sh +++ b/samples/gtk/build.sh @@ -24,12 +24,11 @@ LINKER_ARGS=${!var} var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. -if [ ! -f $DIR/gtk3.bc ]; then +if [ ! -f $DIR/gtk3.klib ]; then echo "Generating GTK stubs (once), may take few mins depending on the hardware..." - cinterop -J-Xmx8g -copt $IPREFIX/atk-1.0 -copt $IPREFIX/gdk-pixbuf-2.0 -copt $IPREFIX/cairo -copt $IPREFIX/pango-1.0 \ - -copt -I/opt/local/lib/glib-2.0/include -copt -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -copt -I/usr/local/lib/glib-2.0/include \ - -copt $IPREFIX/gtk-3.0 -copt $IPREFIX/glib-2.0 -def $DIR/gtk3.def \ + cinterop -J-Xmx8g -compilerOpts "$IPREFIX/atk-1.0 $IPREFIX/gdk-pixbuf-2.0 $IPREFIX/cairo $IPREFIX/pango-1.0 \ + -I/opt/local/lib/glib-2.0/include -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/local/lib/glib-2.0/include \ + $IPREFIX/gtk-3.0 $IPREFIX/glib-2.0" -def $DIR/gtk3.def \ -target $TARGET -o $DIR/gtk3 || exit 1 fi -konanc -target $TARGET $DIR/src -library $DIR/gtk3 -linkerArgs "$LINKER_ARGS" -o $DIR/Gtk3Demo || exit 1 - +konanc -target $TARGET $DIR/src -library $DIR/gtk3 -linkerOpts "$LINKER_ARGS" -o $DIR/Gtk3Demo || exit 1 diff --git a/samples/libcurl/build.sh b/samples/libcurl/build.sh index d3a2e00d2e7..f24991db454 100755 --- a/samples/libcurl/build.sh +++ b/samples/libcurl/build.sh @@ -3,8 +3,8 @@ PATH=../../dist/bin:../../bin:$PATH DIR=. -CFLAGS_macbook=-I/opt/local/include -CFLAGS_linux=-I/usr/include/x86_64-linux-gnu +CFLAGS_macbook="-I /opt/local/include" +CFLAGS_linux="-I /usr/include -I /usr/include/x86_64-linux-gnu" LINKER_ARGS_macbook="-L/opt/local/lib -lcurl" LINKER_ARGS_linux="-L/usr/lib/x86_64-linux-gnu -lcurl" @@ -23,5 +23,5 @@ LINKER_ARGS=${!var} var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. -cinterop -copt "$CFLAGS" -copt -I. -copt -I/usr/include -def $DIR/libcurl.def -target $TARGET -o libcurl || exit 1 -konanc -target $TARGET src -library libcurl -linkerArgs "$LINKER_ARGS" -o Curl || exit 1 +cinterop -compilerOpts "$CFLAGS" -def $DIR/libcurl.def -target $TARGET -o libcurl || exit 1 +konanc -target $TARGET src -library libcurl -linkerOpts "$LINKER_ARGS" -o Curl || exit 1 diff --git a/samples/opengl/build.sh b/samples/opengl/build.sh index bfa14a16f5a..d8b30ac73b6 100755 --- a/samples/opengl/build.sh +++ b/samples/opengl/build.sh @@ -22,4 +22,4 @@ var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. cinterop -def $DIR/opengl.def -target $TARGET -o opengl || exit 1 -konanc -target $TARGET $DIR/OpenGlTeapot.kt -library opengl -linkerArgs "$LINKER_ARGS" -o OpenGlTeapot || exit 1 +konanc -target $TARGET $DIR/OpenGlTeapot.kt -library opengl -linkerOpts "$LINKER_ARGS" -o OpenGlTeapot || exit 1 diff --git a/samples/tensorflow/build.sh b/samples/tensorflow/build.sh index 334545d25a2..155f9a647a2 100755 --- a/samples/tensorflow/build.sh +++ b/samples/tensorflow/build.sh @@ -5,8 +5,8 @@ DIR=. TF_TARGET_DIRECTORY="$HOME/.konan/third-party/tensorflow" TF_TYPE="cpu" # Change to "gpu" for GPU support -CFLAGS_macbook="-I${TF_TARGET_DIRECTORY}/include" -CFLAGS_linux="-I${TF_TARGET_DIRECTORY}/include" +CFLAGS_macbook="-I ${TF_TARGET_DIRECTORY}/include" +CFLAGS_linux="-I ${TF_TARGET_DIRECTORY}/include" if [ x$TARGET == x ]; then case "$OSTYPE" in @@ -31,8 +31,8 @@ if [ ! -d $TF_TARGET_DIRECTORY/include/tensorflow ]; then tar -C $TF_TARGET_DIRECTORY -xz fi -cinterop -def $DIR/tensorflow.def -copt "$CFLAGS" -target $TARGET -o tensorflow || exit 1 +cinterop -def $DIR/tensorflow.def -compilerOpts "$CFLAGS" -target $TARGET -o tensorflow || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/HelloTensorflow.kt -library tensorflow -o HelloTensorflow \ - -linkerArgs "-L$TF_TARGET_DIRECTORY/lib -ltensorflow" || exit 1 + -linkerOpts "-L$TF_TARGET_DIRECTORY/lib -ltensorflow" || exit 1 echo "Note: You may need to specify LD_LIBRARY_PATH or DYLD_LIBRARY_PATH env variables to $TF_TARGET_DIRECTORY/lib if the TensorFlow dynamic library cannot be found." diff --git a/samples/tetris/build.sh b/samples/tetris/build.sh index e94d1d27874..6cc40c15fb6 100755 --- a/samples/tetris/build.sh +++ b/samples/tetris/build.sh @@ -4,7 +4,7 @@ PATH=../../dist/bin:../../bin:$PATH DIR=. DEPS=$(dirname `type -p konanc`)/../dependencies -CFLAGS_macbook=-I$HOME/Library/Frameworks/SDL2.framework/Headers +CFLAGS_macbook="-I $HOME/Library/Frameworks/SDL2.framework/Headers" LINKER_ARGS_macbook="-F $HOME/Library/Frameworks -framework SDL2" COMPILER_ARGS_macbook= # Uncomment this if your path to SDL differs from the one above. @@ -42,6 +42,5 @@ LINKER_ARGS=${!var} var=COMPILER_ARGS_${TARGET} COMPILER_ARGS=${!var} # add -opt for an optimized build. -cinterop -def $DIR/sdl.def -copt "$CFLAGS" -target $TARGET -o sdl || exit 1 -konanc $COMPILER_ARGS -target $TARGET $DIR/Tetris.kt -library sdl -linkerArgs "$LINKER_ARGS" -o Tetris || exit 1 -#strip Tetris.kexe +cinterop -def $DIR/sdl.def -compilerOpts "$CFLAGS" -target $TARGET -o sdl || exit 1 +konanc $COMPILER_ARGS -target $TARGET $DIR/Tetris.kt -library sdl -linkerOpts "$LINKER_ARGS" -o Tetris || exit 1 diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerConfig.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerConfig.kt index 006fa34ac81..822fef59224 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerConfig.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerConfig.kt @@ -76,8 +76,6 @@ open class KonanCompilerConfig( KonanCompileTask::class.java ) { it.initialize(this@KonanCompilerConfig.name) } - protected fun String.toAbsolutePath() = project.file(this).canonicalPath - // DSL methods -------------------------------------------------- // TODO: Check if we copied all data or not diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerTask.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerTask.kt index 72b178c09b0..8aa3716f7cb 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerTask.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanCompilerTask.kt @@ -94,7 +94,7 @@ open class KonanCompileTask: DefaultTask() { addFileArgs("-nativelibrary", nativeLibraries) addArg("-produce", produce) - addListArg("-linkerArgs", linkerOpts) + addListArg("-linkerOpts", linkerOpts) addArgIfNotNull("-target", target) addArgIfNotNull("-language-version", languageVersion) diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanPlugin.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanPlugin.kt index 33f64f8a5c1..ff10caf6426 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanPlugin.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/KonanPlugin.kt @@ -153,15 +153,6 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR } } - private fun String.isSupported(): Boolean { - val os = CompilerDownloadTask.simpleOsName() - return when (os) { - "macos" -> this == "macbook" || this == "iphone" - "linux" -> this == "linux" || this == "raspberrypi" - else -> false - } - } - // TODO: Create default config? what about test sources? override fun apply(project: ProjectInternal?) { if (project == null) { return }