1 extern crate rustc_version; 2 3 use std::env; 4 use std::fs::File; 5 use std::io::Write; 6 use std::path::PathBuf; 7 use std::ops::{Neg,Sub}; 8 9 /* 10 * Let me explain this hack. For the sync shell script it's easiest if every 11 * line in mapping.rs looks exactly the same. This means that specifying an 12 * array literal is not possible. include!() can only expand to expressions, so 13 * just specifying the contents of an array is also not possible. 14 * 15 * This leaves us with trying to find an expression in which every line looks 16 * the same. This can be done using the `-` operator. This can be a unary 17 * operator (first thing on the first line), or a binary operator (later 18 * lines). That is exactly what's going on here, and Neg and Sub simply build a 19 * vector of the operangs. 20 */ 21 struct Mapping(&'static str,&'static str); 22 23 impl Neg for Mapping { 24 type Output = Vec<Mapping>; 25 fn neg(self) -> Vec<Mapping> { 26 vec![self.into()] 27 } 28 } 29 30 impl Sub<Mapping> for Vec<Mapping> { 31 type Output=Vec<Mapping>; 32 fn sub(mut self, rhs: Mapping) -> Vec<Mapping> { 33 self.push(rhs.into()); 34 self 35 } 36 } 37 38 fn main() { 39 let io_commit=match env::var("CORE_IO_COMMIT") { 40 Ok(c) => c, 41 Err(env::VarError::NotUnicode(_)) => panic!("Invalid commit specified in CORE_IO_COMMIT"), 42 Err(env::VarError::NotPresent) => { 43 let mappings=include!("mapping.rs"); 44 45 let compiler=rustc_version::version_meta().commit_hash.expect("Couldn't determine compiler version"); 46 mappings.iter().find(|&&Mapping(elem,_)|elem==compiler).expect("Unknown compiler version, upgrade core_io?").1.to_owned() 47 } 48 }; 49 50 let mut dest_path=PathBuf::from(env::var_os("OUT_DIR").unwrap()); 51 dest_path.push("io.rs"); 52 let mut f=File::create(&dest_path).unwrap(); 53 54 let mut target_path=PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); 55 target_path.push("src"); 56 target_path.push(io_commit); 57 target_path.push("mod.rs"); 58 59 f.write_all(br#"#[path=""#).unwrap(); 60 f.write_all(target_path.into_os_string().into_string().unwrap().as_bytes()).unwrap(); 61 f.write_all(br#""] mod io;"#).unwrap(); 62 } 63