Skip to content

Commit 208a7ad

Browse files
Add a general mechanism for setting rustflags in Cargo that only applies to the current crate. Add tests to ensure --rustflags options are only set for the current crate and not any dependencies.
1 parent 646e9a0 commit 208a7ad

18 files changed

+459
-4
lines changed

src/bin/cargo/commands/bench.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn cli() -> App {
4848
))
4949
.arg_unit_graph()
5050
.arg_timings()
51+
.arg_rustflags()
5152
.after_help("Run `cargo help bench` for more detailed information.\n")
5253
}
5354

src/bin/cargo/commands/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub fn cli() -> App {
4545
.arg_unit_graph()
4646
.arg_future_incompat_report()
4747
.arg_timings()
48+
.arg_rustflags()
4849
.after_help("Run `cargo help build` for more detailed information.\n")
4950
}
5051

src/bin/cargo/commands/check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn cli() -> App {
3737
.arg_unit_graph()
3838
.arg_future_incompat_report()
3939
.arg_timings()
40+
.arg_rustflags()
4041
.after_help("Run `cargo help check` for more detailed information.\n")
4142
}
4243

src/bin/cargo/commands/doc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn cli() -> App {
4040
.arg_ignore_rust_version()
4141
.arg_unit_graph()
4242
.arg_timings()
43+
.arg_rustflags()
4344
.after_help("Run `cargo help doc` for more detailed information.\n")
4445
}
4546

src/bin/cargo/commands/fix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn cli() -> App {
5454
))
5555
.arg_ignore_rust_version()
5656
.arg_timings()
57+
.arg_rustflags()
5758
.after_help("Run `cargo help fix` for more detailed information.\n")
5859
}
5960

src/bin/cargo/commands/install.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn cli() -> App {
8282
)
8383
.arg_message_format()
8484
.arg_timings()
85+
.arg_rustflags()
8586
.after_help("Run `cargo help install` for more detailed information.\n")
8687
}
8788

src/bin/cargo/commands/package.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn cli() -> App {
3535
)
3636
.arg_manifest_path()
3737
.arg_jobs()
38+
.arg_rustflags()
3839
.after_help("Run `cargo help package` for more detailed information.\n")
3940
}
4041

src/bin/cargo/commands/run.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn cli() -> App {
3232
.arg_unit_graph()
3333
.arg_ignore_rust_version()
3434
.arg_timings()
35+
.arg_rustflags()
3536
.after_help("Run `cargo help run` for more detailed information.\n")
3637
}
3738

