From 6a6424f4629c0f52d3464ebc0c133b711da585f7 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Tue, 13 Dec 2016 05:10:26 +0300 Subject: [PATCH] A little more konanc. --- build.gradle | 47 +++++++++++++++++++++++++++++++---------------- cmd/konanc | 6 +++--- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index bf713edf6dd..82a25ea055a 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ allprojects { } } - convention.plugins.clangFlags = new ClangFlags(isLinux(), "$llvmDir") + convention.plugins.clangFlags = new ClangFlags(new PlatformInfo(), "$llvmDir") ext.clangPath = ["$llvmDir/bin"] ext.clangArgs = [] final String binDir @@ -43,6 +43,7 @@ allprojects { ext.jvmArgs = ["-ea"] ext.clangLinkArgs = libLink() + ext.clangOptArgs = optArgs() convention.plugins.execClang = new org.jetbrains.kotlin.ExecClang(project) @@ -68,32 +69,44 @@ allprojects { class ClangFlags { - protected Boolean isLinux + protected PlatformInfo platform protected String llvmDir - ClangFlags(linux, llvm) { - isLinux = linux + private List linkCppAbi, linkPlatform, optArgs + + ClangFlags(platformInfo, llvm) { + platform = platformInfo llvmDir = llvm + + if (platform.isLinux()) { + linkCppAbi= ["-Wl,-Bstatic", "-lc++abi", "-Wl,-Bdynamic"] + linkPlatform= ["-ldl", "-lpthread", "-lm", "-rdynamic"] + optArgs = ["-O3"] + } else if (platform.isMac()) { + // Life is hard. MacOS linker is harder. + // This is how we force the linker to link statically. + linkCppAbi= ["${llvmDir}/lib/libc++abi.a"] + linkPlatform= [] + optArgs = ["-O3", "-flto", "-Wl,-dead_strip"] + } else { + throw unsupportedPlatformException() + } + } public List linkCppAbi() { - if (isLinux) { - return ["-Wl,-Bstatic", "-lc++abi", "-Wl,-Bdynamic"] - } else { - // Life is hard. MacOS linker is harder. - // This is how we force the linker to link statically. - return ["${llvmDir}/lib/libc++abi.a"] - } + return linkCppAbi } public List linkPlatform() { - if (isLinux) { - return ["-ldl", "-lpthread", "-lm", "-rdynamic"] - } else { - return [] - } + return linkPlatform } + public List optArgs() { + return optArgs + } + + public List libLink() { def libs = [] libs.addAll(linkPlatform()) @@ -160,7 +173,9 @@ task dist_compiler(type: Copy) { include('kotlinc-native') def allClangArgs = "${clangArgs.join(' ')} ${clangLinkArgs.join(' ')}" + filter { line -> line.replaceAll("FILTER_CLANG_PLATFORM_ARGS", allClangArgs) } + filter { line -> line.replaceAll("FILTER_CLANG_PLATFORM_OPT", "${clangOptArgs.join(' ')}") } filter { line -> line.replaceAll("FILTER_CLANG_BIN_PATH", "${project.llvmDir}/bin") } into('bin') diff --git a/cmd/konanc b/cmd/konanc index 965c8e48dcb..473586d780b 100755 --- a/cmd/konanc +++ b/cmd/konanc @@ -55,9 +55,6 @@ while [ $# -gt 0 ]; do esac done -# TODO: support -opt on Linux hosts as well. -clang_opt_args=("-O3" "-flto" "-Wl,-dead_strip") - [ -n "$KONAN_COMPILER" ] || KONAN_COMPILER=org.jetbrains.kotlin.cli.bc.K2NativeKt [ -n "$JAVACMD" ] || JAVACMD=java [ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M" @@ -132,8 +129,11 @@ 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_PLATFORM_OPT="FILTER_CLANG_PLATFORM_OPT" CLANG_BIN_PATH="FILTER_CLANG_BIN_PATH" +clang_opt_args=("$CLANG_PLATFORM_OPT") + CLANG="$CLANG_BIN_PATH/clang" # TODO: Compilation without stdlib.kt.bc doesn't work because 'start' requires stdlib, for example.