2016-07-08 16:20:33 +08:00
|
|
|
extern crate rustc_version;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::PathBuf;
|
2019-05-01 09:27:50 +08:00
|
|
|
|
2016-07-08 16:20:33 +08:00
|
|
|
struct Mapping(&'static str,&'static str);
|
|
|
|
|
2019-05-01 09:27:50 +08:00
|
|
|
fn parse_mappings(mut mappings: &'static str) -> Vec<Mapping> {
|
|
|
|
// FIXME: The format used here used to be parsed directly by rustc, which
|
|
|
|
// is why it's kind of weird. It should be changed to a saner format.
|
|
|
|
|
|
|
|
const P1: &'static str = r#"-Mapping(""#;
|
|
|
|
const P2: &'static str = r#"",""#; ;
|
|
|
|
const P3: &'static str = "\")\n";
|
|
|
|
|
|
|
|
trait TakePrefix: Sized {
|
|
|
|
fn take_prefix(&mut self, mid: usize) -> Self;
|
2016-07-08 16:20:33 +08:00
|
|
|
}
|
|
|
|
|
2019-05-01 09:27:50 +08:00
|
|
|
impl<'a> TakePrefix for &'a str {
|
|
|
|
fn take_prefix(&mut self, mid: usize) -> Self {
|
|
|
|
let prefix = &self[..mid];
|
|
|
|
*self = &self[mid..];
|
|
|
|
prefix
|
|
|
|
}
|
2016-07-08 16:20:33 +08:00
|
|
|
}
|
2019-05-01 09:27:50 +08:00
|
|
|
|
|
|
|
let mut result = Vec::with_capacity( mappings.len() / (P1.len()+40+P2.len()+40+P3.len()) );
|
|
|
|
|
|
|
|
while mappings.len() != 0 {
|
|
|
|
match (
|
|
|
|
mappings.take_prefix(P1.len()),
|
|
|
|
mappings.take_prefix(40),
|
|
|
|
mappings.take_prefix(P2.len()),
|
|
|
|
mappings.take_prefix(40),
|
|
|
|
mappings.take_prefix(P3.len()),
|
|
|
|
) {
|
|
|
|
(P1, hash1, P2, hash2, P3) => result.push(Mapping(hash1, hash2)),
|
|
|
|
_ => panic!("Invalid input in mappings"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2016-07-08 16:20:33 +08:00
|
|
|
}
|
|
|
|
|
2019-05-01 09:41:09 +08:00
|
|
|
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] = &[
|
2019-05-15 09:15:20 +08:00
|
|
|
(None, &[("2019-02-24", Some("pattern_guards"))]),
|
|
|
|
(None, &[("2018-08-14", Some("non_exhaustive"))]),
|
|
|
|
(Some("collections"), &[("2018-08-13", None)]),
|
|
|
|
(Some("str_char"), &[("2018-08-13", None)]),
|
|
|
|
(Some("unicode"), &[("2018-08-13", None)]),
|
2019-05-01 09:41:09 +08:00
|
|
|
(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)]),
|
|
|
|
];
|
|
|
|
|
2016-07-08 16:20:33 +08:00
|
|
|
fn main() {
|
2017-04-10 04:50:59 +08:00
|
|
|
let ver=rustc_version::version_meta();
|
|
|
|
|
2016-10-12 08:48:48 +08:00
|
|
|
let io_commit=match env::var("CORE_IO_COMMIT") {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(env::VarError::NotUnicode(_)) => panic!("Invalid commit specified in CORE_IO_COMMIT"),
|
|
|
|
Err(env::VarError::NotPresent) => {
|
2019-05-01 09:27:50 +08:00
|
|
|
let mappings=parse_mappings(include_str!("mapping.rs"));
|
|
|
|
|
2017-04-10 04:50:59 +08:00
|
|
|
let compiler=ver.commit_hash.expect("Couldn't determine compiler version");
|
2016-10-12 08:48:48 +08:00
|
|
|
mappings.iter().find(|&&Mapping(elem,_)|elem==compiler).expect("Unknown compiler version, upgrade core_io?").1.to_owned()
|
|
|
|
}
|
|
|
|
};
|
2018-03-09 07:04:56 +08:00
|
|
|
|
2019-05-01 09:41:09 +08:00
|
|
|
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<date) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curcfg = nextcfg;
|
|
|
|
}
|
|
|
|
if let Some(cfg) = curcfg {
|
|
|
|
println!("cargo:rustc-cfg={}", cfg);
|
|
|
|
}
|
2017-04-10 04:50:59 +08:00
|
|
|
}
|
|
|
|
|
2016-07-08 16:20:33 +08:00
|
|
|
let mut dest_path=PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
dest_path.push("io.rs");
|
|
|
|
let mut f=File::create(&dest_path).unwrap();
|
2019-05-15 09:15:20 +08:00
|
|
|
|
2016-07-08 16:20:33 +08:00
|
|
|
let mut target_path=PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
target_path.push("src");
|
|
|
|
target_path.push(io_commit);
|
|
|
|
target_path.push("mod.rs");
|
|
|
|
|
|
|
|
f.write_all(br#"#[path=""#).unwrap();
|
2018-04-17 23:56:38 +08:00
|
|
|
f.write_all(target_path.into_os_string().into_string().unwrap().replace("\\", "\\\\").as_bytes()).unwrap();
|
2016-07-08 16:20:33 +08:00
|
|
|
f.write_all(br#""] mod io;"#).unwrap();
|
|
|
|
}
|