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;
|
2022-10-26 09:51:03 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-07-30 04:32:54 -05:00
|
|
|
use std::process::{self, Command, Stdio};
|
2021-06-11 11:50:01 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
use super::path::{Dirs, RelPath};
|
2022-10-26 09:51:03 -05:00
|
|
|
use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path};
|
|
|
|
|
|
|
|
pub(crate) struct Compiler {
|
|
|
|
pub(crate) cargo: PathBuf,
|
|
|
|
pub(crate) rustc: PathBuf,
|
|
|
|
pub(crate) rustdoc: PathBuf,
|
|
|
|
pub(crate) rustflags: String,
|
|
|
|
pub(crate) rustdocflags: String,
|
|
|
|
pub(crate) triple: String,
|
|
|
|
pub(crate) runner: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler {
|
|
|
|
pub(crate) fn host() -> Compiler {
|
|
|
|
Compiler {
|
|
|
|
cargo: get_cargo_path(),
|
|
|
|
rustc: get_rustc_path(),
|
|
|
|
rustdoc: get_rustdoc_path(),
|
|
|
|
rustflags: String::new(),
|
|
|
|
rustdocflags: String::new(),
|
|
|
|
triple: get_host_triple(),
|
|
|
|
runner: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn with_triple(triple: String) -> Compiler {
|
|
|
|
Compiler {
|
|
|
|
cargo: get_cargo_path(),
|
|
|
|
rustc: get_rustc_path(),
|
|
|
|
rustdoc: get_rustdoc_path(),
|
|
|
|
rustflags: String::new(),
|
|
|
|
rustdocflags: String::new(),
|
|
|
|
triple,
|
|
|
|
runner: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct CargoProject {
|
2022-12-01 07:30:03 -06:00
|
|
|
source: &'static RelPath,
|
2022-11-28 11:37:30 -06:00
|
|
|
target: &'static str,
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoProject {
|
2022-12-01 07:30:03 -06:00
|
|
|
pub(crate) const fn new(path: &'static RelPath, target: &'static str) -> CargoProject {
|
|
|
|
CargoProject { source: path, target }
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn source_dir(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
self.source.to_path(dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn manifest_path(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
self.source_dir(dirs).join("Cargo.toml")
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
2022-08-28 11:38:09 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
RelPath::BUILD.join(self.target).to_path(dirs)
|
2022-08-28 11:38:09 -05:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command {
|
2022-10-26 09:51:03 -05:00
|
|
|
let mut cmd = Command::new(cargo);
|
|
|
|
|
|
|
|
cmd.arg(command)
|
|
|
|
.arg("--manifest-path")
|
2022-12-01 08:54:37 -06:00
|
|
|
.arg(self.manifest_path(dirs))
|
2022-10-26 09:51:03 -05:00
|
|
|
.arg("--target-dir")
|
2022-12-01 08:54:37 -06:00
|
|
|
.arg(self.target_dir(dirs));
|
2022-10-26 09:51:03 -05:00
|
|
|
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
let mut cmd = self.base_cmd(command, &compiler.cargo, dirs);
|
2022-10-26 09:51:03 -05:00
|
|
|
|
|
|
|
cmd.arg("--target").arg(&compiler.triple);
|
|
|
|
|
|
|
|
cmd.env("RUSTC", &compiler.rustc);
|
|
|
|
cmd.env("RUSTDOC", &compiler.rustdoc);
|
|
|
|
cmd.env("RUSTFLAGS", &compiler.rustflags);
|
|
|
|
cmd.env("RUSTDOCFLAGS", &compiler.rustdocflags);
|
|
|
|
if !compiler.runner.is_empty() {
|
|
|
|
cmd.env(
|
|
|
|
format!("CARGO_TARGET_{}_RUNNER", compiler.triple.to_uppercase().replace('-', "_")),
|
|
|
|
compiler.runner.join(" "),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn fetch(&self, cargo: impl AsRef<Path>, dirs: &Dirs) -> Command {
|
2022-10-26 09:51:03 -05:00
|
|
|
let mut cmd = Command::new(cargo.as_ref());
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
cmd.arg("fetch").arg("--manifest-path").arg(self.manifest_path(dirs));
|
2022-10-26 09:51:03 -05:00
|
|
|
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn clean(&self, cargo: &Path, dirs: &Dirs) -> Command {
|
|
|
|
self.base_cmd("clean", cargo, dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn build(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("build", compiler, dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn test(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("test", compiler, dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn run(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("run", compiler, dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
}
|
2022-08-28 11:38:09 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:51:03 -05:00
|
|
|
#[must_use]
|
2022-08-28 12:41:36 -05:00
|
|
|
pub(crate) fn hyperfine_command(
|
|
|
|
warmup: u64,
|
|
|
|
runs: u64,
|
2022-10-26 09:51:03 -05:00
|
|
|
prepare: Option<&str>,
|
|
|
|
a: &str,
|
|
|
|
b: &str,
|
2022-08-28 12:41:36 -05:00
|
|
|
) -> Command {
|
|
|
|
let mut bench = Command::new("hyperfine");
|
|
|
|
|
|
|
|
if warmup != 0 {
|
|
|
|
bench.arg("--warmup").arg(warmup.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if runs != 0 {
|
|
|
|
bench.arg("--runs").arg(runs.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(prepare) = prepare {
|
2022-10-26 09:51:03 -05:00
|
|
|
bench.arg("--prepare").arg(prepare);
|
2022-08-28 12:41:36 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:51:03 -05:00
|
|
|
bench.arg(a).arg(b);
|
2022-08-28 12:41:36 -05:00
|
|
|
|
|
|
|
bench
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-16 15:58:58 -06:00
|
|
|
env::var("CI").as_deref() == Ok("true")
|
2022-08-05 07:57:19 -05:00
|
|
|
}
|