rust/compiler/rustc_codegen_cranelift/build_system/utils.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2022-08-05 07:57:19 -05:00
use std::env;
2021-06-11 11:50:01 -05:00
use std::fs;
2022-07-31 05:15:56 -05:00
use std::io::Write;
2021-06-11 11:50:01 -05:00
use std::path::Path;
2022-07-30 04:32:54 -05:00
use std::process::{self, Command, Stdio};
2021-06-11 11:50:01 -05:00
#[track_caller]
pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
let src = src.as_ref();
let dst = dst.as_ref();
if let Err(_) = fs::hard_link(src, dst) {
fs::copy(src, dst).unwrap(); // Fallback to copying if hardlinking failed
}
}
2021-06-16 11:01:19 -05:00
#[track_caller]
2021-06-11 11:50:01 -05:00
pub(crate) fn spawn_and_wait(mut cmd: Command) {
if !cmd.spawn().unwrap().wait().unwrap().success() {
process::exit(1);
}
}
2021-06-16 11:01:19 -05:00
2022-07-30 04:32:54 -05:00
#[track_caller]
pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> String {
let mut child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn child process");
let mut stdin = child.stdin.take().expect("Failed to open stdin");
std::thread::spawn(move || {
stdin.write_all(input.as_bytes()).expect("Failed to write to stdin");
});
let output = child.wait_with_output().expect("Failed to read stdout");
if !output.status.success() {
process::exit(1);
}
String::from_utf8(output.stdout).unwrap()
}
2021-06-16 11:01:19 -05:00
pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
for entry in fs::read_dir(from).unwrap() {
let entry = entry.unwrap();
let filename = entry.file_name();
if filename == "." || filename == ".." {
continue;
}
if entry.metadata().unwrap().is_dir() {
fs::create_dir(to.join(&filename)).unwrap();
copy_dir_recursively(&from.join(&filename), &to.join(&filename));
} else {
fs::copy(from.join(&filename), to.join(&filename)).unwrap();
}
}
}
2022-08-05 07:57:19 -05:00
pub(crate) fn is_ci() -> bool {
env::var("CI").as_ref().map(|val| &**val) == Ok("true")
}