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