wrapper: allow Android to specify their own llvm-flags

Given the discussion on
https://android-review.googlesource.com/c/platform/external/toolchain-utils/+/1330695,
it sounds like our Android friends want to be able to tweak llvm-next
flags without going through chromium-review + having to pull their
changes back into Android.

This CL allows them to make changes to their llvm-next flags entirely
locally, and isolates CrOS' llvm-next flags from Android's.

BUG=None
TEST=Built the wrapper for CrOS and Android.

Change-Id: If1bcfe762a4f368db9816557752651edc877f497
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2247003
Reviewed-by: Pirama Arumuga Nainar <pirama@google.com>
Reviewed-by: Tiancong Wang <tcwang@google.com>
Tested-by: George Burgess <gbiv@chromium.org>
diff --git a/compiler_wrapper/build.py b/compiler_wrapper/build.py
index 8ae2340..f98b254 100755
--- a/compiler_wrapper/build.py
+++ b/compiler_wrapper/build.py
@@ -40,7 +40,7 @@
   return args
 
 
-def calc_go_args(args, version):
+def calc_go_args(args, version, build_dir):
   ldFlags = [
       '-X',
       'main.ConfigName=' + args.config,
@@ -55,7 +55,17 @@
   # If the wrapper is intended for Chrome OS, we need to use libc's exec.
   extra_args = []
   if not args.static:
-    extra_args = ['-tags', 'libc_exec']
+    extra_args += ['-tags', 'libc_exec']
+
+  if args.config == 'android':
+    # If android_llvm_next_flags.go DNE, we'll get an obscure "no
+    # llvmNextFlags" build error; complaining here is clearer.
+    if not os.path.exists(
+        os.path.join(build_dir, 'android_llvm_next_flags.go')):
+      sys.exit('In order to build the Android wrapper, you must have a local '
+               'android_llvm_next_flags.go file; please see '
+               'cros_llvm_next_flags.go.')
+    extra_args += ['-tags', 'android_llvm_next_flags']
 
   return [
       'go', 'build', '-o',
@@ -84,7 +94,8 @@
   version = read_version(build_dir)
   # Note: Go does not support using absolute package names.
   # So we run go inside the directory of the the build file.
-  sys.exit(subprocess.call(calc_go_args(args, version), cwd=build_dir))
+  sys.exit(
+      subprocess.call(calc_go_args(args, version, build_dir), cwd=build_dir))
 
 
 if __name__ == '__main__':
diff --git a/compiler_wrapper/config.go b/compiler_wrapper/config.go
index fc43a29..e82e6b3 100644
--- a/compiler_wrapper/config.go
+++ b/compiler_wrapper/config.go
@@ -93,11 +93,6 @@
 	return &cfg, nil
 }
 
-// TODO: Enable test in config_test.go, once we have new llvm-next flags.
-var llvmNextFlags = []string{}
-
-var llvmNextPostFlags = []string{}
-
 // Full hardening.
 // Temporarily disable function splitting because of chromium:434751.
 var crosHardenedConfig = &config{
diff --git a/compiler_wrapper/cros_llvm_next_flags.go b/compiler_wrapper/cros_llvm_next_flags.go
new file mode 100644
index 0000000..6cd7cd2
--- /dev/null
+++ b/compiler_wrapper/cros_llvm_next_flags.go
@@ -0,0 +1,18 @@
+// Copyright 2020 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// +build !android_llvm_next_flags
+
+package main
+
+// This file defines extra flags for llvm-next testing for Chrome OS. Importantly, these flags don't
+// apply to Android's llvm-next wrapper. Android's toolchain-utils copy has a
+// `android_llvm_next_flags.go` file downstream that defines its llvm-next arguments. As you can
+// probably infer, `android_llvm_next_flags.go` is only compiled if the `android_llvm_next_flags`
+// tag is set.
+
+// TODO: Enable test in config_test.go, once we have new llvm-next flags.
+var llvmNextFlags = []string{}
+
+var llvmNextPostFlags = []string{}