build.rs 2.6 KB

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