Debian Codegraph @

third_party/rust/rustix/build.rs

raw
1 use std::env::var;
2 use std::io::Write;
3
4 /// The directory for inline asm.
5 const ASM_PATH: &str = "src/backend/linux_raw/arch";
6
7 fn main() {
8 // Don't rerun this on changes other than build.rs, as we only depend on
9 // the rustc version.
10 println!("cargo:rerun-if-changed=build.rs");
11
12 // Gather target information.
13 let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
14 let env = var("CARGO_CFG_TARGET_ENV").unwrap();
15 let abi = var("CARGO_CFG_TARGET_ABI");
16 let inline_asm_name = format!("{}/{}.rs", ASM_PATH, arch);
17 let inline_asm_name_present = std::fs::metadata(inline_asm_name).is_ok();
18 let os = var("CARGO_CFG_TARGET_OS").unwrap();
19 let pointer_width = var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
20 let endian = var("CARGO_CFG_TARGET_ENDIAN").unwrap();
21
22 // Check for special target variants.
23 let is_x32 = arch == "x86_64" && pointer_width == "32";
24 let is_arm64_ilp32 = arch == "aarch64" && pointer_width == "32";
25 let is_powerpc64be = arch == "powerpc64" && endian == "big";
26 let is_mipseb = (arch == "mips" || arch == "mips32r6") && endian == "big";
27 let is_mips64eb = arch.contains("mips64") && endian == "big";
28 let is_unsupported_abi = is_x32 || is_arm64_ilp32 || is_powerpc64be || is_mipseb || is_mips64eb;
29
30 // Check for `--features=use-libc`. This allows crate users to enable the
31 // libc backend.
32 let feature_use_libc = var("CARGO_FEATURE_USE_LIBC").is_ok();
33
34 // Check for `RUSTFLAGS=--cfg=rustix_use_libc`. This allows end users to
35 // enable the libc backend even if rustix is depended on transitively.
36 let cfg_use_libc = var("CARGO_CFG_RUSTIX_USE_LIBC").is_ok();
37
38 // Check for `--features=rustc-dep-of-std`.
39 let rustc_dep_of_std = var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
40
41 // Check for eg. `RUSTFLAGS=--cfg=rustix_use_experimental_features`. This
42 // is a rustc flag rather than a cargo feature flag because it's
43 // experimental and not something we want accidentally enabled via
44 // `--all-features`.
45 let rustix_use_experimental_features =
46 var("CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_FEATURES").is_ok();
47
48 // Check for eg. `RUSTFLAGS=--cfg=rustix_use_experimental_asm`. This is a
49 // rustc flag rather than a cargo feature flag because it's experimental
50 // and not something we want accidentally enabled via `--all-features`.
51 let rustix_use_experimental_asm = var("CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM").is_ok();
52
53 // Miri doesn't support inline asm, and has builtin support for recognizing
54 // libc FFI calls, so if we're running under miri, use the libc backend.
55 let miri = var("CARGO_CFG_MIRI").is_ok();
56
57 // If experimental features are enabled, auto-detect and use available
58 // features.
59 if rustc_dep_of_std {
60 use_feature("rustc_attrs");
61 use_feature("core_intrinsics");
62 } else if rustix_use_experimental_features {
63 use_feature_or_nothing("rustc_attrs");
64 use_feature_or_nothing("core_intrinsics");
65 }
66
67 // Features needed only in no-std configurations.
68 #[cfg(not(feature = "std"))]
69 {
70 use_feature_or_nothing("core_c_str");
71 use_feature_or_nothing("core_ffi_c");
72 use_feature_or_nothing("alloc_c_string");
73 use_feature_or_nothing("alloc_ffi");
74 }
75
76 // Feature needed for testing.
77 if use_static_assertions() {
78 use_feature("static_assertions");
79 }
80
81 // WASI support can utilize wasi_ext if present.
82 if os == "wasi" {
83 use_feature_or_nothing("wasi_ext");
84 }
85
86 // If the libc backend is requested, or if we're not on a platform for
87 // which we have linux_raw support, use the libc backend.
88 //
89 // For now Android uses the libc backend; in theory it could use the
90 // linux_raw backend, but to do that we'll need to figure out how to
91 // install the toolchain for it.
92 let libc = feature_use_libc
93 || cfg_use_libc
94 || os != "linux"
95 || !inline_asm_name_present
96 || is_unsupported_abi
97 || miri
98 || ((arch == "powerpc64" || arch == "s390x" || arch.starts_with("mips"))
99 && !rustix_use_experimental_asm);
100 if libc {
101 // Use the libc backend.
102 use_feature("libc");
103 } else {
104 // Use the linux_raw backend.
105 use_feature("linux_raw");
106 if rustix_use_experimental_asm {
107 use_feature("asm_experimental_arch");
108 }
109 }
110
111 // Detect whether the compiler requires us to use thumb mode on ARM.
112 if arch == "arm" && use_thumb_mode() {
113 use_feature("thumb_mode");
114 }
115
116 // Rust's libc crate groups some OS's together which have similar APIs;
117 // create similarly-named features to make `cfg` tests more concise.
118 let freebsdlike = os == "freebsd" || os == "dragonfly";
119 if freebsdlike {
120 use_feature("freebsdlike");
121 }
122 let netbsdlike = os == "openbsd" || os == "netbsd";
123 if netbsdlike {
124 use_feature("netbsdlike");
125 }
126 let apple = os == "macos" || os == "ios" || os == "tvos" || os == "visionos" || os == "watchos";
127 if apple {
128 use_feature("apple");
129 }
130 if os == "linux" || os == "l4re" || os == "android" || os == "emscripten" {
131 use_feature("linux_like");
132 }
133 if os == "solaris" || os == "illumos" {
134 use_feature("solarish");
135 }
136 if apple || freebsdlike || netbsdlike {
137 use_feature("bsd");
138 }
139
140 // Add some additional common target combinations.
141
142 // Android and "regular" Linux both use the Linux kernel.
143 if os == "android" || os == "linux" {
144 use_feature("linux_kernel");
145 }
146
147 // These platforms have a 32-bit `time_t`.
148 if libc
149 && (arch == "arm"
150 || arch == "mips"
151 || arch == "sparc"
152 || arch == "x86"
153 || (arch == "wasm32" && os == "emscripten")
154 || (arch == "aarch64" && os == "linux" && abi == Ok("ilp32".to_string())))
155 && (apple
156 || os == "android"
157 || os == "emscripten"
158 || os == "haiku"
159 || env == "gnu"
160 || (env == "musl" && arch == "x86")
161 || (arch == "aarch64" && os == "linux" && abi == Ok("ilp32".to_string())))
162 {
163 use_feature("fix_y2038");
164 }
165
166 println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM");
167 println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_LIBC");
168
169 // Rerun this script if any of our features or configuration flags change,
170 // or if the toolchain we used for feature detection changes.
171 println!("cargo:rerun-if-env-changed=CARGO_FEATURE_USE_LIBC");
172 println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RUSTC_DEP_OF_STD");
173 println!("cargo:rerun-if-env-changed=CARGO_CFG_MIRI");
174 }
175
176 fn use_static_assertions() -> bool {
177 // `offset_from` was made const in Rust 1.65.
178 can_compile("const unsafe fn foo(p: *const u8) -> isize { p.offset_from(p) }")
179 }
180
181 fn use_thumb_mode() -> bool {
182 // In thumb mode, r7 is reserved.
183 !can_compile("pub unsafe fn f() { core::arch::asm!(\"udf #16\", in(\"r7\") 0); }")
184 }
185
186 fn use_feature_or_nothing(feature: &str) {
187 if has_feature(feature) {
188 use_feature(feature);
189 }
190 }
191
192 fn use_feature(feature: &str) {
193 println!("cargo:rustc-cfg={}", feature);
194 }
195
196 /// Test whether the rustc at `var("RUSTC")` supports the given feature.
197 fn has_feature(feature: &str) -> bool {
198 can_compile(format!(
199 "#![allow(stable_features)]\n#![feature({})]",
200 feature
201 ))
202 }
203
204 /// Test whether the rustc at `var("RUSTC")` can compile the given code.
205 fn can_compile<T: AsRef<str>>(test: T) -> bool {
206 use std::process::Stdio;
207
208 let rustc = var("RUSTC").unwrap();
209 let target = var("TARGET").unwrap();
210
211 // Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string, as
212 // documented [here].
213 // [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
214 let wrapper = var("RUSTC_WRAPPER")
215 .ok()
216 .and_then(|w| if w.is_empty() { None } else { Some(w) });
217
218 let mut cmd = if let Some(wrapper) = wrapper {
219 let mut cmd = std::process::Command::new(wrapper);
220 // The wrapper's first argument is supposed to be the path to rustc.
221 cmd.arg(rustc);
222 cmd
223 } else {
224 std::process::Command::new(rustc)
225 };
226
227 cmd.arg("--crate-type=rlib") // Don't require `main`.
228 .arg("--emit=metadata") // Do as little as possible but still parse.
229 .arg("--target")
230 .arg(target)
231 .arg("-o")
232 .arg("-")
233 .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not)
234
235 // If Cargo wants to set RUSTFLAGS, use that.
236 if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
237 if !rustflags.is_empty() {
238 for arg in rustflags.split('\x1f') {
239 cmd.arg(arg);
240 }
241 }
242 }
243
244 let mut child = cmd
245 .arg("-") // Read from stdin.
246 .stdin(Stdio::piped()) // Stdin is a pipe.
247 .stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
248 .spawn()
249 .unwrap();
250
251 writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();
252
253 child.wait().unwrap().success()
254 }