Make interop tool generate one self-contained .kt.bc

This commit is contained in:
Svyatoslav Scherbina
2017-03-30 10:13:01 +03:00
committed by SvyatoslavScherbina
parent f564bd7707
commit ebced0aa2d
8 changed files with 44 additions and 18 deletions
+6 -6
View File
@@ -10,7 +10,7 @@ everything needed to interact with an external library.
Following workflow is expected when interacting with the native library. Following workflow is expected when interacting with the native library.
* create `.def` file describing what to include into bindings * create `.def` file describing what to include into bindings
* use `interop` tool to produce `stubs.bc` and Kotlin bindings * use `interop` tool to produce Kotlin bindings
* run _Kotlin N_ compiler on an application to produce the final executable * run _Kotlin N_ compiler on an application to produce the final executable
Interoperability tool analyses C headers and produces "natural" mapping of Interoperability tool analyses C headers and produces "natural" mapping of
@@ -24,11 +24,11 @@ Build the dependencies and the compiler (see `README.md`).
Prepare stubs for the system sockets library: Prepare stubs for the system sockets library:
cd samples/socket cd samples/socket
../../dist/bin/interop -def:sockets.def ../../dist/bin/interop -def:sockets.def -o:sockets.kt.bc
Compile the echo server: Compile the echo server:
../../dist/bin/kotlinc EchoServer.kt sockets -nativelibrary socketsstubs.bc \ ../../dist/bin/kotlinc EchoServer.kt -library sockets.kt.bc \
-o EchoServer.kexe -o EchoServer.kexe
This whole process is automated in `build.sh` script, which also support cross-compilation This whole process is automated in `build.sh` script, which also support cross-compilation
@@ -60,10 +60,10 @@ Structurally it's a simple property file, looking like this:
Then run interop tool with something like (note that for host libraries not included Then run interop tool with something like (note that for host libraries not included
in sysroot search paths for headers may be needed): in sysroot search paths for headers may be needed):
interop -def:zlib.def -copt:-I/opt/local/include interop -def:zlib.def -copt:-I/opt/local/include -o:zlib.kt.bc
This command will produce directory named `zlib` containing file `zlib.kt` This command will produce `zlib.kt.bc` compiled library and
and file `zlibstubs.bc` containing implementation specific glue bitcode. `zlib.kt.bc-build/kotlin` directory containing Kotlin source code for the library.
If behavior for certain platform shall be modified, one may use format like If behavior for certain platform shall be modified, one may use format like
`compilerOpts.osx` or `compilerOpts.linux` to provide platform-specific values `compilerOpts.osx` or `compilerOpts.linux` to provide platform-specific values
@@ -112,6 +112,14 @@ private fun Properties.getSpaceSeparated(name: String): List<String> {
return this.getProperty(name)?.split(' ')?.filter { it.isNotEmpty() } ?: emptyList() return this.getProperty(name)?.split(' ')?.filter { it.isNotEmpty() } ?: emptyList()
} }
private fun <T> Collection<T>.atMostOne(): T? {
return when (this.size) {
0 -> null
1 -> this.iterator().next()
else -> throw IllegalArgumentException("Collection has more than one element.")
}
}
private fun Properties.getOsSpecific(name: String, private fun Properties.getOsSpecific(name: String,
host: String = detectHost()): String? { host: String = detectHost()): String? {
@@ -323,7 +331,7 @@ private fun processLib(konanHome: String,
val outKtFileRelative = (fqParts + outKtFileName).joinToString("/") val outKtFileRelative = (fqParts + outKtFileName).joinToString("/")
val outKtFile = File(ktGenRoot, outKtFileRelative) val outKtFile = File(ktGenRoot, outKtFileRelative)
val libName = fqParts.joinToString("") + "stubs" val libName = args["-cstubsname"]?.atMostOne() ?: fqParts.joinToString("") + "stubs"
val library = NativeLibrary(headerFiles, compilerOpts, language, excludeSystemLibs) val library = NativeLibrary(headerFiles, compilerOpts, language, excludeSystemLibs)
val configuration = InteropConfiguration( val configuration = InteropConfiguration(
+19
View File
@@ -10,6 +10,8 @@ fi
declare -a java_args declare -a java_args
declare -a interop_args declare -a interop_args
OUTPUT_FILE_NAME=nativelib.kt.bc
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
-D*) -D*)
@@ -20,6 +22,10 @@ while [ $# -gt 0 ]; do
java_args=("${java_args[@]}" "${1:2}") java_args=("${java_args[@]}" "${1:2}")
shift shift
;; ;;
-o:*)
OUTPUT_FILE_NAME="${1:3}"
shift
;;
*) *)
interop_args=("${interop_args[@]}" "$1") interop_args=("${interop_args[@]}" "$1")
shift shift
@@ -54,11 +60,24 @@ INTEROP_TOOL=org.jetbrains.kotlin.native.interop.gen.jvm.MainKt
FLAVOR_ARG=-flavor:native FLAVOR_ARG=-flavor:native
GENERATED_DIR="$OUTPUT_FILE_NAME-build/kotlin"
GENERATED_ARG="-generated:$GENERATED_DIR"
NATIVES_DIR="$OUTPUT_FILE_NAME-build/natives"
NATIVES_ARG="-natives:$NATIVES_DIR"
CSTUBSNAME=cstubs
CSTUBSNAME_ARG="-cstubsname:$CSTUBSNAME"
LIBCLANG_DISABLE_CRASH_RECOVERY=1 \ LIBCLANG_DISABLE_CRASH_RECOVERY=1 \
LD_LIBRARY_PATH=${NATIVE_LIB} \ LD_LIBRARY_PATH=${NATIVE_LIB} \
$JAVACMD $JAVA_OPTS ${java_args[@]} \ $JAVACMD $JAVA_OPTS ${java_args[@]} \
-cp $INTEROP_CLASSPATH \ -cp $INTEROP_CLASSPATH \
$INTEROP_TOOL \ $INTEROP_TOOL \
"$GENERATED_ARG" "$NATIVES_ARG" "$CSTUBSNAME_ARG" \
$FLAVOR_ARG "${interop_args[@]}" \ $FLAVOR_ARG "${interop_args[@]}" \
|| exit 1
# Stubs may be rather big, so we may need more heap space.
XMX_ARG=-J-Xmx3G
$KONAN_HOME/bin/konanc $XMX_ARG "$GENERATED_DIR" -nolink -nativelibrary "$NATIVES_DIR/$CSTUBSNAME.bc" \
-o "$OUTPUT_FILE_NAME"
+2 -2
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.
interop -def:$DIR/stdio.def -copt:"$CFLAGS" -target:$TARGET || exit 1 interop -def:$DIR/stdio.def -copt:"$CFLAGS" -target:$TARGET -o:stdio.kt.bc || exit 1
konanc $COMPILER_ARGS -target $TARGET $DIR/CsvParser.kt stdio -nativelibrary stdiostubs.bc -o CsvParser.kexe || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/CsvParser.kt -library stdio.kt.bc -o CsvParser.kexe || exit 1
+2 -2
View File
@@ -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.
interop -copt:$CFLAGS -def:$DIR/libgit2.def -target:$TARGET || exit 1 interop -copt:$CFLAGS -def:$DIR/libgit2.def -target:$TARGET -o:libgit2.kt.bc || exit 1
konanc -target $TARGET src libgit2 -nativelibrary libgit2stubs.bc -linkerArgs "$LINKER_ARGS" -o GitChurn.kexe || exit 1 konanc -target $TARGET src -library libgit2.kt.bc -linkerArgs "$LINKER_ARGS" -o GitChurn.kexe || exit 1
+2 -3
View File
@@ -21,6 +21,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.
interop -def:$DIR/opengl.def -target:$TARGET || exit 1 interop -def:$DIR/opengl.def -target:$TARGET -o:opengl.kt.bc || exit 1
# OpenGL stubs are rather big, so we need more heap space. konanc -target $TARGET $DIR/OpenGlTeapot.kt -library opengl.kt.bc -linkerArgs "$LINKER_ARGS" -o OpenGlTeapot.kexe || exit 1
konanc -J-Xmx3G -target $TARGET opengl $DIR/OpenGlTeapot.kt -nativelibrary openglstubs.bc -linkerArgs "$LINKER_ARGS" -o OpenGlTeapot.kexe || exit 1
+2 -2
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.
interop -def:$DIR/sockets.def -copt:"$CFLAGS" -target:$TARGET || exit 1 interop -def:$DIR/sockets.def -copt:"$CFLAGS" -target:$TARGET -o:sockets.kt.bc || exit 1
konanc $COMPILER_ARGS -target $TARGET $DIR/EchoServer.kt sockets -nativelibrary socketsstubs.bc -o EchoServer.kexe || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/EchoServer.kt -library sockets.kt.bc -o EchoServer.kexe || exit 1
+2 -2
View File
@@ -39,6 +39,6 @@ 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.
interop -def:$DIR/sdl.def -copt:"$CFLAGS" -target:$TARGET || exit 1 interop -def:$DIR/sdl.def -copt:"$CFLAGS" -target:$TARGET -o:sdl.kt.bc || exit 1
konanc $COMPILER_ARGS -target $TARGET sdl $DIR/Tetris.kt -nativelibrary sdlstubs.bc -linkerArgs "$LINKER_ARGS" -o Tetris.kexe || exit 1 konanc $COMPILER_ARGS -target $TARGET $DIR/Tetris.kt -library sdl.kt.bc -linkerArgs "$LINKER_ARGS" -o Tetris.kexe || exit 1
#strip Tetris.kexe #strip Tetris.kexe