Uniform compiler/linker options treatment (#614)

This commit is contained in:
Nikolay Igotti
2017-05-29 15:37:15 +03:00
committed by GitHub
parent c9ce175224
commit ff8bce79d5
14 changed files with 43 additions and 46 deletions
@@ -34,6 +34,15 @@ fun main(args: Array<String>) {
processLib(konanHome, substitutions, parseArgs(args)) 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>> { private fun parseArgs(args: Array<String>): Map<String, List<String>> {
val commandLine = mutableMapOf<String, MutableList<String>>() val commandLine = mutableMapOf<String, MutableList<String>>()
for (index in 0..args.size - 1 step 2) { 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) { if (index + 1 == args.size) {
throw IllegalArgumentException("Expected an value after $key") throw IllegalArgumentException("Expected an value after $key")
} }
val value = args[index + 1] val value = args[index + 1].asArgList(key)
commandLine[key]?.add(value) ?: commandLine.put(key, mutableListOf(value)) commandLine[key]?.addAll(value) ?: commandLine.put(key, value.toMutableList())
} }
return commandLine return commandLine
} }
@@ -273,8 +282,8 @@ private fun usage() {
Run interop tool with -def <def_file_for_lib>.def Run interop tool with -def <def_file_for_lib>.def
Following flags are supported: Following flags are supported:
-def <file>.def specifies library definition file -def <file>.def specifies library definition file
-copt <c compiler flags> specifies flags passed to clang -compilerOpts <c compiler flags> specifies flags passed to clang
-lopt <linker flags> specifies flags passed to linker -linkerOpts <linker flags> specifies flags passed to linker
-verbose <boolean> increases verbosity -verbose <boolean> increases verbosity
-shims <boolean> adds generation of shims tracing native library calls -shims <boolean> adds generation of shims tracing native library calls
-pkg <fully qualified package name> place the resulting definitions into the package -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 llvmHome = konanProperties.getHostSpecific("llvmHome")!!
val llvmInstallPath = "$dependencies/$llvmHome" val llvmInstallPath = "$dependencies/$llvmHome"
val additionalHeaders = args["-h"].orEmpty() val additionalHeaders = args["-h"].orEmpty()
val additionalCompilerOpts = args["-copt"].orEmpty() val additionalCompilerOpts = args["-copt"].orEmpty()+ args["-compilerOpts"].orEmpty()
val additionalLinkerOpts = args["-lopt"].orEmpty() val additionalLinkerOpts = args["-lopt"].orEmpty() + args["-linkerOpts"].orEmpty()
val generateShims = args["-shims"].isTrue() val generateShims = args["-shims"].isTrue()
val verbose = args["-verbose"].isTrue() val verbose = args["-verbose"].isTrue()
val defaultOpts = konanProperties.defaultCompilerOpts(target, dependencies) val defaultOpts = konanProperties.defaultCompilerOpts(target, dependencies)
val headerFiles = config.getSpaceSeparated("headers") + additionalHeaders val headerFiles = config.getSpaceSeparated("headers") + additionalHeaders
val compilerOpts = val compilerOpts = config.getSpaceSeparated("compilerOpts") + defaultOpts + additionalCompilerOpts
config.getSpaceSeparated("compilerOpts") +
defaultOpts + additionalCompilerOpts
val compiler = "clang" val compiler = "clang"
val language = Language.C val language = Language.C
val excludeSystemLibs = config.getProperty("excludeSystemLibs")?.toBoolean() ?: false val excludeSystemLibs = config.getProperty("excludeSystemLibs")?.toBoolean() ?: false
@@ -334,8 +341,7 @@ private fun processLib(konanHome: String,
val entryPoint = config.getSpaceSeparated("entryPoint").atMostOne() val entryPoint = config.getSpaceSeparated("entryPoint").atMostOne()
val linkerOpts = val linkerOpts =
config.getSpaceSeparated("linkerOpts").toTypedArray() + config.getSpaceSeparated("linkerOpts").toTypedArray() + defaultOpts + additionalLinkerOpts
defaultOpts + additionalLinkerOpts
val linker = args["-linker"]?.atMostOne() ?: config.getProperty("linker") ?: "clang" val linker = args["-linker"]?.atMostOne() ?: config.getProperty("linker") ?: "clang"
val excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet() val excludedFunctions = config.getSpaceSeparated("excludedFunctions").toSet()
@@ -1,3 +1,4 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * 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") @Argument(value = "-nopack", description = "Don't pack the library into a klib file")
public boolean nopack; 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") @Argument(value = "-nostdlib", description = "Don't link with stdlib")
public boolean nostdlib; public boolean nostdlib;
@@ -315,7 +315,7 @@ class RunInteropKonanTest extends KonanTest {
List<String> linkerArguments = interopConf.linkerOpts // TODO: add arguments from .def file List<String> linkerArguments = interopConf.linkerOpts // TODO: add arguments from .def file
List<String> compilerArguments = ["-library", interopBc, "-nativelibrary", interopStubsBc] + List<String> compilerArguments = ["-library", interopBc, "-nativelibrary", interopStubsBc] +
linkerArguments.collectMany { ["-linkerArgs", it] } linkerArguments.collectMany { ["-linkerOpts", it] }
runCompiler(filesToCompile, exe, compilerArguments) runCompiler(filesToCompile, exe, compilerArguments)
} }
+1 -1
View File
@@ -25,6 +25,6 @@ var=CLANG_${TARGET}
CLANG=${!var} CLANG=${!var}
$CLANG -std=c++11 -c $DIR/MessageChannel.cpp -o $DIR/MessageChannel.bc -emit-llvm || exit 1 $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 \ konanc $DIR/Concurrent.kt -library $DIR/MessageChannel \
-nativelibrary $DIR/MessageChannel.bc -o Concurrent || exit 1 -nativelibrary $DIR/MessageChannel.bc -o Concurrent || exit 1
+1 -1
View File
@@ -18,5 +18,5 @@ LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET} var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. 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 konanc $COMPILER_ARGS -target $TARGET $DIR/CsvParser.kt -library stdio -o CsvParser || exit 1
+1 -1
View File
@@ -26,5 +26,5 @@ LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET} var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. 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 konanc -target $TARGET src -library libgit2 -linkerArgs "$LINKER_ARGS" -o GitChurn || exit 1
+5 -6
View File
@@ -24,12 +24,11 @@ LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET} var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. 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..." 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 \ cinterop -J-Xmx8g -compilerOpts "$IPREFIX/atk-1.0 $IPREFIX/gdk-pixbuf-2.0 $IPREFIX/cairo $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 \ -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 \
-copt $IPREFIX/gtk-3.0 -copt $IPREFIX/glib-2.0 -def $DIR/gtk3.def \ $IPREFIX/gtk-3.0 $IPREFIX/glib-2.0" -def $DIR/gtk3.def \
-target $TARGET -o $DIR/gtk3 || exit 1 -target $TARGET -o $DIR/gtk3 || exit 1
fi 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
+4 -4
View File
@@ -3,8 +3,8 @@
PATH=../../dist/bin:../../bin:$PATH PATH=../../dist/bin:../../bin:$PATH
DIR=. DIR=.
CFLAGS_macbook=-I/opt/local/include CFLAGS_macbook="-I /opt/local/include"
CFLAGS_linux=-I/usr/include/x86_64-linux-gnu CFLAGS_linux="-I /usr/include -I /usr/include/x86_64-linux-gnu"
LINKER_ARGS_macbook="-L/opt/local/lib -lcurl" LINKER_ARGS_macbook="-L/opt/local/lib -lcurl"
LINKER_ARGS_linux="-L/usr/lib/x86_64-linux-gnu -lcurl" LINKER_ARGS_linux="-L/usr/lib/x86_64-linux-gnu -lcurl"
@@ -23,5 +23,5 @@ LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET} var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. 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 cinterop -compilerOpts "$CFLAGS" -def $DIR/libcurl.def -target $TARGET -o libcurl || exit 1
konanc -target $TARGET src -library libcurl -linkerArgs "$LINKER_ARGS" -o Curl || exit 1 konanc -target $TARGET src -library libcurl -linkerOpts "$LINKER_ARGS" -o Curl || exit 1
+1 -1
View File
@@ -22,4 +22,4 @@ var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. COMPILER_ARGS=${!var} # add -opt for an optimized build.
cinterop -def $DIR/opengl.def -target $TARGET -o opengl || exit 1 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
+4 -4
View File
@@ -5,8 +5,8 @@ DIR=.
TF_TARGET_DIRECTORY="$HOME/.konan/third-party/tensorflow" TF_TARGET_DIRECTORY="$HOME/.konan/third-party/tensorflow"
TF_TYPE="cpu" # Change to "gpu" for GPU support TF_TYPE="cpu" # Change to "gpu" for GPU support
CFLAGS_macbook="-I${TF_TARGET_DIRECTORY}/include" CFLAGS_macbook="-I ${TF_TARGET_DIRECTORY}/include"
CFLAGS_linux="-I${TF_TARGET_DIRECTORY}/include" CFLAGS_linux="-I ${TF_TARGET_DIRECTORY}/include"
if [ x$TARGET == x ]; then if [ x$TARGET == x ]; then
case "$OSTYPE" in case "$OSTYPE" in
@@ -31,8 +31,8 @@ if [ ! -d $TF_TARGET_DIRECTORY/include/tensorflow ]; then
tar -C $TF_TARGET_DIRECTORY -xz tar -C $TF_TARGET_DIRECTORY -xz
fi 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 \ 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." 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."
+3 -4
View File
@@ -4,7 +4,7 @@ PATH=../../dist/bin:../../bin:$PATH
DIR=. DIR=.
DEPS=$(dirname `type -p konanc`)/../dependencies 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" LINKER_ARGS_macbook="-F $HOME/Library/Frameworks -framework SDL2"
COMPILER_ARGS_macbook= COMPILER_ARGS_macbook=
# Uncomment this if your path to SDL differs from the one above. # Uncomment this if your path to SDL differs from the one above.
@@ -42,6 +42,5 @@ LINKER_ARGS=${!var}
var=COMPILER_ARGS_${TARGET} var=COMPILER_ARGS_${TARGET}
COMPILER_ARGS=${!var} # add -opt for an optimized build. COMPILER_ARGS=${!var} # add -opt for an optimized build.
cinterop -def $DIR/sdl.def -copt "$CFLAGS" -target $TARGET -o sdl || exit 1 cinterop -def $DIR/sdl.def -compilerOpts "$CFLAGS" -target $TARGET -o sdl || exit 1
konanc $COMPILER_ARGS -target $TARGET $DIR/Tetris.kt -library sdl -linkerArgs "$LINKER_ARGS" -o Tetris || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/Tetris.kt -library sdl -linkerOpts "$LINKER_ARGS" -o Tetris || exit 1
#strip Tetris.kexe
@@ -76,8 +76,6 @@ open class KonanCompilerConfig(
KonanCompileTask::class.java KonanCompileTask::class.java
) { it.initialize(this@KonanCompilerConfig.name) } ) { it.initialize(this@KonanCompilerConfig.name) }
protected fun String.toAbsolutePath() = project.file(this).canonicalPath
// DSL methods -------------------------------------------------- // DSL methods --------------------------------------------------
// TODO: Check if we copied all data or not // TODO: Check if we copied all data or not
@@ -94,7 +94,7 @@ open class KonanCompileTask: DefaultTask() {
addFileArgs("-nativelibrary", nativeLibraries) addFileArgs("-nativelibrary", nativeLibraries)
addArg("-produce", produce) addArg("-produce", produce)
addListArg("-linkerArgs", linkerOpts) addListArg("-linkerOpts", linkerOpts)
addArgIfNotNull("-target", target) addArgIfNotNull("-target", target)
addArgIfNotNull("-language-version", languageVersion) 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? // TODO: Create default config? what about test sources?
override fun apply(project: ProjectInternal?) { override fun apply(project: ProjectInternal?) {
if (project == null) { return } if (project == null) { return }