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