From c84fe7fd8530499ad331107bac75e67b3de2113b Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Tue, 6 Dec 2016 13:54:42 +0300 Subject: [PATCH] Collected or scattered means of running the compiler in a shell script. So one can now $ ./gradlew dist $ ./dist/bin/konanc backend.native/tests/runtime/basic/hello0.kt -o hello $ ./hello or one can $ ./gradlew demo and have a complete dist build, with a test program compiled and executed automatically after that --- backend.native/build.gradle | 25 +++-- backend.native/tests/build.gradle | 2 + backend.native/tests/link/src/bar.kt | 7 +- build.gradle | 89 ++++++++++++++++- cmd/konanc | 140 +++++++++++++++++++++++++++ gradle.properties | 5 +- 6 files changed, 255 insertions(+), 13 deletions(-) create mode 100755 cmd/konanc diff --git a/backend.native/build.gradle b/backend.native/build.gradle index fdb3ede9e46..34a8a7b54a1 100644 --- a/backend.native/build.gradle +++ b/backend.native/build.gradle @@ -223,17 +223,26 @@ task run(type: JavaExec) { environment 'LD_LIBRARY_PATH' : "${llvmDir}/lib:${project.buildDir}/nativelibs" } +task jars(type: Jar) { + from 'build/classes/cli_bc', + 'build/classes/compiler', + 'build/classes/hashInteropStubs', + 'build/classes/llvmInteropStubs' - -task jars { - doFirst { - project.buildscript.configurations.classpath.each { - String jarName = it.getName(); - print jarName + ":" - } - } + dependsOn 'build', ':runtime:build', 'external_jars' } +task external_jars(type: Copy) { + from configurations.compilerCompile { + + include 'protobuf-java-3.0.0.jar' + + include 'kotlin-compiler*.jar' + rename 'kotlin-compiler(.*).jar', 'kotlin-compiler.jar' + + into 'build/external_jars' + } +} protobuf { protoc { diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 66fa6d98482..4015e76f2c3 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -187,6 +187,7 @@ class LinkKonanTest extends KonanTest { jvmArgs "-ea" args "-output", outPath, "-runtime", "${runtimeBc.absolutePath}", + "-library", "${stdlibKtBc.absolutePath}", "-library", "${libBc.absolutePath}", "${sourceKt.absolutePath}" @@ -445,6 +446,7 @@ task intrinsic(type: UnitKonanTest) { } task link(type: LinkKonanTest) { + goldValue = "0\n" source = "link/src/bar.kt" lib = "link/lib" } diff --git a/backend.native/tests/link/src/bar.kt b/backend.native/tests/link/src/bar.kt index 3ed2cbacd98..e595ef9495c 100644 --- a/backend.native/tests/link/src/bar.kt +++ b/backend.native/tests/link/src/bar.kt @@ -1,5 +1,6 @@ -package qwerty +import qwerty.* -fun bar(a: Int): Int { - return foo(foo2(a)) +fun main(args: Array) { + val size = foo(foo2(args.size)) + println(size.toString()) } diff --git a/build.gradle b/build.gradle index 5d0deb98374..98ae37c312d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,7 @@ //ant.importBuild 'backend.native/kotlin-ir/build.xml' +defaultTasks 'clean', 'demo' + if (new File("$project.rootDir/local.properties").exists()) { Properties props = new Properties() props.load(new FileInputStream("$project.rootDir/local.properties")) @@ -36,6 +38,8 @@ allprojects { ext.clangArgs << "-B$binDir" ext.clangPath << binDir + ext.jvmArgs=["-ea"] + convention.plugins.execClang = new org.jetbrains.kotlin.ExecClang(project) plugins.withType(NativeComponentPlugin) { @@ -85,4 +89,87 @@ class PlatformInfo { Throwable unsupportedPlatformException() { return new Error("unsupported platform: $osName/$osArch") } -} \ No newline at end of file +} + +task dist_compiler(type: Copy) { + dependsOn ':backend.native:jars', 'dist_runtime' + + destinationDir file('dist') + + from(project(':backend.native').file('build/libs')) { + into('konan/lib') + } + + from(project('Interop').file('Runtime/build/libs')) { + into('konan/lib') + } + + from(project(':backend.native').file('build/external_jars')) { + into('konan/lib') + } + + from(project(':backend.native').file('build/nativelibs')) { + into('konan/nativelib') + } + + from(file('cmd')) { + fileMode(0775) + include('konanc') + include('kotlinc-native') + + filter { line -> line.replaceAll("FILTER_CLANG_PLATFORM_ARGS", clangArgs.join(' ')) } + filter { line -> line.replaceAll("FILTER_CLANG_BIN_PATH", "${project.llvmDir}/bin") } + + into('bin') + } +} + +task stdlib(type: Exec) { + dependsOn 'dist_compiler', 'list_dist' + + commandLine './dist/bin/konanc', + "-J${jvmArgs.join(' ')}", '-Djava.library.path=backend.native/build/nativelibs', + '-nolink', '-nostdlib', 'runtime/src/main/kotlin', '-o', 'dist/lib/stdlib.kt.bc' + + outputs.file(project.file('dist/lib/stdlib.kt.bc')) +} + +task list_dist(type: Exec) { + commandLine 'find', 'dist' +} + +task start(type: Exec) { + dependsOn 'dist_compiler', 'stdlib' + + commandLine './dist/bin/konanc', + "-J${jvmArgs.join(' ')}", '-Djava.library.path=backend.native/build/nativelibs', + '-nolink', 'runtime/src/launcher/kotlin', '-o', 'dist/lib/start.kt.bc' + + outputs.file(project.file('dist/lib/start.kt.bc')) +} + +task dist_runtime(type: Copy) { + dependsOn ':runtime:build' + + destinationDir file('dist') + + from(project(':runtime').file('build')) { + include('*.bc') + into('lib') + } +} + +task dist { + dependsOn 'dist_compiler', 'dist_runtime', 'stdlib', 'start' +} + +task demo(type: Exec) { + dependsOn 'dist' + + commandLine './dist/bin/konanc', 'backend.native/tests/runtime/collections/moderately_large_array.kt', '-o', 'build/demo.kexe' +} + +task clean { + delete 'dist' +} + diff --git a/cmd/konanc b/cmd/konanc new file mode 100755 index 00000000000..553b28fde93 --- /dev/null +++ b/cmd/konanc @@ -0,0 +1,140 @@ +#!/usr/bin/env bash + +if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then + JAVACMD="$JAVA_HOME/bin/java" +else + JAVACMD=java +fi + +declare -a java_args +declare -a konan_args +declare -a clang_args + +while [ $# -gt 0 ]; do + case "$1" in + -D*) + java_args=("${java_args[@]}" "$1") + shift + ;; + -J*) + java_args=("${java_args[@]}" "${1:2}") + shift + ;; + -library) + clang_args=("${libraries[@]}" "$2") + konan_args=("${konan_args[@]}" "$1" "$2") + shift + shift + ;; + -nolink) + NOLINK=YES + shift + ;; + -nostdlib) + NOSTDLIB=YES + shift + ;; + -o) + OUTPUT_NAME=$2 + shift + shift + ;; + -X*) + clang_args=("${clang_args[@]}" "${1:2}") + shift + ;; + *) + konan_args=("${konan_args[@]}" "$1") + shift + ;; + esac +done + + +[ -n "$KONAN_COMPILER" ] || KONAN_COMPILER=org.jetbrains.kotlin.cli.bc.K2NativeKt +[ -n "$JAVACMD" ] || JAVACMD=java +[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M" + +# Based on findScalaHome() from scalac script +findHome() { + local source="${BASH_SOURCE[0]}" + while [ -h "$source" ] ; do + local linked="$(readlink "$source")" + local dir="$(cd -P $(dirname "$source") && cd -P $(dirname "$linked") && pwd)" + source="$dir/$(basename "$linked")" + done + (cd -P "$(dirname "$source")/.." && pwd) +} + +KONAN_HOME="$(findHome)" + +echo $KONAN_HOME + +KONAN_JAR="${KONAN_HOME}/konan/lib/backend.native.jar" +KOTLIN_JAR="${KONAN_HOME}/konan/lib/kotlin-compiler.jar" +INTEROP_JAR="${KONAN_HOME}/konan/lib/Runtime.jar" +PROTOBUF_JAR="${KONAN_HOME}/konan/lib/protobuf-java-3.0.0.jar" + +KONAN_CLASSPATH="$KOTLIN_JAR:$INTEROP_JAR:$PROTOBUF_JAR:$KONAN_JAR" + +NATIVE_LIB="${KONAN_HOME}/konan/nativelib" +DYLD=$DYLD_LIBRARY_PATH:$NATIVE_LIB + +RUNTIME="$KONAN_HOME/lib/runtime.bc" +STDLIB="$KONAN_HOME/lib/stdlib.kt.bc" + +# +# KONAN BACKEND INVOCATION +# + +java_args=("${java_args[@]}" "-noverify") + +konan_args=("-runtime" $RUNTIME "${konan_args[@]}") +[ -z $NOSTDLIB ] && konan_args=("-library" $STDLIB "${konan_args[@]}") + +if [ -z $OUTPUT_NAME ] ; then + if [ -z $NOLINK ] ; then + OUTPUT_NAME="program.kexe" + KTBC_NAME="program.kt.bc" + else + KTBC_NAME="program.kt.bc" + fi +else + if [ -z $NOLINK ] ; then + KTBC_NAME="${OUTPUT_NAME}.kt.bc" + else + KTBC_NAME="${OUTPUT_NAME}" + fi +fi + +DYLD_LIBRARY_PATH=$DYLD LD_LIBRARY_PATH=$DYLD $JAVACMD $JAVA_OPTS ${java_args[@]} -cp $KONAN_CLASSPATH $KONAN_COMPILER -output ${KTBC_NAME} ${konan_args[@]} + +COMPILER_EXIT_CODE="$?" +[ -z $NOLINK ] || exit $COMPILER_EXIT_CODE + +# +# LINK STAGE +# + +# TODO: We should probably copy the dependencies into dist. +DEPENDENCIES="$KONAN_HOME/../dependencies/all" + +LAUNCHER="${KONAN_HOME}/lib/launcher.bc" +START="${KONAN_HOME}/lib/start.kt.bc" + +# We filter this script during installation +# substituting the proper platform dependent arguments here. +CLANG_PLATFORM_ARGS="FILTER_CLANG_PLATFORM_ARGS" +CLANG_BIN_PATH="FILTER_CLANG_BIN_PATH" + +CLANG="$CLANG_BIN_PATH/clang++" + +# TODO: Compilation without stdlib.kt.bc doesn't work because 'start' requires stdlib, for example. +# Probably, we need a .klib compilation mode. +# [ -z $NOSTDLIB ] && clang_args=($STDLIB "${clang_args[@]}") +clang_args=($STDLIB "${clang_args[@]}") +clang_args=($LAUNCHER $START $RUNTIME "${clang_args[@]}") +clang_args=("${clang_args[@]}" "-xc $CLANG_PLATFORM_ARGS") + +$CLANG $KTBC_NAME -o $OUTPUT_NAME ${clang_args[@]} + diff --git a/gradle.properties b/gradle.properties index 4cb81d3b8fe..360690668e3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,4 @@ -kotlin_version=1.1-M02 +#kotlin_version=1.1-M02 +kotlin_version=1.1-M03 +#kotlinCompilerModule=org.jetbrains.kotlin:kotlin-compiler:1.1-20161201.112229-281 +kotlinCompilerModule=org.jetbrains.kotlin:kotlin-compiler:1.1-20161202.152649-287