4 use std::path::{Path, PathBuf};
5 use std::process::{self, Command, Stdio};
7 use super::path::{Dirs, RelPath};
8 use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path};
10 pub(crate) struct Compiler {
11 pub(crate) cargo: PathBuf,
12 pub(crate) rustc: PathBuf,
13 pub(crate) rustdoc: PathBuf,
14 pub(crate) rustflags: String,
15 pub(crate) rustdocflags: String,
16 pub(crate) triple: String,
17 pub(crate) runner: Vec<String>,
21 pub(crate) fn host() -> Compiler {
23 cargo: get_cargo_path(),
24 rustc: get_rustc_path(),
25 rustdoc: get_rustdoc_path(),
26 rustflags: String::new(),
27 rustdocflags: String::new(),
28 triple: get_host_triple(),
33 pub(crate) fn with_triple(triple: String) -> Compiler {
35 cargo: get_cargo_path(),
36 rustc: get_rustc_path(),
37 rustdoc: get_rustdoc_path(),
38 rustflags: String::new(),
39 rustdocflags: String::new(),
46 pub(crate) struct CargoProject {
47 source: &'static RelPath,
52 pub(crate) const fn new(path: &'static RelPath, target: &'static str) -> CargoProject {
53 CargoProject { source: path, target }
56 pub(crate) fn source_dir(&self, dirs: &Dirs) -> PathBuf {
57 self.source.to_path(dirs)
60 pub(crate) fn manifest_path(&self, dirs: &Dirs) -> PathBuf {
61 self.source_dir(dirs).join("Cargo.toml")
64 pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
65 RelPath::BUILD.join(self.target).to_path(dirs)
68 fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command {
69 let mut cmd = Command::new(cargo);
72 .arg("--manifest-path")
73 .arg(self.manifest_path(dirs))
75 .arg(self.target_dir(dirs));
80 fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command {
81 let mut cmd = self.base_cmd(command, &compiler.cargo, dirs);
83 cmd.arg("--target").arg(&compiler.triple);
85 cmd.env("RUSTC", &compiler.rustc);
86 cmd.env("RUSTDOC", &compiler.rustdoc);
87 cmd.env("RUSTFLAGS", &compiler.rustflags);
88 cmd.env("RUSTDOCFLAGS", &compiler.rustdocflags);
89 if !compiler.runner.is_empty() {
91 format!("CARGO_TARGET_{}_RUNNER", compiler.triple.to_uppercase().replace('-', "_")),
92 compiler.runner.join(" "),
100 pub(crate) fn fetch(&self, cargo: impl AsRef<Path>, dirs: &Dirs) -> Command {
101 let mut cmd = Command::new(cargo.as_ref());
103 cmd.arg("fetch").arg("--manifest-path").arg(self.manifest_path(dirs));
109 pub(crate) fn clean(&self, cargo: &Path, dirs: &Dirs) -> Command {
110 self.base_cmd("clean", cargo, dirs)
114 pub(crate) fn build(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
115 self.build_cmd("build", compiler, dirs)
119 pub(crate) fn test(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
120 self.build_cmd("test", compiler, dirs)
124 pub(crate) fn run(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
125 self.build_cmd("run", compiler, dirs)
130 pub(crate) fn hyperfine_command(
133 prepare: Option<&str>,
137 let mut bench = Command::new("hyperfine");
140 bench.arg("--warmup").arg(warmup.to_string());
144 bench.arg("--runs").arg(runs.to_string());
147 if let Some(prepare) = prepare {
148 bench.arg("--prepare").arg(prepare);
157 pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
158 let src = src.as_ref();
159 let dst = dst.as_ref();
160 if let Err(_) = fs::hard_link(src, dst) {
161 fs::copy(src, dst).unwrap(); // Fallback to copying if hardlinking failed
166 pub(crate) fn spawn_and_wait(mut cmd: Command) {
167 if !cmd.spawn().unwrap().wait().unwrap().success() {
173 pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> String {
175 .stdin(Stdio::piped())
176 .stdout(Stdio::piped())
178 .expect("Failed to spawn child process");
180 let mut stdin = child.stdin.take().expect("Failed to open stdin");
181 std::thread::spawn(move || {
182 stdin.write_all(input.as_bytes()).expect("Failed to write to stdin");
185 let output = child.wait_with_output().expect("Failed to read stdout");
186 if !output.status.success() {
190 String::from_utf8(output.stdout).unwrap()
193 pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
194 for entry in fs::read_dir(from).unwrap() {
195 let entry = entry.unwrap();
196 let filename = entry.file_name();
197 if filename == "." || filename == ".." {
200 if entry.metadata().unwrap().is_dir() {
201 fs::create_dir(to.join(&filename)).unwrap();
202 copy_dir_recursively(&from.join(&filename), &to.join(&filename));
204 fs::copy(from.join(&filename), to.join(&filename)).unwrap();
209 pub(crate) fn is_ci() -> bool {
210 env::var("CI").as_deref() == Ok("true")