build.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // rustc-cfg emitted by the build script:
  2. //
  3. // "use_proc_macro"
  4. // Link to extern crate proc_macro. Available on any compiler and any target
  5. // except wasm32. Requires "proc-macro" Cargo cfg to be enabled (default is
  6. // enabled). On wasm32 we never link to proc_macro even if "proc-macro" cfg
  7. // is enabled.
  8. //
  9. // "wrap_proc_macro"
  10. // Wrap types from libproc_macro rather than polyfilling the whole API.
  11. // Enabled on rustc 1.29+ as long as procmacro2_semver_exempt is not set,
  12. // because we can't emulate the unstable API without emulating everything
  13. // else. Also enabled unconditionally on nightly, in which case the
  14. // procmacro2_semver_exempt surface area is implemented by using the
  15. // nightly-only proc_macro API.
  16. //
  17. // "hygiene"
  18. // Enable Span::mixed_site() and non-dummy behavior of Span::resolved_at
  19. // and Span::located_at. Enabled on Rust 1.45+.
  20. //
  21. // "proc_macro_span"
  22. // Enable non-dummy behavior of Span::start and Span::end methods which
  23. // requires an unstable compiler feature. Enabled when building with
  24. // nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable
  25. // features.
  26. //
  27. // "super_unstable"
  28. // Implement the semver exempt API in terms of the nightly-only proc_macro
  29. // API. Enabled when using procmacro2_semver_exempt on a nightly compiler.
  30. //
  31. // "span_locations"
  32. // Provide methods Span::start and Span::end which give the line/column
  33. // location of a token. Enabled by procmacro2_semver_exempt or the
  34. // "span-locations" Cargo cfg. This is behind a cfg because tracking
  35. // location inside spans is a performance hit.
  36. //
  37. // "is_available"
  38. // Use proc_macro::is_available() to detect if the proc macro API is
  39. // available or needs to be polyfilled instead of trying to use the proc
  40. // macro API and catching a panic if it isn't available. Enabled on Rust
  41. // 1.57+.
  42. use std::env;
  43. use std::process::{self, Command};
  44. use std::str;
  45. fn main() {
  46. println!("cargo:rerun-if-changed=build.rs");
  47. let version = match rustc_version() {
  48. Some(version) => version,
  49. None => return,
  50. };
  51. if version.minor < 31 {
  52. eprintln!("Minimum supported rustc version is 1.31");
  53. process::exit(1);
  54. }
  55. let docs_rs = env::var_os("DOCS_RS").is_some();
  56. let semver_exempt = cfg!(procmacro2_semver_exempt) || docs_rs;
  57. if semver_exempt {
  58. // https://github.com/dtolnay/proc-macro2/issues/147
  59. println!("cargo:rustc-cfg=procmacro2_semver_exempt");
  60. }
  61. if semver_exempt || cfg!(feature = "span-locations") {
  62. println!("cargo:rustc-cfg=span_locations");
  63. }
  64. if version.minor < 32 {
  65. println!("cargo:rustc-cfg=no_libprocmacro_unwind_safe");
  66. }
  67. if version.minor < 39 {
  68. println!("cargo:rustc-cfg=no_bind_by_move_pattern_guard");
  69. }
  70. if version.minor < 44 {
  71. println!("cargo:rustc-cfg=no_lexerror_display");
  72. }
  73. if version.minor < 45 {
  74. println!("cargo:rustc-cfg=no_hygiene");
  75. }
  76. if version.minor < 54 {
  77. println!("cargo:rustc-cfg=no_literal_from_str");
  78. }
  79. if version.minor < 55 {
  80. println!("cargo:rustc-cfg=no_group_open_close");
  81. }
  82. if version.minor < 57 {
  83. println!("cargo:rustc-cfg=no_is_available");
  84. }
  85. let target = env::var("TARGET").unwrap();
  86. if !enable_use_proc_macro(&target) {
  87. return;
  88. }
  89. println!("cargo:rustc-cfg=use_proc_macro");
  90. if version.nightly || !semver_exempt {
  91. println!("cargo:rustc-cfg=wrap_proc_macro");
  92. }
  93. if version.nightly && feature_allowed("proc_macro_span") {
  94. println!("cargo:rustc-cfg=proc_macro_span");
  95. }
  96. if semver_exempt && version.nightly {
  97. println!("cargo:rustc-cfg=super_unstable");
  98. }
  99. }
  100. fn enable_use_proc_macro(target: &str) -> bool {
  101. // wasm targets don't have the `proc_macro` crate, disable this feature.
  102. if target.contains("wasm32") {
  103. return false;
  104. }
  105. // Otherwise, only enable it if our feature is actually enabled.
  106. cfg!(feature = "proc-macro")
  107. }
  108. struct RustcVersion {
  109. minor: u32,
  110. nightly: bool,
  111. }
  112. fn rustc_version() -> Option<RustcVersion> {
  113. let rustc = env::var_os("RUSTC")?;
  114. let output = Command::new(rustc).arg("--version").output().ok()?;
  115. let version = str::from_utf8(&output.stdout).ok()?;
  116. let nightly = version.contains("nightly") || version.contains("dev");
  117. let mut pieces = version.split('.');
  118. if pieces.next() != Some("rustc 1") {
  119. return None;
  120. }
  121. let minor = pieces.next()?.parse().ok()?;
  122. Some(RustcVersion { minor, nightly })
  123. }
  124. fn feature_allowed(feature: &str) -> bool {
  125. // Recognized formats:
  126. //
  127. // -Z allow-features=feature1,feature2
  128. //
  129. // -Zallow-features=feature1,feature2
  130. let flags_var;
  131. let flags_var_string;
  132. let flags = if let Some(encoded_rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") {
  133. flags_var = encoded_rustflags;
  134. flags_var_string = flags_var.to_string_lossy();
  135. flags_var_string.split('\x1f')
  136. } else {
  137. return true;
  138. };
  139. for mut flag in flags {
  140. if flag.starts_with("-Z") {
  141. flag = &flag["-Z".len()..];
  142. }
  143. if flag.starts_with("allow-features=") {
  144. flag = &flag["allow-features=".len()..];
  145. return flag.split(',').any(|allowed| allowed == feature);
  146. }
  147. }
  148. // No allow-features= flag, allowed by default.
  149. true
  150. }