build.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ver=rustc_version::version_meta();
  35. let io_commit=match env::var("CORE_IO_COMMIT") {
  36. Ok(c) => c,
  37. Err(env::VarError::NotUnicode(_)) => panic!("Invalid commit specified in CORE_IO_COMMIT"),
  38. Err(env::VarError::NotPresent) => {
  39. let mappings=include!("mapping.rs");
  40. let compiler=ver.commit_hash.expect("Couldn't determine compiler version");
  41. mappings.iter().find(|&&Mapping(elem,_)|elem==compiler).expect("Unknown compiler version, upgrade core_io?").1.to_owned()
  42. }
  43. };
  44. if ver.commit_date.as_ref().map_or(false,|d| &**d>="2018-01-01") {
  45. println!("cargo:rustc-cfg=core_memchr");
  46. }
  47. if ver.commit_date.as_ref().map_or(false,|d| &**d>="2017-06-15") {
  48. println!("cargo:rustc-cfg=no_collections");
  49. }
  50. if ver.commit_date.as_ref().map_or(false,|d| &**d<"2016-12-15") {
  51. println!("cargo:rustc-cfg=rustc_unicode");
  52. } else if ver.commit_date.as_ref().map_or(false,|d| &**d<"2017-03-03") {
  53. println!("cargo:rustc-cfg=std_unicode");
  54. }
  55. let mut dest_path=PathBuf::from(env::var_os("OUT_DIR").unwrap());
  56. dest_path.push("io.rs");
  57. let mut f=File::create(&dest_path).unwrap();
  58. let mut target_path=PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
  59. target_path.push("src");
  60. target_path.push(io_commit);
  61. target_path.push("mod.rs");
  62. f.write_all(br#"#[path=""#).unwrap();
  63. f.write_all(target_path.into_os_string().into_string().unwrap().replace("\\", "\\\\").as_bytes()).unwrap();
  64. f.write_all(br#""] mod io;"#).unwrap();
  65. }