From cdae9bab588880339b260c46f468294a76e554b1 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:51:03 +0000 Subject: [PATCH] Introduce CargoProject type and use it where possible --- build_system/abi_cafe.rs | 15 +- build_system/build_backend.rs | 10 +- build_system/build_sysroot.rs | 13 +- build_system/prepare.rs | 50 ++-- build_system/rustc_info.rs | 20 ++ build_system/tests.rs | 464 ++++++++++++++++++---------------- build_system/utils.rs | 167 ++++++++++-- 7 files changed, 448 insertions(+), 291 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index fae5b271636..8949f9f7e7d 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -1,12 +1,16 @@ -use std::env; use std::path::Path; use super::build_sysroot; use super::config; -use super::prepare; -use super::utils::{cargo_command, spawn_and_wait}; +use super::prepare::GitRepo; +use super::utils::{spawn_and_wait, CargoProject, Compiler}; use super::SysrootKind; +pub(crate) static ABI_CAFE_REPO: GitRepo = + GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); + +static ABI_CAFE: CargoProject = CargoProject::git(&ABI_CAFE_REPO, "."); + pub(crate) fn run( channel: &str, sysroot_kind: SysrootKind, @@ -36,17 +40,16 @@ pub(crate) fn run( ); eprintln!("Running abi-cafe"); - let abi_cafe_path = prepare::ABI_CAFE.source_dir(); - env::set_current_dir(abi_cafe_path.clone()).unwrap(); let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"]; - let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_cafe_path); + let mut cmd = ABI_CAFE.run(&Compiler::host()); cmd.arg("--"); cmd.arg("--pairs"); cmd.args(pairs); cmd.arg("--add-rustc-codegen-backend"); cmd.arg(format!("cgclif:{}", cg_clif_dylib.display())); + cmd.current_dir(ABI_CAFE.source_dir()); spawn_and_wait(cmd); } diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index cda468bcfa2..48648830a9f 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -2,15 +2,16 @@ use std::env; use std::path::PathBuf; use super::rustc_info::get_file_name; -use super::utils::{cargo_command, is_ci}; +use super::utils::{is_ci, CargoProject, Compiler}; + +static CG_CLIF: CargoProject = CargoProject::local("."); pub(crate) fn build_backend( channel: &str, host_triple: &str, use_unstable_features: bool, ) -> PathBuf { - let source_dir = std::env::current_dir().unwrap(); - let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir); + let mut cmd = CG_CLIF.build(&Compiler::host()); cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode @@ -41,7 +42,8 @@ pub(crate) fn build_backend( eprintln!("[BUILD] rustc_codegen_cranelift"); super::utils::spawn_and_wait(cmd); - source_dir + CG_CLIF + .source_dir() .join("target") .join(host_triple) .join(channel) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index ba606278df5..731b94472ab 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command}; use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name}; -use super::utils::{cargo_command, spawn_and_wait, try_hard_link}; +use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; pub(crate) fn build_sysroot( @@ -149,6 +149,8 @@ pub(crate) fn build_sysroot( } } +static STANDARD_LIBRARY: CargoProject = CargoProject::local("build_sysroot"); + fn build_clif_sysroot_for_triple( channel: &str, target_dir: &Path, @@ -185,19 +187,22 @@ fn build_clif_sysroot_for_triple( } // 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())); rustflags.push_str(&format!(" --sysroot={}", target_dir.to_str().unwrap())); if channel == "release" { - build_cmd.arg("--release"); rustflags.push_str(" -Zmir-opt-level=3"); } if let Some(linker) = linker { use std::fmt::Write; write!(rustflags, " -Clinker={}", linker).unwrap(); } - build_cmd.env("RUSTFLAGS", rustflags); + let mut compiler = Compiler::with_triple(triple.to_owned()); + compiler.rustflags = rustflags; + let mut build_cmd = STANDARD_LIBRARY.build(&compiler); + if channel == "release" { + build_cmd.arg("--release"); + } build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 3111f62f6c2..b06d42af147 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -5,30 +5,7 @@ use std::path::{Path, PathBuf}; use std::process::Command; use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; -use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait}; - -pub(crate) const ABI_CAFE: GitRepo = - GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); - -pub(crate) const RAND: GitRepo = - GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); - -pub(crate) const REGEX: GitRepo = - GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); - -pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github( - "rust-lang", - "portable-simd", - "d5cd4a8112d958bd3a252327e0d069a6363249bd", - "portable-simd", -); - -pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github( - "ebobby", - "simple-raytracer", - "804a7a21b9e673a482797aa289a18ed480e4d813", - "", -); +use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler}; pub(crate) fn prepare() { if Path::new("download").exists() { @@ -42,22 +19,25 @@ pub(crate) fn prepare() { eprintln!("[INSTALL] hyperfine"); Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap(); - ABI_CAFE.fetch(); - RAND.fetch(); - REGEX.fetch(); - PORTABLE_SIMD.fetch(); - SIMPLE_RAYTRACER.fetch(); + super::abi_cafe::ABI_CAFE_REPO.fetch(); + super::tests::RAND_REPO.fetch(); + super::tests::REGEX_REPO.fetch(); + super::tests::PORTABLE_SIMD_REPO.fetch(); + super::tests::SIMPLE_RAYTRACER_REPO.fetch(); eprintln!("[LLVM BUILD] simple-raytracer"); - let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir()); + let host_compiler = Compiler::host(); + let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler); spawn_and_wait(build_cmd); fs::copy( - SIMPLE_RAYTRACER - .source_dir() - .join("target") + super::tests::SIMPLE_RAYTRACER + .target_dir() + .join(&host_compiler.triple) .join("debug") .join(get_file_name("main", "bin")), - SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")), + super::tests::SIMPLE_RAYTRACER_REPO + .source_dir() + .join(get_file_name("raytracer_cg_llvm", "bin")), ) .unwrap(); } @@ -100,7 +80,7 @@ enum GitRepoUrl { } impl GitRepo { - const fn github( + pub(crate) const fn github( user: &'static str, repo: &'static str, rev: &'static str, diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs index 3c08b6fa389..8e5ab688e13 100644 --- a/build_system/rustc_info.rs +++ b/build_system/rustc_info.rs @@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String { .to_owned() } +pub(crate) fn get_cargo_path() -> PathBuf { + let cargo_path = Command::new("rustup") + .stderr(Stdio::inherit()) + .args(&["which", "cargo"]) + .output() + .unwrap() + .stdout; + Path::new(String::from_utf8(cargo_path).unwrap().trim()).to_owned() +} + pub(crate) fn get_rustc_path() -> PathBuf { let rustc_path = Command::new("rustup") .stderr(Stdio::inherit()) @@ -33,6 +43,16 @@ pub(crate) fn get_rustc_path() -> PathBuf { Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned() } +pub(crate) fn get_rustdoc_path() -> PathBuf { + let rustc_path = Command::new("rustup") + .stderr(Stdio::inherit()) + .args(&["which", "rustdoc"]) + .output() + .unwrap() + .stdout; + Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned() +} + pub(crate) fn get_default_sysroot() -> PathBuf { let default_sysroot = Command::new("rustc") .stderr(Stdio::inherit()) diff --git a/build_system/tests.rs b/build_system/tests.rs index 248546b077e..b1c5c648839 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -1,8 +1,12 @@ +use crate::build_system::rustc_info::get_cargo_path; + use super::build_sysroot; use super::config; -use super::prepare; +use super::prepare::GitRepo; use super::rustc_info::get_wrapper_file_name; -use super::utils::{cargo_command, hyperfine_command, spawn_and_wait, spawn_and_wait_with_input}; +use super::utils::{ + hyperfine_command, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, +}; use super::SysrootKind; use std::env; use std::ffi::OsStr; @@ -30,7 +34,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib,dylib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("build.example", &|runner| { @@ -39,7 +43,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("jit.mini_core_hello_world", &|runner| { @@ -51,7 +55,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--cfg", "jit", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); @@ -65,7 +69,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--cfg", "jit", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); @@ -79,7 +83,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-g", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]); }), @@ -94,7 +98,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []); }), @@ -106,7 +110,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("issue_91827_extern_types", []); }), @@ -116,7 +120,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("aot.alloc_example", &|runner| { @@ -125,7 +129,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("alloc_example", []); }), @@ -136,7 +140,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cprefer-dynamic", "example/std_example.rs", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); eprintln!("[JIT-lazy] std_example"); @@ -146,7 +150,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cprefer-dynamic", "example/std_example.rs", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("aot.std_example", &|runner| { @@ -155,7 +159,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("std_example", ["arg"]); }), @@ -167,7 +171,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("dst_field_align", []); }), @@ -178,7 +182,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("subslice-patterns-const-eval", []); }), @@ -189,7 +193,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("track-caller-attribute", []); }), @@ -200,7 +204,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("float-minmax-pass", []); }), @@ -210,181 +214,218 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("mod_bench", []); }), ]; +pub(crate) static RAND_REPO: GitRepo = + GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); + +static RAND: CargoProject = CargoProject::git(&RAND_REPO, "."); + +pub(crate) static REGEX_REPO: GitRepo = + GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); + +static REGEX: CargoProject = CargoProject::git(®EX_REPO, "."); + +pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( + "rust-lang", + "portable-simd", + "d5cd4a8112d958bd3a252327e0d069a6363249bd", + "portable-simd", +); + +static PORTABLE_SIMD: CargoProject = CargoProject::git(&PORTABLE_SIMD_REPO, "."); + +pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( + "ebobby", + "simple-raytracer", + "804a7a21b9e673a482797aa289a18ed480e4d813", + "", +); + +pub(crate) static SIMPLE_RAYTRACER: CargoProject = CargoProject::git(&SIMPLE_RAYTRACER_REPO, "."); + +static LIBCORE_TESTS: CargoProject = + CargoProject::local("build_sysroot/sysroot_src/library/core/tests"); + const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ TestCase::new("test.rust-random/rand", &|runner| { - runner.in_dir(prepare::RAND.source_dir(), |runner| { - runner.run_cargo("clean", []); + spawn_and_wait(RAND.clean(&runner.target_compiler.cargo)); - if runner.host_triple == runner.target_triple { - eprintln!("[TEST] rust-random/rand"); - runner.run_cargo("test", ["--workspace"]); - } else { - eprintln!("[AOT] rust-random/rand"); - runner.run_cargo("build", ["--workspace", "--tests"]); - } - }); + if runner.is_native { + eprintln!("[TEST] rust-random/rand"); + let mut test_cmd = RAND.test(&runner.target_compiler); + test_cmd.arg("--workspace"); + spawn_and_wait(test_cmd); + } else { + eprintln!("[AOT] rust-random/rand"); + let mut build_cmd = RAND.build(&runner.target_compiler); + build_cmd.arg("--workspace").arg("--tests"); + spawn_and_wait(build_cmd); + } }), TestCase::new("bench.simple-raytracer", &|runner| { - runner.in_dir(prepare::SIMPLE_RAYTRACER.source_dir(), |runner| { - let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); + let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); - if runner.host_triple == runner.target_triple { - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); - let prepare = runner.cargo_command("clean", []); + if runner.is_native { + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); + let cargo_clif = env::current_dir() + .unwrap() + .join("build") + .join(get_wrapper_file_name("cargo-clif", "bin")); + let source_dir = SIMPLE_RAYTRACER.source_dir(); + let manifest_path = SIMPLE_RAYTRACER.manifest_path(); + let target_dir = SIMPLE_RAYTRACER.target_dir(); - let llvm_build_cmd = cargo_command("cargo", "build", None, Path::new(".")); + let clean_cmd = format!( + "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let llvm_build_cmd = format!( + "cargo build --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let clif_build_cmd = format!( + "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}", + cargo_clif = cargo_clif.display(), + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); - let cargo_clif = runner - .root_dir - .clone() - .join("build") - .join(get_wrapper_file_name("cargo-clif", "bin")); - let clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new(".")); + let bench_compile = + hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); - let bench_compile = - hyperfine_command(1, run_runs, Some(prepare), llvm_build_cmd, clif_build_cmd); + spawn_and_wait(bench_compile); - spawn_and_wait(bench_compile); + eprintln!("[BENCH RUN] ebobby/simple-raytracer"); + fs::copy(target_dir.join("debug").join("main"), source_dir.join("raytracer_cg_clif")) + .unwrap(); - eprintln!("[BENCH RUN] ebobby/simple-raytracer"); - fs::copy(PathBuf::from("./target/debug/main"), PathBuf::from("raytracer_cg_clif")) - .unwrap(); - - let bench_run = hyperfine_command( - 0, - run_runs, - None, - Command::new("./raytracer_cg_llvm"), - Command::new("./raytracer_cg_clif"), - ); - spawn_and_wait(bench_run); - } else { - runner.run_cargo("clean", []); - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); - eprintln!("[COMPILE] ebobby/simple-raytracer"); - runner.run_cargo("build", []); - eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)"); - } - }); + let mut bench_run = hyperfine_command( + 0, + run_runs, + None, + &source_dir.join("raytracer_cg_llvm").display().to_string(), + &source_dir.join("raytracer_cg_clif").display().to_string(), + ); + bench_run.current_dir(SIMPLE_RAYTRACER.source_dir()); + spawn_and_wait(bench_run); + } else { + spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.target_compiler.cargo)); + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); + eprintln!("[COMPILE] ebobby/simple-raytracer"); + spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler)); + eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)"); + } }), TestCase::new("test.libcore", &|runner| { - runner.in_dir( - std::env::current_dir() - .unwrap() - .join("build_sysroot") - .join("sysroot_src") - .join("library") - .join("core") - .join("tests"), - |runner| { - runner.run_cargo("clean", []); + spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo)); - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", []); - } else { - eprintln!("Cross-Compiling: Not running tests"); - runner.run_cargo("build", ["--tests"]); - } - }, - ); + if runner.is_native { + spawn_and_wait(LIBCORE_TESTS.test(&runner.target_compiler)); + } else { + eprintln!("Cross-Compiling: Not running tests"); + let mut build_cmd = LIBCORE_TESTS.build(&runner.target_compiler); + build_cmd.arg("--tests"); + spawn_and_wait(build_cmd); + } }), TestCase::new("test.regex-shootout-regex-dna", &|runner| { - runner.in_dir(prepare::REGEX.source_dir(), |runner| { - runner.run_cargo("clean", []); + spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo)); - // newer aho_corasick versions throw a deprecation warning - let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags); + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); - 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); + let mut build_cmd = REGEX.build(&runner.target_compiler); + build_cmd.arg("--example").arg("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"]); - run_cmd.env("RUSTFLAGS", lint_rust_flags); + if runner.is_native { + let mut run_cmd = REGEX.run(&runner.target_compiler); + run_cmd.arg("--example").arg("shootout-regex-dna"); + run_cmd.env("RUSTFLAGS", lint_rust_flags); - let input = - fs::read_to_string(PathBuf::from("examples/regexdna-input.txt")).unwrap(); - let expected_path = PathBuf::from("examples/regexdna-output.txt"); - let expected = fs::read_to_string(&expected_path).unwrap(); + let input = + fs::read_to_string(REGEX.source_dir().join("examples").join("regexdna-input.txt")) + .unwrap(); + let expected_path = REGEX.source_dir().join("examples").join("regexdna-output.txt"); + let expected = fs::read_to_string(&expected_path).unwrap(); - let output = spawn_and_wait_with_input(run_cmd, input); - // Make sure `[codegen mono items] start` doesn't poison the diff - let output = output - .lines() - .filter(|line| !line.contains("codegen mono items")) - .chain(Some("")) // This just adds the trailing newline - .collect::>() - .join("\r\n"); + let output = spawn_and_wait_with_input(run_cmd, input); + // Make sure `[codegen mono items] start` doesn't poison the diff + let output = output + .lines() + .filter(|line| !line.contains("codegen mono items")) + .chain(Some("")) // This just adds the trailing newline + .collect::>() + .join("\r\n"); - let output_matches = expected.lines().eq(output.lines()); - if !output_matches { - let res_path = PathBuf::from("res.txt"); - fs::write(&res_path, &output).unwrap(); + let output_matches = expected.lines().eq(output.lines()); + if !output_matches { + let res_path = REGEX.source_dir().join("res.txt"); + fs::write(&res_path, &output).unwrap(); - if cfg!(windows) { - println!("Output files don't match!"); - println!("Expected Output:\n{}", expected); - println!("Actual Output:\n{}", output); - } else { - let mut diff = Command::new("diff"); - diff.arg("-u"); - diff.arg(res_path); - diff.arg(expected_path); - spawn_and_wait(diff); - } - - std::process::exit(1); + if cfg!(windows) { + println!("Output files don't match!"); + println!("Expected Output:\n{}", expected); + println!("Actual Output:\n{}", output); + } else { + let mut diff = Command::new("diff"); + diff.arg("-u"); + diff.arg(res_path); + diff.arg(expected_path); + spawn_and_wait(diff); } + + std::process::exit(1); } - }); + } }), TestCase::new("test.regex", &|runner| { - runner.in_dir(prepare::REGEX.source_dir(), |runner| { - runner.run_cargo("clean", []); + spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo)); - // newer aho_corasick versions throw a deprecation warning - let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags); + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); - if runner.host_triple == runner.target_triple { - let mut run_cmd = runner.cargo_command( - "test", - [ - "--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]); - build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); - spawn_and_wait(build_cmd); - } - }); + if runner.is_native { + let mut run_cmd = REGEX.test(&runner.target_compiler); + run_cmd.args([ + "--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 = REGEX.build(&runner.target_compiler); + build_cmd.arg("--tests"); + build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); + spawn_and_wait(build_cmd); + } }), TestCase::new("test.portable-simd", &|runner| { - runner.in_dir(prepare::PORTABLE_SIMD.source_dir(), |runner| { - runner.run_cargo("clean", []); - runner.run_cargo("build", ["--all-targets", "--target", &runner.target_triple]); + spawn_and_wait(PORTABLE_SIMD.clean(&runner.host_compiler.cargo)); - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", ["-q"]); - } - }); + let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler); + build_cmd.arg("--all-targets"); + spawn_and_wait(build_cmd); + + if runner.is_native { + let mut test_cmd = PORTABLE_SIMD.test(&runner.target_compiler); + test_cmd.arg("-q"); + spawn_and_wait(test_cmd); + } }), ]; @@ -442,13 +483,11 @@ pub(crate) fn run_tests( } struct TestRunner { - root_dir: PathBuf, out_dir: PathBuf, + is_native: bool, jit_supported: bool, - rust_flags: String, - run_wrapper: Vec, - host_triple: String, - target_triple: String, + host_compiler: Compiler, + target_compiler: Compiler, } impl TestRunner { @@ -463,19 +502,31 @@ impl TestRunner { let jit_supported = target_triple.contains("x86_64") && is_native && !host_triple.contains("windows"); - let mut rust_flags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); - let mut run_wrapper = Vec::new(); + let mut rustc_clif = root_dir.clone(); + rustc_clif.push("build"); + rustc_clif.push(get_wrapper_file_name("rustc-clif", "bin")); + + let mut rustdoc_clif = root_dir.clone(); + rustdoc_clif.push("build"); + rustdoc_clif.push(get_wrapper_file_name("rustdoc-clif", "bin")); + + let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); + let mut runner = vec![]; if !is_native { match target_triple.as_str() { "aarch64-unknown-linux-gnu" => { // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - rust_flags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rust_flags); - run_wrapper = vec!["qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"]; + rustflags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rustflags); + runner = vec![ + "qemu-aarch64".to_owned(), + "-L".to_owned(), + "/usr/aarch64-linux-gnu".to_owned(), + ]; } "x86_64-pc-windows-gnu" => { // We are cross-compiling for Windows. Run tests in wine. - run_wrapper = vec!["wine"]; + runner = vec!["wine".to_owned()]; } _ => { println!("Unknown non-native platform"); @@ -484,19 +535,31 @@ impl TestRunner { } // FIXME fix `#[linkage = "extern_weak"]` without this - if host_triple.contains("darwin") { - rust_flags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rust_flags); + if target_triple.contains("darwin") { + rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags); } - Self { - root_dir, - out_dir, - jit_supported, - rust_flags, - run_wrapper: run_wrapper.iter().map(|s| s.to_string()).collect(), - host_triple, - target_triple, - } + let host_compiler = Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif.clone(), + rustdoc: rustdoc_clif.clone(), + rustflags: String::new(), + rustdocflags: String::new(), + triple: host_triple, + runner: vec![], + }; + + let target_compiler = Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif.clone(), + rustdoc: rustdoc_clif.clone(), + rustflags: rustflags.clone(), + rustdocflags: rustflags, + triple: target_triple, + runner, + }; + + Self { out_dir, is_native, jit_supported, host_compiler, target_compiler } } pub fn run_testsuite(&self, tests: &[TestCase]) { @@ -516,25 +579,14 @@ impl TestRunner { } } - fn in_dir(&self, new: impl AsRef, callback: impl FnOnce(&TestRunner)) { - let current = env::current_dir().unwrap(); - - env::set_current_dir(new).unwrap(); - callback(self); - env::set_current_dir(current).unwrap(); - } - + #[must_use] fn rustc_command(&self, args: I) -> Command where I: IntoIterator, S: AsRef, { - let mut rustc_clif = self.root_dir.clone(); - rustc_clif.push("build"); - rustc_clif.push(get_wrapper_file_name("rustc-clif", "bin")); - - let mut cmd = Command::new(rustc_clif); - cmd.args(self.rust_flags.split_whitespace()); + let mut cmd = Command::new(&self.target_compiler.rustc); + cmd.args(self.target_compiler.rustflags.split_whitespace()); cmd.arg("-L"); cmd.arg(format!("crate={}", self.out_dir.display())); cmd.arg("--out-dir"); @@ -559,8 +611,8 @@ impl TestRunner { let mut full_cmd = vec![]; // Prepend the RUN_WRAPPER's - if !self.run_wrapper.is_empty() { - full_cmd.extend(self.run_wrapper.iter().cloned()); + if !self.target_compiler.runner.is_empty() { + full_cmd.extend(self.target_compiler.runner.iter().cloned()); } full_cmd.push({ @@ -581,30 +633,4 @@ impl TestRunner { spawn_and_wait(cmd); } - - fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command - where - I: IntoIterator, - { - 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 = 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, subcommand: &str, args: I) - where - I: IntoIterator, - { - spawn_and_wait(self.cargo_command(subcommand, args)); - } } diff --git a/build_system/utils.rs b/build_system/utils.rs index 48da64906e2..75869f38118 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -1,35 +1,156 @@ use std::env; use std::fs; use std::io::Write; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; -pub(crate) fn cargo_command( - cargo: impl AsRef, - 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")); +use super::prepare::GitRepo; +use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path}; - if let Some(triple) = triple { - cmd.arg("--target").arg(triple); - } - - cmd +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, } +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![], + } + } +} + +enum CargoProjectSource { + Local, + GitRepo(&'static GitRepo), +} + +pub(crate) struct CargoProject { + source: CargoProjectSource, + path: &'static str, +} + +impl CargoProject { + pub(crate) const fn local(path: &'static str) -> CargoProject { + CargoProject { source: CargoProjectSource::Local, path } + } + + pub(crate) const fn git(git_repo: &'static GitRepo, path: &'static str) -> CargoProject { + CargoProject { source: CargoProjectSource::GitRepo(git_repo), path } + } + + pub(crate) fn source_dir(&self) -> PathBuf { + match self.source { + CargoProjectSource::Local => std::env::current_dir().unwrap(), + CargoProjectSource::GitRepo(git_repo) => git_repo.source_dir(), + } + .join(self.path) + } + + pub(crate) fn manifest_path(&self) -> PathBuf { + self.source_dir().join("Cargo.toml") + } + + pub(crate) fn target_dir(&self) -> PathBuf { + match self.source { + CargoProjectSource::Local => std::env::current_dir().unwrap(), + CargoProjectSource::GitRepo(git_repo) => git_repo.source_dir(), + } + .join(self.path) + .join("target") + } + + fn base_cmd(&self, command: &str, cargo: &Path) -> Command { + let mut cmd = Command::new(cargo); + + cmd.arg(command) + .arg("--manifest-path") + .arg(self.manifest_path()) + .arg("--target-dir") + .arg(self.target_dir()); + + cmd + } + + fn build_cmd(&self, command: &str, compiler: &Compiler) -> Command { + let mut cmd = self.base_cmd(command, &compiler.cargo); + + 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] + pub(crate) fn fetch(&self, cargo: impl AsRef) -> Command { + let mut cmd = Command::new(cargo.as_ref()); + + cmd.arg("fetch").arg("--manifest-path").arg(self.manifest_path()); + + cmd + } + + #[must_use] + pub(crate) fn clean(&self, cargo: &Path) -> Command { + self.base_cmd("clean", cargo) + } + + #[must_use] + pub(crate) fn build(&self, compiler: &Compiler) -> Command { + self.build_cmd("build", compiler) + } + + #[must_use] + pub(crate) fn test(&self, compiler: &Compiler) -> Command { + self.build_cmd("test", compiler) + } + + #[must_use] + pub(crate) fn run(&self, compiler: &Compiler) -> Command { + self.build_cmd("run", compiler) + } +} + +#[must_use] pub(crate) fn hyperfine_command( warmup: u64, runs: u64, - prepare: Option, - a: Command, - b: Command, + prepare: Option<&str>, + a: &str, + b: &str, ) -> Command { let mut bench = Command::new("hyperfine"); @@ -42,10 +163,10 @@ pub(crate) fn hyperfine_command( } if let Some(prepare) = prepare { - bench.arg("--prepare").arg(format!("{:?}", prepare)); + bench.arg("--prepare").arg(prepare); } - bench.arg(format!("{:?}", a)).arg(format!("{:?}", b)); + bench.arg(a).arg(b); bench }