src/bin/cargo/commands/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub fn cli() -> App {
5656
.arg_unit_graph()
5757
.arg_future_incompat_report()
5858
.arg_timings()
59+
.arg_rustflags()
5960
.after_help(
6061
"Run `cargo help test` for more detailed information.\n\
6162
Run `cargo test -- --help` for test binary options.\n",

src/cargo/core/compiler/fingerprint.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1302,13 +1302,16 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
13021302
// Fill out a bunch more information that we'll be tracking typically
13031303
// hashed to take up less space on disk as we just need to know when things
13041304
// change.
1305-
let extra_flags = if unit.mode.is_doc() {
1305+
let mut rustflags = if unit.mode.is_doc() {
13061306
cx.bcx.rustdocflags_args(unit)
13071307
} else {
13081308
cx.bcx.rustflags_args(unit)
13091309
}
13101310
.to_vec();
13111311

1312+
// Append the command line user specified RUSTFLAGS to the env var or config RUSTFLAGS.
1313+
rustflags.extend(unit.rustflags.iter().map(InternedString::to_string));
1314+
13121315
let profile_hash = util::hash_u64((
13131316
&unit.profile,
13141317
unit.mode,
@@ -1345,7 +1348,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
13451348
metadata,
13461349
config: config.finish(),
13471350
compile_kind,
1348-
rustflags: extra_flags,
1351+
rustflags,
13491352
fs_status: FsStatus::Stale,
13501353
outputs,
13511354
})

src/cargo/core/compiler/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,15 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
241241
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
242242

243243
rustc.args(cx.bcx.rustflags_args(unit));
244+
245+
// Append any user specified rustflags on the command line
246+
// via the --rustflag argument.
247+
rustc.args(&unit.rustflags);
248+
244249
if cx.bcx.config.cli_unstable().binary_dep_depinfo {
245250
rustc.arg("-Z").arg("binary-dep-depinfo");
246251
}
252+
247253
let mut output_options = OutputOptions::new(cx, unit);
248254
let package_id = unit.pkg.package_id();
249255
let target = Target::clone(&unit.target);

src/cargo/core/compiler/standard_lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ pub fn generate_std_roots(
214214
*kind,
215215
mode,
216216
features.clone(),
217+
/*rustflags*/
218+
Default::default(), // Setting command line rustflags for the standard library is not supported
217219
/*is_std*/ true,
218220
/*dep_hash*/ 0,
219221
IsArtifact::No,

src/cargo/core/compiler/unit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub struct UnitInner {
5555
/// The `cfg` features to enable for this unit.
5656
/// This must be sorted.
5757
pub features: Vec<InternedString>,
58+
/// The rustflags to set for this unit.
59+
pub rustflags: Vec<InternedString>,
5860
// if `true`, the dependency is an artifact dependency, requiring special handling when
5961
// calculating output directories, linkage and environment variables provided to builds.
6062
pub artifact: IsArtifact,
@@ -181,6 +183,7 @@ impl UnitInterner {
181183
kind: CompileKind,
182184
mode: CompileMode,
183185
features: Vec<InternedString>,
186+
rustflags: Vec<InternedString>,
184187
is_std: bool,
185188
dep_hash: u64,
186189
artifact: IsArtifact,
@@ -213,6 +216,7 @@ impl UnitInterner {
213216
kind,
214217
mode,
215218
features,
219+
rustflags,
216220
is_std,
217221
dep_hash,
218222
artifact,

src/cargo/core/compiler/unit_dependencies.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ fn compute_deps(
284284
unit,
285285
dep_pkg,
286286
dep_lib,
287+
/*rustflags*/ Default::default(),
287288
dep_unit_for,
288289
unit.kind,
289290
mode,
@@ -295,6 +296,7 @@ fn compute_deps(
295296
unit,
296297
dep_pkg,
297298
dep_lib,
299+
/*rustflags*/ Default::default(),
298300
dep_unit_for,
299301
CompileKind::Host,
300302
mode,
@@ -307,6 +309,7 @@ fn compute_deps(
307309
unit,
308310
dep_pkg,
309311
dep_lib,
312+
/*rustflags*/ Default::default(),
310313
dep_unit_for,
311314
unit.kind.for_target(dep_lib),
312315
mode,
@@ -377,6 +380,7 @@ fn compute_deps(
377380
unit,
378381
&unit.pkg,
379382
t,
383+
/*rustflags*/ Default::default(),
380384
UnitFor::new_normal(unit_for.root_compile_kind()),
381385
unit.kind.for_target(t),
382386
CompileMode::Build,
@@ -479,6 +483,7 @@ fn compute_deps_custom_build(
479483
unit,
480484
&unit.pkg,
481485
&unit.target,
486+
/*rustflags*/ Default::default(),
482487
script_unit_for,
483488
// Build scripts always compiled for the host.
484489
CompileKind::Host,
@@ -568,6 +573,7 @@ fn artifact_targets_to_unit_deps(
568573
target
569574
.clone()
570575
.set_kind(TargetKind::Lib(vec![target_kind.clone()])),
576+
/*rustflags*/ Default::default(),
571577
parent_unit_for,
572578
compile_kind,
573579
CompileMode::Build,
@@ -580,6 +586,7 @@ fn artifact_targets_to_unit_deps(
580586
parent,
581587
artifact_pkg,
582588
target,
589+
/*rustflags*/ Default::default(),
583590
parent_unit_for,
584591
compile_kind,
585592
CompileMode::Build,
@@ -655,6 +662,7 @@ fn compute_deps_doc(
655662
unit,
656663
dep_pkg,
657664
dep_lib,
665+
/*rustflags*/ Default::default(),
658666
dep_unit_for,
659667
unit.kind.for_target(dep_lib),
660668
mode,
@@ -669,6 +677,7 @@ fn compute_deps_doc(
669677
unit,
670678
dep_pkg,
671679
dep_lib,
680+
/*rustflags*/ Default::default(),
672681
dep_unit_for,
673682
unit.kind.for_target(dep_lib),
674683
unit.mode,
@@ -699,6 +708,7 @@ fn compute_deps_doc(
699708
unit,
700709
&unit.pkg,
701710
lib,
711+
/*rustflags*/ Default::default(),
702712
dep_unit_for,
703713
unit.kind.for_target(lib),
704714
unit.mode,
@@ -717,6 +727,7 @@ fn compute_deps_doc(
717727
scrape_unit,
718728
&scrape_unit.pkg,
719729
&scrape_unit.target,
730+
/*rustflags*/ Default::default(),
720731
unit_for,
721732
scrape_unit.kind,
722733
scrape_unit.mode,
@@ -740,11 +751,15 @@ fn maybe_lib(
740751
.map(|t| {
741752
let mode = check_or_build_mode(unit.mode, t);
742753
let dep_unit_for = unit_for.with_dependency(unit, t, unit_for.root_compile_kind());
754+
755+
// When adding the lib targets for the current unit also pass down the user specified
756+
// rustflags for the package.
743757
new_unit_dep(
744758
state,
745759
unit,
746760
&unit.pkg,
747761
t,
762+
unit.rustflags.clone(),
748763
dep_unit_for,
749764
unit.kind.for_target(t),
750765
mode,
@@ -805,6 +820,7 @@ fn dep_build_script(
805820
unit,
806821
&unit.pkg,
807822
t,
823+
Default::default(),
808824
script_unit_for,
809825
unit.kind,
810826
CompileMode::RunCustomBuild,
@@ -839,6 +855,7 @@ fn new_unit_dep(
839855
parent: &Unit,
840856
pkg: &Package,
841857
target: &Target,
858+
rustflags: Vec<InternedString>,
842859
unit_for: UnitFor,
843860
kind: CompileKind,
844861
mode: CompileMode,
@@ -853,7 +870,7 @@ fn new_unit_dep(
853870
kind,
854871
);
855872
new_unit_dep_with_profile(
856-
state, parent, pkg, target, unit_for, kind, mode, profile, artifact,
873+
state, parent, pkg, target, rustflags, unit_for, kind, mode, profile, artifact,
857874
)
858875
}
859876

@@ -862,6 +879,7 @@ fn new_unit_dep_with_profile(
862879
parent: &Unit,
863880
pkg: &Package,
864881
target: &Target,
882+
rustflags: Vec<InternedString>,
865883
unit_for: UnitFor,
866884
kind: CompileKind,
867885
mode: CompileMode,
@@ -885,6 +903,7 @@ fn new_unit_dep_with_profile(
885903
kind,
886904
mode,
887905
features,
906+
rustflags,
888907
state.is_std,
889908
/*dep_hash*/ 0,
890909
artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes),

src/cargo/ops/cargo_compile.rs

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct CompileOptions {
6262
pub build_config: BuildConfig,
6363
/// Feature flags requested by the user.
6464
pub cli_features: CliFeatures,
65+
/// Rustflags requested by the user.
66+
pub rustflags: Vec<InternedString>,
6567
/// A set of packages to build.
6668
pub spec: Packages,
6769
/// Filter to apply to the root package to select which targets will be
@@ -91,6 +93,7 @@ impl CompileOptions {
9193
Ok(CompileOptions {
9294
build_config: BuildConfig::new(config, jobs, keep_going, &[], mode)?,
9395
cli_features: CliFeatures::new_all(false),
96+
rustflags: Vec::new(),
9497
spec: ops::Packages::Packages(Vec::new()),
9598
filter: CompileFilter::Default {
9699
required_features_filterable: false,
@@ -335,6 +338,7 @@ pub fn create_bcx<'a, 'cfg>(
335338
ref build_config,
336339
ref spec,
337340
ref cli_features,
341+
ref rustflags,
338342
ref filter,
339343
ref target_rustdoc_args,
340344
ref target_rustc_args,
@@ -492,6 +496,7 @@ pub fn create_bcx<'a, 'cfg>(
492496
&resolve,
493497
&workspace_resolve,
494498
&resolved_features,
499+
&rustflags,
495500
&pkg_set,
496501
&profiles,
497502
interner,
@@ -532,6 +537,7 @@ pub fn create_bcx<'a, 'cfg>(
532537
&resolve,
533538
&workspace_resolve,
534539
&resolved_features,
540+
&rustflags,
535541
&pkg_set,
536542
&profiles,
537543
interner,
@@ -983,6 +989,7 @@ fn generate_targets(
983989
resolve: &Resolve,
984990
workspace_resolve: &Option<Resolve>,
985991
resolved_features: &features::ResolvedFeatures,
992+
rustflags: &Vec<InternedString>,
986993
package_set: &PackageSet<'_>,
987994
profiles: &Profiles,
988995
interner: &UnitInterner,
@@ -1082,13 +1089,23 @@ fn generate_targets(
10821089
unit_for,
10831090
*kind,
10841091
);
1092+
1093+
// Doc units will not set the user specified rustflags since these use
1094+
// rustdoc specific flags instead.
1095+
let rustflags = if target_mode.is_doc() {
1096+
Default::default()
1097+
} else {
1098+
rustflags.clone()
1099+
};
1100+
10851101
let unit = interner.intern(
10861102
pkg,
10871103
target,
10881104
profile,
10891105
kind.for_target(target),
10901106
target_mode,
10911107
features.clone(),
1108+
rustflags.clone(),
10921109
/*is_std*/ false,
10931110
/*dep_hash*/ 0,
10941111
IsArtifact::No,
@@ -1672,6 +1689,7 @@ fn traverse_and_share(
16721689
new_kind,
16731690
unit.mode,
16741691
unit.features.clone(),
1692+
unit.rustflags.clone(),
16751693
unit.is_std,
16761694
new_dep_hash,
16771695
unit.artifact,
@@ -1913,6 +1931,7 @@ fn override_rustc_crate_types(
19131931
unit.kind,
19141932
unit.mode,
19151933
unit.features.clone(),
1934+
unit.rustflags.clone(),
19161935
unit.is_std,
19171936
unit.dep_hash,
19181937
unit.artifact,

src/cargo/ops/cargo_package.rs

+1
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ fn run_verify(
822822
CompileMode::Build,
823823
)?,
824824
cli_features: opts.cli_features.clone(),
825+
rustflags: Vec::new(),
825826
spec: ops::Packages::Packages(Vec::new()),
826827
filter: ops::CompileFilter::Default {
827828
required_features_filterable: true,

0 commit comments

Comments
 (0)