build.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use std::env;
  2. use std::process::Command;
  3. use std::str::{self, FromStr};
  4. // The rustc-cfg strings below are *not* public API. Please let us know by
  5. // opening a GitHub issue if your build environment requires some way to enable
  6. // these cfgs other than by executing our build script.
  7. fn main() {
  8. let minor = match rustc_minor_version() {
  9. Some(minor) => minor,
  10. None => return,
  11. };
  12. let target = env::var("TARGET").unwrap();
  13. let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
  14. // std::collections::Bound was stabilized in Rust 1.17
  15. // but it was moved to core::ops later in Rust 1.26:
  16. // https://doc.rust-lang.org/core/ops/enum.Bound.html
  17. if minor < 26 {
  18. println!("cargo:rustc-cfg=no_ops_bound");
  19. if minor < 17 {
  20. println!("cargo:rustc-cfg=no_collections_bound");
  21. }
  22. }
  23. // core::cmp::Reverse stabilized in Rust 1.19:
  24. // https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html
  25. if minor < 19 {
  26. println!("cargo:rustc-cfg=no_core_reverse");
  27. }
  28. // CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
  29. // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
  30. // https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
  31. if minor < 20 {
  32. println!("cargo:rustc-cfg=no_de_boxed_c_str");
  33. println!("cargo:rustc-cfg=no_de_boxed_path");
  34. }
  35. // From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
  36. // https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
  37. // https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
  38. if minor < 21 {
  39. println!("cargo:rustc-cfg=no_de_rc_dst");
  40. }
  41. // Duration available in core since Rust 1.25:
  42. // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
  43. if minor < 25 {
  44. println!("cargo:rustc-cfg=no_core_duration");
  45. }
  46. // 128-bit integers stabilized in Rust 1.26:
  47. // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
  48. //
  49. // Disabled on Emscripten targets before Rust 1.40 since
  50. // Emscripten did not support 128-bit integers until Rust 1.40
  51. // (https://github.com/rust-lang/rust/pull/65251)
  52. if minor < 26 || emscripten && minor < 40 {
  53. println!("cargo:rustc-cfg=no_integer128");
  54. }
  55. // Inclusive ranges methods stabilized in Rust 1.27:
  56. // https://github.com/rust-lang/rust/pull/50758
  57. // Also Iterator::try_for_each:
  58. // https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
  59. if minor < 27 {
  60. println!("cargo:rustc-cfg=no_range_inclusive");
  61. println!("cargo:rustc-cfg=no_iterator_try_fold");
  62. }
  63. // Non-zero integers stabilized in Rust 1.28:
  64. // https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
  65. if minor < 28 {
  66. println!("cargo:rustc-cfg=no_num_nonzero");
  67. }
  68. // Current minimum supported version of serde_derive crate is Rust 1.31.
  69. if minor < 31 {
  70. println!("cargo:rustc-cfg=no_serde_derive");
  71. }
  72. // TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
  73. // stabilized in Rust 1.34:
  74. // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
  75. // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
  76. if minor < 34 {
  77. println!("cargo:rustc-cfg=no_core_try_from");
  78. println!("cargo:rustc-cfg=no_num_nonzero_signed");
  79. println!("cargo:rustc-cfg=no_systemtime_checked_add");
  80. }
  81. // Whitelist of archs that support std::sync::atomic module. Ideally we
  82. // would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
  83. // Instead this is based on rustc's compiler/rustc_target/src/spec/*.rs.
  84. let has_atomic64 = target.starts_with("x86_64")
  85. || target.starts_with("i686")
  86. || target.starts_with("aarch64")
  87. || target.starts_with("powerpc64")
  88. || target.starts_with("sparc64")
  89. || target.starts_with("mips64el")
  90. || target.starts_with("riscv64");
  91. let has_atomic32 = has_atomic64 || emscripten;
  92. if minor < 34 || !has_atomic64 {
  93. println!("cargo:rustc-cfg=no_std_atomic64");
  94. }
  95. if minor < 34 || !has_atomic32 {
  96. println!("cargo:rustc-cfg=no_std_atomic");
  97. }
  98. }
  99. fn rustc_minor_version() -> Option<u32> {
  100. let rustc = match env::var_os("RUSTC") {
  101. Some(rustc) => rustc,
  102. None => return None,
  103. };
  104. let output = match Command::new(rustc).arg("--version").output() {
  105. Ok(output) => output,
  106. Err(_) => return None,
  107. };
  108. let version = match str::from_utf8(&output.stdout) {
  109. Ok(version) => version,
  110. Err(_) => return None,
  111. };
  112. let mut pieces = version.split('.');
  113. if pieces.next() != Some("rustc 1") {
  114. return None;
  115. }
  116. let next = match pieces.next() {
  117. Some(next) => next,
  118. None => return None,
  119. };
  120. u32::from_str(next).ok()
  121. }