Skip to content

Commit 850be07

Browse files
committed
add a stopgap experimental_use_whole_archive_for_native_deps attribute
See bazelbuild#1268.
1 parent b778fca commit 850be07

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

rust/private/rust.bzl

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

rust/private/rustc.bzl

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def construct_arguments(
780780
rustc_flags.add("--codegen=linker=" + ld)
781781
rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")
782782

783-
_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration)
783+
_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration, ctx.attr.experimental_use_whole_archive_for_native_deps)
784784

785785
# These always need to be added, even if not linking this crate.
786786
add_crate_link_flags(rustc_flags, dep_info, force_all_deps_direct)
@@ -1259,13 +1259,16 @@ def _get_crate_dirname(crate):
12591259
"""
12601260
return crate.output.dirname
12611261

1262-
def _portable_link_flags(lib, use_pic, ambiguous_libs):
1262+
def _portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps):
12631263
artifact = get_preferred_artifact(lib, use_pic)
12641264
if ambiguous_libs and artifact.path in ambiguous_libs:
12651265
artifact = ambiguous_libs[artifact.path]
12661266
if lib.static_library or lib.pic_static_library:
1267+
modifiers = ""
1268+
if experimental_use_whole_archive_for_native_deps:
1269+
modifiers = ":+whole-archive"
12671270
return [
1268-
"-lstatic=%s" % get_lib_name(artifact),
1271+
"-lstatic%s=%s" % (modifiers, get_lib_name(artifact)),
12691272
]
12701273
elif _is_dylib(lib):
12711274
return [
@@ -1274,18 +1277,18 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs):
12741277

12751278
return []
12761279

1277-
def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs):
1278-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1280+
def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1281+
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
12791282
ret = []
12801283
for lib in linker_input.libraries:
12811284
if lib.alwayslink:
12821285
ret.extend(["-C", "link-arg=/WHOLEARCHIVE:%s" % get_preferred_artifact(lib, use_pic).path])
12831286
else:
1284-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1287+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
12851288
return ret
12861289

1287-
def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
1288-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1290+
def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1291+
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
12891292
ret = []
12901293
for lib in linker_input.libraries:
12911294
if lib.alwayslink:
@@ -1294,11 +1297,11 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
12941297
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path),
12951298
])
12961299
else:
1297-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1300+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
12981301
return ret
12991302

1300-
def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
1301-
linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1303+
def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1304+
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
13021305
ret = []
13031306
for lib in linker_input.libraries:
13041307
if lib.alwayslink:
@@ -1311,16 +1314,16 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
13111314
"link-arg=-Wl,--no-whole-archive",
13121315
])
13131316
else:
1314-
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs))
1317+
ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, experimental_use_whole_archive_for_native_deps))
13151318
return ret
13161319

1317-
def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs):
1318-
link_input, use_pic, _ = linker_input_and_use_pic_and_ambiguous_libs
1320+
def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps):
1321+
link_input, use_pic, _, _ = linker_input_and_use_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps
13191322

13201323
# De-duplicate names.
13211324
return depset([get_preferred_artifact(lib, use_pic).dirname for lib in link_input.libraries]).to_list()
13221325

1323-
def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate_type, toolchain, cc_toolchain, feature_configuration):
1326+
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):
13241327
"""Adds linker flags for all dependencies of the current target.
13251328
13261329
Args:
@@ -1332,7 +1335,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
13321335
toolchain (rust_toolchain): The current `rust_toolchain`
13331336
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`
13341337
feature_configuration (FeatureConfiguration): feature configuration to use with cc_toolchain
1335-
1338+
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
13361339
"""
13371340
if crate_type in ["lib", "rlib"]:
13381341
return
@@ -1347,15 +1350,15 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
13471350
make_link_flags = _make_link_flags_default
13481351

13491352
# TODO(hlopko): Remove depset flattening by using lambdas once we are on >=Bazel 5.0
1350-
args_and_pic_and_ambiguous_libs = [(arg, use_pic, ambiguous_libs) for arg in dep_info.transitive_noncrates.to_list()]
1351-
args.add_all(args_and_pic_and_ambiguous_libs, map_each = _libraries_dirnames, uniquify = True, format_each = "-Lnative=%s")
1353+
args_and_pic_and_ambiguous_libs_and_experimental_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()]
1354+
args.add_all(args_and_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps, map_each = _libraries_dirnames, uniquify = True, format_each = "-Lnative=%s")
13521355
if ambiguous_libs:
13531356
# If there are ambiguous libs, the disambiguation symlinks to them are
13541357
# all created in the same directory. Add it to the library search path.
13551358
ambiguous_libs_dirname = ambiguous_libs.values()[0].dirname
13561359
args.add("-Lnative={}".format(ambiguous_libs_dirname))
13571360

1358-
args.add_all(args_and_pic_and_ambiguous_libs, map_each = make_link_flags)
1361+
args.add_all(args_and_pic_and_ambiguous_libs_and_experimental_use_whole_archive_for_native_deps, map_each = make_link_flags)
13591362

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

rust/private/rustdoc.bzl

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

rust/private/rustdoc_test.bzl

Lines changed: 9 additions & 0 deletions
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)