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

View File

@ -1,17 +1,16 @@
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::process::Command;
use super::rustc_info::get_file_name; use super::rustc_info::get_file_name;
use super::utils::is_ci; use super::utils::{cargo_command, is_ci};
pub(crate) fn build_backend( pub(crate) fn build_backend(
channel: &str, channel: &str,
host_triple: &str, host_triple: &str,
use_unstable_features: bool, use_unstable_features: bool,
) -> PathBuf { ) -> PathBuf {
let mut cmd = Command::new("cargo"); let source_dir = std::env::current_dir().unwrap();
cmd.arg("build").arg("--target").arg(host_triple); 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 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"); eprintln!("[BUILD] rustc_codegen_cranelift");
super::utils::spawn_and_wait(cmd); super::utils::spawn_and_wait(cmd);
Path::new("target") source_dir
.join("target")
.join(host_triple) .join(host_triple)
.join(channel) .join(channel)
.join(get_file_name("rustc_codegen_cranelift", "dylib")) .join(get_file_name("rustc_codegen_cranelift", "dylib"))

View File

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

View File

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

View File

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