2
0

build.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. extern crate rustc_version;
  2. use std::env;
  3. use std::fs::File;
  4. use std::io::Write;
  5. use std::ops::{Neg, Sub};
  6. use std::path::PathBuf;
  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
  48. .commit_date
  49. .as_ref()
  50. .map_or(true, |d| &**d >= "2018-01-01")
  51. {
  52. println!("cargo:rustc-cfg=core_memchr");
  53. }
  54. if ver
  55. .commit_date
  56. .as_ref()
  57. .map_or(true, |d| &**d >= "2017-06-15")
  58. {
  59. println!("cargo:rustc-cfg=no_collections");
  60. }
  61. if ver
  62. .commit_date
  63. .as_ref()
  64. .map_or(false, |d| &**d < "2016-12-15")
  65. {
  66. println!("cargo:rustc-cfg=rustc_unicode");
  67. } else if ver
  68. .commit_date
  69. .as_ref()
  70. .map_or(false, |d| &**d < "2017-03-03")
  71. {
  72. println!("cargo:rustc-cfg=std_unicode");
  73. }
  74. let mut dest_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  75. dest_path.push("io.rs");
  76. let mut f = File::create(&dest_path).unwrap();
  77. let mut target_path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
  78. target_path.push("src");
  79. target_path.push(io_commit);
  80. target_path.push("mod.rs");
  81. f.write_all(br#"#[path=""#).unwrap();
  82. f.write_all(
  83. target_path
  84. .into_os_string()
  85. .into_string()
  86. .unwrap()
  87. .replace("\\", "\\\\")
  88. .as_bytes(),
  89. )
  90. .unwrap();
  91. f.write_all(br#""] mod io;"#).unwrap();
  92. }