cros_setup_toolchains: respect CONFIG_TARGET_SUFFIXES for standalone toolchains

If gold is not the default linker in the chroot for a target, it should
not be the default linker in the standalone toolchain either.

BUG=chromium:699628
TEST='cros_setup_toolchains -t aarch64-cros-linux-gnu --create-packages'
and verified that the default linker in the toolchain tarball is bfd.
TEST='cros_setup_toolchains -t armv7a-cros-linux-gnueabi --create-packages'
and verified that the default linker in the toolchain tarball is gold.

Change-Id: I7e0a3c70b69b9c963352346238a2660964396e36
Reviewed-on: https://chromium-review.googlesource.com/451718
Commit-Ready: Rahul Chaudhry <rahulchaudhry@chromium.org>
Tested-by: Rahul Chaudhry <rahulchaudhry@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/cros_setup_toolchains.py b/scripts/cros_setup_toolchains.py
index 0f64f50..e8c742a 100644
--- a/scripts/cros_setup_toolchains.py
+++ b/scripts/cros_setup_toolchains.py
@@ -999,9 +999,16 @@
     logging.warning('%s: binutils lacks support for the gold linker', target)
   else:
     assert len(srcpath) == 1, '%s: did not match exactly 1 path' % globpath
-    gold_supported = True
     srcpath = srcpath[0]
 
+    # Package the binutils-bin directory without the '-gold' suffix
+    # if gold is not enabled as the default linker for this target.
+    gold_supported = CONFIG_TARGET_SUFFIXES['binutils'].get(target) == '-gold'
+    if not gold_supported:
+      srcpath = srcpath[:-len('-gold')]
+      ld_path = os.path.join(srcpath, 'ld')
+      assert os.path.exists(ld_path), '%s: linker is missing!' % ld_path
+
   srcpath = srcpath[len(output_dir):]
   gccpath = os.path.join('/usr', 'libexec', 'gcc')
   for prog in os.listdir(output_dir + srcpath):
@@ -1016,6 +1023,12 @@
   envd = os.path.join(output_dir, 'etc', 'env.d', 'binutils', '*')
   if gold_supported:
     envd += '-gold'
+  else:
+    # If gold is not enabled as the default linker and 2 env.d
+    # files exist, pick the one without the '-gold' suffix.
+    envds = sorted(glob.glob(envd))
+    if len(envds) == 2 and envds[1] == envds[0] + '-gold':
+      envd = envds[0]
   srcpath = _EnvdGetVar(envd, 'LIBPATH')
   os.symlink(os.path.relpath(srcpath, os.path.dirname(libpath)),
              output_dir + libpath)