Skip to content

Commit

Permalink
fix: crate_type more accurately corresponds to CC linking actions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim authored Jun 23, 2023
1 parent 4f4e2b1 commit 9442aed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 21 additions & 8 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME",
"CPP_LINK_EXECUTABLE_ACTION_NAME",
"CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME",
"CPP_LINK_STATIC_LIBRARY_ACTION_NAME",
)
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:providers.bzl", _BuildInfo = "BuildInfo")
Expand Down Expand Up @@ -369,7 +371,7 @@ def get_cc_user_link_flags(ctx):
"""
return ctx.fragments.cpp.linkopts

def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configuration, rpaths):
def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configuration, rpaths, rustdoc = False):
"""Gathers cc_common linker information
Args:
Expand All @@ -379,6 +381,7 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
rpaths (depset): Depset of directories where loader will look for libraries at runtime.
rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `crate_type`.
Returns:
Expand All @@ -389,13 +392,23 @@ def get_linker_and_args(ctx, attr, crate_type, cc_toolchain, feature_configurati
"""
user_link_flags = get_cc_user_link_flags(ctx)

if crate_type == "proc-macro":
if crate_type in ("bin") or rustdoc:
is_linking_dynamic_library = False
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
elif crate_type in ("dylib"):
is_linking_dynamic_library = True
action_name = CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME
elif crate_type in ("staticlib"):
is_linking_dynamic_library = False
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME
elif crate_type in ("cdylib", "proc-macro"):
# Proc macros get compiled as shared libraries to be loaded by the compiler.
is_linking_dynamic_library = True
action_name = CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME
elif crate_type in ("lib", "rlib"):
fail("Invalid `crate_type` for linking action: {}".format(crate_type))
else:
is_linking_dynamic_library = False
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME
fail("Unknown `crate_type`: {}".format(crate_type))

# Add linkopt's from dependencies. This includes linkopts from transitive
# dependencies since they get merged up.
Expand Down Expand Up @@ -751,7 +764,7 @@ def construct_arguments(
build_flags_files,
emit = ["dep-info", "link"],
force_all_deps_direct = False,
force_link = False,
rustdoc = False,
stamp = False,
remap_path_prefix = "",
use_json_output = False,
Expand Down Expand Up @@ -779,7 +792,7 @@ def construct_arguments(
emit (list): Values for the --emit flag to rustc.
force_all_deps_direct (bool, optional): Whether to pass the transitive rlibs with --extern
to the commandline as opposed to -L.
force_link (bool, optional): Whether to add link flags to the command regardless of `emit`.
rustdoc (bool, optional): Whether to add "bin" link flags to the command regardless of `emit` and `crate_type`.
stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see
https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
remap_path_prefix (str, optional): A value used to remap `${pwd}` to. If set to None, no prefix will be set.
Expand Down Expand Up @@ -954,7 +967,7 @@ def construct_arguments(
add_edition_flags(rustc_flags, crate_info)

# Link!
if ("link" in emit and crate_info.type not in ["rlib", "lib"]) or force_link:
if ("link" in emit and crate_info.type not in ["rlib", "lib"]) or rustdoc:
# Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc
# linker since it won't understand.
compilation_mode = ctx.var["COMPILATION_MODE"]
Expand All @@ -965,7 +978,7 @@ def construct_arguments(
else:
rpaths = depset([])

ld, link_args, link_env = get_linker_and_args(ctx, attr, crate_info.type, cc_toolchain, feature_configuration, rpaths)
ld, link_args, link_env = get_linker_and_args(ctx, attr, crate_info.type, cc_toolchain, feature_configuration, rpaths, rustdoc)

env.update(link_env)
rustc_flags.add("--codegen=linker=" + ld)
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def rustdoc_compile_action(
build_flags_files = build_flags_files,
emit = [],
remap_path_prefix = None,
force_link = True,
rustdoc = True,
force_depend_on_objects = is_test,
)

Expand Down

0 comments on commit 9442aed

Please sign in to comment.