Skip to content

Commit 1842e81

Browse files
committed
Read rustflags from TargetConfig in TargetInfo::new
1 parent fa17b99 commit 1842e81

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

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

+29-36
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use crate::core::compiler::apply_env_config;
1111
use crate::core::compiler::{BuildRunner, CompileKind, CompileMode, CompileTarget, CrateType};
1212
use crate::core::{Dependency, Package, Target, TargetKind, Workspace};
13-
use crate::util::context::{GlobalContext, StringList, TargetConfig};
13+
use crate::util::context::{GlobalContext, TargetConfig};
1414
use crate::util::interning::InternedString;
1515
use crate::util::{CargoResult, Rustc};
1616
use anyhow::Context as _;
@@ -161,14 +161,16 @@ impl TargetInfo {
161161
requested_kinds: &[CompileKind],
162162
rustc: &Rustc,
163163
kind: CompileKind,
164-
// This config is used for `links_overrides` and `linker`.
165-
//
166-
// In the case of target_applies_to_host=true (default) it may contain
167-
// incorrect rustflags.
168164
target_config: &TargetConfig,
169165
) -> CargoResult<TargetInfo> {
170-
let mut rustflags =
171-
extra_args(gctx, requested_kinds, &rustc.host, None, kind, Flags::Rust)?;
166+
let mut rustflags = extra_args(
167+
gctx,
168+
requested_kinds,
169+
None,
170+
target_config,
171+
kind,
172+
Flags::Rust,
173+
)?;
172174
let mut turn = 0;
173175
loop {
174176
let extra_fingerprint = kind.fingerprint_hash();
@@ -290,8 +292,8 @@ impl TargetInfo {
290292
let new_flags = extra_args(
291293
gctx,
292294
requested_kinds,
293-
&rustc.host,
294295
Some(&cfg),
296+
target_config,
295297
kind,
296298
Flags::Rust,
297299
)?;
@@ -324,8 +326,8 @@ impl TargetInfo {
324326
rustdocflags: extra_args(
325327
gctx,
326328
requested_kinds,
327-
&rustc.host,
328329
Some(&cfg),
330+
target_config,
329331
kind,
330332
Flags::Rustdoc,
331333
)?
@@ -678,13 +680,6 @@ enum Flags {
678680
}
679681

680682
impl Flags {
681-
fn as_key(self) -> &'static str {
682-
match self {
683-
Flags::Rust => "rustflags",
684-
Flags::Rustdoc => "rustdocflags",
685-
}
686-
}
687-
688683
fn as_env(self) -> &'static str {
689684
match self {
690685
Flags::Rust => "RUSTFLAGS",
@@ -721,8 +716,8 @@ impl Flags {
721716
fn extra_args(
722717
gctx: &GlobalContext,
723718
requested_kinds: &[CompileKind],
724-
host_triple: &str,
725719
target_cfg: Option<&[Cfg]>,
720+
target_config: &TargetConfig,
726721
kind: CompileKind,
727722
flags: Flags,
728723
) -> CargoResult<Vec<String>> {
@@ -742,7 +737,7 @@ fn extra_args(
742737
// --target. Or, phrased differently, no `--target` behaves the same as `--target
743738
// <host>`, and host artifacts are always "special" (they don't pick up `RUSTFLAGS` for
744739
// example).
745-
return Ok(rustflags_from_host(gctx, flags, host_triple)?.unwrap_or_else(Vec::new));
740+
return Ok(rustflags_from_host(target_config, flags)?.unwrap_or_else(Vec::new));
746741
}
747742
}
748743

@@ -752,9 +747,7 @@ fn extra_args(
752747

753748
if let Some(rustflags) = rustflags_from_env(gctx, flags) {
754749
Ok(rustflags)
755-
} else if let Some(rustflags) =
756-
rustflags_from_target(gctx, host_triple, target_cfg, kind, flags)?
757-
{
750+
} else if let Some(rustflags) = rustflags_from_target(gctx, target_cfg, target_config, flags)? {
758751
Ok(rustflags)
759752
} else if let Some(rustflags) = rustflags_from_build(gctx, flags)? {
760753
Ok(rustflags)
@@ -793,21 +786,18 @@ fn rustflags_from_env(gctx: &GlobalContext, flags: Flags) -> Option<Vec<String>>
793786
/// See [`extra_args`] for more.
794787
fn rustflags_from_target(
795788
gctx: &GlobalContext,
796-
host_triple: &str,
797789
target_cfg: Option<&[Cfg]>,
798-
kind: CompileKind,
790+
target_config: &TargetConfig,
799791
flag: Flags,
800792
) -> CargoResult<Option<Vec<String>>> {
801793
let mut rustflags = Vec::new();
802794

803-
// Then the target.*.rustflags value...
804-
let target = match &kind {
805-
CompileKind::Host => host_triple,
806-
CompileKind::Target(target) => target.short_name(),
795+
let config_flags = match flag {
796+
Flags::Rust => &target_config.rustflags,
797+
Flags::Rustdoc => &target_config.rustdocflags,
807798
};
808-
let key = format!("target.{}.{}", target, flag.as_key());
809-
if let Some(args) = gctx.get::<Option<StringList>>(&key)? {
810-
rustflags.extend(args.as_slice().iter().cloned());
799+
if let Some(args) = config_flags {
800+
rustflags.extend(args.val.as_slice().iter().cloned());
811801
}
812802
// ...including target.'cfg(...)'.rustflags
813803
if let Some(target_cfg) = target_cfg {
@@ -875,13 +865,11 @@ fn target_linker(
875865
/// Gets compiler flags from `[host]` section in the config.
876866
/// See [`extra_args`] for more.
877867
fn rustflags_from_host(
878-
gctx: &GlobalContext,
868+
target_config: &TargetConfig,
879869
flag: Flags,
880-
host_triple: &str,
881870
) -> CargoResult<Option<Vec<String>>> {
882-
let target_cfg = gctx.host_cfg_triple(host_triple)?;
883871
let list = match flag {
884-
Flags::Rust => &target_cfg.rustflags,
872+
Flags::Rust => &target_config.rustflags,
885873
Flags::Rustdoc => {
886874
// host.rustdocflags is not a thing, since it does not make sense
887875
return Ok(None);
@@ -940,9 +928,14 @@ impl<'gctx> RustcTargetData<'gctx> {
940928
let target_applies_to_host = gctx.target_applies_to_host()?;
941929
let host_target = CompileTarget::new(&rustc.host)?;
942930

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

0 commit comments

Comments
 (0)