@@ -391,7 +391,7 @@ def get_cc_user_link_flags(ctx):
391
391
"""
392
392
return ctx .fragments .cpp .linkopts
393
393
394
- def get_linker_and_args (ctx , attr , crate_type , cc_toolchain , feature_configuration , rpaths , rustdoc = False ):
394
+ def get_linker_and_args (ctx , attr , crate_type , cc_toolchain , feature_configuration , rpaths , add_flags_for_binary = False ):
395
395
"""Gathers cc_common linker information
396
396
397
397
Args:
@@ -401,7 +401,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
401
401
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
402
402
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
403
403
rpaths (depset): Depset of directories where loader will look for libraries at runtime.
404
- rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `crate_type`.
404
+ add_flags_for_binary (bool, optional): Whether to add "bin" link flags to the command regardless of `crate_type`.
405
405
406
406
407
407
Returns:
@@ -412,7 +412,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
412
412
"""
413
413
user_link_flags = get_cc_user_link_flags (ctx )
414
414
415
- if crate_type in ("bin" ) or rustdoc :
415
+ if crate_type in ("bin" ) or add_flags_for_binary :
416
416
is_linking_dynamic_library = False
417
417
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
418
418
elif crate_type in ("dylib" ):
@@ -430,7 +430,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
430
430
else :
431
431
fail ("Unknown `crate_type`: {}" .format (crate_type ))
432
432
433
- # Add linkopt's from dependencies. This includes linkopts from transitive
433
+ # Add linkopts from dependencies. This includes linkopts from transitive
434
434
# dependencies since they get merged up.
435
435
for dep in getattr (attr , "deps" , []):
436
436
if CcInfo in dep and dep [CcInfo ].linking_context :
@@ -464,13 +464,15 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
464
464
def _process_build_scripts (
465
465
build_info ,
466
466
dep_info ,
467
- compile_inputs ):
467
+ compile_inputs ,
468
+ include_link_flags = True ):
468
469
"""Gathers the outputs from a target's `cargo_build_script` action.
469
470
470
471
Args:
471
472
build_info (BuildInfo): The target Build's dependency info.
472
473
dep_info (DepInfo): The Depinfo provider form the target Crate's set of inputs.
473
474
compile_inputs (depset): A set of all files that will participate in the build.
475
+ include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
474
476
475
477
Returns:
476
478
tuple: A tuple: A tuple of the following items:
@@ -479,7 +481,7 @@ def _process_build_scripts(
479
481
- (File): An optional path to a generated environment file from a `cargo_build_script` target
480
482
- (depset[File]): All direct and transitive build flags from the current build info.
481
483
"""
482
- extra_inputs , out_dir , build_env_file , build_flags_files = _create_extra_input_args (build_info , dep_info )
484
+ extra_inputs , out_dir , build_env_file , build_flags_files = _create_extra_input_args (build_info , dep_info , include_link_flags = include_link_flags )
483
485
compile_inputs = depset (transitive = [extra_inputs , compile_inputs ])
484
486
return compile_inputs , out_dir , build_env_file , build_flags_files
485
487
@@ -640,7 +642,8 @@ def collect_inputs(
640
642
build_info ,
641
643
stamp = False ,
642
644
force_depend_on_objects = False ,
643
- experimental_use_cc_common_link = False ):
645
+ experimental_use_cc_common_link = False ,
646
+ include_link_flags = True ):
644
647
"""Gather's the inputs and required input information for a rustc action
645
648
646
649
Args:
@@ -659,7 +662,8 @@ def collect_inputs(
659
662
force_depend_on_objects (bool, optional): Forces dependencies of this rule to be objects rather than
660
663
metadata, even for libraries. This is used in rustdoc tests.
661
664
experimental_use_cc_common_link (bool, optional): Whether rules_rust uses cc_common.link to link
662
- rust binaries.
665
+ rust binaries.
666
+ include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
663
667
664
668
Returns:
665
669
tuple: A tuple: A tuple of the following items:
@@ -773,7 +777,7 @@ def collect_inputs(
773
777
# For backwards compatibility, we also check the value of the `rustc_env_files` attribute when
774
778
# `crate_info.rustc_env_files` is not populated.
775
779
build_env_files = crate_info .rustc_env_files if crate_info .rustc_env_files else getattr (files , "rustc_env_files" , [])
776
- compile_inputs , out_dir , build_env_file , build_flags_files = _process_build_scripts (build_info , dep_info , compile_inputs )
780
+ compile_inputs , out_dir , build_env_file , build_flags_files = _process_build_scripts (build_info , dep_info , compile_inputs , include_link_flags = include_link_flags )
777
781
if build_env_file :
778
782
build_env_files = [f for f in build_env_files ] + [build_env_file ]
779
783
compile_inputs = depset (build_env_files , transitive = [compile_inputs ])
@@ -798,7 +802,8 @@ def construct_arguments(
798
802
build_flags_files ,
799
803
emit = ["dep-info" , "link" ],
800
804
force_all_deps_direct = False ,
801
- rustdoc = False ,
805
+ add_flags_for_binary = False ,
806
+ include_link_flags = True ,
802
807
stamp = False ,
803
808
remap_path_prefix = "" ,
804
809
use_json_output = False ,
@@ -827,7 +832,8 @@ def construct_arguments(
827
832
emit (list): Values for the --emit flag to rustc.
828
833
force_all_deps_direct (bool, optional): Whether to pass the transitive rlibs with --extern
829
834
to the commandline as opposed to -L.
830
- rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `emit` and `crate_type`.
835
+ add_flags_for_binary (bool, optional): Whether to add "bin" link flags to the command regardless of `emit` and `crate_type`.
836
+ include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
831
837
stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see
832
838
https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
833
839
remap_path_prefix (str, optional): A value used to remap `${pwd}` to. If set to None, no prefix will be set.
@@ -993,7 +999,7 @@ def construct_arguments(
993
999
add_edition_flags (rustc_flags , crate_info )
994
1000
995
1001
# Link!
996
- if ("link" in emit and crate_info .type not in ["rlib" , "lib" ]) or rustdoc :
1002
+ if ("link" in emit and crate_info .type not in ["rlib" , "lib" ]) or add_flags_for_binary :
997
1003
# Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc
998
1004
# linker since it won't understand.
999
1005
compilation_mode = ctx .var ["COMPILATION_MODE" ]
@@ -1004,7 +1010,7 @@ def construct_arguments(
1004
1010
else :
1005
1011
rpaths = depset ()
1006
1012
1007
- ld , link_args , link_env = get_linker_and_args (ctx , attr , crate_info .type , cc_toolchain , feature_configuration , rpaths , rustdoc )
1013
+ ld , link_args , link_env = get_linker_and_args (ctx , attr , crate_info .type , cc_toolchain , feature_configuration , rpaths , add_flags_for_binary = add_flags_for_binary )
1008
1014
1009
1015
env .update (link_env )
1010
1016
rustc_flags .add (ld , format = "--codegen=linker=%s" )
@@ -1013,7 +1019,7 @@ def construct_arguments(
1013
1019
# Additional context: https://github.com/rust-lang/rust/pull/36574
1014
1020
rustc_flags .add_all (link_args , format_each = "--codegen=link-arg=%s" )
1015
1021
1016
- _add_native_link_flags (rustc_flags , dep_info , linkstamp_outs , ambiguous_libs , crate_info .type , toolchain , cc_toolchain , feature_configuration , compilation_mode )
1022
+ _add_native_link_flags (rustc_flags , dep_info , linkstamp_outs , ambiguous_libs , crate_info .type , toolchain , cc_toolchain , feature_configuration , compilation_mode , include_link_flags = include_link_flags )
1017
1023
1018
1024
use_metadata = _depend_on_metadata (crate_info , force_depend_on_objects )
1019
1025
@@ -1650,12 +1656,13 @@ def add_edition_flags(args, crate):
1650
1656
if crate .edition != "2015" :
1651
1657
args .add (crate .edition , format = "--edition=%s" )
1652
1658
1653
- def _create_extra_input_args (build_info , dep_info ):
1659
+ def _create_extra_input_args (build_info , dep_info , include_link_flags = True ):
1654
1660
"""Gather additional input arguments from transitive dependencies
1655
1661
1656
1662
Args:
1657
1663
build_info (BuildInfo): The BuildInfo provider from the target Crate's set of inputs.
1658
1664
dep_info (DepInfo): The Depinfo provider form the target Crate's set of inputs.
1665
+ include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
1659
1666
1660
1667
Returns:
1661
1668
tuple: A tuple of the following items:
@@ -1680,7 +1687,7 @@ def _create_extra_input_args(build_info, dep_info):
1680
1687
build_env_file = build_info .rustc_env
1681
1688
if build_info .flags :
1682
1689
build_flags_files .append (build_info .flags )
1683
- if build_info .linker_flags :
1690
+ if build_info .linker_flags and include_link_flags :
1684
1691
build_flags_files .append (build_info .linker_flags )
1685
1692
input_files .append (build_info .linker_flags )
1686
1693
@@ -1894,8 +1901,8 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows
1894
1901
1895
1902
return []
1896
1903
1897
- def _make_link_flags_windows (linker_input_and_use_pic_and_ambiguous_libs , flavor_msvc ):
1898
- linker_input , use_pic , ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1904
+ def _make_link_flags_windows (make_link_flags_args , flavor_msvc ):
1905
+ linker_input , use_pic , ambiguous_libs , include_link_flags = make_link_flags_args
1899
1906
ret = []
1900
1907
for lib in linker_input .libraries :
1901
1908
if lib .alwayslink :
@@ -1910,31 +1917,31 @@ def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs, flavor
1910
1917
"-C" ,
1911
1918
"link-arg=-Wl,--no-whole-archive" ,
1912
1919
])
1913
- else :
1920
+ elif include_link_flags :
1914
1921
ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_for_windows , for_windows = True , flavor_msvc = flavor_msvc ))
1915
1922
return ret
1916
1923
1917
- def _make_link_flags_windows_msvc (linker_input_and_use_pic_and_ambiguous_libs ):
1918
- return _make_link_flags_windows (linker_input_and_use_pic_and_ambiguous_libs , flavor_msvc = True )
1924
+ def _make_link_flags_windows_msvc (make_link_flags_args ):
1925
+ return _make_link_flags_windows (make_link_flags_args , flavor_msvc = True )
1919
1926
1920
- def _make_link_flags_windows_gnu (linker_input_and_use_pic_and_ambiguous_libs ):
1921
- return _make_link_flags_windows (linker_input_and_use_pic_and_ambiguous_libs , flavor_msvc = False )
1927
+ def _make_link_flags_windows_gnu (make_link_flags_args ):
1928
+ return _make_link_flags_windows (make_link_flags_args , flavor_msvc = False )
1922
1929
1923
- def _make_link_flags_darwin (linker_input_and_use_pic_and_ambiguous_libs ):
1924
- linker_input , use_pic , ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1930
+ def _make_link_flags_darwin (make_link_flags_args ):
1931
+ linker_input , use_pic , ambiguous_libs , include_link_flags = make_link_flags_args
1925
1932
ret = []
1926
1933
for lib in linker_input .libraries :
1927
1934
if lib .alwayslink :
1928
1935
ret .extend ([
1929
1936
"-C" ,
1930
1937
("link-arg=-Wl,-force_load,%s" % get_preferred_artifact (lib , use_pic ).path ),
1931
1938
])
1932
- else :
1939
+ elif include_link_flags :
1933
1940
ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default , for_darwin = True ))
1934
1941
return ret
1935
1942
1936
- def _make_link_flags_default (linker_input_and_use_pic_and_ambiguous_libs ):
1937
- linker_input , use_pic , ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs
1943
+ def _make_link_flags_default (make_link_flags_args ):
1944
+ linker_input , use_pic , ambiguous_libs , include_link_flags = make_link_flags_args
1938
1945
ret = []
1939
1946
for lib in linker_input .libraries :
1940
1947
if lib .alwayslink :
@@ -1946,17 +1953,17 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs):
1946
1953
"-C" ,
1947
1954
"link-arg=-Wl,--no-whole-archive" ,
1948
1955
])
1949
- else :
1956
+ elif include_link_flags :
1950
1957
ret .extend (_portable_link_flags (lib , use_pic , ambiguous_libs , get_lib_name_default ))
1951
1958
return ret
1952
1959
1953
- def _libraries_dirnames (linker_input_and_use_pic_and_ambiguous_libs ):
1954
- link_input , use_pic , _ = linker_input_and_use_pic_and_ambiguous_libs
1960
+ def _libraries_dirnames (make_link_flags_args ):
1961
+ link_input , use_pic , _ , _ = make_link_flags_args
1955
1962
1956
1963
# De-duplicate names.
1957
1964
return depset ([get_preferred_artifact (lib , use_pic ).dirname for lib in link_input .libraries ]).to_list ()
1958
1965
1959
- def _add_native_link_flags (args , dep_info , linkstamp_outs , ambiguous_libs , crate_type , toolchain , cc_toolchain , feature_configuration , compilation_mode ):
1966
+ def _add_native_link_flags (args , dep_info , linkstamp_outs , ambiguous_libs , crate_type , toolchain , cc_toolchain , feature_configuration , compilation_mode , include_link_flags = True ):
1960
1967
"""Adds linker flags for all dependencies of the current target.
1961
1968
1962
1969
Args:
@@ -1969,6 +1976,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
1969
1976
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`
1970
1977
feature_configuration (FeatureConfiguration): feature configuration to use with cc_toolchain
1971
1978
compilation_mode (bool): The compilation mode for this build.
1979
+ include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
1972
1980
"""
1973
1981
if crate_type in ["lib" , "rlib" ]:
1974
1982
return
@@ -1986,15 +1994,15 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
1986
1994
get_lib_name = get_lib_name_default
1987
1995
1988
1996
# TODO(hlopko): Remove depset flattening by using lambdas once we are on >=Bazel 5.0
1989
- args_and_pic_and_ambiguous_libs = [(arg , use_pic , ambiguous_libs ) for arg in dep_info .transitive_noncrates .to_list ()]
1990
- args .add_all (args_and_pic_and_ambiguous_libs , map_each = _libraries_dirnames , uniquify = True , format_each = "-Lnative=%s" )
1997
+ make_link_flags_args = [(arg , use_pic , ambiguous_libs , include_link_flags ) for arg in dep_info .transitive_noncrates .to_list ()]
1998
+ args .add_all (make_link_flags_args , map_each = _libraries_dirnames , uniquify = True , format_each = "-Lnative=%s" )
1991
1999
if ambiguous_libs :
1992
2000
# If there are ambiguous libs, the disambiguation symlinks to them are
1993
2001
# all created in the same directory. Add it to the library search path.
1994
2002
ambiguous_libs_dirname = ambiguous_libs .values ()[0 ].dirname
1995
2003
args .add (ambiguous_libs_dirname , format = "-Lnative=%s" )
1996
2004
1997
- args .add_all (args_and_pic_and_ambiguous_libs , map_each = make_link_flags )
2005
+ args .add_all (make_link_flags_args , map_each = make_link_flags )
1998
2006
1999
2007
args .add_all (linkstamp_outs , before_each = "-C" , format_each = "link-args=%s" )
2000
2008
@@ -2006,11 +2014,12 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
2006
2014
map_each = _get_dirname ,
2007
2015
format_each = "-Lnative=%s" ,
2008
2016
)
2009
- args .add_all (
2010
- cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration ),
2011
- map_each = get_lib_name ,
2012
- format_each = "-ldylib=%s" ,
2013
- )
2017
+ if include_link_flags :
2018
+ args .add_all (
2019
+ cc_toolchain .dynamic_runtime_lib (feature_configuration = feature_configuration ),
2020
+ map_each = get_lib_name ,
2021
+ format_each = "-ldylib=%s" ,
2022
+ )
2014
2023
else :
2015
2024
# For all other crate types we want to link C++ runtime library statically
2016
2025
# (for example libstdc++.a or libc++.a).
@@ -2019,11 +2028,12 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
2019
2028
map_each = _get_dirname ,
2020
2029
format_each = "-Lnative=%s" ,
2021
2030
)
2022
- args .add_all (
2023
- cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration ),
2024
- map_each = get_lib_name ,
2025
- format_each = "-lstatic=%s" ,
2026
- )
2031
+ if include_link_flags :
2032
+ args .add_all (
2033
+ cc_toolchain .static_runtime_lib (feature_configuration = feature_configuration ),
2034
+ map_each = get_lib_name ,
2035
+ format_each = "-lstatic=%s" ,
2036
+ )
2027
2037
2028
2038
def _get_dirname (file ):
2029
2039
"""A helper function for `_add_native_link_flags`.
0 commit comments