Skip to content

Commit

Permalink
Use cross compile style target/host isolation for all builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Jul 13, 2021
1 parent 197e657 commit 7708522
Show file tree
Hide file tree
Showing 78 changed files with 2,272 additions and 1,014 deletions.
39 changes: 29 additions & 10 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,22 @@ impl Project {
self.root.clone()
}

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

/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/debug`
/// Project's target dir, ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple`
pub fn host_dir(&self) -> PathBuf {
self.root().join("target/host").join(rustc_host())
}

/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple/debug`
pub fn host_debug_dir(&self) -> PathBuf {
self.host_dir().join("debug")
}

/// Project's debug dir, ex: `/path/to/cargo/target/cit/t0/foo/target/target-triple/debug`
pub fn target_debug_dir(&self) -> PathBuf {
self.build_dir().join("debug")
}
Expand All @@ -280,16 +290,25 @@ impl Project {
.join(paths::get_lib_filename(name, kind))
}

/// Path to an example built as a library.
/// `kind` should be one of: "lib", "rlib", "staticlib", "dylib", "proc-macro"
/// ex: `/path/to/cargo/target/cit/t0/foo/target/host/host-triple/debug/examples/libex.rlib`
pub fn host_example_lib(&self, name: &str, kind: &str) -> PathBuf {
self.host_debug_dir()
.join("examples")
.join(paths::get_lib_filename(name, kind))
}

/// Path to a debug binary.
/// ex: `/path/to/cargo/target/cit/t0/foo/target/debug/foo`
/// ex: `/path/to/cargo/target/target-triple/cit/t0/foo/target/target-triple/debug/foo`
pub fn bin(&self, b: &str) -> PathBuf {
self.build_dir()
.join("debug")
.join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
}

/// Path to a release binary.
/// ex: `/path/to/cargo/target/cit/t0/foo/target/release/foo`
/// ex: `/path/to/cargo/target/target-triple/cit/t0/foo/target/target-triple/release/foo`
pub fn release_bin(&self, b: &str) -> PathBuf {
self.build_dir()
.join("release")
Expand All @@ -299,11 +318,11 @@ impl Project {
/// Path to a debug binary for a specific target triple.
/// ex: `/path/to/cargo/target/cit/t0/foo/target/i686-apple-darwin/debug/foo`
pub fn target_bin(&self, target: &str, b: &str) -> PathBuf {
self.build_dir().join(target).join("debug").join(&format!(
"{}{}",
b,
env::consts::EXE_SUFFIX
))
self.root()
.join("target")
.join(target)
.join("debug")
.join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
}

/// Returns an iterator of paths matching the glob pattern, which is
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ pub fn cli() -> App {

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options(
config,
rustc,
CompileMode::Bench,
Some(&ws),
ProfileChecking::Checked,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ pub fn cli() -> App {

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options(
config,
rustc,
CompileMode::Build,
Some(&ws),
ProfileChecking::Checked,
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
}
};
let mode = CompileMode::Check { test };
let compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
let rustc = config.load_global_rustc(Some(&ws));
let compile_opts =
args.compile_options(config, rustc, mode, Some(&ws), ProfileChecking::Unchecked)?;

ops::compile(&ws, &compile_opts)?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let mode = CompileMode::Doc {
deps: !args.is_present("no-deps"),
};
let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts =
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
args.compile_options(config, rustc, mode, Some(&ws), ProfileChecking::Checked)?;
compile_opts.rustdoc_document_private_items = args.is_present("document-private-items");

let doc_opts = DocOptions {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {

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

if let CompileFilter::Default { .. } = opts.filter {
opts.filter = CompileFilter::Only {
Expand Down
7 changes: 7 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
None
};

let ws = args.workspace(config);
let rustc = if ws.is_ok() {
config.load_global_rustc(Some(&ws?))
} else {
config.load_global_rustc(None)
};
let mut compile_opts = args.compile_options(
config,
rustc,
CompileMode::Build,
workspace.as_ref(),
ProfileChecking::Checked,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ pub fn cli() -> App {
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options(
config,
rustc,
CompileMode::Build,
Some(&ws),
ProfileChecking::Checked,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
return Err(CliError::new(err, 101));
}
};
let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options_for_single_package(
config,
rustc,
mode,
Some(&ws),
ProfileChecking::Unchecked,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ pub fn cli() -> App {

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options_for_single_package(
config,
rustc,
CompileMode::Doc { deps: false },
Some(&ws),
ProfileChecking::Checked,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ pub fn cli() -> App {
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let rustc = config.load_global_rustc(Some(&ws));
let mut compile_opts = args.compile_options(
config,
rustc,
CompileMode::Test,
Some(&ws),
ProfileChecking::Checked,
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::core::compiler::CompileKind;
use crate::util::interning::InternedString;
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
use crate::util::{CargoResult, Rustc, RustfixDiagnosticServer};
use crate::Config;
use anyhow::bail;
use cargo_util::ProcessBuilder;
use serde::ser;
Expand Down Expand Up @@ -52,12 +53,14 @@ impl BuildConfig {
/// * `target.$target.libfoo.metadata`
pub fn new(
config: &Config,
rustc: CargoResult<Rustc>,
jobs: Option<u32>,
requested_targets: &[String],
mode: CompileMode,
) -> CargoResult<BuildConfig> {
let cfg = config.build_config()?;
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
let requested_kinds =
CompileKind::from_requested_targets(config, rustc, requested_targets)?;
if jobs == Some(0) {
anyhow::bail!("jobs must be at least 1")
}
Expand Down
64 changes: 39 additions & 25 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,29 @@ fn env_args(
// This means that, e.g., even if the specified --target is the
// same as the host, build scripts in plugins won't get
// RUSTFLAGS.
if requested_kinds != [CompileKind::Host] && kind.is_host() {
// This is probably a build script or plugin and we're
// compiling with --target. In this scenario there are
// no rustflags we can apply.
return Ok(Vec::new());
}

// First try RUSTFLAGS from the environment
if let Ok(a) = env::var(name) {
let args = a
.split(' ')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
return Ok(args.collect());
}
let prefix = if requested_kinds != [CompileKind::Host] && kind.is_host() {
// Next try HOST_RUSTFLAGS from the environment
if let Ok(a) = env::var(format!("HOST_{}", name)) {
let args = a
.split(' ')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
return Ok(args.collect());
}
"host"
} else {
// First try RUSTFLAGS from the environment
if let Ok(a) = env::var(name) {
let args = a
.split(' ')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
return Ok(args.collect());
}
"target"
};

let mut rustflags = Vec::new();

Expand All @@ -616,9 +623,14 @@ fn env_args(
CompileKind::Host => host_triple,
CompileKind::Target(target) => target.short_name(),
};
let key = format!("target.{}.{}", target, name);
let key = format!("{}.{}.{}", prefix, target, name);
if let Some(args) = config.get::<Option<StringList>>(&key)? {
rustflags.extend(args.as_slice().iter().cloned());
} else {
let generic_key = format!("{}.{}", prefix, name);
if let Some(args) = config.get::<Option<StringList>>(&generic_key)? {
rustflags.extend(args.as_slice().iter().cloned());
}
}
// ...including target.'cfg(...)'.rustflags
if let Some(target_cfg) = target_cfg {
Expand All @@ -641,14 +653,16 @@ fn env_args(
}

// Then the `build.rustflags` value.
let build = config.build_config()?;
let list = if name == "rustflags" {
&build.rustflags
} else {
&build.rustdocflags
};
if let Some(list) = list {
return Ok(list.as_slice().to_vec());
if requested_kinds == [CompileKind::Host] || !kind.is_host() {
let build = config.build_config()?;
let list = if name == "rustflags" {
&build.rustflags
} else {
&build.rustdocflags
};
if let Some(list) = list {
return Ok(list.as_slice().to_vec());
}
}

Ok(Vec::new())
Expand Down
12 changes: 10 additions & 2 deletions src/cargo/core/compiler/compile_kind.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::core::Target;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{Config, StableHasher};
use crate::util::{Rustc, StableHasher};
use crate::Config;
use anyhow::{bail, Context as _};
use serde::Serialize;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -50,6 +51,7 @@ impl CompileKind {
/// `CompileKind::Host`.
pub fn from_requested_targets(
config: &Config,
rustc: CargoResult<Rustc>,
targets: &[String],
) -> CargoResult<Vec<CompileKind>> {
if targets.len() > 1 && !config.cli_unstable().multitarget {
Expand All @@ -76,7 +78,13 @@ impl CompileKind {
};
CompileKind::Target(CompileTarget::new(&value)?)
}
None => CompileKind::Host,
None => {
if rustc.is_ok() {
CompileKind::Target(CompileTarget::new(&rustc.unwrap().host)?)
} else {
CompileKind::Host
}
}
};
Ok(vec![kind])
}
Expand Down
7 changes: 6 additions & 1 deletion src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
/// Directory where the fingerprint for the given unit should go.
pub fn fingerprint_dir(&self, unit: &Unit) -> PathBuf {
let dir = self.pkg_dir(unit);
self.layout(unit.kind).fingerprint().join(dir)
let kind = if unit.mode.is_run_custom_build() {
CompileKind::Host
} else {
unit.kind
};
self.layout(kind).fingerprint().join(dir)
}

/// Returns the path for a file in the fingerprint directory.
Expand Down
Loading

0 comments on commit 7708522

Please sign in to comment.