Skip to content

Commit 4bab33b

Browse files
authored
Merge pull request #24 from jethrogb/dry-conditional-cfg
Be more DRY with conditional cfgs in build.rs
2 parents 83eeab3 + 29b0126 commit 4bab33b

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

build.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ fn parse_mappings(mut mappings: &'static str) -> Vec<Mapping> {
4545
result
4646
}
4747

48+
type Cfg = Option<&'static str>;
49+
type Date = &'static str;
50+
/// A `ConditionalCfg` is basically a list of optional feature names
51+
/// (`Cfg`s) separated by `Date`s. The dates specify ranges of compiler
52+
/// versions for which to enable particular features.
53+
type ConditionalCfg = (Cfg, &'static [(Date, Cfg)]);
54+
const CONDITIONAL_CFGS: &'static [ConditionalCfg] = &[
55+
(None, &[("2018-01-01", Some("core_memchr"))]),
56+
(None, &[("2017-06-15", Some("no_collections"))]),
57+
(Some("rustc_unicode"), &[("2016-12-15", Some("std_unicode")), ("2017-03-03", None)]),
58+
];
59+
4860
fn main() {
4961
let ver=rustc_version::version_meta();
5062

@@ -59,18 +71,17 @@ fn main() {
5971
}
6072
};
6173

62-
if ver.commit_date.as_ref().map_or(false,|d| &**d>="2018-01-01") {
63-
println!("cargo:rustc-cfg=core_memchr");
64-
}
65-
66-
if ver.commit_date.as_ref().map_or(false,|d| &**d>="2017-06-15") {
67-
println!("cargo:rustc-cfg=no_collections");
68-
}
69-
70-
if ver.commit_date.as_ref().map_or(false,|d| &**d<"2016-12-15") {
71-
println!("cargo:rustc-cfg=rustc_unicode");
72-
} else if ver.commit_date.as_ref().map_or(false,|d| &**d<"2017-03-03") {
73-
println!("cargo:rustc-cfg=std_unicode");
74+
for &(mut curcfg, rest) in CONDITIONAL_CFGS {
75+
for &(date, nextcfg) in rest {
76+
// if no commit_date is provided, assume compiler is current
77+
if ver.commit_date.as_ref().map_or(false,|d| &**d<date) {
78+
break;
79+
}
80+
curcfg = nextcfg;
81+
}
82+
if let Some(cfg) = curcfg {
83+
println!("cargo:rustc-cfg={}", cfg);
84+
}
7485
}
7586

7687
let mut dest_path=PathBuf::from(env::var_os("OUT_DIR").unwrap());

0 commit comments

Comments
 (0)