]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | #[cfg(feature = "cc")] |
2 | use cc::Build; | |
3 | use std::env::var; | |
4 | use std::io::Write; | |
5 | ||
6 | /// The directory for out-of-line ("outline") libraries. | |
487cf647 | 7 | const OUTLINE_PATH: &str = "src/backend/linux_raw/arch/outline"; |
064997fb FG |
8 | |
9 | fn main() { | |
10 | // Don't rerun this on changes other than build.rs, as we only depend on | |
11 | // the rustc version. | |
12 | println!("cargo:rerun-if-changed=build.rs"); | |
13 | ||
14 | use_feature_or_nothing("rustc_attrs"); | |
15 | ||
16 | // Features only used in no-std configurations. | |
17 | #[cfg(not(feature = "std"))] | |
18 | { | |
19 | use_feature_or_nothing("const_raw_ptr_deref"); | |
20 | use_feature_or_nothing("core_ffi_c"); | |
21 | use_feature_or_nothing("core_c_str"); | |
22 | use_feature_or_nothing("alloc_c_string"); | |
23 | } | |
24 | ||
25 | // Gather target information. | |
26 | let arch = var("CARGO_CFG_TARGET_ARCH").unwrap(); | |
27 | let asm_name = format!("{}/{}.s", OUTLINE_PATH, arch); | |
28 | let asm_name_present = std::fs::metadata(&asm_name).is_ok(); | |
353b0b11 | 29 | let target_os = var("CARGO_CFG_TARGET_OS").unwrap(); |
064997fb FG |
30 | let pointer_width = var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap(); |
31 | let endian = var("CARGO_CFG_TARGET_ENDIAN").unwrap(); | |
32 | ||
33 | // Check for special target variants. | |
34 | let is_x32 = arch == "x86_64" && pointer_width == "32"; | |
35 | let is_arm64_ilp32 = arch == "aarch64" && pointer_width == "32"; | |
36 | let is_powerpc64be = arch == "powerpc64" && endian == "big"; | |
37 | let is_mipseb = arch == "mips" && endian == "big"; | |
38 | let is_mips64eb = arch == "mips64" && endian == "big"; | |
39 | let is_unsupported_abi = is_x32 || is_arm64_ilp32 || is_powerpc64be || is_mipseb || is_mips64eb; | |
40 | ||
41 | // Check for `--features=use-libc`. This allows crate users to enable the | |
42 | // libc backend. | |
43 | let feature_use_libc = var("CARGO_FEATURE_USE_LIBC").is_ok(); | |
44 | ||
45 | // Check for `--features=rustc-dep-of-std`. This is used when rustix is | |
46 | // being used to build std, in which case `can_compile` doesn't work | |
47 | // because `core` isn't available yet, but also, we can assume we have a | |
48 | // recent compiler. | |
49 | let feature_rustc_dep_of_std = var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok(); | |
50 | ||
51 | // Check for `RUSTFLAGS=--cfg=rustix_use_libc`. This allows end users to | |
52 | // enable the libc backend even if rustix is depended on transitively. | |
53 | let cfg_use_libc = var("CARGO_CFG_RUSTIX_USE_LIBC").is_ok(); | |
54 | ||
55 | // Check for eg. `RUSTFLAGS=--cfg=rustix_use_experimental_asm`. This is a | |
56 | // rustc flag rather than a cargo feature flag because it's experimental | |
57 | // and not something we want accidentally enabled via `--all-features`. | |
58 | let rustix_use_experimental_asm = var("CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM").is_ok(); | |
59 | ||
60 | // Miri doesn't support inline asm, and has builtin support for recognizing | |
61 | // libc FFI calls, so if we're running under miri, use the libc backend. | |
62 | let miri = var("CARGO_CFG_MIRI").is_ok(); | |
63 | ||
64 | // If the libc backend is requested, or if we're not on a platform for | |
65 | // which we have linux_raw support, use the libc backend. | |
66 | // | |
67 | // For now Android uses the libc backend; in theory it could use the | |
68 | // linux_raw backend, but to do that we'll need to figure out how to | |
69 | // install the toolchain for it. | |
70 | if feature_use_libc | |
71 | || cfg_use_libc | |
353b0b11 | 72 | || target_os != "linux" |
064997fb FG |
73 | || !asm_name_present |
74 | || is_unsupported_abi | |
75 | || miri | |
76 | { | |
77 | // Use the libc backend. | |
78 | use_feature("libc"); | |
79 | } else { | |
80 | // Use the linux_raw backend. | |
81 | use_feature("linux_raw"); | |
82 | use_feature_or_nothing("core_intrinsics"); | |
83 | ||
84 | // Use inline asm if we have it, or outline asm otherwise. On PowerPC | |
85 | // and MIPS, Rust's inline asm is considered experimental, so only use | |
86 | // it if `--cfg=rustix_use_experimental_asm` is given. | |
87 | if (feature_rustc_dep_of_std || can_compile("use std::arch::asm;")) | |
88 | && (arch != "x86" || has_feature("naked_functions")) | |
89 | && ((arch != "powerpc64" && arch != "mips" && arch != "mips64") | |
90 | || rustix_use_experimental_asm) | |
91 | { | |
92 | use_feature("asm"); | |
93 | if arch == "x86" { | |
94 | use_feature("naked_functions"); | |
95 | } | |
96 | if rustix_use_experimental_asm { | |
97 | use_feature("asm_experimental_arch"); | |
98 | } | |
99 | } else { | |
100 | link_in_librustix_outline(&arch, &asm_name); | |
101 | } | |
102 | } | |
103 | ||
487cf647 FG |
104 | // Detect whether the compiler requires us to use thumb mode on ARM. |
105 | if arch == "arm" && use_thumb_mode() { | |
106 | use_feature("thumb_mode"); | |
107 | } | |
108 | ||
353b0b11 FG |
109 | // Rust's libc crate groups some OS's together which have similar APIs; |
110 | // create similarly-named features to make `cfg` tests more concise. | |
111 | if target_os == "freebsd" || target_os == "dragonfly" { | |
112 | use_feature("freebsdlike"); | |
113 | } | |
114 | if target_os == "openbsd" || target_os == "netbsd" { | |
115 | use_feature("netbsdlike"); | |
116 | } | |
117 | if target_os == "macos" || target_os == "ios" || target_os == "tvos" || target_os == "watchos" { | |
118 | use_feature("apple"); | |
119 | } | |
120 | if target_os == "linux" | |
121 | || target_os == "l4re" | |
122 | || target_os == "android" | |
123 | || target_os == "emscripten" | |
124 | { | |
125 | use_feature("linux_like"); | |
126 | } | |
127 | if target_os == "solaris" || target_os == "illumos" { | |
128 | use_feature("solarish"); | |
129 | } | |
130 | if target_os == "macos" | |
131 | || target_os == "ios" | |
132 | || target_os == "tvos" | |
133 | || target_os == "watchos" | |
134 | || target_os == "freebsd" | |
135 | || target_os == "dragonfly" | |
136 | || target_os == "openbsd" | |
137 | || target_os == "netbsd" | |
138 | { | |
139 | use_feature("bsd"); | |
140 | } | |
141 | ||
064997fb | 142 | println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM"); |
353b0b11 FG |
143 | println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_LIBC"); |
144 | ||
145 | // Rerun this script if any of our features or configuration flags change, | |
146 | // or if the toolchain we used for feature detection changes. | |
147 | println!("cargo:rerun-if-env-changed=CARGO_FEATURE_USE_LIBC"); | |
148 | println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RUSTC_DEP_OF_STD"); | |
149 | println!("cargo:rerun-if-env-changed=CARGO_CFG_MIRI"); | |
064997fb FG |
150 | } |
151 | ||
152 | /// Link in the desired version of librustix_outline_{arch}.a, containing the | |
153 | /// outline assembly code for making syscalls. | |
154 | fn link_in_librustix_outline(arch: &str, asm_name: &str) { | |
155 | let name = format!("rustix_outline_{}", arch); | |
156 | let profile = var("PROFILE").unwrap(); | |
157 | let to = format!("{}/{}/lib{}.a", OUTLINE_PATH, profile, name); | |
158 | println!("cargo:rerun-if-changed={}", to); | |
159 | ||
160 | // If "cc" is not enabled, use a pre-built library. | |
161 | #[cfg(not(feature = "cc"))] | |
162 | { | |
163 | let _ = asm_name; | |
164 | println!("cargo:rustc-link-search={}/{}", OUTLINE_PATH, profile); | |
165 | println!("cargo:rustc-link-lib=static={}", name); | |
166 | } | |
167 | ||
168 | // If "cc" is enabled, build the library from source, update the pre-built | |
169 | // version, and assert that the pre-built version is checked in. | |
170 | #[cfg(feature = "cc")] | |
171 | { | |
172 | let out_dir = var("OUT_DIR").unwrap(); | |
353b0b11 FG |
173 | // Add `-gdwarf-3` so that we always get the same output, regardless of |
174 | // the Rust version we're using. DWARF3 is the version used in | |
175 | // Rust 1.48 and is entirely adequate for our simple needs here. | |
176 | let mut build = Build::new(); | |
177 | if profile == "debug" { | |
178 | build.flag("-gdwarf-3"); | |
179 | } | |
180 | build.file(&asm_name); | |
181 | build.compile(&name); | |
064997fb FG |
182 | println!("cargo:rerun-if-changed={}", asm_name); |
183 | if std::fs::metadata(".git").is_ok() { | |
184 | let from = format!("{}/lib{}.a", out_dir, name); | |
185 | let prev_metadata = std::fs::metadata(&to); | |
186 | std::fs::copy(&from, &to).unwrap(); | |
187 | assert!( | |
188 | prev_metadata.is_ok(), | |
189 | "{} didn't previously exist; please inspect the new file and `git add` it", | |
190 | to | |
191 | ); | |
192 | assert!( | |
193 | std::process::Command::new("git") | |
194 | .arg("diff") | |
195 | .arg("--quiet") | |
196 | .arg(&to) | |
197 | .status() | |
198 | .unwrap() | |
199 | .success(), | |
200 | "{} changed; please inspect the change and `git commit` it", | |
201 | to | |
202 | ); | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
487cf647 FG |
207 | fn use_thumb_mode() -> bool { |
208 | // In thumb mode, r7 is reserved. | |
209 | !can_compile("pub unsafe fn f() { core::arch::asm!(\"udf #16\", in(\"r7\") 0); }") | |
210 | } | |
211 | ||
064997fb FG |
212 | fn use_feature_or_nothing(feature: &str) { |
213 | if has_feature(feature) { | |
214 | use_feature(feature); | |
215 | } | |
216 | } | |
217 | ||
218 | fn use_feature(feature: &str) { | |
219 | println!("cargo:rustc-cfg={}", feature); | |
220 | } | |
221 | ||
222 | /// Test whether the rustc at `var("RUSTC")` supports the given feature. | |
223 | fn has_feature(feature: &str) -> bool { | |
353b0b11 | 224 | can_compile(format!( |
064997fb FG |
225 | "#![allow(stable_features)]\n#![feature({})]", |
226 | feature | |
227 | )) | |
228 | } | |
229 | ||
230 | /// Test whether the rustc at `var("RUSTC")` can compile the given code. | |
353b0b11 | 231 | fn can_compile<T: AsRef<str>>(test: T) -> bool { |
064997fb | 232 | use std::process::Stdio; |
353b0b11 | 233 | |
064997fb FG |
234 | let out_dir = var("OUT_DIR").unwrap(); |
235 | let rustc = var("RUSTC").unwrap(); | |
487cf647 | 236 | let target = var("TARGET").unwrap(); |
064997fb | 237 | |
353b0b11 FG |
238 | // Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string, |
239 | // as documented [here]. | |
240 | // [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads | |
241 | let wrapper = var("RUSTC_WRAPPER") | |
242 | .ok() | |
243 | .and_then(|w| if w.is_empty() { None } else { Some(w) }); | |
244 | ||
245 | let mut cmd = if let Some(wrapper) = wrapper { | |
246 | let mut cmd = std::process::Command::new(wrapper); | |
247 | // The wrapper's first argument is supposed to be the path to rustc. | |
248 | cmd.arg(rustc); | |
249 | cmd | |
250 | } else { | |
251 | std::process::Command::new(rustc) | |
252 | }; | |
253 | ||
254 | cmd.arg("--crate-type=rlib") // Don't require `main`. | |
064997fb | 255 | .arg("--emit=metadata") // Do as little as possible but still parse. |
487cf647 FG |
256 | .arg("--target") |
257 | .arg(target) | |
064997fb | 258 | .arg("--out-dir") |
353b0b11 FG |
259 | .arg(out_dir); // Put the output somewhere inconsequential. |
260 | ||
261 | // If Cargo wants to set RUSTFLAGS, use that. | |
262 | if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") { | |
263 | if !rustflags.is_empty() { | |
264 | for arg in rustflags.split('\x1f') { | |
265 | cmd.arg(arg); | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | let mut child = cmd | |
064997fb FG |
271 | .arg("-") // Read from stdin. |
272 | .stdin(Stdio::piped()) // Stdin is a pipe. | |
353b0b11 | 273 | .stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing. |
064997fb FG |
274 | .spawn() |
275 | .unwrap(); | |
276 | ||
353b0b11 | 277 | writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap(); |
064997fb FG |
278 | |
279 | child.wait().unwrap().success() | |
280 | } |