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_time64", 11 "freebsd10", 12 "freebsd11", 13 "freebsd12", 14 "freebsd13", 15 "freebsd14", 16 "freebsd15", 17 "libc_align", 18 "libc_cfg_target_vendor", 19 "libc_const_extern_fn", 20 "libc_const_extern_fn_unstable", 21 "libc_const_size_of", 22 "libc_core_cvoid", 23 "libc_deny_warnings", 24 "libc_int128", 25 "libc_long_array", 26 "libc_non_exhaustive", 27 "libc_packedN", 28 "libc_priv_mod_use", 29 "libc_ptr_addr_of", 30 "libc_thread_local", 31 "libc_underscore_const_names", 32 "libc_union", 33 "libc_ctest", 34 ]; 35 36 // Extra values to allow for check-cfg. 37 const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[ 38 ("target_os", &["switch", "aix", "ohos", "hurd", "visionos"]), 39 ("target_env", &["illumos", "wasi", "aix", "ohos"]), 40 ( 41 "target_arch", 42 &["loongarch64", "mips32r6", "mips64r6", "csky"], 43 ), 44 ]; 45 46 fn main() { 47 // Avoid unnecessary re-building. 48 println!("cargo:rerun-if-changed=build.rs"); 49 50 let (rustc_minor_ver, is_nightly) = rustc_minor_nightly(); 51 let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok(); 52 let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok(); 53 let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok(); 54 let libc_ci = env::var("LIBC_CI").is_ok(); 55 let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok() || rustc_minor_ver >= 80; 56 57 if env::var("CARGO_FEATURE_USE_STD").is_ok() { 58 println!( 59 "cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \ 60 please consider using the `std` cargo feature instead\"" 61 ); 62 } 63 64 // The ABI of libc used by std is backward compatible with FreeBSD 12. 65 // The ABI of libc from crates.io is backward compatible with FreeBSD 11. 66 // 67 // On CI, we detect the actual FreeBSD version and match its ABI exactly, 68 // running tests to ensure that the ABI is correct. 69 let which_freebsd = if libc_ci { 70 which_freebsd().unwrap_or(11) 71 } else if rustc_dep_of_std { 72 12 73 } else { 74 11 75 }; 76 match which_freebsd { 77 x if x < 10 => panic!("FreeBSD older than 10 is not supported"), 78 10 => set_cfg("freebsd10"), 79 11 => set_cfg("freebsd11"), 80 12 => set_cfg("freebsd12"), 81 13 => set_cfg("freebsd13"), 82 14 => set_cfg("freebsd14"), 83 _ => set_cfg("freebsd15"), 84 } 85 86 match emcc_version_code() { 87 Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"), 88 // Non-Emscripten or version < 3.1.42. 89 Some(_) | None => (), 90 } 91 92 // On CI: deny all warnings 93 if libc_ci { 94 set_cfg("libc_deny_warnings"); 95 } 96 97 // Rust >= 1.15 supports private module use: 98 if rustc_minor_ver >= 15 || rustc_dep_of_std { 99 set_cfg("libc_priv_mod_use"); 100 } 101 102 // Rust >= 1.19 supports unions: 103 if rustc_minor_ver >= 19 || rustc_dep_of_std { 104 set_cfg("libc_union"); 105 } 106 107 // Rust >= 1.24 supports const mem::size_of: 108 if rustc_minor_ver >= 24 || rustc_dep_of_std { 109 set_cfg("libc_const_size_of"); 110 } 111 112 // Rust >= 1.25 supports repr(align): 113 if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature { 114 set_cfg("libc_align"); 115 } 116 117 // Rust >= 1.26 supports i128 and u128: 118 if rustc_minor_ver >= 26 || rustc_dep_of_std { 119 set_cfg("libc_int128"); 120 } 121 122 // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it. 123 // Otherwise, it defines an incompatible type to retaining 124 // backwards-compatibility. 125 if rustc_minor_ver >= 30 || rustc_dep_of_std { 126 set_cfg("libc_core_cvoid"); 127 } 128 129 // Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor). 130 if rustc_minor_ver >= 33 || rustc_dep_of_std { 131 set_cfg("libc_packedN"); 132 set_cfg("libc_cfg_target_vendor"); 133 } 134 135 // Rust >= 1.40 supports #[non_exhaustive]. 136 if rustc_minor_ver >= 40 || rustc_dep_of_std { 137 set_cfg("libc_non_exhaustive"); 138 } 139 140 // Rust >= 1.47 supports long array: 141 if rustc_minor_ver >= 47 || rustc_dep_of_std { 142 set_cfg("libc_long_array"); 143 } 144 145 if rustc_minor_ver >= 51 || rustc_dep_of_std { 146 set_cfg("libc_ptr_addr_of"); 147 } 148 149 // Rust >= 1.37.0 allows underscores as anonymous constant names. 150 if rustc_minor_ver >= 37 || rustc_dep_of_std { 151 set_cfg("libc_underscore_const_names"); 152 } 153 154 // #[thread_local] is currently unstable 155 if rustc_dep_of_std { 156 set_cfg("libc_thread_local"); 157 } 158 159 // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C". 160 if rustc_minor_ver >= 62 { 161 set_cfg("libc_const_extern_fn"); 162 } else { 163 // Rust < 1.62.0 requires a crate feature and feature gate. 164 if const_extern_fn_cargo_feature { 165 if !is_nightly || rustc_minor_ver < 40 { 166 panic!("const-extern-fn requires a nightly compiler >= 1.40"); 167 } 168 set_cfg("libc_const_extern_fn_unstable"); 169 set_cfg("libc_const_extern_fn"); 170 } 171 } 172 173 // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the 174 // codebase. libc can configure it if the appropriate environment variable is passed. Since 175 // rust-lang/rust enforces it, this is useful when using a custom libc fork there. 176 // 177 // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg 178 if libc_check_cfg { 179 for cfg in ALLOWED_CFGS { 180 if rustc_minor_ver >= 75 { 181 println!("cargo:rustc-check-cfg=cfg({})", cfg); 182 } else { 183 println!("cargo:rustc-check-cfg=values({})", cfg); 184 } 185 } 186 for &(name, values) in CHECK_CFG_EXTRA { 187 let values = values.join("\",\""); 188 if rustc_minor_ver >= 75 { 189 println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values); 190 } else { 191 println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values); 192 } 193 } 194 } 195 } 196 197 /// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver` 198 /// is used instead. 199 fn rustc_version_cmd(is_clippy_driver: bool) -> Output { 200 let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env"); 201 202 let mut cmd = match env::var_os("RUSTC_WRAPPER") { 203 Some(ref wrapper) if wrapper.is_empty() => Command::new(rustc), 204 Some(wrapper) => { 205 let mut cmd = Command::new(wrapper); 206 cmd.arg(rustc); 207 if is_clippy_driver { 208 cmd.arg("--rustc"); 209 } 210 211 cmd 212 } 213 None => Command::new(rustc), 214 }; 215 216 cmd.arg("--version"); 217 218 let output = cmd.output().ok().expect("Failed to get rustc version"); 219 220 if !output.status.success() { 221 panic!( 222 "failed to run rustc: {}", 223 String::from_utf8_lossy(output.stderr.as_slice()) 224 ); 225 } 226 227 output 228 } 229 230 /// Return the minor version of `rustc`, as well as a bool indicating whether or not the version 231 /// is a nightly. 232 fn rustc_minor_nightly() -> (u32, bool) { 233 macro_rules! otry { 234 ($e:expr) => { 235 match $e { 236 Some(e) => e, 237 None => panic!("Failed to get rustc version"), 238 } 239 }; 240 } 241 242 let mut output = rustc_version_cmd(false); 243 244 if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") { 245 output = rustc_version_cmd(true); 246 } 247 248 let version = otry!(str::from_utf8(&output.stdout).ok()); 249 250 let mut pieces = version.split('.'); 251 252 if pieces.next() != Some("rustc 1") { 253 panic!("Failed to get rustc version"); 254 } 255 256 let minor = pieces.next(); 257 258 // If `rustc` was built from a tarball, its version string 259 // will have neither a git hash nor a commit date 260 // (e.g. "rustc 1.39.0"). Treat this case as non-nightly, 261 // since a nightly build should either come from CI 262 // or a git checkout 263 let nightly_raw = otry!(pieces.next()).split('-').nth(1); 264 let nightly = nightly_raw 265 .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly")) 266 .unwrap_or(false); 267 let minor = otry!(otry!(minor).parse().ok()); 268 269 (minor, nightly) 270 } 271 272 fn which_freebsd() -> Option<i32> { 273 let output = std::process::Command::new("freebsd-version").output().ok(); 274 if output.is_none() { 275 return None; 276 } 277 let output = output.unwrap(); 278 if !output.status.success() { 279 return None; 280 } 281 282 let stdout = String::from_utf8(output.stdout).ok(); 283 if stdout.is_none() { 284 return None; 285 } 286 let stdout = stdout.unwrap(); 287 288 match &stdout { 289 s if s.starts_with("10") => Some(10), 290 s if s.starts_with("11") => Some(11), 291 s if s.starts_with("12") => Some(12), 292 s if s.starts_with("13") => Some(13), 293 s if s.starts_with("14") => Some(14), 294 s if s.starts_with("15") => Some(15), 295 _ => None, 296 } 297 } 298 299 fn emcc_version_code() -> Option<u64> { 300 let output = std::process::Command::new("emcc") 301 .arg("-dumpversion") 302 .output() 303 .ok(); 304 if output.is_none() { 305 return None; 306 } 307 let output = output.unwrap(); 308 if !output.status.success() { 309 return None; 310 } 311 312 let stdout = String::from_utf8(output.stdout).ok(); 313 if stdout.is_none() { 314 return None; 315 } 316 let version = stdout.unwrap(); 317 318 // Some Emscripten versions come with `-git` attached, so split the 319 // version string also on the `-` char. 320 let mut pieces = version.trim().split(|c| c == '.' || c == '-'); 321 322 let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 323 let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 324 let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); 325 326 Some(major * 10000 + minor * 100 + patch) 327 } 328 329 fn set_cfg(cfg: &str) { 330 if !ALLOWED_CFGS.contains(&cfg) { 331 panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg); 332 } 333 println!("cargo:rustc-cfg={}", cfg); 334 } 335