Teach cc_configure about BAZEL_LINKLIBS env variable

Using this variable is will be possible to specify system libraries that
should be added after default linker flags, after libraries to link, and
after user link flags (i.e. after --linkopt and `linkopts` rule attribute).

Flag separator is `:`, similarly to `BAZEL_LINKOPTS`. Escaping is done by
`%`.

Default value is empty string.

With this it's possible to force Bazel to statically link `libstdc++` by
using:

```
BAZEL_LINKOPTS=-static-libstdc++ BAZEL_LINKLIBS=-l%:libstdc++.a bazel build //foo
```

Fixes https://github.com/bazelbuild/bazel/issues/2840.
Relevant for https://github.com/bazelbuild/bazel/issues/8652.

RELNOTES: Bazel's C++ autoconfiguration now understands `BAZEL_LINKLIBS` environment variable to specify system libraries that should be appended to the link command line.

Closes #8660.

PiperOrigin-RevId: 253946433
diff --git a/tools/cpp/BUILD.tpl b/tools/cpp/BUILD.tpl
index 11d1dc5..10d7b69 100644
--- a/tools/cpp/BUILD.tpl
+++ b/tools/cpp/BUILD.tpl
@@ -85,6 +85,7 @@
     dbg_compile_flags = [%{dbg_compile_flags}],
     cxx_flags = [%{cxx_flags}],
     link_flags = [%{link_flags}],
+    link_libs = [%{link_libs}],
     opt_link_flags = [%{opt_link_flags}],
     unfiltered_compile_flags = [%{unfiltered_compile_flags}],
     coverage_compile_flags = [%{coverage_compile_flags}],
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index 6582cc3..66dce02 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -126,6 +126,7 @@
         "BAZEL_HOST_SYSTEM",
         "BAZEL_CXXOPTS",
         "BAZEL_LINKOPTS",
+        "BAZEL_LINKLIBS",
         "BAZEL_PYTHON",
         "BAZEL_SH",
         "BAZEL_TARGET_CPU",
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index f2aa625..b817d33 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -342,6 +342,12 @@
         "-lstdc++:-lm",
         False,
     ), ":")
+    link_libs = split_escaped(get_env_var(
+        repository_ctx,
+        "BAZEL_LINKLIBS",
+        "",
+        False,
+    ), ":")
     supports_gold_linker = _is_gold_supported(repository_ctx, cc)
     cc_path = repository_ctx.path(cc)
     if not str(cc_path).startswith(str(repository_ctx.path(".")) + "/"):
@@ -478,6 +484,7 @@
                     "-pass-exit-codes",
                 )
             ) + link_opts),
+            "%{link_libs}": get_starlark_list(link_libs),
             "%{opt_compile_flags}": get_starlark_list(
                 [
                     # No debug symbols.
diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl
index d7da983..f2b12d9 100644
--- a/tools/cpp/unix_cc_toolchain_config.bzl
+++ b/tools/cpp/unix_cc_toolchain_config.bzl
@@ -854,7 +854,7 @@
                         iterate_over = "user_link_flags",
                         expand_if_available = "user_link_flags",
                     ),
-                ],
+                ] + ([flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else []),
             ),
         ],
     )
@@ -1191,6 +1191,7 @@
         "opt_compile_flags": attr.string_list(),
         "cxx_flags": attr.string_list(),
         "link_flags": attr.string_list(),
+        "link_libs": attr.string_list(),
         "opt_link_flags": attr.string_list(),
         "unfiltered_compile_flags": attr.string_list(),
         "coverage_compile_flags": attr.string_list(),