forked from rust-lang/libc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
183 lines (156 loc) · 6.04 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::env;
use std::process::Command;
use std::str;
fn main() {
// Avoid unnecessary re-building.
println!("cargo:rerun-if-changed=build.rs");
let (rustc_minor_ver, is_nightly) = rustc_minor_nightly();
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok();
let libc_ci = env::var("LIBC_CI").is_ok();
if env::var("CARGO_FEATURE_USE_STD").is_ok() {
println!(
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
please consider using the `std` cargo feature instead\""
);
}
// The ABI of libc used by libstd is backward compatible with FreeBSD 10.
// The ABI of libc from crates.io is backward compatible with FreeBSD 11.
//
// On CI, we detect the actual FreeBSD version and match its ABI exactly,
// running tests to ensure that the ABI is correct.
match which_freebsd() {
Some(10) if libc_ci || rustc_dep_of_std => {
println!("cargo:rustc-cfg=freebsd10")
}
Some(11) if libc_ci => println!("cargo:rustc-cfg=freebsd11"),
Some(12) if libc_ci => println!("cargo:rustc-cfg=freebsd12"),
Some(13) if libc_ci => println!("cargo:rustc-cfg=freebsd13"),
Some(14) if libc_ci => println!("cargo:rustc-cfg=freebsd14"),
Some(_) | None => println!("cargo:rustc-cfg=freebsd11"),
}
// On CI: deny all warnings
if libc_ci {
println!("cargo:rustc-cfg=libc_deny_warnings");
}
// Rust >= 1.15 supports private module use:
if rustc_minor_ver >= 15 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_priv_mod_use");
}
// Rust >= 1.19 supports unions:
if rustc_minor_ver >= 19 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_union");
}
// Rust >= 1.24 supports const mem::size_of:
if rustc_minor_ver >= 24 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_const_size_of");
}
// Rust >= 1.25 supports repr(align):
if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
println!("cargo:rustc-cfg=libc_align");
}
// Rust >= 1.26 supports i128 and u128:
if rustc_minor_ver >= 26 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_int128");
}
// Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
// Otherwise, it defines an incompatible type to retaining
// backwards-compatibility.
if rustc_minor_ver >= 30 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_core_cvoid");
}
// Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
if rustc_minor_ver >= 33 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_packedN");
println!("cargo:rustc-cfg=libc_cfg_target_vendor");
}
// Rust >= 1.40 supports #[non_exhaustive].
if rustc_minor_ver >= 40 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_non_exhaustive");
}
// Rust >= 1.47 supports long array:
if rustc_minor_ver >= 47 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_long_array");
}
if rustc_minor_ver >= 51 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_ptr_addr_of");
}
// Rust >= 1.37.0 allows underscores as anonymous constant names.
if rustc_minor_ver >= 37 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_underscore_const_names");
}
// #[thread_local] is currently unstable
if rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_thread_local");
}
// Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
if rustc_minor_ver >= 62 {
println!("cargo:rustc-cfg=libc_const_extern_fn");
} else {
// Rust < 1.62.0 requires a crate feature and feature gate.
if const_extern_fn_cargo_feature {
if !is_nightly || rustc_minor_ver < 40 {
panic!("const-extern-fn requires a nightly compiler >= 1.40");
}
println!("cargo:rustc-cfg=libc_const_extern_fn_unstable");
println!("cargo:rustc-cfg=libc_const_extern_fn");
}
}
}
fn rustc_minor_nightly() -> (u32, bool) {
macro_rules! otry {
($e:expr) => {
match $e {
Some(e) => e,
None => panic!("Failed to get rustc version"),
}
};
}
let rustc = otry!(env::var_os("RUSTC"));
let output = Command::new(rustc)
.arg("--version")
.output()
.ok()
.expect("Failed to get rustc version");
let version = otry!(str::from_utf8(&output.stdout).ok());
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
panic!("Failed to get rustc version");
}
let minor = pieces.next();
// If `rustc` was built from a tarball, its version string
// will have neither a git hash nor a commit date
// (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
// since a nightly build should either come from CI
// or a git checkout
let nightly_raw = otry!(pieces.next()).split('-').nth(1);
let nightly = nightly_raw
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
.unwrap_or(false);
let minor = otry!(otry!(minor).parse().ok());
(minor, nightly)
}
fn which_freebsd() -> Option<i32> {
let output = std::process::Command::new("freebsd-version").output().ok();
if output.is_none() {
return None;
}
let output = output.unwrap();
if !output.status.success() {
return None;
}
let stdout = String::from_utf8(output.stdout).ok();
if stdout.is_none() {
return None;
}
let stdout = stdout.unwrap();
match &stdout {
s if s.starts_with("10") => Some(10),
s if s.starts_with("11") => Some(11),
s if s.starts_with("12") => Some(12),
s if s.starts_with("13") => Some(13),
s if s.starts_with("14") => Some(14),
_ => None,
}
}