1 use std::process::{Command, Output}; 2 use std::{env, str}; 3 4 // List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we 5 // need to know all the possible cfgs that this script will set. If you need to set another cfg 6 // make sure to add it to this list as well. 7 const ALLOWED_CFGS: &'static [&'static str] = &[ 8 "emscripten_new_stat_abi", 9 "espidf_time32", 10 "freebsd10", 11 "freebsd11", 12 "freebsd12", 13 "freebsd13", 14 "freebsd14", 15 "freebsd15", 16 "libc_deny_warnings", 17 "libc_thread_local", 18 "libc_ctest", 19 ]; 20 21 // Extra values to allow for check-cfg. 22 const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[ 23 ( 24 "target_os", 25 &[ 26 "switch", "aix", "ohos", "hurd", "rtems", "visionos", "nuttx", 27 ], 28 ), 29 ("target_env", &["illumos", "wasi", "aix", "ohos"]), 30 ( 31 "target_arch", 32 &["loongarch64", "mips32r6", "mips64r6", "csky"], 33 ), 34 ]; 35 36 fn main() { 37 // Avoid unnecessary re-building. 38 println!("cargo:rerun-if-changed=build.rs"); 39 40 let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly(); 41 let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok(); 42 let libc_ci = env::var("LIBC_CI").is_ok(); 43 let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok() || rustc_minor_ver >= 80; 44 45 // The ABI of libc used by std is backward compatible with FreeBSD 12. 46 // The ABI of libc from crates.io is backward compatible with FreeBSD 11. 47 // 48 // On CI, we detect the actual FreeBSD version and match its ABI exactly, 49 // running tests to ensure that the ABI is correct. 50 let which_freebsd = if libc_ci { 51 which_freebsd().unwrap_or(11) 52 } else if rustc_dep_of_std { 53 12 54 } else { 55 11 56 }; 57 match which_freebsd { 58 x if x < 10 => panic!("FreeBSD older than 10 is not supported"), 59 10 => set_cfg("freebsd10"), 60 11 => set_cfg("freebsd11"), 61 12 => set_cfg("freebsd12"), 62 13 => set_cfg("freebsd13"), 63 14 => set_cfg("freebsd14"), 64 _ => set_cfg("freebsd15"), 65 } 66 67 match emcc_version_code() { 68 Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"), 69 // Non-Emscripten or version < 3.1.42. 70 Some(_) | None => (), 71 } 72 73 // On CI: deny all warnings 74 if libc_ci { 75 set_cfg("libc_deny_warnings"); 76 } 77 78 // #[thread_local] is currently unstable 79 if rustc_dep_of_std { 80 set_cfg("libc_thread_local"); 81 } 82 83 // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the 84 // codebase. libc can configure it if the appropriate environment variable is passed. Since 85 // rust-lang/rust enforces it, this is useful when using a custom libc fork there. 86 // 87 // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg 88 if libc_check_cfg { 89 for cfg in ALLOWED_CFGS { 90 if rustc_minor_ver >= 75 { 91 println!("cargo:rustc-check-cfg=cfg({})", cfg); 92 } else { 93 println!("cargo:rustc-check-cfg=values({})", cfg); 94 } 95 } 96 for &(name, values) in CHECK_CFG_EXTRA { 97 let values = values.join("\",\""); 98 if rustc_minor_ver >= 75 { 99 println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values); 100 } else { 101 println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values); 102 } 103 } 104 } 105 } 106 107 /// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver` 108 /// is used instead. 109 fn rustc_version_cmd(is_clippy_driver: bool) -> Output { 110 let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env"); 111 112 let mut cmd = match env::var_os("RUSTC_WRAPPER") { 113 Some(ref wrapper) if wrapper.is_empty() => Command::new(rustc), 114 Some(wrapper) => { 115 let mut cmd = Command::new(wrapper); 116 cmd.arg(rustc); 117 if is_clippy_driver { 118 cmd.arg("--rustc"); 119 } 120 121 cmd 122 } 123 None => Command::new(rustc), 124 }; 125 126 cmd.arg("--version"); 127 128 let output = cmd.output().expect("Failed to get rustc version"); 129 130 if !output.status.success() { 131 panic!( 132 "failed to run rustc: {}", 133 String::from_utf8_lossy(output.stderr.as_slice()) 134 ); 135 } 136 137 output 138 } 139 140 /// Return the minor version of `rustc`, as well as a bool indicating whether or not the version 141 /// is a nightly. 142 fn rustc_minor_nightly() -> (u32, bool) { 143 macro_rules! otry { 144 ($e:expr) => { 145 match $e { 146 Some(e) => e, 147 None => panic!("Failed to get rustc version"), 148 } 149 }; 150 } 151 152 let mut output = rustc_version_cmd(false); 153 154 if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") { 155 output = rustc_version_cmd(true); 156 } 157 158 let version = otry!(str::from_utf8(&output.stdout).ok()); 159 160 let mut pieces = version.split('.'); 161 162 if pieces.next() != Some("rustc 1") { 163 panic!("Failed to get rustc version"); 164 } 165 166 let minor = pieces.next(); 167 168 // If `rustc` was built from a tarball, its version string 169 // will have neither a git hash nor a commit date 170 // (e.g. "rustc 1.39.0"). Treat this case as non-nightly, 171 // since a nightly build should either come from CI 172 // or a git checkout 173 let nightly_raw = otry!(pieces.next()).split('-').nth(1); 174 let nightly = nightly_raw 175 .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly")) 176 .unwrap_or(false); 177 let minor = otry!(otry!(minor).parse().ok()); 178 179 (minor, nightly) 180 } 181 182 fn which_freebsd() -> Option<i32> { 183 let output = std::process::Command::new("freebsd-version") 184 .output() 185 .ok()?; 186 if !output.status.success() { 187 return None; 188 } 189 190 let stdout = String::from_utf8(output.stdout).ok()?; 191 192 match &stdout { 193 s if s.starts_with("10") => Some(10), 194 s if s.starts_with("11") => Some(11), 195 s if s.starts_with("12") => Some(12), 196 s if s.starts_with("13") => Some(13), 197 s if s.starts_with("14") => Some(14), 198 s if s.starts_with("15") => Some(15), 199 _ => None, 200 } 201 } 202 203 fn emcc_version_code() -> Option<u64> { 204 let output = std::process::Command::new("emcc") 205 .arg("-dumpversion") 206 .output() 207 .ok()?; 208 if !output.status.success() { 209 return None; 210 } 211 212 let version = String::from_utf8(output.stdout).ok()?; 213 214 // Some Emscripten versions come with `-git` attached, so split the 215 // version string also on the `-` char. 216 let mut pieces = version.trim().split(['.', '-']); 217 218 let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 219 let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 220 let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 221 222 Some(major * 10000 + minor * 100 + patch) 223 } 224 225 fn set_cfg(cfg: &str) { 226 if !ALLOWED_CFGS.contains(&cfg) { 227 panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg); 228 } 229 println!("cargo:rustc-cfg={}", cfg); 230 } 231