From 29b012608c514fe9f975b751a6cfa9c322e89223 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Tue, 30 Apr 2019 18:41:09 -0700 Subject: [PATCH] Be more DRY with conditional cfgs in build.rs --- build.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/build.rs b/build.rs index c4c7f03..17f5359 100644 --- a/build.rs +++ b/build.rs @@ -45,6 +45,18 @@ fn parse_mappings(mut mappings: &'static str) -> Vec { result } +type Cfg = Option<&'static str>; +type Date = &'static str; +/// A `ConditionalCfg` is basically a list of optional feature names +/// (`Cfg`s) separated by `Date`s. The dates specify ranges of compiler +/// versions for which to enable particular features. +type ConditionalCfg = (Cfg, &'static [(Date, Cfg)]); +const CONDITIONAL_CFGS: &'static [ConditionalCfg] = &[ + (None, &[("2018-01-01", Some("core_memchr"))]), + (None, &[("2017-06-15", Some("no_collections"))]), + (Some("rustc_unicode"), &[("2016-12-15", Some("std_unicode")), ("2017-03-03", None)]), +]; + fn main() { let ver=rustc_version::version_meta(); @@ -59,18 +71,17 @@ fn main() { } }; - if ver.commit_date.as_ref().map_or(false,|d| &**d>="2018-01-01") { - println!("cargo:rustc-cfg=core_memchr"); - } - - if ver.commit_date.as_ref().map_or(false,|d| &**d>="2017-06-15") { - println!("cargo:rustc-cfg=no_collections"); - } - - if ver.commit_date.as_ref().map_or(false,|d| &**d<"2016-12-15") { - println!("cargo:rustc-cfg=rustc_unicode"); - } else if ver.commit_date.as_ref().map_or(false,|d| &**d<"2017-03-03") { - println!("cargo:rustc-cfg=std_unicode"); + for &(mut curcfg, rest) in CONDITIONAL_CFGS { + for &(date, nextcfg) in rest { + // if no commit_date is provided, assume compiler is current + if ver.commit_date.as_ref().map_or(false,|d| &**d