Skip to content

Commit 6d94df8

Browse files
committed
Pass rustflags to artifacts built with implicit targets when using target-applies-to-host
1 parent 2f17770 commit 6d94df8

File tree

11 files changed

+74
-28
lines changed

11 files changed

+74
-28
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.
@@ -311,7 +312,7 @@ impl TargetInfo {
311312
crate_types: RefCell::new(map),
312313
sysroot,
313314
sysroot_target_libdir,
314-
rustflags,
315+
rustflags: rustflags.into(),
315316
rustdocflags: extra_args(
316317
gctx,
317318
requested_kinds,
@@ -866,7 +867,10 @@ pub struct RustcTargetData<'gctx> {
866867

867868
/// Build information for the "host", which is information about when
868869
/// `rustc` is invoked without a `--target` flag. This is used for
869-
/// procedural macros, build scripts, etc.
870+
/// selecting a linker, and applying link overrides.
871+
///
872+
/// The configuration read into this depends on whether or not
873+
/// `target-applies-to-host=true`.
870874
host_config: TargetConfig,
871875
/// Information about the host platform.
872876
host_info: TargetInfo,
@@ -887,7 +891,10 @@ impl<'gctx> RustcTargetData<'gctx> {
887891
let mut target_config = HashMap::new();
888892
let mut target_info = HashMap::new();
889893
let target_applies_to_host = gctx.target_applies_to_host()?;
894+
let host_target = CompileTarget::new(&rustc.host)?;
890895
let host_info = TargetInfo::new(gctx, requested_kinds, &rustc, CompileKind::Host)?;
896+
897+
// This config is used for link overrides and choosing a linker.
891898
let host_config = if target_applies_to_host {
892899
gctx.target_cfg_triple(&rustc.host)?
893900
} else {
@@ -900,9 +907,21 @@ impl<'gctx> RustcTargetData<'gctx> {
900907
// needs access to the target config data, create a copy so that it
901908
// can be found. See `rebuild_unit_graph_shared` for why this is done.
902909
if requested_kinds.iter().any(CompileKind::is_host) {
903-
let ct = CompileTarget::new(&rustc.host)?;
904-
target_info.insert(ct, host_info.clone());
905-
target_config.insert(ct, gctx.target_cfg_triple(&rustc.host)?);
910+
target_config.insert(host_target, gctx.target_cfg_triple(&rustc.host)?);
911+
912+
// If target_applies_to_host is true, the host_info is the target info,
913+
// otherwise we need to build target info for the target.
914+
if target_applies_to_host {
915+
target_info.insert(host_target, host_info.clone());
916+
} else {
917+
let host_target_info = TargetInfo::new(
918+
gctx,
919+
requested_kinds,
920+
&rustc,
921+
CompileKind::Target(host_target),
922+
)?;
923+
target_info.insert(host_target, host_target_info);
924+
}
906925
};
907926

908927
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
@@ -1416,7 +1416,7 @@ fn calculate_normal(
14161416
let extra_flags = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
14171417
build_runner.bcx.rustdocflags_args(unit)
14181418
} else {
1419-
build_runner.bcx.rustflags_args(unit)
1419+
&unit.rustflags
14201420
}
14211421
.to_vec();
14221422

@@ -1489,7 +1489,7 @@ fn calculate_run_custom_build(
14891489
An I/O error happened. Please make sure you can access the file.
14901490
14911491
By default, if your project contains a build script, cargo scans all files in
1492-
it to determine whether a rebuild is needed. If you don't expect to access the
1492+
it to determine whether a rebuild is needed. If you don't expect to access the
14931493
file, specify `rerun-if-changed` in your build script.
14941494
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed for more information.";
14951495
pkg_fingerprint(build_runner.bcx, &unit.pkg).map_err(|err| {
@@ -1519,7 +1519,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
15191519
.collect::<CargoResult<Vec<_>>>()?
15201520
};
15211521

1522-
let rustflags = build_runner.bcx.rustflags_args(unit).to_vec();
1522+
let rustflags = unit.rustflags.to_vec();
15231523

15241524
Ok(Fingerprint {
15251525
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
@@ -176,6 +176,7 @@ pub fn generate_std_roots(
176176
package_set: &PackageSet<'_>,
177177
interner: &UnitInterner,
178178
profiles: &Profiles,
179+
target_data: &RustcTargetData<'_>,
179180
) -> CargoResult<HashMap<CompileKind, Vec<Unit>>> {
180181
// Generate the root Units for the standard library.
181182
let std_ids = crates
@@ -214,6 +215,7 @@ pub fn generate_std_roots(
214215
*kind,
215216
mode,
216217
features.clone(),
218+
target_data.info(*kind).rustflags.clone(),
217219
/*is_std*/ true,
218220
/*dep_hash*/ 0,
219221
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
@@ -359,6 +359,7 @@ pub fn create_bcx<'a, 'gctx>(
359359
let generator = UnitGenerator {
360360
ws,
361361
packages: &to_builds,
362+
target_data: &target_data,
362363
filter,
363364
requested_kinds: &build_config.requested_kinds,
364365
explicit_host_kind,
@@ -398,6 +399,7 @@ pub fn create_bcx<'a, 'gctx>(
398399
&pkg_set,
399400
interner,
400401
&profiles,
402+
&target_data,
401403
)?
402404
} else {
403405
Default::default()
@@ -703,6 +705,7 @@ fn traverse_and_share(
703705
to_host.unwrap(),
704706
unit.mode,
705707
unit.features.clone(),
708+
unit.rustflags.clone(),
706709
unit.is_std,
707710
unit.dep_hash,
708711
unit.artifact,
@@ -728,6 +731,7 @@ fn traverse_and_share(
728731
canonical_kind,
729732
unit.mode,
730733
unit.features.clone(),
734+
unit.rustflags.clone(),
731735
unit.is_std,
732736
new_dep_hash,
733737
unit.artifact,
@@ -889,6 +893,7 @@ fn override_rustc_crate_types(
889893
unit.kind,
890894
unit.mode,
891895
unit.features.clone(),
896+
unit.rustflags.clone(),
892897
unit.is_std,
893898
unit.dep_hash,
894899
unit.artifact,

src/cargo/ops/cargo_compile/unit_generator.rs

Lines changed: 4 additions & 1 deletion
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,
169171
kind.for_target(target),
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,

0 commit comments

Comments
 (0)