| 1 |
#![allow(unknown_lints)] |
| 2 |
#![allow(unexpected_cfgs)] |
| 3 |
|
| 4 |
use std::env; |
| 5 |
use std::ffi::OsString; |
| 6 |
use std::iter; |
| 7 |
use std::path::Path; |
| 8 |
use std::process::{self, Command, Stdio}; |
| 9 |
use std::str; |
| 10 |
|
| 11 |
fn main() { |
| 12 |
let rustc = rustc_minor_version().unwrap_or(u32::MAX); |
| 13 |
|
| 14 |
if rustc >= 80 { |
| 15 |
println!("cargo:rustc-check-cfg=cfg(fuzzing)"); |
| 16 |
println!("cargo:rustc-check-cfg=cfg(no_is_available)"); |
| 17 |
println!("cargo:rustc-check-cfg=cfg(no_literal_byte_character)"); |
| 18 |
println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)"); |
| 19 |
println!("cargo:rustc-check-cfg=cfg(no_source_text)"); |
| 20 |
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)"); |
| 21 |
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)"); |
| 22 |
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)"); |
| 23 |
println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)"); |
| 24 |
println!("cargo:rustc-check-cfg=cfg(randomize_layout)"); |
| 25 |
println!("cargo:rustc-check-cfg=cfg(span_locations)"); |
| 26 |
println!("cargo:rustc-check-cfg=cfg(super_unstable)"); |
| 27 |
println!("cargo:rustc-check-cfg=cfg(wrap_proc_macro)"); |
| 28 |
} |
| 29 |
|
| 30 |
let docs_rs = env::var_os("DOCS_RS").is_some(); |
| 31 |
let semver_exempt = cfg!(procmacro2_semver_exempt) || docs_rs; |
| 32 |
if semver_exempt { |
| 33 |
// https://github.com/dtolnay/proc-macro2/issues/147 |
| 34 |
println!("cargo:rustc-cfg=procmacro2_semver_exempt"); |
| 35 |
} |
| 36 |
|
| 37 |
if semver_exempt || cfg!(feature = "span-locations") { |
| 38 |
// Provide methods Span::start and Span::end which give the line/column |
| 39 |
// location of a token. This is behind a cfg because tracking location |
| 40 |
// inside spans is a performance hit. |
| 41 |
println!("cargo:rustc-cfg=span_locations"); |
| 42 |
} |
| 43 |
|
| 44 |
if rustc < 57 { |
| 45 |
// Do not use proc_macro::is_available() to detect whether the proc |
| 46 |
// macro API is available vs needs to be polyfilled. Instead, use the |
| 47 |
// proc macro API unconditionally and catch the panic that occurs if it |
| 48 |
// isn't available. |
| 49 |
println!("cargo:rustc-cfg=no_is_available"); |
| 50 |
} |
| 51 |
|
| 52 |
if rustc < 66 { |
| 53 |
// Do not call libproc_macro's Span::source_text. Always return None. |
| 54 |
println!("cargo:rustc-cfg=no_source_text"); |
| 55 |
} |
| 56 |
|
| 57 |
if rustc < 79 { |
| 58 |
// Do not call Literal::byte_character nor Literal::c_string. They can |
| 59 |
// be emulated by way of Literal::from_str. |
| 60 |
println!("cargo:rustc-cfg=no_literal_byte_character"); |
| 61 |
println!("cargo:rustc-cfg=no_literal_c_string"); |
| 62 |
} |
| 63 |
|
| 64 |
if !cfg!(feature = "proc-macro") { |
| 65 |
println!("cargo:rerun-if-changed=build.rs"); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
println!("cargo:rerun-if-changed=build/probe.rs"); |
| 70 |
|
| 71 |
let proc_macro_span; |
| 72 |
let consider_rustc_bootstrap; |
| 73 |
if compile_probe(false) { |
| 74 |
// This is a nightly or dev compiler, so it supports unstable features |
| 75 |
// regardless of RUSTC_BOOTSTRAP. No need to rerun build script if |
| 76 |
// RUSTC_BOOTSTRAP is changed. |
| 77 |
proc_macro_span = true; |
| 78 |
consider_rustc_bootstrap = false; |
| 79 |
} else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") { |
| 80 |
if compile_probe(true) { |
| 81 |
// This is a stable or beta compiler for which the user has set |
| 82 |
// RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script |
| 83 |
// if they change it. |
| 84 |
proc_macro_span = true; |
| 85 |
consider_rustc_bootstrap = true; |
| 86 |
} else if rustc_bootstrap == "1" { |
| 87 |
// This compiler does not support the proc macro Span API in the |
| 88 |
// form that proc-macro2 expects. No need to pay attention to |
| 89 |
// RUSTC_BOOTSTRAP. |
| 90 |
proc_macro_span = false; |
| 91 |
consider_rustc_bootstrap = false; |
| 92 |
} else { |
| 93 |
// This is a stable or beta compiler for which RUSTC_BOOTSTRAP is |
| 94 |
// set to restrict the use of unstable features by this crate. |
| 95 |
proc_macro_span = false; |
| 96 |
consider_rustc_bootstrap = true; |
| 97 |
} |
| 98 |
} else { |
| 99 |
// Without RUSTC_BOOTSTRAP, this compiler does not support the proc |
| 100 |
// macro Span API in the form that proc-macro2 expects, but try again if |
| 101 |
// the user turns on unstable features. |
| 102 |
proc_macro_span = false; |
| 103 |
consider_rustc_bootstrap = true; |
| 104 |
} |
| 105 |
|
| 106 |
if proc_macro_span || !semver_exempt { |
| 107 |
// Wrap types from libproc_macro rather than polyfilling the whole API. |
| 108 |
// Enabled as long as procmacro2_semver_exempt is not set, because we |
| 109 |
// can't emulate the unstable API without emulating everything else. |
| 110 |
// Also enabled unconditionally on nightly, in which case the |
| 111 |
// procmacro2_semver_exempt surface area is implemented by using the |
| 112 |
// nightly-only proc_macro API. |
| 113 |
println!("cargo:rustc-cfg=wrap_proc_macro"); |
| 114 |
} |
| 115 |
|
| 116 |
if proc_macro_span { |
| 117 |
// Enable non-dummy behavior of Span::start and Span::end methods which |
| 118 |
// requires an unstable compiler feature. Enabled when building with |
| 119 |
// nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable |
| 120 |
// features. |
| 121 |
println!("cargo:rustc-cfg=proc_macro_span"); |
| 122 |
} |
| 123 |
|
| 124 |
if semver_exempt && proc_macro_span { |
| 125 |
// Implement the semver exempt API in terms of the nightly-only |
| 126 |
// proc_macro API. |
| 127 |
println!("cargo:rustc-cfg=super_unstable"); |
| 128 |
} |
| 129 |
|
| 130 |
if consider_rustc_bootstrap { |
| 131 |
println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP"); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
fn compile_probe(rustc_bootstrap: bool) -> bool { |
| 136 |
if env::var_os("RUSTC_STAGE").is_some() { |
| 137 |
// We are running inside rustc bootstrap. This is a highly non-standard |
| 138 |
// environment with issues such as: |
| 139 |
// |
| 140 |
// https://github.com/rust-lang/cargo/issues/11138 |
| 141 |
// https://github.com/rust-lang/rust/issues/114839 |
| 142 |
// |
| 143 |
// Let's just not use nightly features here. |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
let rustc = cargo_env_var("RUSTC"); |
| 148 |
let out_dir = cargo_env_var("OUT_DIR"); |
| 149 |
let probefile = Path::new("build").join("probe.rs"); |
| 150 |
|
| 151 |
let rustc_wrapper = env::var_os("RUSTC_WRAPPER").filter(|wrapper| !wrapper.is_empty()); |
| 152 |
let rustc_workspace_wrapper = |
| 153 |
env::var_os("RUSTC_WORKSPACE_WRAPPER").filter(|wrapper| !wrapper.is_empty()); |
| 154 |
let mut rustc = rustc_wrapper |
| 155 |
.into_iter() |
| 156 |
.chain(rustc_workspace_wrapper) |
| 157 |
.chain(iter::once(rustc)); |
| 158 |
let mut cmd = Command::new(rustc.next().unwrap()); |
| 159 |
cmd.args(rustc); |
| 160 |
|
| 161 |
if !rustc_bootstrap { |
| 162 |
cmd.env_remove("RUSTC_BOOTSTRAP"); |
| 163 |
} |
| 164 |
|
| 165 |
cmd.stderr(Stdio::null()) |
| 166 |
.arg("--edition=2021") |
| 167 |
.arg("--crate-name=proc_macro2") |
| 168 |
.arg("--crate-type=lib") |
| 169 |
.arg("--cap-lints=allow") |
| 170 |
.arg("--emit=dep-info,metadata") |
| 171 |
.arg("--out-dir") |
| 172 |
.arg(out_dir) |
| 173 |
.arg(probefile); |
| 174 |
|
| 175 |
if let Some(target) = env::var_os("TARGET") { |
| 176 |
cmd.arg("--target").arg(target); |
| 177 |
} |
| 178 |
|
| 179 |
// If Cargo wants to set RUSTFLAGS, use that. |
| 180 |
if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") { |
| 181 |
if !rustflags.is_empty() { |
| 182 |
for arg in rustflags.split('\x1f') { |
| 183 |
cmd.arg(arg); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
match cmd.status() { |
| 189 |
Ok(status) => status.success(), |
| 190 |
Err(_) => false, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
fn rustc_minor_version() -> Option<u32> { |
| 195 |
let rustc = cargo_env_var("RUSTC"); |
| 196 |
let output = Command::new(rustc).arg("--version").output().ok()?; |
| 197 |
let version = str::from_utf8(&output.stdout).ok()?; |
| 198 |
let mut pieces = version.split('.'); |
| 199 |
if pieces.next() != Some("rustc 1") { |
| 200 |
return None; |
| 201 |
} |
| 202 |
pieces.next()?.parse().ok() |
| 203 |
} |
| 204 |
|
| 205 |
fn cargo_env_var(key: &str) -> OsString { |
| 206 |
env::var_os(key).unwrap_or_else(|| { |
| 207 |
eprintln!( |
| 208 |
"Environment variable ${} is not set during execution of build script", |
| 209 |
key, |
| 210 |
); |
| 211 |
process::exit(1); |
| 212 |
}) |
| 213 |
} |