1 use std::env;
2 use std::ffi::OsString;
3 use std::path::Path;
4 use std::process::{self, Command, Stdio};
5 use std::str;
6
7 #[cfg(all(feature = "backtrace", not(feature = "std")))]
8 compile_error! {
9 "`backtrace` feature without `std` feature is not supported"
10 }
11
main()12 fn main() {
13 let mut error_generic_member_access = false;
14 if cfg!(feature = "std") {
15 println!("cargo:rerun-if-changed=build/probe.rs");
16
17 let consider_rustc_bootstrap;
18 if compile_probe(false) {
19 // This is a nightly or dev compiler, so it supports unstable
20 // features regardless of RUSTC_BOOTSTRAP. No need to rerun build
21 // script if RUSTC_BOOTSTRAP is changed.
22 error_generic_member_access = true;
23 consider_rustc_bootstrap = false;
24 } else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
25 if compile_probe(true) {
26 // This is a stable or beta compiler for which the user has set
27 // RUSTC_BOOTSTRAP to turn on unstable features. Rerun build
28 // script if they change it.
29 error_generic_member_access = true;
30 consider_rustc_bootstrap = true;
31 } else if rustc_bootstrap == "1" {
32 // This compiler does not support the generic member access API
33 // in the form that anyhow expects. No need to pay attention to
34 // RUSTC_BOOTSTRAP.
35 error_generic_member_access = false;
36 consider_rustc_bootstrap = false;
37 } else {
38 // This is a stable or beta compiler for which RUSTC_BOOTSTRAP
39 // is set to restrict the use of unstable features by this
40 // crate.
41 error_generic_member_access = false;
42 consider_rustc_bootstrap = true;
43 }
44 } else {
45 // Without RUSTC_BOOTSTRAP, this compiler does not support the
46 // generic member access API in the form that anyhow expects, but
47 // try again if the user turns on unstable features.
48 error_generic_member_access = false;
49 consider_rustc_bootstrap = true;
50 }
51
52 if error_generic_member_access {
53 println!("cargo:rustc-cfg=std_backtrace");
54 println!("cargo:rustc-cfg=error_generic_member_access");
55 }
56
57 if consider_rustc_bootstrap {
58 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");
59 }
60 }
61
62 let rustc = match rustc_minor_version() {
63 Some(rustc) => rustc,
64 None => return,
65 };
66
67 if rustc < 51 {
68 // core::ptr::addr_of
69 // https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
70 println!("cargo:rustc-cfg=anyhow_no_ptr_addr_of");
71 }
72
73 if rustc < 52 {
74 // core::fmt::Arguments::as_str
75 // https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html#stabilized-apis
76 println!("cargo:rustc-cfg=anyhow_no_fmt_arguments_as_str");
77
78 // #![deny(unsafe_op_in_unsafe_fn)]
79 // https://github.com/rust-lang/rust/issues/71668
80 println!("cargo:rustc-cfg=anyhow_no_unsafe_op_in_unsafe_fn_lint");
81 }
82
83 if !error_generic_member_access && cfg!(feature = "std") && rustc >= 65 {
84 // std::backtrace::Backtrace
85 // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis
86 println!("cargo:rustc-cfg=std_backtrace");
87 }
88 }
89
compile_probe(rustc_bootstrap: bool) -> bool90 fn compile_probe(rustc_bootstrap: bool) -> bool {
91 if env::var_os("RUSTC_STAGE").is_some() {
92 // We are running inside rustc bootstrap. This is a highly non-standard
93 // environment with issues such as:
94 //
95 // https://github.com/rust-lang/cargo/issues/11138
96 // https://github.com/rust-lang/rust/issues/114839
97 //
98 // Let's just not use nightly features here.
99 return false;
100 }
101
102 let rustc = cargo_env_var("RUSTC");
103 let out_dir = cargo_env_var("OUT_DIR");
104 let probefile = Path::new("build").join("probe.rs");
105
106 // Make sure to pick up Cargo rustc configuration.
107 let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
108 let mut cmd = Command::new(wrapper);
109 // The wrapper's first argument is supposed to be the path to rustc.
110 cmd.arg(rustc);
111 cmd
112 } else {
113 Command::new(rustc)
114 };
115
116 if !rustc_bootstrap {
117 cmd.env_remove("RUSTC_BOOTSTRAP");
118 }
119
120 cmd.stderr(Stdio::null())
121 .arg("--edition=2018")
122 .arg("--crate-name=anyhow")
123 .arg("--crate-type=lib")
124 .arg("--emit=dep-info,metadata")
125 .arg("--out-dir")
126 .arg(out_dir)
127 .arg(probefile);
128
129 if let Some(target) = env::var_os("TARGET") {
130 cmd.arg("--target").arg(target);
131 }
132
133 // If Cargo wants to set RUSTFLAGS, use that.
134 if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
135 if !rustflags.is_empty() {
136 for arg in rustflags.split('\x1f') {
137 cmd.arg(arg);
138 }
139 }
140 }
141
142 match cmd.status() {
143 Ok(status) => status.success(),
144 Err(_) => false,
145 }
146 }
147
rustc_minor_version() -> Option<u32>148 fn rustc_minor_version() -> Option<u32> {
149 let rustc = cargo_env_var("RUSTC");
150 let output = Command::new(rustc).arg("--version").output().ok()?;
151 let version = str::from_utf8(&output.stdout).ok()?;
152 let mut pieces = version.split('.');
153 if pieces.next() != Some("rustc 1") {
154 return None;
155 }
156 pieces.next()?.parse().ok()
157 }
158
cargo_env_var(key: &str) -> OsString159 fn cargo_env_var(key: &str) -> OsString {
160 env::var_os(key).unwrap_or_else(|| {
161 eprintln!(
162 "Environment variable ${} is not set during execution of build script",
163 key,
164 );
165 process::exit(1);
166 })
167 }
168