diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 59aa170212c..4e0cf00db6a 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -678,6 +678,15 @@ impl<'a, 'cfg> Context<'a, 'cfg> { self.target_config(kind).ar.as_ref().map(|s| s.as_ref()) } + /// Get the list of cfg 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.as_ref().map(|s| &s[..]).unwrap_or(&[]) + } + /// Get the target configuration for a particular host or target fn target_config(&self, kind: Kind) -> &TargetConfig { match kind { diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index 6a324a378a1..54c688134be 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -5,7 +5,7 @@ use std::str; use std::sync::{Mutex, Arc}; use core::PackageId; -use util::{CargoResult, Human, Freshness}; +use util::{CargoResult, Human, Freshness, Cfg}; use util::{internal, ChainError, profile, paths}; use super::job::Work; @@ -124,6 +124,26 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) } } + let mut cfg_map = HashMap::new(); + for cfg in cx.cfg(unit.kind) { + match *cfg { + Cfg::Name(ref n) => { cfg_map.insert(n.clone(), None); } + Cfg::KeyPair(ref k, ref v) => { + match *cfg_map.entry(k.clone()).or_insert(Some(Vec::new())) { + Some(ref mut values) => values.push(v.clone()), + None => { /* ... */ } + } + } + } + } + for (k, v) in cfg_map { + let k = format!("CARGO_CFG_{}", super::envify(&k)); + match v { + Some(list) => { cmd.env(&k, list.join(",")); } + None => { cmd.env(&k, ""); } + } + } + // Gather the set of native dependencies that this package has along with // some other variables to close over. // diff --git a/tests/build-script.rs b/tests/build-script.rs index e0669311039..18fca1ea710 100644 --- a/tests/build-script.rs +++ b/tests/build-script.rs @@ -2259,3 +2259,30 @@ fn rustc_and_rustdoc_set_correctly() { assert_that(build.cargo_process("bench"), execs().with_status(0)); } + +#[test] +fn cfg_env_vars_available() { + let build = project("builder") + .file("Cargo.toml", r#" + [package] + name = "builder" + version = "0.0.1" + authors = [] + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + use std::env; + + fn main() { + let fam = env::var("CARGO_CFG_TARGET_FAMILY").unwrap(); + if cfg!(unix) { + assert_eq!(fam, "unix"); + } else { + assert_eq!(fam, "windows"); + } + } + "#); + assert_that(build.cargo_process("bench"), + execs().with_status(0)); +}