xref: /drstd/dlibc/build.rs (revision 9670759b785600bf6315e4173e46a602f16add7a)
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"]),
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 libstd is backward compatible with FreeBSD 10.
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 || rustc_dep_of_std => set_cfg("freebsd10"),
69         Some(11) if libc_ci => set_cfg("freebsd11"),
70         Some(12) if libc_ci => 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             println!("cargo:rustc-check-cfg=values({})", cfg);
171         }
172         for &(name, values) in CHECK_CFG_EXTRA {
173             let values = values.join("\",\"");
174             println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
175         }
176     }
177 
178     //TODO:增加平台内存分配器
179     //#[cfg(target_os = "dragonos")]
180     let mut c = cc::Build::new();
181     //#[cfg(target_os = "dragonos")]
182     c.flag("-nostdinc")
183     .flag("-nostdlib")
184     .flag("-fno-stack-protector")
185     .flag("-Wno-expansion-to-defined")
186     .file("src/unix/platform/dragonos/c/dragonos_malloc.c");
187     //#[cfg(target_os = "dragonos")]
188     c.define("HAVE_MMAP", "0");
189     //#[cfg(target_os = "dragonos")]
190     c.compile("dlibc_c");
191     //#[cfg(target_os = "dragonos")]
192     println!("cargo:rustc-link-lib=static=dlibc_c");
193 
194 
195 }
196 
197 fn rustc_minor_nightly() -> (u32, bool) {
198     macro_rules! otry {
199         ($e:expr) => {
200             match $e {
201                 Some(e) => e,
202                 None => panic!("Failed to get rustc version"),
203             }
204         };
205     }
206 
207     let rustc = otry!(env::var_os("RUSTC"));
208     let output = Command::new(rustc)
209         .arg("--version")
210         .output()
211         .ok()
212         .expect("Failed to get rustc version");
213     if !output.status.success() {
214         panic!(
215             "failed to run rustc: {}",
216             String::from_utf8_lossy(output.stderr.as_slice())
217         );
218     }
219 
220     let version = otry!(str::from_utf8(&output.stdout).ok());
221     let mut pieces = version.split('.');
222 
223     if pieces.next() != Some("rustc 1") {
224         panic!("Failed to get rustc version");
225     }
226 
227     let minor = pieces.next();
228 
229     // If `rustc` was built from a tarball, its version string
230     // will have neither a git hash nor a commit date
231     // (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
232     // since a nightly build should either come from CI
233     // or a git checkout
234     let nightly_raw = otry!(pieces.next()).split('-').nth(1);
235     let nightly = nightly_raw
236         .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
237         .unwrap_or(false);
238     let minor = otry!(otry!(minor).parse().ok());
239 
240     (minor, nightly)
241 }
242 
243 fn which_freebsd() -> Option<i32> {
244     let output = std::process::Command::new("freebsd-version").output().ok();
245     if output.is_none() {
246         return None;
247     }
248     let output = output.unwrap();
249     if !output.status.success() {
250         return None;
251     }
252 
253     let stdout = String::from_utf8(output.stdout).ok();
254     if stdout.is_none() {
255         return None;
256     }
257     let stdout = stdout.unwrap();
258 
259     match &stdout {
260         s if s.starts_with("10") => Some(10),
261         s if s.starts_with("11") => Some(11),
262         s if s.starts_with("12") => Some(12),
263         s if s.starts_with("13") => Some(13),
264         s if s.starts_with("14") => Some(14),
265         _ => None,
266     }
267 }
268 
269 fn emcc_version_code() -> Option<u64> {
270     let output = std::process::Command::new("emcc")
271         .arg("-dumpversion")
272         .output()
273         .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 version = stdout.unwrap();
287     let mut pieces = version.trim().split('.');
288 
289     let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
290     let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
291     let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
292 
293     Some(major * 10000 + minor * 100 + patch)
294 }
295 
296 fn set_cfg(cfg: &str) {
297     if !ALLOWED_CFGS.contains(&cfg) {
298         panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
299     }
300     println!("cargo:rustc-cfg={}", cfg);
301 }
302