Skip to content

Commit 35eaf32

Browse files
committed
Pass rustflags to artifacts built with implicit targets when using target-applies-to-host
1 parent ec9a9af commit 35eaf32

File tree

11 files changed

+71
-32
lines changed

11 files changed

+71
-32
lines changed

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,6 @@ impl<'a, 'gctx> BuildContext<'a, 'gctx> {
134134
self.build_config.jobs
135135
}
136136

137-
/// Extra compiler flags to pass to `rustc` for a given unit.
138-
///
139-
/// Although it depends on the caller, in the current Cargo implementation,
140-
/// these flags take precedence over those from [`BuildContext::extra_args_for`].
141-
///
142-
/// As of now, these flags come from environment variables and configurations.
143-
/// See [`TargetInfo.rustflags`] for more on how Cargo collects them.
144-
///
145-
/// [`TargetInfo.rustflags`]: TargetInfo::rustflags
146-
pub fn rustflags_args(&self, unit: &Unit) -> &[String] {
147-
&self.target_data.info(unit.kind).rustflags
148-
}
149-
150137
/// Extra compiler flags to pass to `rustdoc` for a given unit.
151138
///
152139
/// Although it depends on the caller, in the current Cargo implementation,

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::cell::RefCell;
2323
use std::collections::hash_map::{Entry, HashMap};
2424
use std::path::{Path, PathBuf};
2525
use std::str::{self, FromStr};
26+
use std::sync::Arc;
2627

