blob: 845294007409dc180c4f3bcc713cea31c7534fc5 [file] [log] [blame]
Isaac Woods79c80c42018-09-17 19:33:52 +01001use std::env;
2use std::process::Command;
3use std::str;
4
5fn main() {
gnzlbga17a91c2019-02-07 11:37:21 +01006 let rustc_minor_ver =
7 rustc_minor_version().expect("Failed to get rustc version");
8 let rustc_dep_of_std =
9 std::env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
10 let align_cargo_feature = std::env::var("CARGO_FEATURE_ALIGN").is_ok();
11
gnzlbg4ac26af2019-05-24 13:22:03 +020012 if std::env::var("CARGO_FEATURE_USE_STD").is_ok() {
13 println!(
14 "cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
15 please consider using the `std` cargo feature instead\""
16 );
17 }
18
gnzlbg7437d0a2019-05-16 14:53:51 +020019 if std::env::var("LIBC_CI").is_ok() {
20 if let Some(12) = which_freebsd() {
21 println!("cargo:rustc-cfg=freebsd12");
22 }
23 }
24
gnzlbga17a91c2019-02-07 11:37:21 +010025 // Rust >= 1.15 supports private module use:
26 if rustc_minor_ver >= 15 || rustc_dep_of_std {
27 println!("cargo:rustc-cfg=libc_priv_mod_use");
28 }
29
30 // Rust >= 1.19 supports unions:
31 if rustc_minor_ver >= 19 || rustc_dep_of_std {
32 println!("cargo:rustc-cfg=libc_union");
33 }
34
35 // Rust >= 1.24 supports const mem::size_of:
36 if rustc_minor_ver >= 24 || rustc_dep_of_std {
37 println!("cargo:rustc-cfg=libc_const_size_of");
38 }
39
40 // Rust >= 1.25 supports repr(align):
41 if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
42 println!("cargo:rustc-cfg=libc_align");
43 }
44
45 // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
46 // Otherwise, it defines an incompatible type to retaining
47 // backwards-compatibility.
48 if rustc_minor_ver >= 30 || rustc_dep_of_std {
49 println!("cargo:rustc-cfg=libc_core_cvoid");
50 }
51
52 // Rust >= 1.33 supports repr(packed(N))
53 if rustc_minor_ver >= 33 || rustc_dep_of_std {
54 println!("cargo:rustc-cfg=libc_packedN");
Isaac Woods79c80c42018-09-17 19:33:52 +010055 }
56}
57
58fn rustc_minor_version() -> Option<u32> {
59 macro_rules! otry {
60 ($e:expr) => {
61 match $e {
62 Some(e) => e,
63 None => return None,
64 }
65 };
66 }
67
68 let rustc = otry!(env::var_os("RUSTC"));
69 let output = otry!(Command::new(rustc).arg("--version").output().ok());
70 let version = otry!(str::from_utf8(&output.stdout).ok());
71 let mut pieces = version.split('.');
72
73 if pieces.next() != Some("rustc 1") {
74 return None;
75 }
76
77 otry!(pieces.next()).parse().ok()
78}
gnzlbg7437d0a2019-05-16 14:53:51 +020079
80fn which_freebsd() -> Option<i32> {
81 let output = std::process::Command::new("freebsd-version").output().ok();
82 if output.is_none() {
83 return None;
84 }
85 let output = output.unwrap();
86 if !output.status.success() {
87 return None;
88 }
89
90 let stdout = String::from_utf8(output.stdout).ok();
91 if stdout.is_none() {
92 return None;
93 }
94 let stdout = stdout.unwrap();
95
96 match &stdout {
97 s if s.starts_with("11") => Some(11),
98 s if s.starts_with("12") => Some(12),
99 _ => None,
100 }
101}