Introduce GitRepo type

This will make it easier to move downloaded repos around
This commit is contained in:
bjorn3 2022-09-12 11:31:02 +00:00
parent e1a7791fcb
commit f1dc206c4f

View File

@ -7,49 +7,45 @@
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait}; 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",
"<none>",
);
pub(crate) fn prepare() { pub(crate) fn prepare() {
prepare_sysroot(); prepare_sysroot();
// FIXME maybe install this only locally?
eprintln!("[INSTALL] hyperfine"); eprintln!("[INSTALL] hyperfine");
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap(); Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
clone_repo_shallow_github( ABI_CAFE.fetch();
"abi-cafe", RAND.fetch();
"Gankra", REGEX.fetch();
"abi-cafe", PORTABLE_SIMD.fetch();
"4c6dc8c9c687e2b3a760ff2176ce236872b37212", SIMPLE_RAYTRACER.fetch();
);
apply_patches("abi-cafe", Path::new("abi-cafe"));
clone_repo_shallow_github(
"rand",
"rust-random",
"rand",
"0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
);
apply_patches("rand", Path::new("rand"));
clone_repo_shallow_github(
"regex",
"rust-lang",
"regex",
"341f207c1071f7290e3f228c710817c280c8dca1",
);
clone_repo_shallow_github(
"portable-simd",
"rust-lang",
"portable-simd",
"d5cd4a8112d958bd3a252327e0d069a6363249bd",
);
apply_patches("portable-simd", Path::new("portable-simd"));
clone_repo_shallow_github(
"simple-raytracer",
"ebobby",
"simple-raytracer",
"804a7a21b9e673a482797aa289a18ed480e4d813",
);
eprintln!("[LLVM BUILD] simple-raytracer"); eprintln!("[LLVM BUILD] simple-raytracer");
let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer")); let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer"));
@ -88,38 +84,74 @@ fn prepare_sysroot() {
apply_patches("sysroot", &sysroot_src); apply_patches("sysroot", &sysroot_src);
} }
pub(crate) struct GitRepo {
url: GitRepoUrl,
rev: &'static str,
patch_name: &'static str,
}
enum GitRepoUrl {
Github { user: &'static str, repo: &'static str },
}
impl GitRepo {
const fn github(
user: &'static str,
repo: &'static str,
rev: &'static str,
patch_name: &'static str,
) -> GitRepo {
GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
}
pub(crate) fn source_dir(&self) -> PathBuf {
match self.url {
GitRepoUrl::Github { user: _, repo } => PathBuf::from(format!("{}", repo)),
}
}
fn fetch(&self) {
match self.url {
GitRepoUrl::Github { user, repo } => {
clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev);
}
}
apply_patches(self.patch_name, &self.source_dir());
}
}
#[allow(dead_code)] #[allow(dead_code)]
fn clone_repo(target_dir: &str, repo: &str, rev: &str) { fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
eprintln!("[CLONE] {}", repo); eprintln!("[CLONE] {}", repo);
// Ignore exit code as the repo may already have been checked out // Ignore exit code as the repo may already have been checked out
Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap(); Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap();
let mut clean_cmd = Command::new("git"); let mut clean_cmd = Command::new("git");
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir); clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir);
spawn_and_wait(clean_cmd); spawn_and_wait(clean_cmd);
let mut checkout_cmd = Command::new("git"); let mut checkout_cmd = Command::new("git");
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir); checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir);
spawn_and_wait(checkout_cmd); spawn_and_wait(checkout_cmd);
} }
fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev: &str) { fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) {
if cfg!(windows) { if cfg!(windows) {
// Older windows doesn't have tar or curl by default. Fall back to using git. // Older windows doesn't have tar or curl by default. Fall back to using git.
clone_repo(target_dir, &format!("https://github.com/{}/{}.git", username, repo), rev); clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev);
return; return;
} }
let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", username, repo, rev); let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev);
let archive_file = format!("{}.tar.gz", rev); let archive_file = format!("{}.tar.gz", rev);
let archive_dir = format!("{}-{}", repo, rev); let archive_dir = format!("{}-{}", repo, rev);
eprintln!("[DOWNLOAD] {}/{} from {}", username, repo, archive_url); eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
// Remove previous results if they exists // Remove previous results if they exists
let _ = std::fs::remove_file(&archive_file); let _ = std::fs::remove_file(&archive_file);
let _ = std::fs::remove_dir_all(&archive_dir); let _ = std::fs::remove_dir_all(&archive_dir);
let _ = std::fs::remove_dir_all(target_dir); let _ = std::fs::remove_dir_all(&download_dir);
// Download zip archive // Download zip archive
let mut download_cmd = Command::new("curl"); let mut download_cmd = Command::new("curl");
@ -132,9 +164,9 @@ fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev:
spawn_and_wait(unpack_cmd); spawn_and_wait(unpack_cmd);
// Rename unpacked dir to the expected name // Rename unpacked dir to the expected name
std::fs::rename(archive_dir, target_dir).unwrap(); std::fs::rename(archive_dir, &download_dir).unwrap();
init_git_repo(Path::new(target_dir)); init_git_repo(&download_dir);
// Cleanup // Cleanup
std::fs::remove_file(archive_file).unwrap(); std::fs::remove_file(archive_file).unwrap();
@ -175,6 +207,10 @@ fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
} }
fn apply_patches(crate_name: &str, target_dir: &Path) { fn apply_patches(crate_name: &str, target_dir: &Path) {
if crate_name == "<none>" {
return;
}
for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) { for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
eprintln!( eprintln!(
"[PATCH] {:?} <- {:?}", "[PATCH] {:?} <- {:?}",