2728
/// Information about the platform target gleaned from querying rustc.
2829
///
@@ -52,7 +53,7 @@ pub struct TargetInfo {
5253
/// target libraries.
5354
pub sysroot_target_libdir: PathBuf,
5455
/// Extra flags to pass to `rustc`, see [`extra_args`].
55-
pub rustflags: Vec<String>,
56+
pub rustflags: Arc<[String]>,
5657
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
5758
pub rustdocflags: Vec<String>,
5859
/// Whether or not rustc (stably) supports the `--check-cfg` flag.
@@ -312,7 +313,7 @@ impl TargetInfo {
312313
crate_types: RefCell::new(map),
313314
sysroot,
314315
sysroot_target_libdir,
315-
rustflags,
316+
rustflags: rustflags.into(),
316317
rustdocflags: extra_args(
317318
gctx,
318319
requested_kinds,
@@ -867,7 +868,10 @@ pub struct RustcTargetData<'gctx> {
867868

868869
/// Build information for the "host", which is information about when
869870
/// `rustc` is invoked without a `--target` flag. This is used for
870-
/// procedural macros, build scripts, etc.
871+
/// selecting a linker, and applying link overrides.
872+
///
873+
/// The configuration read into this depends on whether or not
874+
/// `target-applies-to-host=true`.
871875
host_config: TargetConfig,
872876
/// Information about the host platform.
873877
host_info: TargetInfo,
@@ -889,7 +893,10 @@ impl<'gctx> RustcTargetData<'gctx> {
889893
let mut target_config = HashMap::new();
890894
let mut target_info = HashMap::new();
891895
let target_applies_to_host = gctx.target_applies_to_host()?;
896+
let host_target = CompileTarget::new(&rustc.host)?;
892897
let host_info = TargetInfo::new(gctx, requested_kinds, &rustc, CompileKind::Host)?;
898+
899+
// This config is used for link overrides and choosing a linker.
893900
let host_config = if target_applies_to_host {
894901
gctx.target_cfg_triple(&rustc.host)?
895902
} else {
@@ -902,9 +909,21 @@ impl<'gctx> RustcTargetData<'gctx> {
902909
// needs access to the target config data, create a copy so that it
903910
// can be found. See `rebuild_unit_graph_shared` for why this is done.
904911
if requested_kinds.iter().any(CompileKind::is_host) {
905-
let ct = CompileTarget::new(&rustc.host)?;
906-
target_info.insert(ct, host_info.clone());
907-
target_config.insert(ct, gctx.target_cfg_triple(&rustc.host)?);
912+
target_config.insert(host_target, gctx.target_cfg_triple(&rustc.host)?);
913+
914+
// If target_applies_to_host is true, the host_info is the target info,
915+
// otherwise we need to build target info for the target.
916+
if target_applies_to_host {
917+
target_info.insert(host_target, host_info.clone());
918+
} else {
919+
let host_target_info = TargetInfo::new(
920+
gctx,
921+
requested_kinds,
922+
&rustc,
923+
CompileKind::Target(host_target),
924+
)?;
925+
target_info.insert(host_target, host_target_info);
926+
}
908927
};
909928

910929
let mut res = RustcTargetData {

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
352352
cmd.env("RUSTC_WORKSPACE_WRAPPER", wrapper);
353353
}
354354
}
355-
cmd.env(
356-
"CARGO_ENCODED_RUSTFLAGS",
357-
bcx.rustflags_args(unit).join("\x1f"),
358-
);
355+
cmd.env("CARGO_ENCODED_RUSTFLAGS", unit.rustflags.join("\x1f"));
359356
cmd.env_remove("RUSTFLAGS");
360357

361358
if build_runner.bcx.ws.gctx().extra_verbose() {

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ fn calculate_normal(
14171417
let extra_flags = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
14181418
build_runner.bcx.rustdocflags_args(unit)
14191419
} else {
1420-
build_runner.bcx.rustflags_args(unit)
1420+
&unit.rustflags
14211421
}
14221422
.to_vec();
14231423

@@ -1512,7 +1512,7 @@ fn calculate_run_custom_build(
15121512
An I/O error happened. Please make sure you can access the file.
15131513
15141514
By default, if your project contains a build script, cargo scans all files in
1515-
it to determine whether a rebuild is needed. If you don't expect to access the
1515+
it to determine whether a rebuild is needed. If you don't expect to access the
15161516
file, specify `rerun-if-changed` in your build script.
15171517
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed for more information.";
15181518
pkg_fingerprint(build_runner.bcx, &unit.pkg).map_err(|err| {
@@ -1542,7 +1542,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
15421542
.collect::<CargoResult<Vec<_>>>()?
15431543
};
15441544

1545-
let rustflags = build_runner.bcx.rustflags_args(unit).to_vec();
1545+
let rustflags = unit.rustflags.to_vec();
15461546

15471547
Ok(Fingerprint {
15481548
local: Mutex::new(local),

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
683683
base.inherit_jobserver(&build_runner.jobserver);
684684
build_deps_args(&mut base, build_runner, unit)?;
685685
add_cap_lints(build_runner.bcx, unit, &mut base);
686-
base.args(build_runner.bcx.rustflags_args(unit));
686+
base.args(&unit.rustflags);
687687
if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
688688
base.arg("-Z").arg("binary-dep-depinfo");
689689
}

src/cargo/core/compiler/standard_lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ pub fn generate_std_roots(
178178
package_set: &PackageSet<'_>,
179179
interner: &UnitInterner,
180180
profiles: &Profiles,
181+
target_data: &RustcTargetData<'_>,
181182
) -> CargoResult<HashMap<CompileKind, Vec<Unit>>> {
182183
// Generate the root Units for the standard library.
183184
let std_ids = crates
@@ -216,6 +217,7 @@ pub fn generate_std_roots(
216217
*kind,
217218
mode,
218219
features.clone(),
220+
target_data.info(*kind).rustflags.clone(),
219221
/*is_std*/ true,
220222
/*dep_hash*/ 0,
221223
IsArtifact::No,

src/cargo/core/compiler/unit.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fmt;
1414
use std::hash::{Hash, Hasher};
1515
use std::ops::Deref;
1616
use std::rc::Rc;
17+
use std::sync::Arc;
1718

1819
/// All information needed to define a unit.
1920
///
@@ -59,6 +60,17 @@ pub struct UnitInner {
5960
/// The `cfg` features to enable for this unit.
6061
/// This must be sorted.
6162
pub features: Vec<InternedString>,
63+
/// Extra compiler flags to pass to `rustc` for a given unit.
64+
///
65+
/// Although it depends on the caller, in the current Cargo implementation,
66+
/// these flags take precedence over those from [`BuildContext::extra_args_for`].
67+
///
68+
/// As of now, these flags come from environment variables and configurations.
69+
/// See [`TargetInfo.rustflags`] for more on how Cargo collects them.
70+
///
71+
/// [`BuildContext::extra_args_for`]: crate::core::compiler::build_context::BuildContext::extra_args_for
72+
/// [`TargetInfo.rustflags`]: crate::core::compiler::build_context::TargetInfo::rustflags
73+
pub rustflags: Arc<[String]>,
6274
// if `true`, the dependency is an artifact dependency, requiring special handling when
6375
// calculating output directories, linkage and environment variables provided to builds.
6476
pub artifact: IsArtifact,
@@ -151,6 +163,7 @@ impl fmt::Debug for Unit {
151163
.field("kind", &self.kind)
152164
.field("mode", &self.mode)
153165
.field("features", &self.features)
166+
.field("rustflags", &self.rustflags)
154167
.field("artifact", &self.artifact.is_true())
155168
.field(
156169
"artifact_target_for_features",
@@ -198,6 +211,7 @@ impl UnitInterner {
198211
kind: CompileKind,
199212
mode: CompileMode,
200213
features: Vec<InternedString>,
214+
rustflags: Arc<[String]>,
201215
is_std: bool,
202216
dep_hash: u64,
203217
artifact: IsArtifact,
@@ -231,6 +245,7 @@ impl UnitInterner {
231245
kind,
232246
mode,
233247
features,
248+
rustflags,
234249
is_std,
235250
dep_hash,
236251
artifact,

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ fn new_unit_dep_with_profile(
859859
kind,
860860
mode,
861861
features,
862+
state.target_data.info(kind).rustflags.clone(),
862863
state.is_std,
863864
/*dep_hash*/ 0,
864865
artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes),

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ pub fn create_bcx<'a, 'gctx>(
360360
let generator = UnitGenerator {
361361
ws,
362362
packages: &to_builds,
363+
target_data: &target_data,
363364
filter,
364365
requested_kinds: &build_config.requested_kinds,
365366
explicit_host_kind,
@@ -399,6 +400,7 @@ pub fn create_bcx<'a, 'gctx>(
399400
&pkg_set,
400401
interner,
401402
&profiles,
403+
&target_data,
402404
)?
403405
} else {
404406
Default::default()
@@ -694,6 +696,7 @@ fn traverse_and_share(
694696
to_host.unwrap(),
695697
unit.mode,
696698
unit.features.clone(),
699+
unit.rustflags.clone(),
697700
unit.is_std,
698701
unit.dep_hash,
699702
unit.artifact,
@@ -719,6 +722,7 @@ fn traverse_and_share(
719722
canonical_kind,
720723
unit.mode,
721724
unit.features.clone(),
725+
unit.rustflags.clone(),
722726
unit.is_std,
723727
new_dep_hash,
724728
unit.artifact,
@@ -880,6 +884,7 @@ fn override_rustc_crate_types(
880884
unit.kind,
881885
unit.mode,
882886
unit.features.clone(),
887+
unit.rustflags.clone(),
883888
unit.is_std,
884889
unit.dep_hash,
885890
unit.artifact,

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::fmt::Write;
44

55
use crate::core::compiler::rustdoc::RustdocScrapeExamples;
66
use crate::core::compiler::unit_dependencies::IsArtifact;
7-
use crate::core::compiler::UnitInterner;
87
use crate::core::compiler::{CompileKind, CompileMode, Unit};
8+
use crate::core::compiler::{RustcTargetData, UnitInterner};
99
use crate::core::dependency::DepKind;
1010
use crate::core::profiles::{Profiles, UnitFor};
1111
use crate::core::resolver::features::{self, FeaturesFor};
@@ -47,6 +47,7 @@ struct Proposal<'a> {
4747
pub(super) struct UnitGenerator<'a, 'gctx> {
4848
pub ws: &'a Workspace<'gctx>,
4949
pub packages: &'a [&'a Package],
50+
pub target_data: &'a RustcTargetData<'gctx>,
5051
pub filter: &'a CompileFilter,
5152
pub requested_kinds: &'a [CompileKind],
5253
pub explicit_host_kind: CompileKind,
@@ -162,13 +163,15 @@ impl<'a> UnitGenerator<'a, '_> {
162163
unit_for,
163164
kind,
164165
);
166+
let kind = kind.for_target(target);
165167
self.interner.intern(
166168
pkg,
167169
target,
168170
profile,
169-
kind.for_target(target),
171+
kind,
170172
target_mode,
171173
features.clone(),
174+
self.target_data.info(kind).rustflags.clone(),
172175
/*is_std*/ false,
173176
/*dep_hash*/ 0,
174177
IsArtifact::No,

tests/testsuite/rustflags.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,12 @@ fn host_config_rustflags_with_target() {
15921592
fn target_applies_to_host_rustflags_works() {
15931593
// Ensures that rustflags are passed to the target when
15941594
// target_applies_to_host=false
1595-
let p = project().file("src/lib.rs", r#"#[cfg(feature = "flag")] compile_error!("flag passed");"#).build();
1595+
let p = project()
1596+
.file(
1597+
"src/lib.rs",
1598+
r#"#[cfg(feature = "flag")] compile_error!("flag passed");"#,
1599+
)
1600+
.build();
15961601

15971602
// Use RUSTFLAGS to pass an argument that would generate an error
15981603
// but it is ignored.
@@ -1601,6 +1606,11 @@ fn target_applies_to_host_rustflags_works() {
16011606
.arg("-Ztarget-applies-to-host")
16021607
.env("CARGO_TARGET_APPLIES_TO_HOST", "false")
16031608
.env("RUSTFLAGS", r#"--cfg feature="flag""#)
1604-
.with_status(0)
1609+
.with_status(101)
1610+
.with_stderr_data(
1611+
"[CHECKING] foo v0.0.1 ([ROOT]/foo)
1612+
[ERROR] flag passed
1613+
...",
1614+
)
16051615
.run();
1606-
}
1616+
}

0 commit comments

Comments
 (0)