Skip to content

Commit 9442aed

Browse files
authored
fix: crate_type more accurately corresponds to CC linking actions (#1975)
1 parent 4f4e2b1 commit 9442aed

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

rust/private/rustc.bzl

+21-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ load(
1919
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
2020
"CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME",
2121
"CPP_LINK_EXECUTABLE_ACTION_NAME",
22+
"CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME",
23+
"CPP_LINK_STATIC_LIBRARY_ACTION_NAME",
2224
)
2325
load("//rust/private:common.bzl", "rust_common")
2426
load("//rust/private:providers.bzl", _BuildInfo = "BuildInfo")
@@ -369,7 +371,7 @@ def get_cc_user_link_flags(ctx):
369371
"""
370372
return ctx.fragments.cpp.linkopts
371373

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):
373375
"""Gathers cc_common linker information
374376
375377
Args:
@@ -379,6 +381,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
379381
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
380382
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
381383
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`.
382385
383386
384387
Returns:
@@ -389,13 +392,23 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
389392
"""
390393
user_link_flags = get_cc_user_link_flags(ctx)
391394

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"):
393405
# Proc macros get compiled as shared libraries to be loaded by the compiler.
394406
is_linking_dynamic_library = True
395407
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))
396410
else:
397-
is_linking_dynamic_library = False
398-
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
411+
fail("Unknown `crate_type`: {}".format(crate_type))
399412

400413
# Add linkopt's from dependencies. This includes linkopts from transitive
401414
# dependencies since they get merged up.
@@ -751,7 +764,7 @@ def construct_arguments(
751764
build_flags_files,
752765
emit = ["dep-info", "link"],
753766
force_all_deps_direct = False,
754-
force_link = False,
767+
rustdoc = False,
755768
stamp = False,
756769
remap_path_prefix = "",
757770
use_json_output = False,
@@ -779,7 +792,7 @@ def construct_arguments(
779792
emit (list): Values for the --emit flag to rustc.
780793
force_all_deps_direct (bool, optional): Whether to pass the transitive rlibs with --extern
781794
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`.
783796
stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see
784797
https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
785798
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(
954967
add_edition_flags(rustc_flags, crate_info)
955968

956969
# 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:
958971
# Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc
959972
# linker since it won't understand.
960973
compilation_mode = ctx.var["COMPILATION_MODE"]
@@ -965,7 +978,7 @@ def construct_arguments(
965978
else:
966979
rpaths = depset([])
967980

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)
969982

970983
env.update(link_env)
971984
rustc_flags.add("--codegen=linker=" + ld)

rust/private/rustdoc.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def rustdoc_compile_action(
122122
build_flags_files = build_flags_files,
123123
emit = [],
124124
remap_path_prefix = None,
125-
force_link = True,
125+
rustdoc = True,
126126
force_depend_on_objects = is_test,
127127
)
128128

0 commit comments

Comments
 (0)