@@ -780,7 +780,7 @@ def construct_arguments(
780
780
rustc_flags .add ("--codegen=linker=" + ld )
781
781
rustc_flags .add_joined ("--codegen" , link_args , join_with = " " , format_joined = "link-args=%s" )
782
782
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 )
784
784
785
785
# These always need to be added, even if not linking this crate.
786
786
add_crate_link_flags (rustc_flags , dep_info , force_all_deps_direct )
@@ -1259,13 +1259,16 @@ def _get_crate_dirname(crate):
1259
1259
"""
1260
1260
return crate .output .dirname
1261
1261
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 ):
1263
1263
artifact = get_preferred_artifact (lib , use_pic )
1264
1264
if ambiguous_libs and artifact .path in ambiguous_libs :
1265
1265
artifact = ambiguous_libs [artifact .path ]
1266
1266
if lib .static_library or lib .pic_static_library :
1267
+ modifiers = ""
1268
+ if experimental_use_whole_archive_for_native_deps :
1269
+ modifiers = ":+whole-archive"
1267
1270
return [
1268
- "-lstatic=%s" % get_lib_name (artifact ),
1271
+ "-lstatic%s =%s" % ( modifiers , get_lib_name (artifact ) ),
1269
1272
]
1270
1273
elif _is_dylib (lib ):
1271
1274
return [
@@ -1274,18 +1277,18 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs):
1274
1277
1275
1278
return []
1276
1279
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
1279
1282
ret = []
1280
1283
for lib in linker_input .libraries :
1281
1284
if lib .alwayslink :
1282
1285
ret .extend (["-C" , "link-arg=/WHOLEARCHIVE:%s" % get_preferred_artifact (lib , use_pic ).path ])
1283
1286
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 ))
1285
1288
return ret
1286
1289
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
1289
1292
ret = []
1290
1293
for lib in linker_input .libraries :
1291
1294
if lib .alwayslink :
@@ -1294,11 +1297,11 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs):
1294
1297
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact (lib , use_pic ).path ),
1295
1298
])
1296
1299
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 ))
1298
1301
return ret
1299
1302
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
1302
1305
ret = []
1303
1306
for lib in linker_input .libraries :
1304
1307
if lib .alwayslink :
@@ -1311,16 +1314,16 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
1311
1314
"link-arg=-Wl,--no-whole-archive" ,
1312
1315
])
1313
1316
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 ))
1315
1318
return ret
1316
1319
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
1319
1322
1320
1323
# De-duplicate names.
1321
1324
return depset ([get_preferred_artifact (lib , use_pic ).dirname for lib in link_input .libraries ]).to_list ()
1322
1325
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 ):
1324
1327
"""Adds linker flags for all dependencies of the current target.
1325
1328
1326
1329
Args:
@@ -1332,7 +1335,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
1332
1335
toolchain (rust_toolchain): The current `rust_toolchain`
1333
1336
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`
1334
1337
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
1336
1339
"""
1337
1340
if crate_type in ["lib" , "rlib" ]:
1338
1341
return
@@ -1347,15 +1350,15 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
1347
1350
make_link_flags = _make_link_flags_default
1348
1351
1349
1352
# 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" )
1352
1355
if ambiguous_libs :
1353
1356
# If there are ambiguous libs, the disambiguation symlinks to them are
1354
1357
# all created in the same directory. Add it to the library search path.
1355
1358
ambiguous_libs_dirname = ambiguous_libs .values ()[0 ].dirname
1356
1359
args .add ("-Lnative={}" .format (ambiguous_libs_dirname ))
1357
1360
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 )
1359
1362
1360
1363
for linkstamp_out in linkstamp_outs :
1361
1364
args .add_all (["-C" , "link-arg=%s" % linkstamp_out .path ])
0 commit comments