Introduce cargo_command helper

This commit is contained in:
bjorn3 2022-08-28 16:38:09 +00:00
parent c677cba06b
commit be305c2b1f
6 changed files with 81 additions and 71 deletions

View File

@ -1,10 +1,9 @@
use std::env;
use std::path::Path;
use std::process::Command;
use super::build_sysroot;
use super::config;
use super::utils::spawn_and_wait;
use super::utils::{cargo_command, spawn_and_wait};
use super::SysrootKind;
pub(crate) fn run(
@ -38,14 +37,11 @@ pub(crate) fn run(
eprintln!("Running abi-checker");
let mut abi_checker_path = env::current_dir().unwrap();
abi_checker_path.push("abi-checker");
env::set_current_dir(abi_checker_path.clone()).unwrap();
env::set_current_dir(&abi_checker_path.clone()).unwrap();
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
let mut cmd = Command::new("cargo");
cmd.arg("run");
cmd.arg("--target");
cmd.arg(target_triple);
let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_checker_path);
cmd.arg("--");
cmd.arg("--pairs");
cmd.args(pairs);

View File

@ -1,17 +1,16 @@
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::path::PathBuf;
use super::rustc_info::get_file_name;
use super::utils::is_ci;
use super::utils::{cargo_command, is_ci};
pub(crate) fn build_backend(
channel: &str,
host_triple: &str,
use_unstable_features: bool,
) -> PathBuf {
let mut cmd = Command::new("cargo");
cmd.arg("build").arg("--target").arg(host_triple);
let source_dir = std::env::current_dir().unwrap();
let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir);
cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode
@ -42,7 +41,8 @@ pub(crate) fn build_backend(
eprintln!("[BUILD] rustc_codegen_cranelift");
super::utils::spawn_and_wait(cmd);
Path::new("target")
source_dir
.join("target")
.join(host_triple)
.join(channel)
.join(get_file_name("rustc_codegen_cranelift", "dylib"))

View File

@ -3,7 +3,7 @@
use std::process::{self, Command};
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
use super::utils::{spawn_and_wait, try_hard_link};
use super::utils::{cargo_command, spawn_and_wait, try_hard_link};
use super::SysrootKind;
pub(crate) fn build_sysroot(
@ -185,8 +185,7 @@ fn build_clif_sysroot_for_triple(
}
// Build sysroot
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot"));
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
if channel == "release" {

View File

@ -5,7 +5,7 @@
use std::process::Command;
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
use super::utils::{copy_dir_recursively, spawn_and_wait};
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};
pub(crate) fn prepare() {
prepare_sysroot();
@ -52,8 +52,7 @@ pub(crate) fn prepare() {
);
eprintln!("[LLVM BUILD] simple-raytracer");
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer"));
spawn_and_wait(build_cmd);
fs::copy(
Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),

View File

@ -1,7 +1,7 @@
use super::build_sysroot;
use super::config;
use super::rustc_info::get_wrapper_file_name;
use super::utils::{spawn_and_wait, spawn_and_wait_with_input};
use super::utils::{cargo_command, spawn_and_wait, spawn_and_wait_with_input};
use build_system::SysrootKind;
use std::env;
use std::ffi::OsStr;
@ -218,20 +218,14 @@ const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
TestCase::new("test.rust-random/rand", &|runner| {
runner.in_dir(["rand"], |runner| {
runner.run_cargo(["clean"]);
runner.run_cargo("clean", []);
if runner.host_triple == runner.target_triple {
eprintln!("[TEST] rust-random/rand");
runner.run_cargo(["test", "--workspace"]);
runner.run_cargo("test", ["--workspace"]);
} else {
eprintln!("[AOT] rust-random/rand");
runner.run_cargo([
"build",
"--workspace",
"--target",
&runner.target_triple,
"--tests",
]);
runner.run_cargo("build", ["--workspace", "--tests"]);
}
});
}),
@ -247,11 +241,19 @@ const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
bench_compile.arg("--warmup");
bench_compile.arg("1");
bench_compile.arg("--prepare");
bench_compile.arg(format!("{:?}", runner.cargo_command(["clean"])));
bench_compile.arg(format!("{:?}", runner.cargo_command("clean", [])));
bench_compile.arg("cargo build");
bench_compile.arg(format!("{:?}", runner.cargo_command(["build"])));
let cargo_clif = runner
.root_dir
.clone()
.join("build")
.join(get_wrapper_file_name("cargo-clif", "bin"));
let mut clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new("."));
clif_build_cmd.env("RUSTFLAGS", &runner.rust_flags);
bench_compile.arg(format!("{:?}", clif_build_cmd));
spawn_and_wait(bench_compile);
eprintln!("[BENCH RUN] ebobby/simple-raytracer");
@ -265,51 +267,39 @@ const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
bench_run.arg(PathBuf::from("./raytracer_cg_clif"));
spawn_and_wait(bench_run);
} else {
runner.run_cargo(["clean"]);
runner.run_cargo("clean", []);
eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)");
eprintln!("[COMPILE] ebobby/simple-raytracer");
runner.run_cargo(["build", "--target", &runner.target_triple]);
runner.run_cargo("build", []);
eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)");
}
});
}),
TestCase::new("test.libcore", &|runner| {
runner.in_dir(["build_sysroot", "sysroot_src", "library", "core", "tests"], |runner| {
runner.run_cargo(["clean"]);
runner.run_cargo("clean", []);
if runner.host_triple == runner.target_triple {
runner.run_cargo(["test"]);
runner.run_cargo("test", []);
} else {
eprintln!("Cross-Compiling: Not running tests");
runner.run_cargo(["build", "--target", &runner.target_triple, "--tests"]);
runner.run_cargo("build", ["--tests"]);
}
});
}),
TestCase::new("test.regex-shootout-regex-dna", &|runner| {
runner.in_dir(["regex"], |runner| {
runner.run_cargo(["clean"]);
runner.run_cargo("clean", []);
// newer aho_corasick versions throw a deprecation warning
let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
let mut build_cmd = runner.cargo_command([
"build",
"--example",
"shootout-regex-dna",
"--target",
&runner.target_triple,
]);
let mut build_cmd = runner.cargo_command("build", ["--example", "shootout-regex-dna"]);
build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
spawn_and_wait(build_cmd);
if runner.host_triple == runner.target_triple {
let mut run_cmd = runner.cargo_command([
"run",
"--example",
"shootout-regex-dna",
"--target",
&runner.target_triple,
]);
let mut run_cmd = runner.cargo_command("run", ["--example", "shootout-regex-dna"]);
run_cmd.env("RUSTFLAGS", lint_rust_flags);
let input =
@ -350,28 +340,30 @@ const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
}),
TestCase::new("test.regex", &|runner| {
runner.in_dir(["regex"], |runner| {
runner.run_cargo(["clean"]);
runner.run_cargo("clean", []);
// newer aho_corasick versions throw a deprecation warning
let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
if runner.host_triple == runner.target_triple {
let mut run_cmd = runner.cargo_command([
let mut run_cmd = runner.cargo_command(
"test",
"--tests",
"--",
"--exclude-should-panic",
"--test-threads",
"1",
"-Zunstable-options",
"-q",
]);
[
"--tests",
"--",
"--exclude-should-panic",
"--test-threads",
"1",
"-Zunstable-options",
"-q",
],
);
run_cmd.env("RUSTFLAGS", lint_rust_flags);
spawn_and_wait(run_cmd);
} else {
eprintln!("Cross-Compiling: Not running tests");
let mut build_cmd =
runner.cargo_command(["build", "--tests", "--target", &runner.target_triple]);
runner.cargo_command("build", ["--tests", "--target", &runner.target_triple]);
build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
spawn_and_wait(build_cmd);
}
@ -379,11 +371,11 @@ const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
}),
TestCase::new("test.portable-simd", &|runner| {
runner.in_dir(["portable-simd"], |runner| {
runner.run_cargo(["clean"]);
runner.run_cargo(["build", "--all-targets", "--target", &runner.target_triple]);
runner.run_cargo("clean", []);
runner.run_cargo("build", ["--all-targets", "--target", &runner.target_triple]);
if runner.host_triple == runner.target_triple {
runner.run_cargo(["test", "-q"]);
runner.run_cargo("test", ["-q"]);
}
});
}),
@ -590,25 +582,29 @@ fn run_out_command<'a, I>(&self, name: &str, args: I)
spawn_and_wait(cmd);
}
fn cargo_command<I, S>(&self, args: I) -> Command
fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
I: IntoIterator<Item = &'a str>,
{
let mut cargo_clif = self.root_dir.clone();
cargo_clif.push("build");
cargo_clif.push(get_wrapper_file_name("cargo-clif", "bin"));
let mut cmd = Command::new(cargo_clif);
let mut cmd = cargo_command(
cargo_clif,
subcommand,
if subcommand == "clean" { None } else { Some(&self.target_triple) },
Path::new("."),
);
cmd.args(args);
cmd.env("RUSTFLAGS", &self.rust_flags);
cmd
}
fn run_cargo<'a, I>(&self, args: I)
fn run_cargo<'a, I>(&self, subcommand: &str, args: I)
where
I: IntoIterator<Item = &'a str>,
{
spawn_and_wait(self.cargo_command(args));
spawn_and_wait(self.cargo_command(subcommand, args));
}
}

View File

@ -4,6 +4,26 @@
use std::path::Path;
use std::process::{self, Command, Stdio};
pub(crate) fn cargo_command(
cargo: impl AsRef<Path>,
subcommand: &str,
triple: Option<&str>,
source_dir: &Path,
) -> Command {
let mut cmd = Command::new(cargo.as_ref());
cmd.arg(subcommand)
.arg("--manifest-path")
.arg(source_dir.join("Cargo.toml"))
.arg("--target-dir")
.arg(source_dir.join("target"));
if let Some(triple) = triple {
cmd.arg("--target").arg(triple);
}
cmd
}
#[track_caller]
pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
let src = src.as_ref();