From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../build_system/prepare.rs | 154 +++++++++++---------- 1 file changed, 78 insertions(+), 76 deletions(-) (limited to 'compiler/rustc_codegen_cranelift/build_system/prepare.rs') 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); } } -- cgit v1.2.3