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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-04-10 04:50:59 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|