Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target specific rustc_flags last to the command line #2927

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,6 @@ def construct_arguments(
# Deduplicate data paths due to https://github.com/bazelbuild/bazel/issues/14681
data_paths = depset(direct = getattr(attr, "data", []), transitive = [crate_info.compile_data_targets]).to_list()

rustc_flags.add_all(
expand_list_element_locations(
ctx,
getattr(attr, "rustc_flags", []),
data_paths,
),
)
add_edition_flags(rustc_flags, crate_info)

# Link!
Expand Down Expand Up @@ -1105,6 +1098,15 @@ def construct_arguments(
if _is_no_std(ctx, toolchain, crate_info):
rustc_flags.add('--cfg=feature="no_std"')

# Add target specific flags last, so they can override previous flags
rustc_flags.add_all(
expand_list_element_locations(
ctx,
getattr(attr, "rustc_flags", []),
data_paths,
),
)

# Needed for bzlmod-aware runfiles resolution.
env["REPOSITORY_NAME"] = ctx.label.workspace_name

Expand Down
10 changes: 7 additions & 3 deletions test/toolchain/toolchain_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ EXEC_TOOLCHAIN_FLAG = "missing"
TOOLCHAIN_FLAG = "before"
CONFIG_FLAG = "after"
CRATE_FLAGS = {"cdylib": ["cdylib_flag"], "rlib": ["rlib_flag"]}
TARGET_FLAG = "-Ccodegen-units=1"

def _toolchain_adds_rustc_flags_impl(ctx, crate_type):
""" Tests adding extra_rustc_flags on the toolchain, asserts that:

- extra_rustc_flags added by the toolchain are applied BEFORE flags added by a config on the commandline
- The exec flags from the toolchain don't go on the commandline for a non-exec target
- crate type rustc flags are added
- target specific rustc flags are added AFTER the crate type rustc flags
"""
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)
Expand All @@ -25,16 +27,16 @@ def _toolchain_adds_rustc_flags_impl(ctx, crate_type):

asserts.true(
env,
action.argv[-2:] == [TOOLCHAIN_FLAG, CONFIG_FLAG],
action.argv[-3:] == [TOOLCHAIN_FLAG, CONFIG_FLAG, TARGET_FLAG],
"Unexpected rustc flags: {}\nShould have ended with: {}".format(
action.argv,
[TOOLCHAIN_FLAG, CONFIG_FLAG],
[TOOLCHAIN_FLAG, CONFIG_FLAG, TARGET_FLAG],
),
)

asserts.true(
env,
action.argv[-3] == CRATE_FLAGS[crate_type][0],
action.argv[-4] == CRATE_FLAGS[crate_type][0],
"Unexpected rustc flags: {}\nShould have contained: {}".format(
action.argv,
CRATE_FLAGS["rlib"],
Expand Down Expand Up @@ -140,12 +142,14 @@ def _define_targets():
name = "lib",
srcs = ["lib.rs"],
edition = "2021",
rustc_flags = [TARGET_FLAG],
)

rust_shared_library(
name = "shared_lib",
srcs = ["lib.rs"],
edition = "2021",
rustc_flags = [TARGET_FLAG],
)

native.filegroup(
Expand Down