Skip to content

Commit

Permalink
Auto merge of #7425 - alexcrichton:kind-string, r=ehuss
Browse files Browse the repository at this point in the history
Refactor `Kind` to carry target name in `Target`

This commit is an internal refactoring of Cargo's compilation backend to
eventually support compiling multiple target simultaneously. The
original motivation for this came up in discussion of #7297 and this has
long been something I've intended to update Cargo for. Nothing in the
backend currently exposes the ability to actually build multiple target
simultaneously, but this should have no function change with respect to
all current consumers. Eventually we'll need to refactor APIs of how you
enter the compilation backend to compile for multiple targets.
  • Loading branch information
bors committed Sep 26, 2019
2 parents d2db2bd + f745ca7 commit d8e62ee
Show file tree
Hide file tree
Showing 26 changed files with 434 additions and 386 deletions.
2 changes: 1 addition & 1 deletion crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ pub static RUSTC: Rustc = Rustc::new(

/// The rustc host such as `x86_64-unknown-linux-gnu`.
pub fn rustc_host() -> String {
RUSTC.with(|r| r.host.clone())
RUSTC.with(|r| r.host.to_string())
}

pub fn is_nightly() -> bool {
Expand Down
54 changes: 19 additions & 35 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::cell::RefCell;
use std::path::Path;

use serde::ser;

use crate::core::compiler::{CompileKind, CompileTarget};
use crate::util::ProcessBuilder;
use crate::util::{CargoResult, CargoResultExt, Config, RustfixDiagnosticServer};
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};

/// Configuration information for a rustc build.
#[derive(Debug)]
pub struct BuildConfig {
/// The target arch triple.
/// Default: host arch.
pub requested_target: Option<String>,
/// The requested kind of compilation for this session
pub requested_kind: CompileKind,
/// Number of rustc jobs to run in parallel.
pub jobs: u32,
/// `true` if we are building for release.
Expand Down Expand Up @@ -46,36 +45,21 @@ impl BuildConfig {
requested_target: &Option<String>,
mode: CompileMode,
) -> CargoResult<BuildConfig> {
let requested_target = match requested_target {
&Some(ref target) if target.ends_with(".json") => {
let path = Path::new(target).canonicalize().chain_err(|| {
failure::format_err!("Target path {:?} is not a valid file", target)
})?;
Some(
path.into_os_string()
.into_string()
.map_err(|_| failure::format_err!("Target path is not valid unicode"))?,
)
}
other => other.clone(),
let requested_kind = match requested_target {
Some(s) => CompileKind::Target(CompileTarget::new(s)?),
None => match config.get_string("build.target")? {
Some(cfg) => {
let value = if cfg.val.ends_with(".json") {
let path = cfg.definition.root(config).join(&cfg.val);
path.to_str().expect("must be utf-8 in toml").to_string()
} else {
cfg.val
};
CompileKind::Target(CompileTarget::new(&value)?)
}
None => CompileKind::Host,
},
};
if let Some(ref s) = requested_target {
if s.trim().is_empty() {
failure::bail!("target was empty")
}
}
let cfg_target = match config.get_string("build.target")? {
Some(ref target) if target.val.ends_with(".json") => {
let path = target.definition.root(config).join(&target.val);
let path_string = path
.into_os_string()
.into_string()
.map_err(|_| failure::format_err!("Target path is not valid unicode"));
Some(path_string?)
}
other => other.map(|t| t.val),
};
let target = requested_target.or(cfg_target);

if jobs == Some(0) {
failure::bail!("jobs must be at least 1")
Expand All @@ -91,7 +75,7 @@ impl BuildConfig {
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);

Ok(BuildConfig {
requested_target: target,
requested_kind,
jobs,
release: false,
mode,
Expand Down
130 changes: 63 additions & 67 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str;

use cargo_platform::Cfg;
use log::debug;

use crate::core::compiler::unit::UnitInterner;
use crate::core::compiler::{BuildConfig, BuildOutput, Kind, Unit};
use crate::core::compiler::CompileTarget;
use crate::core::compiler::{BuildConfig, BuildOutput, CompileKind, Unit};
use crate::core::profiles::Profiles;
use crate::core::{Dependency, Workspace};
use crate::core::{Dependency, InternedString, Workspace};
use crate::core::{PackageId, PackageSet};
use crate::util::errors::CargoResult;
use crate::util::{profile, Config, Rustc};
use crate::util::{Config, Rustc};
use cargo_platform::Cfg;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str;

mod target_info;
pub use self::target_info::{FileFlavor, TargetInfo};
Expand All @@ -33,15 +31,24 @@ pub struct BuildContext<'a, 'cfg> {
pub extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
pub packages: &'a PackageSet<'cfg>,

/// Information about the compiler.
pub rustc: Rustc,
/// Build information for the host arch.
pub host_config: TargetConfig,
/// Build information for the target.
pub target_config: TargetConfig,
pub target_info: TargetInfo,
pub host_info: TargetInfo,
/// Source of interning new units as they're created.
pub units: &'a UnitInterner<'a>,

/// Information about the compiler that we've detected on the local system.
pub rustc: Rustc,

/// Build information for the "host", which is information about when
/// `rustc` is invoked without a `--target` flag. This is used for
/// procedural macros, build scripts, etc.
host_config: TargetConfig,
host_info: TargetInfo,

/// Build information for targets that we're building for. This will be
/// empty if the `--target` flag is not passed, and currently also only ever
/// has at most one entry, but eventually we'd like to support multi-target
/// builds with Cargo.
target_config: HashMap<CompileTarget, TargetConfig>,
target_info: HashMap<CompileTarget, TargetInfo>,
}

impl<'a, 'cfg> BuildContext<'a, 'cfg> {
Expand All @@ -57,19 +64,26 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
let rustc = config.load_global_rustc(Some(ws))?;

let host_config = TargetConfig::new(config, &rustc.host)?;
let target_config = match build_config.requested_target.as_ref() {
Some(triple) => TargetConfig::new(config, triple)?,
None => host_config.clone(),
};
let (host_info, target_info) = {
let _p = profile::start("BuildContext::probe_target_info");
debug!("probe_target_info");
let host_info =
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::Host)?;
let target_info =
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::Target)?;
(host_info, target_info)
};
let host_info = TargetInfo::new(
config,
build_config.requested_kind,
&rustc,
CompileKind::Host,
)?;
let mut target_config = HashMap::new();
let mut target_info = HashMap::new();
if let CompileKind::Target(target) = build_config.requested_kind {
target_config.insert(target, TargetConfig::new(config, target.short_name())?);
target_info.insert(
target,
TargetInfo::new(
config,
build_config.requested_kind,
&rustc,
CompileKind::Target(target),
)?,
);
}

Ok(BuildContext {
ws,
Expand All @@ -88,38 +102,31 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
}

/// Whether a dependency should be compiled for the host or target platform,
/// specified by `Kind`.
pub fn dep_platform_activated(&self, dep: &Dependency, kind: Kind) -> bool {
/// specified by `CompileKind`.
pub fn dep_platform_activated(&self, dep: &Dependency, kind: CompileKind) -> bool {
// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
let platform = match dep.platform() {
Some(p) => p,
None => return true,
};
let (name, info) = match kind {
Kind::Host => (self.host_triple(), &self.host_info),
Kind::Target => (self.target_triple(), &self.target_info),
};
platform.matches(name, info.cfg())
let name = kind.short_name(self);
platform.matches(&name, self.cfg(kind))
}

/// Gets the user-specified linker for a particular host or target.
pub fn linker(&self, kind: Kind) -> Option<&Path> {
pub fn linker(&self, kind: CompileKind) -> Option<&Path> {
self.target_config(kind).linker.as_ref().map(|s| s.as_ref())
}

/// Gets the user-specified `ar` program for a particular host or target.
pub fn ar(&self, kind: Kind) -> Option<&Path> {
pub fn ar(&self, kind: CompileKind) -> Option<&Path> {
self.target_config(kind).ar.as_ref().map(|s| s.as_ref())
}

/// Gets the list of `cfg`s printed out from the compiler for the specified kind.
pub fn cfg(&self, kind: Kind) -> &[Cfg] {
let info = match kind {
Kind::Host => &self.host_info,
Kind::Target => &self.target_info,
};
info.cfg()
pub fn cfg(&self, kind: CompileKind) -> &[Cfg] {
self.info(kind).cfg()
}

/// Gets the host architecture triple.
Expand All @@ -128,23 +135,15 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
/// - machine: x86_64,
/// - hardware-platform: unknown,
/// - operating system: linux-gnu.
pub fn host_triple(&self) -> &str {
&self.rustc.host
}

pub fn target_triple(&self) -> &str {
self.build_config
.requested_target
.as_ref()
.map(|s| s.as_str())
.unwrap_or_else(|| self.host_triple())
pub fn host_triple(&self) -> InternedString {
self.rustc.host
}

/// Gets the target configuration for a particular host or target.
fn target_config(&self, kind: Kind) -> &TargetConfig {
pub fn target_config(&self, kind: CompileKind) -> &TargetConfig {
match kind {
Kind::Host => &self.host_config,
Kind::Target => &self.target_config,
CompileKind::Host => &self.host_config,
CompileKind::Target(s) => &self.target_config[&s],
}
}

Expand All @@ -165,10 +164,10 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
pkg.source_id().is_path() || self.config.extra_verbose()
}

fn info(&self, kind: Kind) -> &TargetInfo {
pub fn info(&self, kind: CompileKind) -> &TargetInfo {
match kind {
Kind::Host => &self.host_info,
Kind::Target => &self.target_info,
CompileKind::Host => &self.host_info,
CompileKind::Target(s) => &self.target_info[&s],
}
}

Expand All @@ -180,11 +179,8 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
///
/// `lib_name` is the `links` library name and `kind` is whether it is for
/// Host or Target.
pub fn script_override(&self, lib_name: &str, kind: Kind) -> Option<&BuildOutput> {
match kind {
Kind::Host => self.host_config.overrides.get(lib_name),
Kind::Target => self.target_config.overrides.get(lib_name),
}
pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> {
self.target_config(kind).overrides.get(lib_name)
}
}

Expand Down
Loading

0 comments on commit d8e62ee

Please sign in to comment.