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