Use lld to link device binaries.

Our ARM32 toolchain builds are not reproducible.  Switching from ld to
lld fixes that.  We do this by creating a wrapper that calls the
existing compiler and adds an argument to use lld.  We do this for
ARM64 as well for consistency.

Bug: 140823999
Change-Id: I050aa3f287aaca74de6ee988a1e0f4edda08c336
diff --git a/config_toml.py b/config_toml.py
index b8351d0..1c13146 100644
--- a/config_toml.py
+++ b/config_toml.py
@@ -13,7 +13,9 @@
 # limitations under the License.
 """Handles generation of config.toml for the rustc build."""
 
+import os
 import paths
+import stat
 
 host_targets = ['x86_64-unknown-linux-gnu']
 device_targets = ['aarch64-linux-android', 'arm-linux-androideabi']
@@ -40,12 +42,20 @@
 """.format(cc=cc, cxx=cxx, ar=ar, ranlib=ranlib, target=target)
 
         def device_config(target):
+            wrapper_name = paths.this_path('clang-with-lld-%s' % target)
+            with open(wrapper_name, 'w') as f:
+                f.write("""\
+#!/bin/sh
+{real_cc} $* -fuse-ld=lld
+""".format(real_cc=paths.ndk_cc(target, 29)))
+            s = os.stat(wrapper_name)
+            os.chmod(wrapper_name, s.st_mode | stat.S_IEXEC)
             return """\
 [target.{target}]
 cc="{cc}"
 ar="{ar}"
 android-ndk="{ndk}"
-""".format(ndk=paths.ndk(), ar=ar, cc=paths.ndk_cc(target, 29), target=target)
+""".format(ndk=paths.ndk(), ar=ar, cc=wrapper_name, target=target)
 
         host_configs = '\n'.join(
             [host_config(target) for target in host_targets])
diff --git a/paths.py b/paths.py
index d684d43..cda9ba9 100644
--- a/paths.py
+++ b/paths.py
@@ -36,6 +36,11 @@
     return os.path.realpath(os.path.join(THIS_DIR, 'patches', *args))
 
 
+def this_path(*args):
+    """Generates a path relative to this directory."""
+    return os.path.realpath(os.path.join(THIS_DIR, *args))
+
+
 def out_path(*args):
     """Generates a path relative to the output directory of the build."""
     return workspace_path('out', *args)