[llvm] r351248 - gn build: Move target flags from toolchain to a .gni file.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 15 13:24:01 PST 2019


Author: pcc
Date: Tue Jan 15 13:24:00 2019
New Revision: 351248

URL: http://llvm.org/viewvc/llvm-project?rev=351248&view=rev
Log:
gn build: Move target flags from toolchain to a .gni file.

While here, add a use_lld flag and default it to true when using
clang on non-mac.

Differential Revision: https://reviews.llvm.org/D56710

Added:
    llvm/trunk/utils/gn/build/toolchain/target_flags.gni
Modified:
    llvm/trunk/utils/gn/build/BUILD.gn
    llvm/trunk/utils/gn/build/toolchain/BUILD.gn
    llvm/trunk/utils/gn/build/toolchain/compiler.gni

Modified: llvm/trunk/utils/gn/build/BUILD.gn
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gn/build/BUILD.gn?rev=351248&r1=351247&r2=351248&view=diff
==============================================================================
--- llvm/trunk/utils/gn/build/BUILD.gn (original)
+++ llvm/trunk/utils/gn/build/BUILD.gn Tue Jan 15 13:24:00 2019
@@ -1,6 +1,7 @@
 import("//llvm/utils/gn/build/buildflags.gni")
 import("//llvm/utils/gn/build/mac_sdk.gni")
 import("//llvm/utils/gn/build/toolchain/compiler.gni")
