19
19
"@bazel_tools//tools/build_defs/cc:action_names.bzl" ,
20
20
"CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME" ,
21
21
"CPP_LINK_EXECUTABLE_ACTION_NAME" ,
22
+ "CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME" ,
23
+ "CPP_LINK_STATIC_LIBRARY_ACTION_NAME" ,
22
24
)
23
25
load ("//rust/private:common.bzl" , "rust_common" )
24
26
load ("//rust/private:providers.bzl" , _BuildInfo = "BuildInfo" )
@@ -369,7 +371,7 @@ def get_cc_user_link_flags(ctx):
369
371
"""
370
372
return ctx .fragments .cpp .linkopts
371
373
372
- def get_linker_and_args (ctx , attr , crate_type , cc_toolchain , feature_configuration , rpaths ):
374
+ def get_linker_and_args (ctx , attr , crate_type , cc_toolchain , feature_configuration , rpaths , rustdoc = False ):
373
375
"""Gathers cc_common linker information
374
376
375
377
Args:
@@ -379,6 +381,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
379
381
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
380
382
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
381
383
rpaths (depset): Depset of directories where loader will look for libraries at runtime.
384
+ rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `crate_type`.
382
385
383
386
384
387
Returns:
@@ -389,13 +392,23 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
389
392
"""
390
393
user_link_flags = get_cc_user_link_flags (ctx )
391
394
392
- if crate_type == "proc-macro" :
395
+ if crate_type in ("bin" ) or rustdoc :
396
+ is_linking_dynamic_library = False
397
+ action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
398
+ elif crate_type in ("dylib" ):
399
+ is_linking_dynamic_library = True
400
+ action_name = CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME
401
+ elif crate_type in ("staticlib" ):
402
+ is_linking_dynamic_library = False
403
+ action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME
404
+ elif crate_type in ("cdylib" , "proc-macro" ):
393
405
# Proc macros get compiled as shared libraries to be loaded by the compiler.
394
406
is_linking_dynamic_library = True
395
407
action_name = CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME
408
+ elif crate_type in ("lib" , "rlib" ):
409
+ fail ("Invalid `crate_type` for linking action: {}" .format (crate_type ))
396
410
else :
397
- is_linking_dynamic_library = False
398
- action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
411
+ fail ("Unknown `crate_type`: {}" .format (crate_type ))
399
412
400
413
# Add linkopt's from dependencies. This includes linkopts from transitive
401
414
# dependencies since they get merged up.
@@ -751,7 +764,7 @@ def construct_arguments(
751
764
build_flags_files ,
752
765
emit = ["dep-info" , "link" ],
753
766
force_all_deps_direct = False ,
754
- force_link = False ,
767
+ rustdoc = False ,
755
768
stamp = False ,
756
769
remap_path_prefix = "" ,
757
770
use_json_output = False ,
@@ -779,7 +792,7 @@ def construct_arguments(
779
792
emit (list): Values for the --emit flag to rustc.
780
793
force_all_deps_direct (bool, optional): Whether to pass the transitive rlibs with --extern
781
794
to the commandline as opposed to -L.
782
- force_link (bool, optional): Whether to add link flags to the command regardless of `emit`.
795
+ rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `emit` and `crate_type `.
783
796
stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see
784
797
https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
785
798
remap_path_prefix (str, optional): A value used to remap `${pwd}` to. If set to None, no prefix will be set.
@@ -954,7 +967,7 @@ def construct_arguments(
954
967
add_edition_flags (rustc_flags , crate_info )
955
968
956
969
# Link!
957
- if ("link" in emit and crate_info .type not in ["rlib" , "lib" ]) or force_link :
970
+ if ("link" in emit and crate_info .type not in ["rlib" , "lib" ]) or rustdoc :
958
971
# Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc
959
972
# linker since it won't understand.
960
973
compilation_mode = ctx .var ["COMPILATION_MODE" ]
@@ -965,7 +978,7 @@ def construct_arguments(
965
978
else :
966
979
rpaths = depset ([])
967
980
968
- ld , link_args , link_env = get_linker_and_args (ctx , attr , crate_info .type , cc_toolchain , feature_configuration , rpaths )
981
+ ld , link_args , link_env = get_linker_and_args (ctx , attr , crate_info .type , cc_toolchain , feature_configuration , rpaths , rustdoc )
969
982
970
983
env .update (link_env )
971
984
rustc_flags .add ("--codegen=linker=" + ld )
0 commit comments