Skip to content

Commit 41c1e04

Browse files
krasimirgggoffrie
authored andcommitted
add a stopgap experimental_use_whole_archive_for_native_deps attribute (bazelbuild#1269)
This adds a new stopgap attribute enabling us to fall back to the old rustc behavior wrt whole-archive handling, see bazelbuild#1268.
1 parent caf4106 commit 41c1e04

File tree

7 files changed

+95
-45
lines changed

7 files changed

+95
-45
lines changed

docs/defs.md

+18-11
Large diffs are not rendered by default.

docs/flatten.md

+23-13
Large diffs are not rendered by default.

docs/rust_doc.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
## rust_doc
1010

1111
<pre>
12-
rust_doc(<a href="#rust_doc-name">name</a>, <a href="#rust_doc-crate">crate</a>, <a href="#rust_doc-html_after_content">html_after_content</a>, <a href="#rust_doc-html_before_content">html_before_content</a>, <a href="#rust_doc-html_in_header">html_in_header</a>, <a href="#rust_doc-markdown_css">markdown_css</a>)
12+
rust_doc(<a href="#rust_doc-name">name</a>, <a href="#rust_doc-crate">crate</a>, <a href="#rust_doc-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>, <a href="#rust_doc-html_after_content">html_after_content</a>,
13+
<a href="#rust_doc-html_before_content">html_before_content</a>, <a href="#rust_doc-html_in_header">html_in_header</a>, <a href="#rust_doc-markdown_css">markdown_css</a>)
1314
</pre>
1415

1516
Generates code documentation.
@@ -56,6 +57,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
5657
| :------------- | :------------- | :------------- | :------------- | :------------- |
5758
| <a id="rust_doc-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
5859
| <a id="rust_doc-crate"></a>crate | The label of the target to generate code documentation for.<br><br><code>rust_doc</code> can generate HTML code documentation for the source files of <code>rust_library</code> or <code>rust_binary</code> targets. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
60+
| <a id="rust_doc-experimental_use_whole_archive_for_native_deps"></a>experimental_use_whole_archive_for_native_deps | Whether to use +whole-archive linking modifier for native dependencies.<br><br>TODO: This is a stopgap feature and will be removed, see https://github.com/bazelbuild/rules_rust/issues/1268. | Boolean | optional | False |
5961
| <a id="rust_doc-html_after_content"></a>html_after_content | File to add in <code>&lt;body&gt;</code>, after content. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
6062
| <a id="rust_doc-html_before_content"></a>html_before_content | File to add in <code>&lt;body&gt;</code>, before content. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
6163
| <a id="rust_doc-html_in_header"></a>html_in_header | File to add to <code>&lt;head&gt;</code>. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
@@ -67,7 +69,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
6769
## rust_doc_test
6870

6971
<pre>
70-
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>)
72+
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>)
7173
</pre>
7274

7375
Runs Rust documentation tests.
@@ -115,5 +117,6 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
115117
| :------------- | :------------- | :------------- | :------------- | :------------- |
116118
| <a id="rust_doc_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
117119
| <a id="rust_doc_test-crate"></a>crate | The label of the target to generate code documentation for. <code>rust_doc_test</code> can generate HTML code documentation for the source files of <code>rust_library</code> or <code>rust_binary</code> targets. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
120+
| <a id="rust_doc_test-experimental_use_whole_archive_for_native_deps"></a>experimental_use_whole_archive_for_native_deps | Whether to use +whole-archive linking modifier for native dependencies.<br><br>TODO: This is a stopgap feature and will be removed, see https://github.com/bazelbuild/rules_rust/issues/1268. | Boolean | optional | False |
118121

119122

rust/private/rust.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ _common_attrs = {
493493
"edition": attr.string(
494494
doc = "The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.",
495495
),
496+
"experimental_use_whole_archive_for_native_deps": attr.bool(
497+
doc = dedent("""\
498+
Whether to use +whole-archive linking modifier for native dependencies.
499+
500+
TODO: This is a stopgap feature and will be removed,
501+
see https://github.com/bazelbuild/rules_rust/issues/1268.
502+
"""),
503+
default = False,
504+
),
496505
# Previously `proc_macro_deps` were a part of `deps`, and then proc_macro_host_transition was
497506
# used into cfg="host" using `@local_config_platform//:host`.
498507
# This fails for remote execution, which needs cfg="exec", and there isn't anything like

rust/private/rustc.bzl

+22-19
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def construct_arguments(
762762
rustc_flags.add("--codegen=linker=" + ld)
763763
rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")
764764

765-
_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration)
765+
_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration, attr.experimental_use_whole_archive_for_native_deps)
766766

