Skip to content

Commit

Permalink
Teach cc_configure about BAZEL_LINKLIBS env variable
Browse files Browse the repository at this point in the history
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 bazelbuild#2840.
Relevant for bazelbuild#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 bazelbuild#8660.

PiperOrigin-RevId: 253946433
  • Loading branch information
hlopko authored and siberex committed Jul 4, 2019
1 parent a96d19c commit a3d7a0e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions tools/cpp/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cc_toolchain_config(
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}],
Expand Down
1 change: 1 addition & 0 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ cc_autoconf = repository_rule(
"BAZEL_HOST_SYSTEM",
"BAZEL_CXXOPTS",
"BAZEL_LINKOPTS",
"BAZEL_LINKLIBS",
"BAZEL_PYTHON",
"BAZEL_SH",
"BAZEL_TARGET_CPU",
Expand Down
7 changes: 7 additions & 0 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"-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(".")) + "/"):
Expand Down Expand Up @@ -478,6 +484,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"-pass-exit-codes",
)
) + link_opts),
"%{link_libs}": get_starlark_list(link_libs),
"%{opt_compile_flags}": get_starlark_list(
[
# No debug symbols.
Expand Down
3 changes: 2 additions & 1 deletion tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def _impl(ctx):
iterate_over = "user_link_flags",
expand_if_available = "user_link_flags",
),
],
] + ([flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else []),
),
],
)
Expand Down Expand Up @@ -1191,6 +1191,7 @@ cc_toolchain_config = rule(
"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(),
Expand Down

0 comments on commit a3d7a0e

Please sign in to comment.