2022-08-24 11:40:58 -05:00
|
|
|
use std::env;
|
2021-07-07 04:14:20 -05:00
|
|
|
use std::fs;
|
2023-10-09 03:52:46 -05:00
|
|
|
use std::io;
|
2022-12-14 12:30:46 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-10-09 03:52:46 -05:00
|
|
|
use std::process::{self, Command};
|
2023-07-22 08:32:34 -05:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-08-09 13:20:12 -05:00
|
|
|
use crate::path::{Dirs, RelPath};
|
|
|
|
use crate::shared_utils::rustflags_to_cmd_env;
|
2022-12-14 12:30:46 -06:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2022-12-14 12:30:46 -06:00
|
|
|
pub(crate) struct Compiler {
|
|
|
|
pub(crate) cargo: PathBuf,
|
|
|
|
pub(crate) rustc: PathBuf,
|
|
|
|
pub(crate) rustdoc: PathBuf,
|
2023-08-09 13:20:12 -05:00
|
|
|
pub(crate) rustflags: Vec<String>,
|
|
|
|
pub(crate) rustdocflags: Vec<String>,
|
2022-12-14 12:30:46 -06:00
|
|
|
pub(crate) triple: String,
|
|
|
|
pub(crate) runner: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler {
|
2023-01-24 11:56:42 -06:00
|
|
|
pub(crate) fn set_cross_linker_and_runner(&mut self) {
|
|
|
|
match self.triple.as_str() {
|
|
|
|
"aarch64-unknown-linux-gnu" => {
|
|
|
|
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
|
2023-08-09 13:20:12 -05:00
|
|
|
self.rustflags.push("-Clinker=aarch64-linux-gnu-gcc".to_owned());
|
|
|
|
self.rustdocflags.push("-Clinker=aarch64-linux-gnu-gcc".to_owned());
|
2023-01-24 11:56:42 -06:00
|
|
|
self.runner = vec![
|
|
|
|
"qemu-aarch64".to_owned(),
|
|
|
|
"-L".to_owned(),
|
|
|
|
"/usr/aarch64-linux-gnu".to_owned(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
"s390x-unknown-linux-gnu" => {
|
|
|
|
// We are cross-compiling for s390x. Use the correct linker and run tests in qemu.
|
2023-08-09 13:20:12 -05:00
|
|
|
self.rustflags.push("-Clinker=s390x-linux-gnu-gcc".to_owned());
|
|
|
|
self.rustdocflags.push("-Clinker=s390x-linux-gnu-gcc".to_owned());
|
2023-01-24 11:56:42 -06:00
|
|
|
self.runner = vec![
|
|
|
|
"qemu-s390x".to_owned(),
|
|
|
|
"-L".to_owned(),
|
|
|
|
"/usr/s390x-linux-gnu".to_owned(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
"x86_64-pc-windows-gnu" => {
|
|
|
|
// We are cross-compiling for Windows. Run tests in wine.
|
|
|
|
self.runner = vec!["wine".to_owned()];
|
|
|
|
}
|
|
|
|
_ => {
|
2023-10-09 03:52:46 -05:00
|
|
|
eprintln!("Unknown non-native platform");
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
2022-12-14 12:30:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct CargoProject {
|
|
|
|
source: &'static RelPath,
|
|
|
|
target: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoProject {
|
|
|
|
pub(crate) const fn new(path: &'static RelPath, target: &'static str) -> CargoProject {
|
|
|
|
CargoProject { source: path, target }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn source_dir(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
self.source.to_path(dirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn manifest_path(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
self.source_dir(dirs).join("Cargo.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
RelPath::BUILD.join(self.target).to_path(dirs)
|
|
|
|
}
|
2022-10-23 09:22:55 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
#[must_use]
|
2022-12-14 12:30:46 -06:00
|
|
|
fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command {
|
|
|
|
let mut cmd = Command::new(cargo);
|
|
|
|
|
|
|
|
cmd.arg(command)
|
|
|
|
.arg("--manifest-path")
|
|
|
|
.arg(self.manifest_path(dirs))
|
|
|
|
.arg("--target-dir")
|
2023-01-24 11:56:42 -06:00
|
|
|
.arg(self.target_dir(dirs))
|
2023-06-15 12:56:01 -05:00
|
|
|
.arg("--locked");
|
|
|
|
|
|
|
|
if dirs.frozen {
|
|
|
|
cmd.arg("--frozen");
|
|
|
|
}
|
2022-12-14 12:30:46 -06:00
|
|
|
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
#[must_use]
|
2022-12-14 12:30:46 -06:00
|
|
|
fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
let mut cmd = self.base_cmd(command, &compiler.cargo, dirs);
|
|
|
|
|
|
|
|
cmd.arg("--target").arg(&compiler.triple);
|
|
|
|
|
|
|
|
cmd.env("RUSTC", &compiler.rustc);
|
|
|
|
cmd.env("RUSTDOC", &compiler.rustdoc);
|
2023-08-09 13:20:12 -05:00
|
|
|
rustflags_to_cmd_env(&mut cmd, "RUSTFLAGS", &compiler.rustflags);
|
|
|
|
rustflags_to_cmd_env(&mut cmd, "RUSTDOCFLAGS", &compiler.rustdocflags);
|
2022-12-14 12:30:46 -06:00
|
|
|
if !compiler.runner.is_empty() {
|
|
|
|
cmd.env(
|
|
|
|
format!("CARGO_TARGET_{}_RUNNER", compiler.triple.to_uppercase().replace('-', "_")),
|
|
|
|
compiler.runner.join(" "),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
pub(crate) fn clean(&self, dirs: &Dirs) {
|
|
|
|
let _ = fs::remove_dir_all(self.target_dir(dirs));
|
2022-12-14 12:30:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub(crate) fn build(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("build", compiler, dirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub(crate) fn test(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("test", compiler, dirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub(crate) fn run(&self, compiler: &Compiler, dirs: &Dirs) -> Command {
|
|
|
|
self.build_cmd("run", compiler, dirs)
|
|
|
|
}
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
#[must_use]
|
2022-10-23 09:22:55 -05:00
|
|
|
pub(crate) fn hyperfine_command(
|
|
|
|
warmup: u64,
|
|
|
|
runs: u64,
|
2022-12-14 12:30:46 -06:00
|
|
|
prepare: Option<&str>,
|
2023-07-22 08:32:34 -05:00
|
|
|
cmds: &[(&str, &str)],
|
|
|
|
markdown_export: &Path,
|
2022-10-23 09:22:55 -05:00
|
|
|
) -> Command {
|
|
|
|
let mut bench = Command::new("hyperfine");
|
|
|
|
|
2023-07-22 08:32:34 -05:00
|
|
|
bench.arg("--export-markdown").arg(markdown_export);
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
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-12-14 12:30:46 -06:00
|
|
|
bench.arg("--prepare").arg(prepare);
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
|
2023-07-22 08:32:34 -05:00
|
|
|
for &(name, cmd) in cmds {
|
|
|
|
if name != "" {
|
|
|
|
bench.arg("-n").arg(name);
|
|
|
|
}
|
|
|
|
bench.arg(cmd);
|
|
|
|
}
|
2022-10-23 09:22:55 -05:00
|
|
|
|
|
|
|
bench
|
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
#[must_use]
|
|
|
|
pub(crate) fn git_command<'a>(repo_dir: impl Into<Option<&'a Path>>, cmd: &str) -> Command {
|
|
|
|
let mut git_cmd = Command::new("git");
|
|
|
|
git_cmd
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.name=Dummy")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.email=dummy@example.com")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("core.autocrlf=false")
|
2023-07-22 08:32:34 -05:00
|
|
|
.arg("-c")
|
|
|
|
.arg("commit.gpgSign=false")
|
2023-01-24 11:56:42 -06:00
|
|
|
.arg(cmd);
|
|
|
|
if let Some(repo_dir) = repo_dir.into() {
|
|
|
|
git_cmd.current_dir(repo_dir);
|
|
|
|
}
|
|
|
|
git_cmd
|
|
|
|
}
|
|
|
|
|
2021-07-07 04:14:20 -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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
pub(crate) fn spawn_and_wait(mut cmd: Command) {
|
2023-10-09 03:52:46 -05:00
|
|
|
let status = cmd.spawn().unwrap().wait().unwrap();
|
|
|
|
if !status.success() {
|
|
|
|
eprintln!("{cmd:?} exited with status {:?}", status);
|
2021-07-07 04:14:20 -05:00
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
// Based on the retry function in rust's src/ci/shared.sh
|
|
|
|
#[track_caller]
|
|
|
|
pub(crate) fn retry_spawn_and_wait(tries: u64, mut cmd: Command) {
|
|
|
|
for i in 1..tries + 1 {
|
|
|
|
if i != 1 {
|
2023-10-09 03:52:46 -05:00
|
|
|
eprintln!("Command failed. Attempt {i}/{tries}:");
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
|
|
|
if cmd.spawn().unwrap().wait().unwrap().success() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(i * 5));
|
|
|
|
}
|
2023-10-09 03:52:46 -05:00
|
|
|
eprintln!("The command has failed after {tries} attempts.");
|
2023-01-24 11:56:42 -06:00
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn remove_dir_if_exists(path: &Path) {
|
|
|
|
match fs::remove_dir_all(&path) {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
|
|
|
|
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 04:14:20 -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-24 11:40:58 -05:00
|
|
|
|
|
|
|
pub(crate) fn is_ci() -> bool {
|
2023-02-09 05:38:16 -06:00
|
|
|
env::var("CI").is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_ci_opt() -> bool {
|
|
|
|
env::var("CI_OPT").is_ok()
|
2022-08-24 11:40:58 -05:00
|
|
|
}
|
2023-06-15 12:56:01 -05:00
|
|
|
|
2023-07-22 08:32:34 -05:00
|
|
|
static IN_GROUP: AtomicBool = AtomicBool::new(false);
|
|
|
|
pub(crate) struct LogGroup {
|
|
|
|
is_gha: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LogGroup {
|
|
|
|
pub(crate) fn guard(name: &str) -> LogGroup {
|
|
|
|
let is_gha = env::var("GITHUB_ACTIONS").is_ok();
|
|
|
|
|
|
|
|
assert!(!IN_GROUP.swap(true, Ordering::SeqCst));
|
|
|
|
if is_gha {
|
|
|
|
eprintln!("::group::{name}");
|
|
|
|
}
|
|
|
|
|
|
|
|
LogGroup { is_gha }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LogGroup {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.is_gha {
|
|
|
|
eprintln!("::endgroup::");
|
|
|
|
}
|
|
|
|
IN_GROUP.store(false, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
pub(crate) fn maybe_incremental(cmd: &mut Command) {
|
|
|
|
if is_ci() || std::env::var("CARGO_BUILD_INCREMENTAL").map_or(false, |val| val == "false") {
|
|
|
|
// Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
|
|
|
|
cmd.env("CARGO_BUILD_INCREMENTAL", "false");
|
|
|
|
} else {
|
|
|
|
// Force incr comp even in release mode unless in CI or incremental builds are explicitly disabled
|
|
|
|
cmd.env("CARGO_BUILD_INCREMENTAL", "true");
|
|
|
|
}
|
|
|
|
}
|