Skip to content

Commit 7708522

Browse files
committed
Use cross compile style target/host isolation for all builds.
1 parent 197e657 commit 7708522

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2272
-1014
lines changed

crates/cargo-test-support/src/lib.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,22 @@ impl Project {
256256
self.root.clone()
257257
}
258258

259-
/// Project's target dir, ex: `/path/to/cargo/target/cit/t0/foo/target`
259+
/// Project's target dir, ex: `/path/to/cargo/target/cit/t0/foo/target/target-triple`
260260
pub fn build_dir(&self) -> PathBuf {
261-
self.root().join("target")
261+
self.root().join("target").join(rustc_host())
262262
}
263263

264-
/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/debug`
264+
/// Project's target dir, ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple`
265+
pub fn host_dir(&self) -> PathBuf {
266+
self.root().join("target/host").join(rustc_host())
267+
}
268+
269+
/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple/debug`
270+
pub fn host_debug_dir(&self) -> PathBuf {
271+
self.host_dir().join("debug")
272+
}
273+
274+
/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/target-triple/debug`
265275
pub fn target_debug_dir(&self) -> PathBuf {
266276
self.build_dir().join("debug")
267277
}
@@ -280,16 +290,25 @@ impl Project {
280290
.join(paths::get_lib_filename(name, kind))
281291
}
282292

293+
/// Path to an example built as a library.
294+
/// `kind` should be one of: "lib", "rlib", "staticlib", "dylib", "proc-macro"
295+
/// ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple/debug/examples/libex.rlib`
296+
pub fn host_example_lib(&self, name: &str, kind: &str) -> PathBuf {
297+
self.host_debug_dir()
298+
.join("examples")
299+
.join(paths::get_lib_filename(name, kind))
300+
}
301+
283302
/// Path to a debug binary.
284-
/// ex: `/path/to/cargo/target/cit/t0/foo/target/debug/foo`
303+
/// ex: `/path/to/cargo/target/target-triple/cit/t0/foo/target/target-triple/debug/foo`
285304
pub fn bin(&self, b: &str) -> PathBuf {
286305
self.build_dir()
287306
.join("debug")
288307
.join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
289308
}
290309