+import("//llvm/utils/gn/build/toolchain/target_flags.gni")
 
 config("compiler_defaults") {
   defines = []
@@ -9,7 +10,8 @@ config("compiler_defaults") {
     defines += [ "NDEBUG" ]
   }
 
-  cflags = []
+  cflags = target_flags + target_cflags
+  ldflags = target_flags + target_ldflags
 
   if (host_os == "mac" && clang_base_path != "") {
     cflags += [
@@ -104,6 +106,10 @@ config("compiler_defaults") {
       cflags += [ "-Wno-nonportable-include-path" ]
     }
   }
+
+  if (use_lld) {
+    ldflags += [ "-fuse-ld=lld" ]
+  }
 }
 
 config("no_rtti") {

Modified: llvm/trunk/utils/gn/build/toolchain/BUILD.gn
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gn/build/toolchain/BUILD.gn?rev=351248&r1=351247&r2=351248&view=diff
==============================================================================
--- llvm/trunk/utils/gn/build/toolchain/BUILD.gn (original)
+++ llvm/trunk/utils/gn/build/toolchain/BUILD.gn Tue Jan 15 13:24:00 2019
@@ -12,16 +12,10 @@ declare_args() {
 template("unix_toolchain") {
   toolchain(target_name) {
     forward_variables_from(invoker, "*")
-    if (!defined(target_cflags)) {
-      target_cflags = ""
-    }
-    if (!defined(target_ldflags)) {
-      target_ldflags = ""
-    }
 
     tool("cc") {
       depfile = "{{output}}.d"
-      command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} $target_cflags"
+      command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
       depsformat = "gcc"
       description = "CC {{output}}"
       outputs = [
@@ -31,7 +25,7 @@ template("unix_toolchain") {
 
     tool("cxx") {
       depfile = "{{output}}.d"
-      command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} $target_cflags"
+      command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
       depsformat = "gcc"
       description = "CXX {{output}}"
       outputs = [
@@ -41,7 +35,7 @@ template("unix_toolchain") {
 
     tool("asm") {
       depfile = "{{output}}.d"
-      command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{asmflags}} $target_cflags"
+      command = "$cc -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{asmflags}}"
       depsformat = "gcc"
       description = "ASM {{output}}"
       outputs = [
@@ -69,10 +63,10 @@ template("unix_toolchain") {
     tool("solink") {
       outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
       if (current_os == "mac") {
-        command = "$ld -shared {{ldflags}} -o $outfile {{libs}} {{inputs}} $target_ldflags"
+        command = "$ld -shared {{ldflags}} -o $outfile {{libs}} {{inputs}}"
         default_output_extension = ".dylib"
       } else {
-        command = "$ld -shared {{ldflags}} -Wl,-z,defs -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{libs}} {{inputs}} $target_ldflags"
+        command = "$ld -shared {{ldflags}} -Wl,-z,defs -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{libs}} {{inputs}}"
         default_output_extension = ".so"
       }
       description = "SOLINK $outfile"
@@ -87,10 +81,10 @@ template("unix_toolchain") {
     tool("solink_module") {
       outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
       if (current_os == "mac") {
-        command = "$ld -shared {{ldflags}} -Wl,-flat_namespace -Wl,-undefined,suppress -o $outfile {{libs}} {{inputs}} $target_ldflags"
+        command = "$ld -shared {{ldflags}} -Wl,-flat_namespace -Wl,-undefined,suppress -o $outfile {{libs}} {{inputs}}"
         default_output_extension = ".dylib"
       } else {
-        command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{libs}} {{inputs}} $target_ldflags"
+        command = "$ld -shared {{ldflags}} -Wl,-soname,{{target_output_name}}{{output_extension}} -o $outfile {{libs}} {{inputs}}"
         default_output_extension = ".so"
       }
       description = "SOLINK $outfile"
@@ -104,10 +98,9 @@ template("unix_toolchain") {
     tool("link") {
       outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
       if (current_os == "mac") {
-        command =
-            "$ld {{ldflags}} -o $outfile {{libs}} {{inputs}} $target_ldflags"
+        command = "$ld {{ldflags}} -o $outfile {{libs}} {{inputs}}"
       } else {
-        command = "$ld {{ldflags}} -o $outfile {{libs}} -Wl,--start-group {{inputs}} -Wl,--end-group $target_ldflags"
+        command = "$ld {{ldflags}} -o $outfile {{libs}} -Wl,--start-group {{inputs}} -Wl,--end-group"
       }
       description = "LINK $outfile"
       outputs = [
@@ -173,19 +166,8 @@ if (android_ndk_path != "") {
     toolchain_args = {
       current_os = "android"
       current_cpu = "arm64"
+      use_lld = true
     }
-
-    libcxx_path = "$android_ndk_path/sources/cxx-stl/llvm-libc++"
-    platform_lib_path =
-        "$android_ndk_path/platforms/android-21/arch-arm64/usr/lib"
-    libgcc_path = "$android_ndk_path/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x"
-
-    target_flags =
-        "--target=aarch64-linux-android21 --sysroot=$android_ndk_path/sysroot"
-    target_cflags = "$target_flags -isystem $libcxx_path/include"
-    target_ldflags = "$target_flags -fuse-ld=lld -B$platform_lib_path -L$platform_lib_path -L$libgcc_path"
-    target_ldflags +=
-        " -nostdlib++ -L$libcxx_path/libs/arm64-v8a -l:libc++.a.21"
   }
 }
 

Modified: llvm/trunk/utils/gn/build/toolchain/compiler.gni
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gn/build/toolchain/compiler.gni?rev=351248&r1=351247&r2=351248&view=diff
==============================================================================
--- llvm/trunk/utils/gn/build/toolchain/compiler.gni (original)
+++ llvm/trunk/utils/gn/build/toolchain/compiler.gni Tue Jan 15 13:24:00 2019
@@ -19,4 +19,7 @@ declare_args() {
   # Set if the host compiler is clang.  On by default on Mac or if
   # clang_base_path is set.
   is_clang = host_os == "mac" || clang_base_path != ""
+
+  # Set this to true to link with LLD instead of the default linker.
+  use_lld = clang_base_path != "" && host_os != "mac"
 }

Added: llvm/trunk/utils/gn/build/toolchain/target_flags.gni
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gn/build/toolchain/target_flags.gni?rev=351248&view=auto
==============================================================================
--- llvm/trunk/utils/gn/build/toolchain/target_flags.gni (added)
+++ llvm/trunk/utils/gn/build/toolchain/target_flags.gni Tue Jan 15 13:24:00 2019
@@ -0,0 +1,34 @@
+import("//llvm/triples.gni")
+import("//llvm/utils/gn/build/toolchain/compiler.gni")
+
+target_flags = []
+target_cflags = []
+target_ldflags = []
+
+if (current_os == "android") {
+  assert(current_cpu == "arm64", "current_cpu not supported")
+
+  libcxx_path = "$android_ndk_path/sources/cxx-stl/llvm-libc++"
+  platform_lib_path =
+      "$android_ndk_path/platforms/android-21/arch-arm64/usr/lib"
+  libgcc_path = "$android_ndk_path/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x"
+
+  target_flags += [
+    "--target=$llvm_current_triple",
+    "--sysroot=$android_ndk_path/sysroot",
+  ]
+  target_cflags += [
+    "-isystem",
+    "$libcxx_path/include",
+  ]
+  target_ldflags += [
+    "-B$platform_lib_path",
+    "-L$platform_lib_path",
+    "-L$libgcc_path",
+  ]
+  target_ldflags += [
+    "-nostdlib++",
+    "-L$libcxx_path/libs/arm64-v8a",
+    "-l:libc++.a.21",
+  ]
+}




More information about the llvm-commits mailing list