about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2022-09-12 11:31:02 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2022-10-13 13:19:01 +0000
commitf1dc206c4f87ba637200dc65d6c125534dcf13eb (patch)
treef3d52c7c1706e75a2dc3b4a9d16b07ae865c1a29
parente1a7791fcb8f5f0f257f3c95241e723ea6abbdbc (diff)
downloadrust-f1dc206c4f87ba637200dc65d6c125534dcf13eb.tar.gz
rust-f1dc206c4f87ba637200dc65d6c125534dcf13eb.zip
Introduce GitRepo type
This will make it easier to move downloaded repos around
-rw-r--r--build_system/prepare.rs132
1 files changed, 84 insertions, 48 deletions
diff --git a/build_system/prepare.rs b/build_system/prepare.rs
index 3ad9f87e318..5c68d7ac972 100644
--- a/build_system/prepare.rs
+++ b/build_system/prepare.rs
@@ -7,49 +7,45 @@ 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",
+    "<none>",
+);
+
 pub(crate) fn prepare() {
     prepare_sysroot();
 
+    // FIXME maybe install this only locally?
     eprintln!("[INSTALL] hyperfine");
     Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
 
-    clone_repo_shallow_github(
-        "abi-cafe",
-        "Gankra",
-        "abi-cafe",
-        "4c6dc8c9c687e2b3a760ff2176ce236872b37212",
-    );
-    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",
-    );
+    ABI_CAFE.fetch();
+    RAND.fetch();
+    REGEX.fetch();
+    PORTABLE_SIMD.fetch();
+    SIMPLE_RAYTRACER.fetch();
 
     eprintln!("[LLVM BUILD] 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);
 }
 
+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)]
-fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
+fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
     eprintln!("[CLONE] {}", repo);
     // 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");
-    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);
 
     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);
 }
 
-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) {
         // 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;
     }
 
-    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_dir = format!("{}-{}", repo, rev);
 
-    eprintln!("[DOWNLOAD] {}/{} from {}", username, repo, archive_url);
+    eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
 
     // Remove previous results if they exists
     let _ = std::fs::remove_file(&archive_file);
     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
     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);
 
     // 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
     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) {
+    if crate_name == "<none>" {
+        return;
+    }
+
     for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
         eprintln!(
             "[PATCH] {:?} <- {:?}",