]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_codegen_cranelift/build_system/prepare.rs
Rollup merge of #103383 - compiler-errors:tait-scope, r=oli-obk
[rust.git] / compiler / rustc_codegen_cranelift / build_system / prepare.rs
index d23b7f00dcf16da744be926925043d5716cad145..3111f62f6c2156eaec0ef00a9a7213acf8950f88 100644 (file)
@@ -1,64 +1,63 @@
 use std::env;
 use std::ffi::OsStr;
-use std::ffi::OsString;
 use std::fs;
-use std::path::Path;
+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::{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() {
+    if Path::new("download").exists() {
+        std::fs::remove_dir_all(Path::new("download")).unwrap();
+    }
+    std::fs::create_dir_all(Path::new("download")).unwrap();
+
     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-checker",
-        "Gankra",
-        "abi-checker",
-        "a2232d45f202846f5c02203c9f27355360f9a2ff",
-    );
-    apply_patches("abi-checker", Path::new("abi-checker"));
-
-    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",
-        "b8d6b6844602f80af79cd96401339ec594d472d8",
-    );
-    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 mut build_cmd = Command::new("cargo");
-    build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
+    let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir());
     spawn_and_wait(build_cmd);
     fs::copy(
-        Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
-        Path::new("simple-raytracer").join(get_file_name("raytracer_cg_llvm", "bin")),
+        SIMPLE_RAYTRACER
+            .source_dir()
+            .join("target")
+            .join("debug")
+            .join(get_file_name("main", "bin")),
+        SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")),
     )
     .unwrap();
 }
@@ -90,38 +89,78 @@ 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 } => {
+                std::env::current_dir().unwrap().join("download").join(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_file = format!("{}.tar.gz", rev);
-    let archive_dir = format!("{}-{}", repo, rev);
+    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));
 
-    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");
@@ -130,13 +169,13 @@ fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev:
 
     // Unpack tar archive
     let mut unpack_cmd = Command::new("tar");
-    unpack_cmd.arg("xf").arg(&archive_file);
+    unpack_cmd.arg("xf").arg(&archive_file).current_dir(downloads_dir);
     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();
@@ -156,14 +195,20 @@ fn init_git_repo(repo_dir: &Path) {
     spawn_and_wait(git_commit_cmd);
 }
 
-fn get_patches(crate_name: &str) -> Vec<OsString> {
-    let mut patches: Vec<_> = fs::read_dir("patches")
+fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
+    let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches"))
         .unwrap()
         .map(|entry| entry.unwrap().path())
         .filter(|path| path.extension() == Some(OsStr::new("patch")))
-        .map(|path| path.file_name().unwrap().to_owned())
-        .filter(|file_name| {
-            file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
+        .filter(|path| {
+            path.file_name()
+                .unwrap()
+                .to_str()
+                .unwrap()
+                .split_once("-")
+                .unwrap()
+                .1
+                .starts_with(crate_name)
         })
         .collect();
     patches.sort();
@@ -171,11 +216,18 @@ fn get_patches(crate_name: &str) -> Vec<OsString> {
 }
 
 fn apply_patches(crate_name: &str, target_dir: &Path) {
-    for patch in get_patches(crate_name) {
-        eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
-        let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
+    if crate_name == "<none>" {
+        return;
+    }
+
+    for patch in get_patches(&std::env::current_dir().unwrap(), 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).arg("-q").current_dir(target_dir);
+        apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir);
         spawn_and_wait(apply_patch_cmd);
     }
 }