Uniform compiler/linker options treatment (#614)
This commit is contained in:
+17
-11
@@ -34,6 +34,15 @@ fun main(args: Array<String>) {
|
||||
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("(?<!\\\\)\\Q \\E")).filter { it.isNotEmpty() }.map { it.replace("\\ ", " ") }
|
||||
else
|
||||
listOf(this)
|
||||
|
||||
private fun parseArgs(args: Array<String>): Map<String, List<String>> {
|
||||
val commandLine = mutableMapOf<String, MutableList<String>>()
|
||||
for (index in 0..args.size - 1 step 2) {
|
||||
@@ -44,8 +53,8 @@ private fun parseArgs(args: Array<String>): Map<String, List<String>> {
|
||||
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_file_for_lib>.def
|
||||
Following flags are supported:
|
||||
-def <file>.def specifies library definition file
|
||||
-copt <c compiler flags> specifies flags passed to clang
|
||||
-lopt <linker flags> specifies flags passed to linker
|
||||
-compilerOpts <c compiler flags> specifies flags passed to clang
|
||||
-linkerOpts <linker flags> specifies flags passed to linker
|
||||
-verbose <boolean> increases verbosity
|
||||
-shims <boolean> adds generation of shims tracing native library calls
|
||||
-pkg <fully qualified package name> 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()
|
||||
|
||||
|
||||
@@ -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 = "<arg>", description = "Pass arguments to linker", delimiter = " ")
|
||||
public String[] linkerArguments;
|
||||
|
||||
@Argument(value = "-nostdlib", description = "Don't link with stdlib")
|
||||
public boolean nostdlib;
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ class RunInteropKonanTest extends KonanTest {
|
||||
List<String> linkerArguments = interopConf.linkerOpts // TODO: add arguments from .def file
|
||||
|
||||
List<String> compilerArguments = ["-library", interopBc, "-nativelibrary", interopStubsBc] +
|
||||
linkerArguments.collectMany { ["-linkerArgs", it] }
|
||||
linkerArguments.collectMany { ["-linkerOpts", it] }
|
||||
|
||||
runCompiler(filesToCompile, exe, compilerArguments)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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."
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user