build.rs 1.8 KB

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