291310
/// Path to a release binary.
292-
/// ex: `/path/to/cargo/target/cit/t0/foo/target/release/foo`
311+
/// ex: `/path/to/cargo/target/target-triple/cit/t0/foo/target/target-triple/release/foo`
293312
pub fn release_bin(&self, b: &str) -> PathBuf {
294313
self.build_dir()
295314
.join("release")
@@ -299,11 +318,11 @@ impl Project {
299318
/// Path to a debug binary for a specific target triple.
300319
/// ex: `/path/to/cargo/target/cit/t0/foo/target/i686-apple-darwin/debug/foo`
301320
pub fn target_bin(&self, target: &str, b: &str) -> PathBuf {
302-
self.build_dir().join(target).join("debug").join(&format!(
303-
"{}{}",
304-
b,
305-
env::consts::EXE_SUFFIX
306-
))
321+
self.root()
322+
.join("target")
323+
.join(target)
324+
.join("debug")
325+
.join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
307326
}
308327

309328
/// Returns an iterator of paths matching the glob pattern, which is

src/bin/cargo/commands/bench.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ pub fn cli() -> App {
5151

5252
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5353
let ws = args.workspace(config)?;
54+
let rustc = config.load_global_rustc(Some(&ws));
5455
let mut compile_opts = args.compile_options(
5556
config,
57+
rustc,
5658
CompileMode::Bench,
5759
Some(&ws),
5860
ProfileChecking::Checked,

src/bin/cargo/commands/build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ pub fn cli() -> App {
4949

5050
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5151
let ws = args.workspace(config)?;
52+
let rustc = config.load_global_rustc(Some(&ws));
5253
let mut compile_opts = args.compile_options(
5354
config,
55+
rustc,
5456
CompileMode::Build,
5557
Some(&ws),
5658
ProfileChecking::Checked,

src/bin/cargo/commands/check.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5454
}
5555
};
5656
let mode = CompileMode::Check { test };
57-
let compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
57+
let rustc = config.load_global_rustc(Some(&ws));
58+
let compile_opts =
59+
args.compile_options(config, rustc, mode, Some(&ws), ProfileChecking::Unchecked)?;
5860

5961
ops::compile(&ws, &compile_opts)?;
6062
Ok(())

src/bin/cargo/commands/doc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
4040
let mode = CompileMode::Doc {
4141
deps: !args.is_present("no-deps"),
4242
};
43+
let rustc = config.load_global_rustc(Some(&ws));
4344
let mut compile_opts =
44-
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
45+
args.compile_options(config, rustc, mode, Some(&ws), ProfileChecking::Checked)?;
4546
compile_opts.rustdoc_document_private_items = args.is_present("document-private-items");
4647

4748
let doc_opts = DocOptions {

src/bin/cargo/commands/fix.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
8383

8484
// Unlike other commands default `cargo fix` to all targets to fix as much
8585
// code as we can.
86-
let mut opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
86+
let rustc = config.load_global_rustc(Some(&ws));
87+
let mut opts =
88+
args.compile_options(config, rustc, mode, Some(&ws), ProfileChecking::Unchecked)?;
8789

8890
if let CompileFilter::Default { .. } = opts.filter {
8991
opts.filter = CompileFilter::Only {

src/bin/cargo/commands/install.rs

+7
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
127127
None
128128
};
129129

130+
let ws = args.workspace(config);
131+
let rustc = if ws.is_ok() {
132+
config.load_global_rustc(Some(&ws?))
133+
} else {
134+
config.load_global_rustc(None)
135+
};
130136
let mut compile_opts = args.compile_options(
131137
config,
138+
rustc,
132139
CompileMode::Build,
133140
workspace.as_ref(),
134141
ProfileChecking::Checked,

src/bin/cargo/commands/run.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ pub fn cli() -> App {
3333
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
3434
let ws = args.workspace(config)?;
3535

36+
let rustc = config.load_global_rustc(Some(&ws));
3637
let mut compile_opts = args.compile_options(
3738
config,
39+
rustc,
3840
CompileMode::Build,
3941
Some(&ws),
4042
ProfileChecking::Checked,

src/bin/cargo/commands/rustc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6060
return Err(CliError::new(err, 101));
6161
}
6262
};
63+
let rustc = config.load_global_rustc(Some(&ws));
6364
let mut compile_opts = args.compile_options_for_single_package(
6465
config,
66+
rustc,
6567
mode,
6668
Some(&ws),
6769
ProfileChecking::Unchecked,

src/bin/cargo/commands/rustdoc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ pub fn cli() -> App {
4040

4141
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
4242
let ws = args.workspace(config)?;
43+
let rustc = config.load_global_rustc(Some(&ws));
4344
let mut compile_opts = args.compile_options_for_single_package(
4445
config,
46+
rustc,
4547
CompileMode::Doc { deps: false },
4648
Some(&ws),
4749
ProfileChecking::Checked,

src/bin/cargo/commands/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ pub fn cli() -> App {
6262
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6363
let ws = args.workspace(config)?;
6464

65+
let rustc = config.load_global_rustc(Some(&ws));
6566
let mut compile_opts = args.compile_options(
6667
config,
68+
rustc,
6769
CompileMode::Test,
6870
Some(&ws),
6971
ProfileChecking::Checked,

src/cargo/core/compiler/build_config.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::compiler::CompileKind;
22
use crate::util::interning::InternedString;
3-
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
3+
use crate::util::{CargoResult, Rustc, RustfixDiagnosticServer};
4+
use crate::Config;
45
use anyhow::bail;
56
use cargo_util::ProcessBuilder;
67
use serde::ser;
@@ -52,12 +53,14 @@ impl BuildConfig {
5253
/// * `target.$target.libfoo.metadata`
5354
pub fn new(
5455
config: &Config,
56+
rustc: CargoResult<Rustc>,
5557
jobs: Option<u32>,
5658
requested_targets: &[String],
5759
mode: CompileMode,
5860
) -> CargoResult<BuildConfig> {
5961
let cfg = config.build_config()?;
60-
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
62+
let requested_kinds =
63+
CompileKind::from_requested_targets(config, rustc, requested_targets)?;
6164
if jobs == Some(0) {
6265
anyhow::bail!("jobs must be at least 1")
6366
}

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

+39-25
Original file line numberDiff line numberDiff line change
@@ -588,22 +588,29 @@ fn env_args(
588588
// This means that, e.g., even if the specified --target is the
589589
// same as the host, build scripts in plugins won't get
590590
// RUSTFLAGS.
591-
if requested_kinds != [CompileKind::Host] && kind.is_host() {
592-
// This is probably a build script or plugin and we're
593-
// compiling with --target. In this scenario there are
594-
// no rustflags we can apply.
595-
return Ok(Vec::new());
596-
}
597-
598-
// First try RUSTFLAGS from the environment
599-
if let Ok(a) = env::var(name) {
600-
let args = a
601-
.split(' ')
602-
.map(str::trim)
603-
.filter(|s| !s.is_empty())
604-
.map(str::to_string);
605-
return Ok(args.collect());
606-
}
591+
let prefix = if requested_kinds != [CompileKind::Host] && kind.is_host() {
592+
// Next try HOST_RUSTFLAGS from the environment
593+
if let Ok(a) = env::var(format!("HOST_{}", name)) {
594+
let args = a
595+
.split(' ')
596+
.map(str::trim)
597+
.filter(|s| !s.is_empty())
598+
.map(str::to_string);
599+
return Ok(args.collect());
600+
}
601+
"host"
602+
} else {
603+
// First try RUSTFLAGS from the environment
604+
if let Ok(a) = env::var(name) {
605+
let args = a
606+
.split(' ')
607+
.map(str::trim)
608+
.filter(|s| !s.is_empty())
609+
.map(str::to_string);
610+
return Ok(args.collect());
611+
}
612+
"target"
613+
};
607614

608615
let mut rustflags = Vec::new();
609616

@@ -616,9 +623,14 @@ fn env_args(
616623
CompileKind::Host => host_triple,
617624
CompileKind::Target(target) => target.short_name(),
618625
};
619-
let key = format!("target.{}.{}", target, name);
626+
let key = format!("{}.{}.{}", prefix, target, name);
620627
if let Some(args) = config.get::<Option<StringList>>(&key)? {
621628
rustflags.extend(args.as_slice().iter().cloned());
629+
} else {
630+
let generic_key = format!("{}.{}", prefix, name);
631+
if let Some(args) = config.get::<Option<StringList>>(&generic_key)? {
632+
rustflags.extend(args.as_slice().iter().cloned());
633+
}
622634
}
623635
// ...including target.'cfg(...)'.rustflags
624636
if let Some(target_cfg) = target_cfg {
@@ -641,14 +653,16 @@ fn env_args(
641653
}
642654

643655
// Then the `build.rustflags` value.
644-
let build = config.build_config()?;
645-
let list = if name == "rustflags" {
646-
&build.rustflags
647-
} else {
648-
&build.rustdocflags
649-
};
650-
if let Some(list) = list {
651-
return Ok(list.as_slice().to_vec());
656+
if requested_kinds == [CompileKind::Host] || !kind.is_host() {
657+
let build = config.build_config()?;
658+
let list = if name == "rustflags" {
659+
&build.rustflags
660+
} else {
661+
&build.rustdocflags
662+
};
663+
if let Some(list) = list {
664+
return Ok(list.as_slice().to_vec());
665+
}
652666
}
653667

654668
Ok(Vec::new())

src/cargo/core/compiler/compile_kind.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::core::Target;
22
use crate::util::errors::CargoResult;
33
use crate::util::interning::InternedString;
4-
use crate::util::{Config, StableHasher};
4+
use crate::util::{Rustc, StableHasher};
5+
use crate::Config;
56
use anyhow::{bail, Context as _};
67
use serde::Serialize;
78
use std::collections::BTreeSet;
@@ -50,6 +51,7 @@ impl CompileKind {
5051
/// `CompileKind::Host`.
5152
pub fn from_requested_targets(
5253
config: &Config,
54+
rustc: CargoResult<Rustc>,
5355
targets: &[String],
5456
) -> CargoResult<Vec<CompileKind>> {
5557
if targets.len() > 1 && !config.cli_unstable().multitarget {
@@ -76,7 +78,13 @@ impl CompileKind {
7678
};
7779
CompileKind::Target(CompileTarget::new(&value)?)
7880
}
79-
None => CompileKind::Host,
81+
None => {
82+
if rustc.is_ok() {
83+
CompileKind::Target(CompileTarget::new(&rustc.unwrap().host)?)
84+
} else {
85+
CompileKind::Host
86+
}
87+
}
8088
};
8189
Ok(vec![kind])
8290
}

src/cargo/core/compiler/context/compilation_files.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
247247
/// Directory where the fingerprint for the given unit should go.
248248
pub fn fingerprint_dir(&self, unit: &Unit) -> PathBuf {
249249
let dir = self.pkg_dir(unit);
250-
self.layout(unit.kind).fingerprint().join(dir)
250+
let kind = if unit.mode.is_run_custom_build() {
251+
CompileKind::Host
252+
} else {
253+
unit.kind
254+
};
255+
self.layout(kind).fingerprint().join(dir)
251256
}
252257

253258
/// Returns the path for a file in the fingerprint directory.

0 commit comments

Comments
 (0)