From b04c52664e86fecbaaff56d716f57b98d7624486 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Wed, 7 Dec 2016 13:11:40 +0300 Subject: [PATCH] Moved link args computation to the root build.gradle. Pass the link args to konanc. --- backend.native/tests/build.gradle | 37 ++++---------------------- build.gradle | 44 ++++++++++++++++++++++++++++++- cmd/konanc | 4 +-- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index e9d7458ea2d..c44448940ee 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -100,36 +100,9 @@ abstract class KonanTest extends DefaultTask { return "${project.llvmDir}/bin/${tool}" } - private linux() { - return project.isLinux() + protected List clangLinkArgs() { + return project.clangLinkArgs } - - protected List linkCppAbi() { - if (linux()) { - 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 ["${project.llvmDir}/lib/libc++abi.a"] - } - } - - protected List linkPlatform() { - if (linux()) { - return ["-ldl", "-lpthread", "-lm", "-rdynamic"] - } - else { - return [] - } - } - - protected List libLink() { - def libs = [] - libs.addAll(linkPlatform()) - libs.addAll(linkCppAbi()) - return libs - } - } class UnitKonanTest extends KonanTest { @@ -140,7 +113,7 @@ class UnitKonanTest extends KonanTest { def argList = [] argList.addAll([ runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath, "-o", exe.absolutePath, testC, mainC ]) - argList.addAll(libLink()) + argList.addAll(clangLinkArgs()) args argList } } @@ -153,7 +126,7 @@ class RunKonanTest extends KonanTest { def argList = [] argList.addAll([ launcherBc.absolutePath, startKtBc.absolutePath, runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath, "-o", exe.absolutePath ]) - argList.addAll(libLink()) + argList.addAll(clangLinkArgs()) args argList } } @@ -213,7 +186,7 @@ class LinkKonanTest extends KonanTest { def argList = [] argList.addAll([ runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath, "-o", exe.absolutePath, testC, mainC ]) - argList.addAll(libLink()) + argList.addAll(clangLinkArgs()) args argList } } diff --git a/build.gradle b/build.gradle index 402cbfb5c35..bf713edf6dd 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ allprojects { } } + convention.plugins.clangFlags = new ClangFlags(isLinux(), "$llvmDir") ext.clangPath = ["$llvmDir/bin"] ext.clangArgs = [] final String binDir @@ -41,6 +42,8 @@ allprojects { ext.jvmArgs = ["-ea"] + ext.clangLinkArgs = libLink() + convention.plugins.execClang = new org.jetbrains.kotlin.ExecClang(project) plugins.withType(NativeComponentPlugin) { @@ -63,6 +66,44 @@ allprojects { } } +class ClangFlags { + + protected Boolean isLinux + protected String llvmDir + + ClangFlags(linux, llvm) { + isLinux = linux + llvmDir = llvm + } + + 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"] + } + } + + public List linkPlatform() { + if (isLinux) { + return ["-ldl", "-lpthread", "-lm", "-rdynamic"] + } else { + return [] + } + } + + public List libLink() { + def libs = [] + libs.addAll(linkPlatform()) + libs.addAll(linkCppAbi()) + return libs + } +} + + + class PlatformInfo { private final String osName = System.properties['os.name'] private final String osArch = System.properties['os.arch'] @@ -118,7 +159,8 @@ task dist_compiler(type: Copy) { include('konanc') include('kotlinc-native') - filter { line -> line.replaceAll("FILTER_CLANG_PLATFORM_ARGS", clangArgs.join(' ')) } + def allClangArgs = "${clangArgs.join(' ')} ${clangLinkArgs.join(' ')}" + filter { line -> line.replaceAll("FILTER_CLANG_PLATFORM_ARGS", allClangArgs) } filter { line -> line.replaceAll("FILTER_CLANG_BIN_PATH", "${project.llvmDir}/bin") } into('bin') diff --git a/cmd/konanc b/cmd/konanc index f8b8b5bcbc6..e49acf59b08 100755 --- a/cmd/konanc +++ b/cmd/konanc @@ -131,7 +131,7 @@ START="${KONAN_HOME}/lib/start.kt.bc" CLANG_PLATFORM_ARGS="FILTER_CLANG_PLATFORM_ARGS" CLANG_BIN_PATH="FILTER_CLANG_BIN_PATH" -CLANG="$CLANG_BIN_PATH/clang++" +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. @@ -139,7 +139,7 @@ CLANG="$CLANG_BIN_PATH/clang++" [ "$OPTIMIZE" == "YES" ] && clang_args=("-flto -O3 ${clang_args[@]}") clang_args=($STDLIB "${clang_args[@]}") clang_args=($LAUNCHER $START $RUNTIME "${clang_args[@]}") -clang_args=("${clang_args[@]} -xc $CLANG_PLATFORM_ARGS") +clang_args=("${clang_args[@]} $CLANG_PLATFORM_ARGS") $CLANG $KTBC_NAME -o $OUTPUT_NAME ${clang_args[@]}