Consult BAZEL_LINKOPTS env variable when detecting linker flags in autogenerated cc toolchain
Relevant to #4031, fixes #4524.
Does not affect the osx + xcode toolchain that supports both C++ and ObjC!
RELNOTES: BAZEL_LINKOPTS is now consulted when autoconfiguring c++ toolchain
Now linker flags for the autogenerated C++ toolchain can be controlled by
BAZEL_LINKOPTS environment variable (using colon as a flag separator):
BAZEL_LINKOPTS=-lc++ bazel build //...
The default value is "-lstdc++:-lm".
PiperOrigin-RevId: 187173297
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index 610f5a7..858c5bc 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -48,6 +48,7 @@
"ABI_VERSION",
"BAZEL_COMPILER",
"BAZEL_HOST_SYSTEM",
+ "BAZEL_LINKOPTS",
"BAZEL_PYTHON",
"BAZEL_SH",
"BAZEL_TARGET_CPU",
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index f67b9b5..4cd0830 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -19,6 +19,7 @@
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
"escape_string",
"get_env_var",
+ "split_escaped",
"which",
"tpl",
)
@@ -234,11 +235,7 @@
"cxx_flag": [
"-std=c++0x",
] + _escaped_cplus_include_paths(repository_ctx),
- "linker_flag": [
- "-lstdc++",
- "-lm", # Some systems expect -lm in addition to -lstdc++
- # Anticipated future default.
- ] + (
+ "linker_flag": (
["-fuse-ld=gold"] if supports_gold_linker else []
) + _add_option_if_supported(
repository_ctx, cc, "-Wl,-no-as-needed"
@@ -257,7 +254,7 @@
] + _add_option_if_supported(
# Have gcc return the exit code from ld.
repository_ctx, cc, "-pass-exit-codes")
- ),
+ ) + split_escaped(get_env_var(repository_ctx, "BAZEL_LINKOPTS", "-lstdc++:-lm"), ":"),
"cxx_builtin_include_directory": escaped_cxx_include_directories,
"objcopy_embed_flag": ["-I", "binary"],
"unfiltered_cxx_flag":