C++: Move default linker flags with libs after libraries
Default linker flags contains flags like -lstdc++ which should come after the
user libraries. The library flags are removed from default linker flags and added to link_libs instead which comes after user libraries.
See #9254
Incompatible flag bug: https://github.com/bazelbuild/bazel/issues/10905
I introduce a new method to repositories ctx that allows checking the value of a Starlark semantic option from a repository rule.
RELNOTES:none
PiperOrigin-RevId: 299336105
Change-Id: I81b9ee0c72e2a2252b406f1c584997389a530e40
diff --git a/cc/private/toolchain/unix_cc_configure.bzl b/cc/private/toolchain/unix_cc_configure.bzl
index cacc29c..084ab73 100644
--- a/cc/private/toolchain/unix_cc_configure.bzl
+++ b/cc/private/toolchain/unix_cc_configure.bzl
@@ -400,16 +400,21 @@
"-std=c++0x",
False,
), ":")
+
+ bazel_linkopts = "-lstdc++:-lm"
+ bazel_linklibs = ""
+ if hasattr(repository_ctx, "flag_enabled") and repository_ctx.flag_enabled("incompatible_linkopts_to_linklibs"):
+ bazel_linkopts, bazel_linklibs = bazel_linklibs, bazel_linkopts
link_opts = split_escaped(get_env_var(
repository_ctx,
"BAZEL_LINKOPTS",
- "-lstdc++:-lm",
+ bazel_linkopts,
False,
), ":")
link_libs = split_escaped(get_env_var(
repository_ctx,
"BAZEL_LINKLIBS",
- "",
+ bazel_linklibs,
False,
), ":")
gold_linker_path = _find_gold_linker_path(repository_ctx, cc)