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