Skip to content

WIP: Add a general mechanism for setting rustflags in Cargo for the current crate only #11046

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn cli() -> App {
))
.arg_unit_graph()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help bench` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn cli() -> App {
.arg_unit_graph()
.arg_future_incompat_report()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help build` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn cli() -> App {
.arg_unit_graph()
.arg_future_incompat_report()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help check` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn cli() -> App {
.arg_ignore_rust_version()
.arg_unit_graph()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help doc` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn cli() -> App {
))
.arg_ignore_rust_version()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help fix` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn cli() -> App {
)
.arg_message_format()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help install` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn cli() -> App {
)
.arg_manifest_path()
.arg_jobs()
.arg_rustflags()
.after_help("Run `cargo help package` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn cli() -> App {
.arg_unit_graph()
.arg_ignore_rust_version()
.arg_timings()
.arg_rustflags()
.after_help("Run `cargo help run` for more detailed information.\n")
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn cli() -> App {
.arg_unit_graph()
.arg_future_incompat_report()
.arg_timings()
.arg_rustflags()
.after_help(
"Run `cargo help test` for more detailed information.\n\
Run `cargo test -- --help` for test binary options.\n",
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,13 +1302,16 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
// Fill out a bunch more information that we'll be tracking typically
// hashed to take up less space on disk as we just need to know when things
// change.
let extra_flags = if unit.mode.is_doc() {
let mut rustflags = if unit.mode.is_doc() {
cx.bcx.rustdocflags_args(unit)
} else {
cx.bcx.rustflags_args(unit)
}
.to_vec();

// Append the command line user specified RUSTFLAGS to the env var or config RUSTFLAGS.
rustflags.extend(unit.rustflags.iter().map(InternedString::to_string));

let profile_hash = util::hash_u64((
&unit.profile,
unit.mode,
Expand Down Expand Up @@ -1345,7 +1348,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
metadata,
config: config.finish(),
compile_kind,
rustflags: extra_flags,
rustflags,
fs_status: FsStatus::Stale,
outputs,
})
Expand Down
6 changes: 6 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,15 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);

rustc.args(cx.bcx.rustflags_args(unit));

// Append any user specified rustflags on the command line
// via the --rustflag argument.
rustc.args(&unit.rustflags);

if cx.bcx.config.cli_unstable().binary_dep_depinfo {
rustc.arg("-Z").arg("binary-dep-depinfo");
}

let mut output_options = OutputOptions::new(cx, unit);
let package_id = unit.pkg.package_id();
let target = Target::clone(&unit.target);
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ pub fn generate_std_roots(
*kind,
mode,
features.clone(),
/*rustflags*/
Default::default(), // Setting command line rustflags for the standard library is not supported
/*is_std*/ true,
/*dep_hash*/ 0,
IsArtifact::No,
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct UnitInner {
/// The `cfg` features to enable for this unit.
/// This must be sorted.
pub features: Vec<InternedString>,
/// The rustflags to set for this unit.
pub rustflags: Vec<InternedString>,
// if `true`, the dependency is an artifact dependency, requiring special handling when
// calculating output directories, linkage and environment variables provided to builds.
pub artifact: IsArtifact,
Expand Down Expand Up @@ -181,6 +183,7 @@ impl UnitInterner {
kind: CompileKind,
mode: CompileMode,
features: Vec<InternedString>,
rustflags: Vec<InternedString>,
is_std: bool,
dep_hash: u64,
artifact: IsArtifact,
Expand Down Expand Up @@ -213,6 +216,7 @@ impl UnitInterner {
kind,
mode,
features,
rustflags,
is_std,
dep_hash,
artifact,
Expand Down
21 changes: 20 additions & 1 deletion src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn compute_deps(
unit,
dep_pkg,
dep_lib,
/*rustflags*/ Default::default(),
dep_unit_for,
unit.kind,
mode,
Expand All @@ -295,6 +296,7 @@ fn compute_deps(
unit,
dep_pkg,
dep_lib,
/*rustflags*/ Default::default(),
dep_unit_for,
CompileKind::Host,
mode,
Expand All @@ -307,6 +309,7 @@ fn compute_deps(
unit,
dep_pkg,
dep_lib,
/*rustflags*/ Default::default(),
dep_unit_for,
unit.kind.for_target(dep_lib),
mode,
Expand Down Expand Up @@ -377,6 +380,7 @@ fn compute_deps(
unit,
&unit.pkg,
t,
/*rustflags*/ Default::default(),
UnitFor::new_normal(unit_for.root_compile_kind()),
unit.kind.for_target(t),
CompileMode::Build,
Expand Down Expand Up @@ -479,6 +483,7 @@ fn compute_deps_custom_build(
unit,
&unit.pkg,
&unit.target,
/*rustflags*/ Default::default(),
script_unit_for,
// Build scripts always compiled for the host.
CompileKind::Host,
Expand Down Expand Up @@ -568,6 +573,7 @@ fn artifact_targets_to_unit_deps(
target
.clone()
.set_kind(TargetKind::Lib(vec![target_kind.clone()])),
/*rustflags*/ Default::default(),
parent_unit_for,
compile_kind,
CompileMode::Build,
Expand All @@ -580,6 +586,7 @@ fn artifact_targets_to_unit_deps(
parent,
artifact_pkg,
target,
/*rustflags*/ Default::default(),
parent_unit_for,
compile_kind,
CompileMode::Build,
Expand Down Expand Up @@ -655,6 +662,7 @@ fn compute_deps_doc(
unit,
dep_pkg,
dep_lib,
/*rustflags*/ Default::default(),
dep_unit_for,
unit.kind.for_target(dep_lib),
mode,
Expand All @@ -669,6 +677,7 @@ fn compute_deps_doc(
unit,
dep_pkg,
dep_lib,
/*rustflags*/ Default::default(),
dep_unit_for,
unit.kind.for_target(dep_lib),
unit.mode,
Expand Down Expand Up @@ -699,6 +708,7 @@ fn compute_deps_doc(
unit,
&unit.pkg,
lib,
/*rustflags*/ Default::default(),
dep_unit_for,
unit.kind.for_target(lib),
unit.mode,
Expand All @@ -717,6 +727,7 @@ fn compute_deps_doc(
scrape_unit,
&scrape_unit.pkg,
&scrape_unit.target,
/*rustflags*/ Default::default(),
unit_for,
scrape_unit.kind,
scrape_unit.mode,
Expand All @@ -740,11 +751,15 @@ fn maybe_lib(
.map(|t| {
let mode = check_or_build_mode(unit.mode, t);
let dep_unit_for = unit_for.with_dependency(unit, t, unit_for.root_compile_kind());

// When adding the lib targets for the current unit also pass down the user specified
// rustflags for the package.
new_unit_dep(
state,
unit,
&unit.pkg,
t,
unit.rustflags.clone(),
dep_unit_for,
unit.kind.for_target(t),
mode,
Expand Down Expand Up @@ -805,6 +820,7 @@ fn dep_build_script(
unit,
&unit.pkg,
t,
Default::default(),
script_unit_for,
unit.kind,
CompileMode::RunCustomBuild,
Expand Down Expand Up @@ -839,6 +855,7 @@ fn new_unit_dep(
parent: &Unit,
pkg: &Package,
target: &Target,
rustflags: Vec<InternedString>,
unit_for: UnitFor,
kind: CompileKind,
mode: CompileMode,
Expand All @@ -853,7 +870,7 @@ fn new_unit_dep(
kind,
);
new_unit_dep_with_profile(
state, parent, pkg, target, unit_for, kind, mode, profile, artifact,
state, parent, pkg, target, rustflags, unit_for, kind, mode, profile, artifact,
)
}

Expand All @@ -862,6 +879,7 @@ fn new_unit_dep_with_profile(
parent: &Unit,
pkg: &Package,
target: &Target,
rustflags: Vec<InternedString>,
unit_for: UnitFor,
kind: CompileKind,
mode: CompileMode,
Expand All @@ -885,6 +903,7 @@ fn new_unit_dep_with_profile(
kind,
mode,
features,
rustflags,
state.is_std,
/*dep_hash*/ 0,
artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes),
Expand Down
19 changes: 19 additions & 0 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct CompileOptions {
pub build_config: BuildConfig,
/// Feature flags requested by the user.
pub cli_features: CliFeatures,
/// Rustflags requested by the user.
pub rustflags: Vec<InternedString>,
/// A set of packages to build.
pub spec: Packages,
/// Filter to apply to the root package to select which targets will be
Expand Down Expand Up @@ -91,6 +93,7 @@ impl CompileOptions {
Ok(CompileOptions {
build_config: BuildConfig::new(config, jobs, keep_going, &[], mode)?,
cli_features: CliFeatures::new_all(false),
rustflags: Vec::new(),
spec: ops::Packages::Packages(Vec::new()),
filter: CompileFilter::Default {
required_features_filterable: false,
Expand Down Expand Up @@ -335,6 +338,7 @@ pub fn create_bcx<'a, 'cfg>(
ref build_config,
ref spec,
ref cli_features,
ref rustflags,
ref filter,
ref target_rustdoc_args,
ref target_rustc_args,
Expand Down Expand Up @@ -492,6 +496,7 @@ pub fn create_bcx<'a, 'cfg>(
&resolve,
&workspace_resolve,
&resolved_features,
&rustflags,
&pkg_set,
&profiles,
interner,
Expand Down Expand Up @@ -532,6 +537,7 @@ pub fn create_bcx<'a, 'cfg>(
&resolve,
&workspace_resolve,
&resolved_features,
&rustflags,
&pkg_set,
&profiles,
interner,
Expand Down Expand Up @@ -983,6 +989,7 @@ fn generate_targets(
resolve: &Resolve,
workspace_resolve: &Option<Resolve>,
resolved_features: &features::ResolvedFeatures,
rustflags: &Vec<InternedString>,
package_set: &PackageSet<'_>,
profiles: &Profiles,
interner: &UnitInterner,
Expand Down Expand Up @@ -1082,13 +1089,23 @@ fn generate_targets(
unit_for,
*kind,
);

// Doc units will not set the user specified rustflags since these use
// rustdoc specific flags instead.
let rustflags = if target_mode.is_doc() {
Default::default()
} else {
rustflags.clone()
};

let unit = interner.intern(
pkg,
target,
profile,
kind.for_target(target),
target_mode,
features.clone(),
rustflags.clone(),
/*is_std*/ false,
/*dep_hash*/ 0,
IsArtifact::No,
Expand Down Expand Up @@ -1672,6 +1689,7 @@ fn traverse_and_share(
new_kind,
unit.mode,
unit.features.clone(),
unit.rustflags.clone(),
unit.is_std,
new_dep_hash,
unit.artifact,
Expand Down Expand Up @@ -1913,6 +1931,7 @@ fn override_rustc_crate_types(
unit.kind,
unit.mode,
unit.features.clone(),
unit.rustflags.clone(),
unit.is_std,
unit.dep_hash,
unit.artifact,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ fn run_verify(
CompileMode::Build,
)?,
cli_features: opts.cli_features.clone(),
rustflags: Vec::new(),
spec: ops::Packages::Packages(Vec::new()),
filter: ops::CompileFilter::Default {
required_features_filterable: true,
Expand Down
Loading