From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../build_system/abi_cafe.rs | 20 +- .../build_system/build_backend.rs | 13 +- .../build_system/build_sysroot.rs | 98 ++-- .../rustc_codegen_cranelift/build_system/mod.rs | 91 +++- .../rustc_codegen_cranelift/build_system/path.rs | 70 +++ .../build_system/prepare.rs | 154 +++--- .../build_system/rustc_info.rs | 20 + .../rustc_codegen_cranelift/build_system/tests.rs | 553 +++++++++++---------- .../rustc_codegen_cranelift/build_system/utils.rs | 145 +++++- 9 files changed, 721 insertions(+), 443 deletions(-) create mode 100644 compiler/rustc_codegen_cranelift/build_system/path.rs (limited to 'compiler/rustc_codegen_cranelift/build_system') diff --git a/compiler/rustc_codegen_cranelift/build_system/abi_cafe.rs b/compiler/rustc_codegen_cranelift/build_system/abi_cafe.rs index fae5b2716..a081fdaa1 100644 --- a/compiler/rustc_codegen_cranelift/build_system/abi_cafe.rs +++ b/compiler/rustc_codegen_cranelift/build_system/abi_cafe.rs @@ -1,16 +1,21 @@ -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::path::Dirs; +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::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); + pub(crate) fn run( channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, + dirs: &Dirs, cg_clif_dylib: &Path, host_triple: &str, target_triple: &str, @@ -27,26 +32,25 @@ pub(crate) fn run( eprintln!("Building sysroot for abi-cafe"); build_sysroot::build_sysroot( + dirs, channel, sysroot_kind, - target_dir, cg_clif_dylib, host_triple, target_triple, ); 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(), dirs); 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(dirs)); spawn_and_wait(cmd); } diff --git a/compiler/rustc_codegen_cranelift/build_system/build_backend.rs b/compiler/rustc_codegen_cranelift/build_system/build_backend.rs index cda468bcf..fde8ef424 100644 --- a/compiler/rustc_codegen_cranelift/build_system/build_backend.rs +++ b/compiler/rustc_codegen_cranelift/build_system/build_backend.rs @@ -1,16 +1,19 @@ use std::env; use std::path::PathBuf; +use super::path::{Dirs, RelPath}; 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::new(&RelPath::SOURCE, "cg_clif"); pub(crate) fn build_backend( + dirs: &Dirs, 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(), dirs); cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode @@ -41,8 +44,8 @@ pub(crate) fn build_backend( eprintln!("[BUILD] rustc_codegen_cranelift"); super::utils::spawn_and_wait(cmd); - source_dir - .join("target") + CG_CLIF + .target_dir(dirs) .join(host_triple) .join(channel) .join(get_file_name("rustc_codegen_cranelift", "dylib")) diff --git a/compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs b/compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs index 856aecc49..cbbf09b9b 100644 --- a/compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs +++ b/compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs @@ -1,57 +1,60 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{self, Command}; +use super::path::{Dirs, RelPath}; 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; +static DIST_DIR: RelPath = RelPath::DIST; +static BIN_DIR: RelPath = RelPath::DIST.join("bin"); +static LIB_DIR: RelPath = RelPath::DIST.join("lib"); +static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib"); + pub(crate) fn build_sysroot( + dirs: &Dirs, channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, cg_clif_dylib_src: &Path, host_triple: &str, target_triple: &str, ) { eprintln!("[BUILD] sysroot {:?}", sysroot_kind); - if target_dir.exists() { - fs::remove_dir_all(target_dir).unwrap(); - } - fs::create_dir_all(target_dir.join("bin")).unwrap(); - fs::create_dir_all(target_dir.join("lib")).unwrap(); + DIST_DIR.ensure_fresh(dirs); + BIN_DIR.ensure_exists(dirs); + LIB_DIR.ensure_exists(dirs); // Copy the backend - let cg_clif_dylib_path = target_dir - .join(if cfg!(windows) { - // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the - // binaries. - "bin" - } else { - "lib" - }) - .join(get_file_name("rustc_codegen_cranelift", "dylib")); + let cg_clif_dylib_path = if cfg!(windows) { + // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the + // binaries. + BIN_DIR + } else { + LIB_DIR + } + .to_path(dirs) + .join(get_file_name("rustc_codegen_cranelift", "dylib")); try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path); // Build and copy rustc and cargo wrappers - for wrapper in ["rustc-clif", "cargo-clif"] { + for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] { let wrapper_name = get_wrapper_file_name(wrapper, "bin"); let mut build_cargo_wrapper_cmd = Command::new("rustc"); build_cargo_wrapper_cmd - .arg(PathBuf::from("scripts").join(format!("{wrapper}.rs"))) + .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs"))) .arg("-o") - .arg(target_dir.join(wrapper_name)) + .arg(DIST_DIR.to_path(dirs).join(wrapper_name)) .arg("-g"); spawn_and_wait(build_cargo_wrapper_cmd); } let default_sysroot = super::rustc_info::get_default_sysroot(); - let rustlib = target_dir.join("lib").join("rustlib"); - let host_rustlib_lib = rustlib.join(host_triple).join("lib"); - let target_rustlib_lib = rustlib.join(target_triple).join("lib"); + let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib"); + let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); @@ -112,24 +115,18 @@ pub(crate) fn build_sysroot( } } SysrootKind::Clif => { - build_clif_sysroot_for_triple( - channel, - target_dir, - host_triple, - &cg_clif_dylib_path, - None, - ); + build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None); if host_triple != target_triple { // When cross-compiling it is often necessary to manually pick the right linker - let linker = if target_triple == "aarch64-unknown-linux-gnu" { - Some("aarch64-linux-gnu-gcc") - } else { - None + let linker = match target_triple { + "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), + "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"), + _ => None, }; build_clif_sysroot_for_triple( + dirs, channel, - target_dir, target_triple, &cg_clif_dylib_path, linker, @@ -142,21 +139,26 @@ pub(crate) fn build_sysroot( let file = file.unwrap().path(); let filename = file.file_name().unwrap().to_str().unwrap(); if filename.contains("std-") && !filename.contains(".rlib") { - try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap())); + try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap())); } } } } } +// FIXME move to download/ or dist/ +pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version"); +pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src"); +static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot"); + fn build_clif_sysroot_for_triple( + dirs: &Dirs, channel: &str, - target_dir: &Path, triple: &str, cg_clif_dylib_path: &Path, linker: Option<&str>, ) { - match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) { + match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { eprintln!("Failed to get rustc version for patched sysroot source: {}", e); eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source"); @@ -174,7 +176,7 @@ fn build_clif_sysroot_for_triple( } } - let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel); + let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel); if !super::config::get_bool("keep_sysroot") { // Cleanup the deps dir, but keep build scripts and the incremental cache for faster @@ -185,27 +187,27 @@ 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())); + rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).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, dirs); + if channel == "release" { + build_cmd.arg("--release"); + } build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); // Copy all relevant files to the sysroot - for entry in - fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps")) - .unwrap() - { + for entry in fs::read_dir(build_dir.join("deps")).unwrap() { let entry = entry.unwrap(); if let Some(ext) = entry.path().extension() { if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" { @@ -216,7 +218,7 @@ fn build_clif_sysroot_for_triple( }; try_hard_link( entry.path(), - target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()), + RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()), ); } } diff --git a/compiler/rustc_codegen_cranelift/build_system/mod.rs b/compiler/rustc_codegen_cranelift/build_system/mod.rs index b25270d83..1afc9a55c 100644 --- a/compiler/rustc_codegen_cranelift/build_system/mod.rs +++ b/compiler/rustc_codegen_cranelift/build_system/mod.rs @@ -8,20 +8,37 @@ mod abi_cafe; mod build_backend; mod build_sysroot; mod config; +mod path; mod prepare; mod rustc_info; mod tests; mod utils; +const USAGE: &str = r#"The build system of cg_clif. + +USAGE: + ./y.rs prepare [--out-dir DIR] + ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + +OPTIONS: + --sysroot none|clif|llvm + Which sysroot libraries to use: + `none` will not include any standard library in the sysroot. + `clif` will build the standard library using Cranelift. + `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM. + + --out-dir DIR + Specify the directory in which the download, build and dist directories are stored. + By default this is the working directory. + + --no-unstable-features + fSome features are not yet ready for production usage. This option will disable these + features. This includes the JIT mode and inline assembly support. +"#; + fn usage() { - eprintln!("Usage:"); - eprintln!(" ./y.rs prepare"); - eprintln!( - " ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" - ); - eprintln!( - " ./y.rs test [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" - ); + eprintln!("{USAGE}"); } macro_rules! arg_error { @@ -34,6 +51,7 @@ macro_rules! arg_error { #[derive(PartialEq, Debug)] enum Command { + Prepare, Build, Test, } @@ -48,8 +66,6 @@ pub(crate) enum SysrootKind { pub fn main() { env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); - // The target dir is expected in the default location. Guard against the user changing it. - env::set_var("CARGO_TARGET_DIR", "target"); if is_ci() { // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway @@ -58,13 +74,7 @@ pub fn main() { let mut args = env::args().skip(1); let command = match args.next().as_deref() { - Some("prepare") => { - if args.next().is_some() { - arg_error!("./y.rs prepare doesn't expect arguments"); - } - prepare::prepare(); - process::exit(0); - } + Some("prepare") => Command::Prepare, Some("build") => Command::Build, Some("test") => Command::Test, Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), @@ -75,15 +85,15 @@ pub fn main() { } }; - let mut target_dir = PathBuf::from("build"); + let mut out_dir = PathBuf::from("."); let mut channel = "release"; let mut sysroot_kind = SysrootKind::Clif; let mut use_unstable_features = true; while let Some(arg) = args.next().as_deref() { match arg { - "--target-dir" => { - target_dir = PathBuf::from(args.next().unwrap_or_else(|| { - arg_error!("--target-dir requires argument"); + "--out-dir" => { + out_dir = PathBuf::from(args.next().unwrap_or_else(|| { + arg_error!("--out-dir requires argument"); })) } "--debug" => channel = "debug", @@ -101,7 +111,6 @@ pub fn main() { arg => arg_error!("Unexpected argument {}", arg), } } - target_dir = std::env::current_dir().unwrap().join(target_dir); let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { host_triple @@ -122,13 +131,43 @@ pub fn main() { host_triple.clone() }; - let cg_clif_dylib = build_backend::build_backend(channel, &host_triple, use_unstable_features); + // FIXME allow changing the location of these dirs using cli arguments + let current_dir = std::env::current_dir().unwrap(); + out_dir = current_dir.join(out_dir); + let dirs = path::Dirs { + source_dir: current_dir.clone(), + download_dir: out_dir.join("download"), + build_dir: out_dir.join("build"), + dist_dir: out_dir.join("dist"), + }; + + path::RelPath::BUILD.ensure_exists(&dirs); + + { + // Make sure we always explicitly specify the target dir + let target = + path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs); + env::set_var("CARGO_TARGET_DIR", &target); + let _ = std::fs::remove_file(&target); + std::fs::File::create(target).unwrap(); + } + + if command == Command::Prepare { + prepare::prepare(&dirs); + process::exit(0); + } + + let cg_clif_dylib = + build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features); match command { + Command::Prepare => { + // Handled above + } Command::Test => { tests::run_tests( + &dirs, channel, sysroot_kind, - &target_dir, &cg_clif_dylib, &host_triple, &target_triple, @@ -137,7 +176,7 @@ pub fn main() { abi_cafe::run( channel, sysroot_kind, - &target_dir, + &dirs, &cg_clif_dylib, &host_triple, &target_triple, @@ -145,9 +184,9 @@ pub fn main() { } Command::Build => { build_sysroot::build_sysroot( + &dirs, channel, sysroot_kind, - &target_dir, &cg_clif_dylib, &host_triple, &target_triple, diff --git a/compiler/rustc_codegen_cranelift/build_system/path.rs b/compiler/rustc_codegen_cranelift/build_system/path.rs new file mode 100644 index 000000000..e93981f1d --- /dev/null +++ b/compiler/rustc_codegen_cranelift/build_system/path.rs @@ -0,0 +1,70 @@ +use std::fs; +use std::path::PathBuf; + +#[derive(Debug, Clone)] +pub(crate) struct Dirs { + pub(crate) source_dir: PathBuf, + pub(crate) download_dir: PathBuf, + pub(crate) build_dir: PathBuf, + pub(crate) dist_dir: PathBuf, +} + +#[doc(hidden)] +#[derive(Debug, Copy, Clone)] +pub(crate) enum PathBase { + Source, + Download, + Build, + Dist, +} + +impl PathBase { + fn to_path(self, dirs: &Dirs) -> PathBuf { + match self { + PathBase::Source => dirs.source_dir.clone(), + PathBase::Download => dirs.download_dir.clone(), + PathBase::Build => dirs.build_dir.clone(), + PathBase::Dist => dirs.dist_dir.clone(), + } + } +} + +#[derive(Debug, Copy, Clone)] +pub(crate) enum RelPath { + Base(PathBase), + Join(&'static RelPath, &'static str), +} + +impl RelPath { + pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source); + pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download); + pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build); + pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist); + + pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts"); + pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); + pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches"); + + pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath { + RelPath::Join(self, suffix) + } + + pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf { + match self { + RelPath::Base(base) => base.to_path(dirs), + RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix), + } + } + + pub(crate) fn ensure_exists(&self, dirs: &Dirs) { + fs::create_dir_all(self.to_path(dirs)).unwrap(); + } + + pub(crate) fn ensure_fresh(&self, dirs: &Dirs) { + let path = self.to_path(dirs); + if path.exists() { + fs::remove_dir_all(&path).unwrap(); + } + fs::create_dir_all(path).unwrap(); + } +} diff --git a/compiler/rustc_codegen_cranelift/build_system/prepare.rs b/compiler/rustc_codegen_cranelift/build_system/prepare.rs index 3111f62f6..8ac67e8f9 100644 --- a/compiler/rustc_codegen_cranelift/build_system/prepare.rs +++ b/compiler/rustc_codegen_cranelift/build_system/prepare.rs @@ -1,92 +1,75 @@ -use std::env; use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; +use super::build_sysroot::{SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; +use super::path::{Dirs, RelPath}; 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", - "", -); - -pub(crate) fn prepare() { - if Path::new("download").exists() { - std::fs::remove_dir_all(Path::new("download")).unwrap(); +use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler}; + +pub(crate) fn prepare(dirs: &Dirs) { + if RelPath::DOWNLOAD.to_path(dirs).exists() { + std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); } - std::fs::create_dir_all(Path::new("download")).unwrap(); + std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); - prepare_sysroot(); + prepare_sysroot(dirs); // FIXME maybe install this only locally? eprintln!("[INSTALL] hyperfine"); - Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap(); + Command::new("cargo") + .arg("install") + .arg("hyperfine") + .env_remove("CARGO_TARGET_DIR") + .spawn() + .unwrap() + .wait() + .unwrap(); - ABI_CAFE.fetch(); - RAND.fetch(); - REGEX.fetch(); - PORTABLE_SIMD.fetch(); - SIMPLE_RAYTRACER.fetch(); + super::abi_cafe::ABI_CAFE_REPO.fetch(dirs); + super::tests::RAND_REPO.fetch(dirs); + super::tests::REGEX_REPO.fetch(dirs); + super::tests::PORTABLE_SIMD_REPO.fetch(dirs); + super::tests::SIMPLE_RAYTRACER_REPO.fetch(dirs); 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, dirs); spawn_and_wait(build_cmd); fs::copy( - SIMPLE_RAYTRACER - .source_dir() - .join("target") + super::tests::SIMPLE_RAYTRACER + .target_dir(dirs) + .join(&host_compiler.triple) .join("debug") .join(get_file_name("main", "bin")), - SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")), + RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")), ) .unwrap(); } -fn prepare_sysroot() { +fn prepare_sysroot(dirs: &Dirs) { let rustc_path = get_rustc_path(); let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust"); - let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src"); + let sysroot_src = SYSROOT_SRC; assert!(sysroot_src_orig.exists()); - if sysroot_src.exists() { - fs::remove_dir_all(&sysroot_src).unwrap(); - } - fs::create_dir_all(sysroot_src.join("library")).unwrap(); + sysroot_src.ensure_fresh(dirs); + fs::create_dir_all(sysroot_src.to_path(dirs).join("library")).unwrap(); eprintln!("[COPY] sysroot src"); - copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library")); + copy_dir_recursively( + &sysroot_src_orig.join("library"), + &sysroot_src.to_path(dirs).join("library"), + ); let rustc_version = get_rustc_version(); - fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap(); + fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap(); eprintln!("[GIT] init"); - let mut git_init_cmd = Command::new("git"); - git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src); - spawn_and_wait(git_init_cmd); - - init_git_repo(&sysroot_src); + init_git_repo(&sysroot_src.to_path(dirs)); - apply_patches("sysroot", &sysroot_src); + apply_patches(dirs, "sysroot", &sysroot_src.to_path(dirs)); } pub(crate) struct GitRepo { @@ -100,7 +83,7 @@ enum GitRepoUrl { } impl GitRepo { - const fn github( + pub(crate) const fn github( user: &'static str, repo: &'static str, rev: &'static str, @@ -109,21 +92,25 @@ impl GitRepo { GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name } } - pub(crate) fn source_dir(&self) -> PathBuf { + pub(crate) const fn source_dir(&self) -> RelPath { match self.url { - GitRepoUrl::Github { user: _, repo } => { - std::env::current_dir().unwrap().join("download").join(repo) - } + GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo), } } - fn fetch(&self) { + fn fetch(&self, dirs: &Dirs) { match self.url { GitRepoUrl::Github { user, repo } => { - clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev); + clone_repo_shallow_github( + dirs, + &self.source_dir().to_path(dirs), + user, + repo, + self.rev, + ); } } - apply_patches(self.patch_name, &self.source_dir()); + apply_patches(dirs, self.patch_name, &self.source_dir().to_path(dirs)); } } @@ -142,18 +129,16 @@ fn clone_repo(download_dir: &Path, repo: &str, rev: &str) { spawn_and_wait(checkout_cmd); } -fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) { +fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: &str, rev: &str) { if cfg!(windows) { // Older windows doesn't have tar or curl by default. Fall back to using git. clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev); return; } - let downloads_dir = std::env::current_dir().unwrap().join("download"); - let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev); - let archive_file = downloads_dir.join(format!("{}.tar.gz", rev)); - let archive_dir = downloads_dir.join(format!("{}-{}", repo, rev)); + let archive_file = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}.tar.gz", rev)); + let archive_dir = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}-{}", repo, rev)); eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url); @@ -169,7 +154,7 @@ fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: & // Unpack tar archive let mut unpack_cmd = Command::new("tar"); - unpack_cmd.arg("xf").arg(&archive_file).current_dir(downloads_dir); + unpack_cmd.arg("xf").arg(&archive_file).current_dir(RelPath::DOWNLOAD.to_path(dirs)); spawn_and_wait(unpack_cmd); // Rename unpacked dir to the expected name @@ -191,12 +176,21 @@ fn init_git_repo(repo_dir: &Path) { spawn_and_wait(git_add_cmd); let mut git_commit_cmd = Command::new("git"); - git_commit_cmd.arg("commit").arg("-m").arg("Initial commit").arg("-q").current_dir(repo_dir); + git_commit_cmd + .arg("-c") + .arg("user.name=Dummy") + .arg("-c") + .arg("user.email=dummy@example.com") + .arg("commit") + .arg("-m") + .arg("Initial commit") + .arg("-q") + .current_dir(repo_dir); spawn_and_wait(git_commit_cmd); } -fn get_patches(source_dir: &Path, crate_name: &str) -> Vec { - let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches")) +fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec { + let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs)) .unwrap() .map(|entry| entry.unwrap().path()) .filter(|path| path.extension() == Some(OsStr::new("patch"))) @@ -215,19 +209,27 @@ fn get_patches(source_dir: &Path, crate_name: &str) -> Vec { patches } -fn apply_patches(crate_name: &str, target_dir: &Path) { +fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) { if crate_name == "" { return; } - for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) { + for patch in get_patches(dirs, crate_name) { eprintln!( "[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch.file_name().unwrap() ); let mut apply_patch_cmd = Command::new("git"); - apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir); + apply_patch_cmd + .arg("-c") + .arg("user.name=Dummy") + .arg("-c") + .arg("user.email=dummy@example.com") + .arg("am") + .arg(patch) + .arg("-q") + .current_dir(target_dir); spawn_and_wait(apply_patch_cmd); } } diff --git a/compiler/rustc_codegen_cranelift/build_system/rustc_info.rs b/compiler/rustc_codegen_cranelift/build_system/rustc_info.rs index 3c08b6fa3..8e5ab688e 100644 --- a/compiler/rustc_codegen_cranelift/build_system/rustc_info.rs +++ b/compiler/rustc_codegen_cranelift/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/compiler/rustc_codegen_cranelift/build_system/tests.rs b/compiler/rustc_codegen_cranelift/build_system/tests.rs index a414b60f4..1c372736e 100644 --- a/compiler/rustc_codegen_cranelift/build_system/tests.rs +++ b/compiler/rustc_codegen_cranelift/build_system/tests.rs @@ -1,15 +1,20 @@ use super::build_sysroot; use super::config; -use super::prepare; -use super::rustc_info::get_wrapper_file_name; -use super::utils::{cargo_command, hyperfine_command, spawn_and_wait, spawn_and_wait_with_input}; -use build_system::SysrootKind; +use super::path::{Dirs, RelPath}; +use super::prepare::GitRepo; +use super::rustc_info::{get_cargo_path, get_wrapper_file_name}; +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; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::Command; +static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example"); + struct TestCase { config: &'static str, func: &'static dyn Fn(&TestRunner), @@ -30,7 +35,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 +44,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 +56,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 +70,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 +84,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 +99,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 +111,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 +121,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 +130,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 +141,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 +151,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 +160,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 +172,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 +183,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 +194,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 +205,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,205 +215,252 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("mod_bench", []); }), + TestCase::new("aot.issue-72793", &|runner| { + runner.run_rustc([ + "example/issue-72793.rs", + "--crate-type", + "bin", + "--target", + &runner.target_compiler.triple, + ]); + runner.run_out_command("issue-72793", []); + }), ]; +pub(crate) static RAND_REPO: GitRepo = + GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); + +static RAND: CargoProject = CargoProject::new(&RAND_REPO.source_dir(), "rand"); + +pub(crate) static REGEX_REPO: GitRepo = + GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); + +static REGEX: CargoProject = CargoProject::new(®EX_REPO.source_dir(), "regex"); + +pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( + "rust-lang", + "portable-simd", + "d5cd4a8112d958bd3a252327e0d069a6363249bd", + "portable-simd", +); + +static PORTABLE_SIMD: CargoProject = + CargoProject::new(&PORTABLE_SIMD_REPO.source_dir(), "portable_simd"); + +pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( + "ebobby", + "simple-raytracer", + "804a7a21b9e673a482797aa289a18ed480e4d813", + "", +); + +pub(crate) static SIMPLE_RAYTRACER: CargoProject = + CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); + +static LIBCORE_TESTS: CargoProject = + CargoProject::new(&RelPath::BUILD_SYSROOT.join("sysroot_src/library/core/tests"), "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", []); - - 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"]); - } - }); + spawn_and_wait(RAND.clean(&runner.target_compiler.cargo, &runner.dirs)); + + if runner.is_native { + eprintln!("[TEST] rust-random/rand"); + let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs); + test_cmd.arg("--workspace"); + spawn_and_wait(test_cmd); + } else { + eprintln!("[AOT] rust-random/rand"); + let mut build_cmd = RAND.build(&runner.target_compiler, &runner.dirs); + 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(); - - if runner.host_triple == runner.target_triple { - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); - let prepare = runner.cargo_command("clean", []); - - let llvm_build_cmd = cargo_command("cargo", "build", None, Path::new(".")); - - 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(prepare), llvm_build_cmd, clif_build_cmd); - - spawn_and_wait(bench_compile); - - 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 run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); + + if runner.is_native { + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); + let cargo_clif = RelPath::DIST + .to_path(&runner.dirs) + .join(get_wrapper_file_name("cargo-clif", "bin")); + let manifest_path = SIMPLE_RAYTRACER.manifest_path(&runner.dirs); + let target_dir = SIMPLE_RAYTRACER.target_dir(&runner.dirs); + + 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 bench_compile = + hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); + + spawn_and_wait(bench_compile); + + eprintln!("[BENCH RUN] ebobby/simple-raytracer"); + fs::copy( + target_dir.join("debug").join("main"), + RelPath::BUILD.to_path(&runner.dirs).join("raytracer_cg_clif"), + ) + .unwrap(); + + let mut bench_run = + hyperfine_command(0, run_runs, None, "./raytracer_cg_llvm", "./raytracer_cg_clif"); + bench_run.current_dir(RelPath::BUILD.to_path(&runner.dirs)); + spawn_and_wait(bench_run); + } else { + spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.target_compiler.cargo, &runner.dirs)); + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); + eprintln!("[COMPILE] ebobby/simple-raytracer"); + spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); + 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", []); - - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", []); - } else { - eprintln!("Cross-Compiling: Not running tests"); - runner.run_cargo("build", ["--tests"]); - } - }, - ); + spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs)); + + if runner.is_native { + spawn_and_wait(LIBCORE_TESTS.test(&runner.target_compiler, &runner.dirs)); + } else { + eprintln!("Cross-Compiling: Not running tests"); + let mut build_cmd = LIBCORE_TESTS.build(&runner.target_compiler, &runner.dirs); + 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", []); - - // 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"]); - 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); - - 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 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(); - - 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); + spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo, &runner.dirs)); + + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); + + let mut build_cmd = REGEX.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--example").arg("shootout-regex-dna"); + build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); + spawn_and_wait(build_cmd); + + if runner.is_native { + let mut run_cmd = REGEX.run(&runner.target_compiler, &runner.dirs); + run_cmd.arg("--example").arg("shootout-regex-dna"); + run_cmd.env("RUSTFLAGS", lint_rust_flags); + + let input = fs::read_to_string( + REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-input.txt"), + ) + .unwrap(); + let expected_path = + REGEX.source_dir(&runner.dirs).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_matches = expected.lines().eq(output.lines()); + if !output_matches { + let res_path = REGEX.source_dir(&runner.dirs).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); } - }); + } }), TestCase::new("test.regex", &|runner| { - runner.in_dir(prepare::REGEX.source_dir(), |runner| { - 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( - "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); - } - }); + spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo, &runner.dirs)); + + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); + + if runner.is_native { + let mut run_cmd = REGEX.test(&runner.target_compiler, &runner.dirs); + 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, &runner.dirs); + 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, &runner.dirs)); - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", ["-q"]); - } - }); + let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--all-targets"); + spawn_and_wait(build_cmd); + + if runner.is_native { + let mut test_cmd = PORTABLE_SIMD.test(&runner.target_compiler, &runner.dirs); + test_cmd.arg("-q"); + spawn_and_wait(test_cmd); + } }), ]; pub(crate) fn run_tests( + dirs: &Dirs, channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, cg_clif_dylib: &Path, host_triple: &str, target_triple: &str, ) { - let runner = TestRunner::new(host_triple.to_string(), target_triple.to_string()); + let runner = TestRunner::new(dirs.clone(), host_triple.to_string(), target_triple.to_string()); if config::get_bool("testsuite.no_sysroot") { build_sysroot::build_sysroot( + dirs, channel, SysrootKind::None, - &target_dir, cg_clif_dylib, &host_triple, &target_triple, ); - let _ = fs::remove_dir_all(Path::new("target").join("out")); + BUILD_EXAMPLE_OUT_DIR.ensure_fresh(dirs); runner.run_testsuite(NO_SYSROOT_SUITE); } else { eprintln!("[SKIP] no_sysroot tests"); @@ -419,9 +471,9 @@ pub(crate) fn run_tests( if run_base_sysroot || run_extended_sysroot { build_sysroot::build_sysroot( + dirs, channel, sysroot_kind, - &target_dir, cg_clif_dylib, &host_triple, &target_triple, @@ -442,40 +494,50 @@ 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, + dirs: Dirs, + host_compiler: Compiler, + target_compiler: Compiler, } impl TestRunner { - pub fn new(host_triple: String, target_triple: String) -> Self { - let root_dir = env::current_dir().unwrap(); - - let mut out_dir = root_dir.clone(); - out_dir.push("target"); - out_dir.push("out"); - + pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self { let is_native = host_triple == target_triple; 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 rustc_clif = + RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustc-clif", "bin")); + let rustdoc_clif = + RelPath::DIST.to_path(&dirs).join(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(), + ]; + } + "s390x-unknown-linux-gnu" => { + // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. + rustflags = format!("-Clinker=s390x-linux-gnu-gcc{}", rustflags); + 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. - run_wrapper = vec!["wine"]; + runner = vec!["wine".to_owned()]; } _ => { println!("Unknown non-native platform"); @@ -484,19 +546,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, + rustdoc: rustdoc_clif, + rustflags: rustflags.clone(), + rustdocflags: rustflags, + triple: target_triple, + runner, + }; + + Self { is_native, jit_supported, dirs, host_compiler, target_compiler } } pub fn run_testsuite(&self, tests: &[TestCase]) { @@ -516,29 +590,18 @@ 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(format!("crate={}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display())); cmd.arg("--out-dir"); - cmd.arg(format!("{}", self.out_dir.display())); + cmd.arg(format!("{}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display())); cmd.arg("-Cdebuginfo=2"); cmd.args(args); cmd @@ -559,15 +622,13 @@ 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({ - let mut out_path = self.out_dir.clone(); - out_path.push(name); - out_path.to_str().unwrap().to_string() - }); + full_cmd.push( + BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).join(name).to_str().unwrap().to_string(), + ); for arg in args.into_iter() { full_cmd.push(arg.to_string()); @@ -581,30 +642,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/compiler/rustc_codegen_cranelift/build_system/utils.rs b/compiler/rustc_codegen_cranelift/build_system/utils.rs index c627af4e6..2be70e8e4 100644 --- a/compiler/rustc_codegen_cranelift/build_system/utils.rs +++ b/compiler/rustc_codegen_cranelift/build_system/utils.rs @@ -1,35 +1,138 @@ 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::path::{Dirs, RelPath}; +use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path}; + +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![], + } + } +} + +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) + } - if let Some(triple) = triple { - cmd.arg("--target").arg(triple); + 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") + .arg(self.target_dir(dirs)); + + cmd + } + + 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); + 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 } - cmd + #[must_use] + pub(crate) fn fetch(&self, cargo: impl AsRef, dirs: &Dirs) -> Command { + let mut cmd = Command::new(cargo.as_ref()); + + cmd.arg("fetch").arg("--manifest-path").arg(self.manifest_path(dirs)); + + cmd + } + + #[must_use] + pub(crate) fn clean(&self, cargo: &Path, dirs: &Dirs) -> Command { + self.base_cmd("clean", cargo, dirs) + } + + #[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) + } } +#[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 +145,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 } -- cgit v1.2.3