Skip to content

Commit

Permalink
Expose rustc cfg values to build scripts
Browse files Browse the repository at this point in the history
This commit is Cargo's portion of the implementation of [RFC 1721] where it will
expose values printed by `rustc --print cfg` to build scripts.

[RFC 1721]: https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md

This will in turn be used to communicate features like `-C
target-feature=+crt-static` which can be used to compile objects for statically
linking against the msvcrt on MSVC.
  • Loading branch information
alexcrichton committed Nov 2, 2016
1 parent 717adc8 commit 23587ee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 21 additions & 1 deletion src/cargo/ops/cargo_rustc/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
//
Expand Down
27 changes: 27 additions & 0 deletions tests/build-script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 23587ee

Please sign in to comment.