767767
# These always need to be added, even if not linking this crate.
768768
extra_link_inputs = add_crate_link_flags(ctx, toolchain, rustc_flags, crate_info, dep_info, force_all_deps_direct)
@@ -1306,13 +1306,16 @@ def _get_crate_dirname(crate):
13061306
"""
13071307
return crate.output.dirname
13081308

1309-
def _portable_link_flags(lib, use_pic, ambiguous_libs):
1309+
def _portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps):
13101310
artifact = get_preferred_artifact(lib, use_pic)
13111311
if ambiguous_libs and artifact.path in ambiguous_libs:
13121312
artifact = ambiguous_libs[artifact.path]
13131313
if lib.static_library or lib.pic_static_library:
1314+
modifiers = ""
1315+
if experimental_use_whole_archive_for_native_deps:
1316+
modifiers = ":+whole-archive"
13141317
return [
1315-
"-lstatic=%s" % get_lib_name(artifact),
1318+
"-lstatic%s=%s" % (modifiers, get_lib_name(artifact)),
13161319
]
13171320
elif _is_dylib(lib):
13181321
return [
@@ -1321,18 +1324,18 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs):
13211324

13221325
return []
13231326

1324-
def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs):
1325-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1327+
def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1328+
linker_input, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps = linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps
13261329
ret = []
13271330
for lib in linker_input.libraries:
13281331
if lib.alwayslink:
13291332
ret.extend(["-C", "link-arg=/WHOLEARCHIVE:%s" % get_preferred_artifact(lib, use_pic).path])
13301333
else:
1331-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1334+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
13321335
return ret
13331336

1334-
def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
1335-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1337+
def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1338+
linker_input, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps = linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps
13361339
ret = []
13371340
for lib in linker_input.libraries:
13381341
if lib.alwayslink:
@@ -1341,11 +1344,11 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
13411344
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path),
13421345
])
13431346
else:
1344-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1347+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
13451348
return ret
13461349

1347-
def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
1348-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1350+
def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1351+
linker_input, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps = linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps
13491352
ret = []
13501353
for lib in linker_input.libraries:
13511354
if lib.alwayslink:
@@ -1358,16 +1361,16 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
13581361
"link-arg=-Wl,--no-whole-archive",
13591362
])
13601363
else:
1361-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1364+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
13621365
return ret
13631366

1364-
def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs):
1365-
link_input, use_pic, _ = linker_input_and_use_pic_and_ambiguous_libs
1367+
def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1368+
link_input, use_pic, _, _ = linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps
13661369

13671370
# De-duplicate names.
13681371
return depset([get_preferred_artifact(lib, use_pic).dirname for lib in link_input.libraries]).to_list()
13691372

1370-
def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate_type, toolchain, cc_toolchain, feature_configuration):
1373+
def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate_type, toolchain, cc_toolchain, feature_configuration, experimental_use_whole_archive_for_native_deps):
13711374
"""Adds linker flags for all dependencies of the current target.
13721375
13731376
Args:
@@ -1379,7 +1382,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
13791382
toolchain (rust_toolchain): The current `rust_toolchain`
13801383
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`
13811384
feature_configuration (FeatureConfiguration): feature configuration to use with cc_toolchain
1382-
1385+
experimental_use_whole_archive_for_native_deps (bool): Whether to use the whole-archive link modifier for native deps, see https://github.com/bazelbuild/rules_rust/issues/1268
13831386
"""
13841387
if crate_type in ["lib", "rlib"]:
13851388
return
@@ -1394,15 +1397,15 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
13941397
make_link_flags = _make_link_flags_default
13951398

13961399
# TODO(hlopko): Remove depset flattening by using lambdas once we are on >=Bazel 5.0
1397-
args_and_pic_and_ambiguous_libs = [(arg, use_pic, ambiguous_libs) for arg in dep_info.transitive_noncrates.to_list()]
1398-
args.add_all(args_and_pic_and_ambiguous_libs, map_each = _libraries_dirnames, uniquify = True, format_each = "-Lnative=%s")
1400+
args_and_pic_and_ambiguous_libs_and_use_whole_archive_for_native_deps = [(arg, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps) for arg in dep_info.transitive_noncrates.to_list()]
1401+
args.add_all(args_and_pic_and_ambiguous_libs_and_use_whole_archive_for_native_deps, map_each = _libraries_dirnames, uniquify = True, format_each = "-Lnative=%s")
13991402
if ambiguous_libs:
14001403
# If there are ambiguous libs, the disambiguation symlinks to them are
14011404
# all created in the same directory. Add it to the library search path.
14021405
ambiguous_libs_dirname = ambiguous_libs.values()[0].dirname
14031406
args.add("-Lnative={}".format(ambiguous_libs_dirname))
14041407

1405-
args.add_all(args_and_pic_and_ambiguous_libs, map_each = make_link_flags)
1408+
args.add_all(args_and_pic_and_ambiguous_libs_and_use_whole_archive_for_native_deps, map_each = make_link_flags)
14061409

14071410
for linkstamp_out in linkstamp_outs:
14081411
args.add_all(["-C", "link-arg=%s" % linkstamp_out.path])

rust/private/rustdoc.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ rust_doc = rule(
266266
providers = [rust_common.crate_info],
267267
mandatory = True,
268268
),
269+
"experimental_use_whole_archive_for_native_deps": attr.bool(
270+
doc = dedent("""\
271+
Whether to use +whole-archive linking modifier for native dependencies.
272+
273+
TODO: This is a stopgap feature and will be removed,
274+
see https://github.com/bazelbuild/rules_rust/issues/1268.
275+
"""),
276+
default = False,
277+
),
269278
"html_after_content": attr.label(
270279
doc = "File to add in `<body>`, after content.",
271280
allow_single_file = [".html", ".md"],

rust/private/rustdoc_test.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ rust_doc_test = rule(
154154
providers = [rust_common.crate_info],
155155
mandatory = True,
156156
),
157+
"experimental_use_whole_archive_for_native_deps": attr.bool(
158+
doc = dedent("""\
159+
Whether to use +whole-archive linking modifier for native dependencies.
160+
161+
TODO: This is a stopgap feature and will be removed,
162+
see https://github.com/bazelbuild/rules_rust/issues/1268.
163+
"""),
164+
default = False,
165+
),
157166
"_cc_toolchain": attr.label(
158167
doc = (
159168
"In order to use find_cc_toolchain, your rule has to depend " +

0 commit comments

Comments
 (0)