Test whether the rustc at `var("RUSTC")` can compile the given code.
(test: T)
| 233 | |
| 234 | /// Test whether the rustc at `var("RUSTC")` can compile the given code. |
| 235 | fn can_compile<T: AsRef<str>>(test: T) -> bool { |
| 236 | use std::process::Stdio; |
| 237 | |
| 238 | let rustc = var("RUSTC").unwrap(); |
| 239 | let target = var("TARGET").unwrap(); |
| 240 | |
| 241 | // Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string, as |
| 242 | // documented [here]. |
| 243 | // [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads |
| 244 | let wrapper = var("RUSTC_WRAPPER") |
| 245 | .ok() |
| 246 | .and_then(|w| if w.is_empty() { None } else { Some(w) }); |
| 247 | |
| 248 | let mut cmd = if let Some(wrapper) = wrapper { |
| 249 | let mut cmd = std::process::Command::new(wrapper); |
| 250 | // The wrapper's first argument is supposed to be the path to rustc. |
| 251 | cmd.arg(rustc); |
| 252 | cmd |
| 253 | } else { |
| 254 | std::process::Command::new(rustc) |
| 255 | }; |
| 256 | |
| 257 | let out_dir = var("OUT_DIR").unwrap(); |
| 258 | let out_file = PathBuf::from(out_dir).join("rustix_test_can_compile"); |
| 259 | cmd.arg("--crate-type=rlib") // Don't require `main`. |
| 260 | .arg("--emit=metadata") // Do as little as possible but still parse. |
| 261 | .arg("--target") |
| 262 | .arg(target) |
| 263 | .arg("-o") |
| 264 | .arg(out_file) |
| 265 | .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not) |
| 266 | |
| 267 | // If Cargo wants to set RUSTFLAGS, use that. |
| 268 | if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") { |
| 269 | if !rustflags.is_empty() { |
| 270 | for arg in rustflags.split('\x1f') { |
| 271 | cmd.arg(arg); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | let mut child = cmd |
| 277 | .arg("-") // Read from stdin. |
| 278 | .stdin(Stdio::piped()) // Stdin is a pipe. |
| 279 | .stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing. |
| 280 | .spawn() |
| 281 | .unwrap(); |
| 282 | |
| 283 | writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap(); |
| 284 | |
| 285 | child.wait().unwrap().success() |
| 286 | } |
no outgoing calls
no test coverage detected