[llvm] 1978482 - gn build: Fix Android build.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 17:41:38 PDT 2023


Author: Peter Collingbourne
Date: 2023-08-01T17:41:21-07:00
New Revision: 19784825379e1dce0fe3b9c8a1af95bd60576fa6

URL: https://github.com/llvm/llvm-project/commit/19784825379e1dce0fe3b9c8a1af95bd60576fa6
DIFF: https://github.com/llvm/llvm-project/commit/19784825379e1dce0fe3b9c8a1af95bd60576fa6.diff

LOG: gn build: Fix Android build.

Remove --unwindlib=none flag that was added by D143598. It is required
to not pass this flag when linking binaries. With this flag, linking
e.g. llvm-symbolizer will fail.

There was a missing dependency from most targets to the implicitly linked
libraries (libunwind and builtins), which was causing the build for those
targets to fail when they were built on their own. I never noticed this
because I always build the implicitly linked libraries together with
their dependencies. Make the dependency explicit by introducing a new
target //llvm/utils/gn/build/libs/implicit representing the platform's
implicitly linked libraries, and depend on that from the LLVM libraries,
so that "normal" binaries like llvm-symbolizer will have all of their
dependencies available.

D143598 set the arch subdirectory to i686 for Android. However, the arch
subdirectory on Android is i386, and has been for a long time.

$ find android-ndk-r* -name i386
android-ndk-r18b/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/7.0.2/lib/linux/i386
android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.2/lib/linux/i386
android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.7/lib/linux/i386
android-ndk-r21-beta1/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/lib/linux/i386
android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.8/lib/linux/i386
android-ndk-r22/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/11.0.5/lib/linux/i386
android-ndk-r23-beta5/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/12.0.5/lib/linux/i386
android-ndk-r24/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.1/lib/linux/i386
android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.7/lib/linux/i386

Using the wrong name prevents Clang from being able to find libunwind.a on
x86. Fix the code to use i386 as the arch subdirectory, consistent with
the NDK.

Bring back -Wl,-z,defs which was removed for Android by D143598, presumably
because of the missing unwind library.

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

Added: 
    llvm/utils/gn/build/libs/implicit/BUILD.gn

Modified: 
    llvm/utils/gn/build/BUILD.gn
    llvm/utils/gn/build/BUILDCONFIG.gn
    llvm/utils/gn/build/toolchain/target_flags.gni
    llvm/utils/gn/secondary/libunwind/src/BUILD.gn

Removed: 
    


################################################################################
diff  --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn
index 14e65c18f3abfc..16af2429795818 100644
--- a/llvm/utils/gn/build/BUILD.gn
+++ b/llvm/utils/gn/build/BUILD.gn
@@ -448,7 +448,7 @@ config("zdefs") {
   # -Wl,-z,defs doesn't work with sanitizers.
   # https://clang.llvm.org/docs/AddressSanitizer.html
   if (current_os != "ios" && current_os != "mac" && current_os != "win" &&
-      current_os != "android" && !(use_asan || use_tsan || use_ubsan)) {
+      !(use_asan || use_tsan || use_ubsan)) {
     ldflags = [ "-Wl,-z,defs" ]
   }
 }

diff  --git a/llvm/utils/gn/build/BUILDCONFIG.gn b/llvm/utils/gn/build/BUILDCONFIG.gn
index 858b811c13b158..0473d45a33c172 100644
--- a/llvm/utils/gn/build/BUILDCONFIG.gn
+++ b/llvm/utils/gn/build/BUILDCONFIG.gn
@@ -49,3 +49,22 @@ if (host_os == "win") {
 }
 
 set_default_toolchain(host_toolchain)
+
+if (current_os == "android") {
+  foreach(target_type,
+          [
+            "executable",
+            "loadable_module",
+            "shared_library",
+          ]) {
+    template(target_type) {
+      target(target_type, target_name) {
+        forward_variables_from(invoker, "*")
+        if (!defined(deps)) {
+          deps = []
+        }
+        deps += [ "//llvm/utils/gn/build/libs/implicit" ]
+      }
+    }
+  }
+}

diff  --git a/llvm/utils/gn/build/libs/implicit/BUILD.gn b/llvm/utils/gn/build/libs/implicit/BUILD.gn
new file mode 100644
index 00000000000000..6f330d2ded612e
--- /dev/null
+++ b/llvm/utils/gn/build/libs/implicit/BUILD.gn
@@ -0,0 +1,10 @@
+# This target represents the library dependencies that are implicitly linked by
+# the compiler.
+if (current_os == "android") {
+  group("implicit") {
+    deps = [
+      "//compiler-rt/lib/builtins",
+      "//libunwind/src:unwind_static",
+    ]
+  }
+}

diff  --git a/llvm/utils/gn/build/toolchain/target_flags.gni b/llvm/utils/gn/build/toolchain/target_flags.gni
index 09b8cfabbd6719..04d3d0fbcea2eb 100644
--- a/llvm/utils/gn/build/toolchain/target_flags.gni
+++ b/llvm/utils/gn/build/toolchain/target_flags.gni
@@ -17,10 +17,7 @@ if (current_os == "android") {
     "--gcc-toolchain=$android_ndk_path/toolchains/llvm/prebuilt/linux-x86_64",
     "-fno-emulated-tls",
   ]
-  target_ldflags += [
-    "-static-libstdc++",
-    "--unwindlib=none",
-  ]
+  target_ldflags += [ "-static-libstdc++" ]
   if (current_cpu == "arm") {
     target_flags += [ "-march=armv7-a" ]
   }

diff  --git a/llvm/utils/gn/secondary/libunwind/src/BUILD.gn b/llvm/utils/gn/secondary/libunwind/src/BUILD.gn
index 59f39816977d98..dd6bd577108766 100644
--- a/llvm/utils/gn/secondary/libunwind/src/BUILD.gn
+++ b/llvm/utils/gn/secondary/libunwind/src/BUILD.gn
@@ -57,7 +57,7 @@ if (current_os == "android") {
   } else if (current_cpu == "x64") {
     unwind_output_dir = "$crt_current_out_dir/x86_64"
   } else if (current_cpu == "x86") {
-    unwind_output_dir = "$crt_current_out_dir/i686"
+    unwind_output_dir = "$crt_current_out_dir/i386"
   }
 } else {
   unwind_output_dir = runtimes_dir


        


More information about the llvm-commits mailing list