[gn build] Fix Android compiler-rt targets

If a sysroot was specified, it would take precedence over the Android
NDK sysroot since it would appear after in the command line.

Also only build runtimes for enabled target arches. Many places have
copied this around so create and use supported_android_toolchains.

Reviewed By: pcc

Differential Revision: https://reviews.llvm.org/D113606
diff --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn
index 3b22f6f..39f58c6 100644
--- a/llvm/utils/gn/build/BUILD.gn
+++ b/llvm/utils/gn/build/BUILD.gn
@@ -269,7 +269,8 @@
     if (current_os == "win") {
       assert(is_clang, "sysroot only works with clang-cl as host compiler")
       cflags += [ "/winsysroot" + rebase_path(sysroot, root_build_dir) ]
-    } else if (current_os != "ios" && current_os != "mac") {
+    } else if (current_os != "ios" && current_os != "mac" &&
+               current_os != "android") {
       cflags += [ "--sysroot=" + rebase_path(sysroot, root_build_dir) ]
     }
   }
diff --git a/llvm/utils/gn/secondary/compiler-rt/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/BUILD.gn
index a234a3c..b4933bd 100644
--- a/llvm/utils/gn/secondary/compiler-rt/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/BUILD.gn
@@ -11,12 +11,7 @@
 } else {
   supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
 }
-if (android_ndk_path != "") {
-  supported_toolchains += [
-    "//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
-    "//llvm/utils/gn/build/toolchain:stage2_android_arm",
-  ]
-}
+supported_toolchains += supported_android_toolchains
 group("compiler-rt") {
   deps = [ "//compiler-rt/include($host_toolchain)" ]
   foreach(toolchain, supported_toolchains) {
diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/scudo/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/scudo/BUILD.gn
index c8c057f..430f55b 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/scudo/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/scudo/BUILD.gn
@@ -1,15 +1,11 @@
+import("//llvm/lib/Target/targets.gni")
 import("//llvm/utils/gn/build/toolchain/compiler.gni")
 
 supported_toolchains = []
 if (target_os == "linux" || target_os == "fuchsia") {
   supported_toolchains += [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
 }
-if (android_ndk_path != "") {
-  supported_toolchains += [
-    "//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
-    "//llvm/utils/gn/build/toolchain:stage2_android_arm",
-  ]
-}
+supported_toolchains += supported_android_toolchains
 
 group("scudo") {
   deps = []
diff --git a/llvm/utils/gn/secondary/compiler-rt/test/hwasan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/test/hwasan/BUILD.gn
index 2c39486..0ec6469 100644
--- a/llvm/utils/gn/secondary/compiler-rt/test/hwasan/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/test/hwasan/BUILD.gn
@@ -1,5 +1,6 @@
 import("//compiler-rt/target.gni")
 import("//compiler-rt/test/test.gni")
+import("//llvm/lib/Target/targets.gni")
 import("//llvm/utils/gn/build/toolchain/compiler.gni")
 import("//llvm/utils/gn/build/write_cmake_config.gni")
 import("//llvm/version.gni")
@@ -59,7 +60,7 @@
 if (host_os == "linux" && host_cpu == "x64") {
   supported_toolchains += [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
 }
-if (android_ndk_path != "") {
+if (llvm_build_AArch64 && android_ndk_path != "") {
   supported_toolchains +=
       [ "//llvm/utils/gn/build/toolchain:stage2_android_aarch64" ]
 }
diff --git a/llvm/utils/gn/secondary/libunwind/BUILD.gn b/llvm/utils/gn/secondary/libunwind/BUILD.gn
index f66246d..5d95bbe 100644
--- a/llvm/utils/gn/secondary/libunwind/BUILD.gn
+++ b/llvm/utils/gn/secondary/libunwind/BUILD.gn
@@ -1,12 +1,8 @@
+import("//llvm/lib/Target/targets.gni")
 import("//llvm/utils/gn/build/toolchain/compiler.gni")
 
-supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
-if (android_ndk_path != "") {
-  supported_toolchains += [
-    "//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
-    "//llvm/utils/gn/build/toolchain:stage2_android_arm",
-  ]
-}
+supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ] +
+                       supported_android_toolchains
 
 group("libunwind") {
   deps = []
diff --git a/llvm/utils/gn/secondary/lldb/tools/driver/BUILD.gn b/llvm/utils/gn/secondary/lldb/tools/driver/BUILD.gn
index 7f69adc..4883af4 100644
--- a/llvm/utils/gn/secondary/lldb/tools/driver/BUILD.gn
+++ b/llvm/utils/gn/secondary/lldb/tools/driver/BUILD.gn
@@ -1,3 +1,4 @@
+import("//llvm/lib/Target/targets.gni")
 import("//llvm/utils/TableGen/tablegen.gni")
 import("//llvm/utils/gn/build/toolchain/compiler.gni")
 import("//llvm/utils/gn/build/write_cmake_config.gni")
@@ -16,13 +17,7 @@
   }
 }
 
-supported_toolchains = []
-if (android_ndk_path != "") {
-  supported_toolchains += [
-    "//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
-    "//llvm/utils/gn/build/toolchain:stage2_android_arm",
-  ]
-}
+supported_toolchains = supported_android_toolchains
 
 executable("lldb") {
   configs += [ "//llvm/utils/gn/build:lldb_code" ]
diff --git a/llvm/utils/gn/secondary/llvm/lib/Target/targets.gni b/llvm/utils/gn/secondary/llvm/lib/Target/targets.gni
index 22da518..699b01f 100644
--- a/llvm/utils/gn/secondary/llvm/lib/Target/targets.gni
+++ b/llvm/utils/gn/secondary/llvm/lib/Target/targets.gni
@@ -1,3 +1,5 @@
+import("//llvm/utils/gn/build/toolchain/compiler.gni")
+
 declare_args() {
   # The target archs LLVM should support. Defaults to the host arch.
   # Set to a list, e.g. `llvm_targets_to_build = [ "X86", "ARM" ]`,
@@ -97,3 +99,16 @@
 } else {
   assert(false, "Unsuppored host_cpu '$host_cpu'.")
 }
+
+supported_android_toolchains = []
+
+if (android_ndk_path != "") {
+  if (llvm_build_AArch64) {
+    supported_android_toolchains +=
+        [ "//llvm/utils/gn/build/toolchain:stage2_android_aarch64" ]
+  }
+  if (llvm_build_ARM) {
+    supported_android_toolchains +=
+        [ "//llvm/utils/gn/build/toolchain:stage2_android_arm" ]
+  }
+}