Bail out quickly from _find_gold_linker_path for GCC.

GCC can't make use of an absolute gold path, so keep parsing specific to clang. This avoids silly problems like https://stackoverflow.com/questions/65336037/how-do-i-fix-a-bazel-build-error-in-tensorflow.

Closes #12729.

PiperOrigin-RevId: 356515434
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index fb567a1..b18fcb3 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -171,13 +171,14 @@
     ])
     return result.stderr.find(pattern) == -1
 
-def _find_linker_path(repository_ctx, cc, linker):
+def _find_linker_path(repository_ctx, cc, linker, is_clang):
     """Checks if a given linker is supported by the C compiler.
 
     Args:
       repository_ctx: repository_ctx.
       cc: path to the C compiler.
       linker: linker to find
+      is_clang: whether the compiler is known to be clang
 
     Returns:
       String to put as value to -fuse-ld= flag, or None if linker couldn't be found.
@@ -187,7 +188,7 @@
         str(repository_ctx.path("tools/cpp/empty.cc")),
         "-o",
         "/dev/null",
-        # Some macos clang versions don't fail when setting -fuse-ld=gold, adding
+        # Some macOS clang versions don't fail when setting -fuse-ld=gold, adding
         # these lines to force it to. This also means that we will not detect
         # gold when only a very old (year 2010 and older) is present.
         "-Wl,--start-lib",
@@ -198,24 +199,18 @@
     if result.return_code != 0:
         return None
 
+    if not is_clang:
+        return linker
+
     for line in result.stderr.splitlines():
         if line.find(linker) == -1:
             continue
         for flag in line.split(" "):
             if flag.find(linker) == -1:
                 continue
-            if flag.find("--enable-" + linker) > -1 or flag.find("--with-plugin-ld") > -1:
-                # skip build configuration options of gcc itself
-                # TODO(hlopko): Add redhat-like worker on the CI (#9392)
-                continue
 
-            # flag is '-fuse-ld=gold' for GCC or "/usr/lib/ld.gold" for Clang
-            # strip space, single quote, and double quotes
-            flag = flag.strip(" \"'")
-
-            # remove -fuse-ld= from GCC output so we have only the flag value part
-            flag = flag.replace("-fuse-ld=", "")
-            return flag
+            # flag looks like "/usr/lib/ld.gold".
+            return flag.strip(" \"'")
     auto_configure_warning(
         "CC with -fuse-ld=" + linker + " returned 0, but its -v output " +
         "didn't contain '" + linker + "', falling back to the default linker.",
@@ -415,8 +410,8 @@
         False,
     ), ":")
     gold_or_lld_linker_path = (
-        _find_linker_path(repository_ctx, cc, "lld") or
-        _find_linker_path(repository_ctx, cc, "gold")
+        _find_linker_path(repository_ctx, cc, "lld", is_clang) or
+        _find_linker_path(repository_ctx, cc, "gold", is_clang)
     )
     cc_path = repository_ctx.path(cc)
     if not str(cc_path).startswith(str(repository_ctx.path(".")) + "/"):