Skip to content

Commit 23587ee

Browse files
committed
Expose rustc cfg values to build scripts
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.
1 parent 717adc8 commit 23587ee

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/cargo/ops/cargo_rustc/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,15 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
678678
self.target_config(kind).ar.as_ref().map(|s| s.as_ref())
679679
}
680680

681+
/// Get the list of cfg printed out from the compiler for the specified kind
682+
pub fn cfg(&self, kind: Kind) -> &[Cfg] {
683+
let info = match kind {
684+
Kind::Host => &self.host_info,
685+
Kind::Target => &self.target_info,
686+
};
687+
info.cfg.as_ref().map(|s| &s[..]).unwrap_or(&[])
688+
}
689+
681690
/// Get the target configuration for a particular host or target
682691
fn target_config(&self, kind: Kind) -> &TargetConfig {
683692
match kind {

src/cargo/ops/cargo_rustc/custom_build.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::str;
55
use std::sync::{Mutex, Arc};
66

77
use core::PackageId;
8-
use util::{CargoResult, Human, Freshness};
8+
use util::{CargoResult, Human, Freshness, Cfg};
99
use util::{internal, ChainError, profile, paths};
1010

1111
use super::job::Work;
@@ -124,6 +124,26 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
124124
}
125125
}
126126

127+
let mut cfg_map = HashMap::new();
128+
for cfg in cx.cfg(unit.kind) {
129+
match *cfg {
130+
Cfg::Name(ref n) => { cfg_map.insert(n.clone(), None); }
131+
Cfg::KeyPair(ref k, ref v) => {
132+
match *cfg_map.entry(k.clone()).or_insert(Some(Vec::new())) {
133+
Some(ref mut values) => values.push(v.clone()),
134+
None => { /* ... */ }
135+
}
136+
}
137+
}
138+
}
139+
for (k, v) in cfg_map {
140+
let k = format!("CARGO_CFG_{}", super::envify(&k));
141+
match v {
142+
Some(list) => { cmd.env(&k, list.join(",")); }
143+
None => { cmd.env(&k, ""); }
144+
}
145+
}
146+
127147
// Gather the set of native dependencies that this package has along with
128148
// some other variables to close over.
129149
//

tests/build-script.rs

+27
Original file line numberDiff line numberDiff line change
@@ -2259,3 +2259,30 @@ fn rustc_and_rustdoc_set_correctly() {
22592259
assert_that(build.cargo_process("bench"),
22602260
execs().with_status(0));
22612261
}
2262+
2263+
#[test]
2264+
fn cfg_env_vars_available() {
2265+
let build = project("builder")
2266+
.file("Cargo.toml", r#"
2267+
[package]
2268+
name = "builder"
2269+
version = "0.0.1"
2270+
authors = []
2271+
build = "build.rs"
2272+
"#)
2273+
.file("src/lib.rs", "")
2274+
.file("build.rs", r#"
2275+
use std::env;
2276+
2277+
fn main() {
2278+
let fam = env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
2279+
if cfg!(unix) {
2280+
assert_eq!(fam, "unix");
2281+
} else {
2282+
assert_eq!(fam, "windows");
2283+
}
2284+
}
2285+
"#);
2286+
assert_that(build.cargo_process("bench"),
2287+
execs().with_status(0));
2288+
}

0 commit comments

Comments
 (0)