Skip to content

Commit 9162ecc

Browse files
committed
Read rustflags from TargetConfig in TargetInfo::new
1 parent d7c3c8c commit 9162ecc

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

Diff for: src/cargo/core/compiler/build_context/target_info.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::core::compiler::{
1212
BuildOutput, BuildRunner, CompileKind, CompileMode, CompileTarget, CrateType,
1313
};
1414
use crate::core::{Dependency, Package, Target, TargetKind, Workspace};
15-
use crate::util::context::{GlobalContext, StringList, TargetConfig};
15+
use crate::util::context::{GlobalContext, TargetConfig};
1616
use crate::util::interning::InternedString;
1717
use crate::util::{CargoResult, Rustc};
1818
use anyhow::Context as _;
@@ -26,7 +26,7 @@ use std::str::{self, FromStr};
2626
use std::sync::Arc;
2727

2828
/// Information about the platform target gleaned from querying rustc and from
29-
/// collating configs.
29+
/// merging configs.
3030
///
3131
/// [`RustcTargetData`] keeps several of these, one for the host and the others
3232
/// for other specified targets. If no target is specified, it uses a clone from
@@ -162,14 +162,16 @@ impl TargetInfo {
162162
requested_kinds: &[CompileKind],
163163
rustc: &Rustc,
164164
kind: CompileKind,
165-
// This config is used for `links_overrides` and `linker`.
166-
//
167-
// In the case of target_applies_to_host=true (default) it may contain
168-
// incorrect rustflags.
169165
target_config: &TargetConfig,
170166
) -> CargoResult<TargetInfo> {
171-
let mut rustflags =
172-
extra_args(gctx, requested_kinds, &rustc.host, None, kind, Flags::Rust)?;
167+
let mut rustflags = extra_args(
168+
gctx,
169+
requested_kinds,
170+
None,
171+
target_config,
172+
kind,
173+
Flags::Rust,
174+
)?;
173175
let mut turn = 0;
174176
loop {
175177
let extra_fingerprint = kind.fingerprint_hash();
@@ -291,8 +293,8 @@ impl TargetInfo {
291293
let new_flags = extra_args(
292294
gctx,
293295
requested_kinds,
294-
&rustc.host,
295296
Some(&cfg),
297+
target_config,
296298
kind,
297299
Flags::Rust,
298300
)?;
@@ -325,8 +327,8 @@ impl TargetInfo {
325327
rustdocflags: extra_args(
326328
gctx,
327329
requested_kinds,
328-
&rustc.host,
329330
Some(&cfg),
331+
target_config,
330332
kind,
331333
Flags::Rustdoc,
332334
)?
@@ -679,13 +681,6 @@ enum Flags {
679681
}
680682

681683
impl Flags {
682-
fn as_key(self) -> &'static str {
683-
match self {
684-
Flags::Rust => "rustflags",
685-
Flags::Rustdoc => "rustdocflags",
686-
}
687-
}
688-
689684
fn as_env(self) -> &'static str {
690685
match self {
691686
Flags::Rust => "RUSTFLAGS",
@@ -722,8 +717,8 @@ impl Flags {
722717
fn extra_args(
723718
gctx: &GlobalContext,
724719
requested_kinds: &[CompileKind],
725-
host_triple: &str,
726720
target_cfg: Option<&[Cfg]>,
721+
target_config: &TargetConfig,
727722
kind: CompileKind,
728723
flags: Flags,
729724
) -> CargoResult<Vec<String>> {
@@ -743,7 +738,7 @@ fn extra_args(
743738
// --target. Or, phrased differently, no `--target` behaves the same as `--target
744739
// <host>`, and host artifacts are always "special" (they don't pick up `RUSTFLAGS` for
745740
// example).
746-
return Ok(rustflags_from_host(gctx, flags, host_triple)?.unwrap_or_else(Vec::new));
741+
return Ok(rustflags_from_host(target_config, flags)?.unwrap_or_else(Vec::new));
747742
}
748743
}
749744

@@ -753,9 +748,7 @@ fn extra_args(
753748

754749
if let Some(rustflags) = rustflags_from_env(gctx, flags) {
755750
Ok(rustflags)
756-
} else if let Some(rustflags) =
757-
rustflags_from_target(gctx, host_triple, target_cfg, kind, flags)?
758-
{
751+
} else if let Some(rustflags) = rustflags_from_target(gctx, target_cfg, target_config, flags)? {
759752
Ok(rustflags)
760753
} else if let Some(rustflags) = rustflags_from_build(gctx, flags)? {
761754
Ok(rustflags)
@@ -794,21 +787,18 @@ fn rustflags_from_env(gctx: &GlobalContext, flags: Flags) -> Option<Vec<String>>
794787
/// See [`extra_args`] for more.
795788
fn rustflags_from_target(
796789
gctx: &GlobalContext,
797-
host_triple: &str,
798790
target_cfg: Option<&[Cfg]>,
799-
kind: CompileKind,
791+
target_config: &TargetConfig,
800792
flag: Flags,
801793
) -> CargoResult<Option<Vec<String>>> {
802794
let mut rustflags = Vec::new();
803795

804-
// Then the target.*.rustflags value...
805-
let target = match &kind {
806-
CompileKind::Host => host_triple,
807-
CompileKind::Target(target) => target.short_name(),
796+
let config_flags = match flag {
797+
Flags::Rust => &target_config.rustflags,
798+
Flags::Rustdoc => &target_config.rustdocflags,
808799
};
809-
let key = format!("target.{}.{}", target, flag.as_key());
810-
if let Some(args) = gctx.get::<Option<StringList>>(&key)? {
811-
rustflags.extend(args.as_slice().iter().cloned());
800+
if let Some(args) = config_flags {
801+
rustflags.extend(args.val.as_slice().iter().cloned());
812802
}
813803
// ...including target.'cfg(...)'.rustflags
814804
if let Some(target_cfg) = target_cfg {
@@ -876,13 +866,11 @@ fn target_linker(
876866
/// Gets compiler flags from `[host]` section in the config.
877867
/// See [`extra_args`] for more.
878868
fn rustflags_from_host(
879-
gctx: &GlobalContext,
869+
target_config: &TargetConfig,
880870
flag: Flags,
881-
host_triple: &str,
882871
) -> CargoResult<Option<Vec<String>>> {
883-
let target_cfg = gctx.host_cfg_triple(host_triple)?;
884872
let list = match flag {
885-
Flags::Rust => &target_cfg.rustflags,
873+
Flags::Rust => &target_config.rustflags,
886874
Flags::Rustdoc => {
887875
// host.rustdocflags is not a thing, since it does not make sense
888876
return Ok(None);
@@ -941,9 +929,14 @@ impl<'gctx> RustcTargetData<'gctx> {
941929
let target_applies_to_host = gctx.target_applies_to_host()?;
942930
let host_target = CompileTarget::new(&rustc.host)?;
943931

944-
// This config is used for link overrides and choosing a linker.
945932
let host_config = if target_applies_to_host {
946-
gctx.target_cfg_triple(&rustc.host)?
933+
let mut config = gctx.target_cfg_triple(&rustc.host)?;
934+
if requested_kinds != [CompileKind::Host] {
935+
// When an explicit target flag is passed rustflags are ignored for host artifacts.
936+
config.rustflags = None;
937+
config.rustdocflags = None;
938+
}
939+
config
947940
} else {
948941
gctx.host_cfg_triple(&rustc.host)?
949942
};

0 commit comments

Comments
 (0)