Skip to content

Commit f1dc206

Browse files
committed
Introduce GitRepo type
This will make it easier to move downloaded repos around
1 parent e1a7791 commit f1dc206

File tree

1 file changed

+84
-48
lines changed

1 file changed

+84
-48
lines changed

build_system/prepare.rs

Lines changed: 84 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,45 @@ use std::process::Command;
77
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
88
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};
99

10+
pub(crate) const ABI_CAFE: GitRepo = GitRepo::github(
11+
"Gankra",
12+
"abi-cafe",
13+
"4c6dc8c9c687e2b3a760ff2176ce236872b37212",
14+
"abi-cafe",
15+
);
16+
17+
pub(crate) const RAND: GitRepo =
18+
GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand");
19+
20+
pub(crate) const REGEX: GitRepo =
21+
GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex");
22+
23+
pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github(
24+
"rust-lang",
25+
"portable-simd",
26+
"d5cd4a8112d958bd3a252327e0d069a6363249bd",
27+
"portable-simd",
28+
);
29+
30+
pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github(
31+
"ebobby",
32+
"simple-raytracer",
33+
"804a7a21b9e673a482797aa289a18ed480e4d813",
34+
"<none>",
35+
);
36+
1037
pub(crate) fn prepare() {
1138
prepare_sysroot();
1239

40+
// FIXME maybe install this only locally?
1341
eprintln!("[INSTALL] hyperfine");
1442
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
1543

16-
clone_repo_shallow_github(
17-
"abi-cafe",
18-
"Gankra",
19-
"abi-cafe",
20-
"4c6dc8c9c687e2b3a760ff2176ce236872b37212",
21-
);
22-
apply_patches("abi-cafe", Path::new("abi-cafe"));
23-
24-
clone_repo_shallow_github(
25-
"rand",
26-
"rust-random",
27-
"rand",
28-
"0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
29-
);
30-
apply_patches("rand", Path::new("rand"));
31-
32-
clone_repo_shallow_github(
33-
"regex",
34-
"rust-lang",
35-
"regex",
36-
"341f207c1071f7290e3f228c710817c280c8dca1",
37-
);
38-
39-
clone_repo_shallow_github(
40-
"portable-simd",
41-
"rust-lang",
42-
"portable-simd",
43-
"d5cd4a8112d958bd3a252327e0d069a6363249bd",
44-
);
45-
apply_patches("portable-simd", Path::new("portable-simd"));
46-
47-
clone_repo_shallow_github(
48-
"simple-raytracer",
49-
"ebobby",
50-
"simple-raytracer",
51-
"804a7a21b9e673a482797aa289a18ed480e4d813",
52-
);
44+
ABI_CAFE.fetch();
45+
RAND.fetch();
46+
REGEX.fetch();
47+
PORTABLE_SIMD.fetch();
48+
SIMPLE_RAYTRACER.fetch();
5349

5450
eprintln!("[LLVM BUILD] simple-raytracer");
5551
let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer"));
@@ -88,38 +84,74 @@ fn prepare_sysroot() {
8884
apply_patches("sysroot", &sysroot_src);
8985
}
9086

87+
pub(crate) struct GitRepo {
88+
url: GitRepoUrl,
89+
rev: &'static str,
90+
patch_name: &'static str,
91+
}
92+
93+
enum GitRepoUrl {
94+
Github { user: &'static str, repo: &'static str },
95+
}
96+
97+
impl GitRepo {
98+
const fn github(
99+
user: &'static str,
100+
repo: &'static str,
101+
rev: &'static str,
102+
patch_name: &'static str,
103+
) -> GitRepo {
104+
GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
105+
}
106+
107+
pub(crate) fn source_dir(&self) -> PathBuf {
108+
match self.url {
109+
GitRepoUrl::Github { user: _, repo } => PathBuf::from(format!("{}", repo)),
110+
}
111+
}
112+
113+
fn fetch(&self) {
114+
match self.url {
115+
GitRepoUrl::Github { user, repo } => {
116+
clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev);
117+
}
118+
}
119+
apply_patches(self.patch_name, &self.source_dir());
120+
}
121+
}
122+
91123
#[allow(dead_code)]
92-
fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
124+
fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
93125
eprintln!("[CLONE] {}", repo);
94126
// Ignore exit code as the repo may already have been checked out
95-
Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap();
127+
Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap();
96128

97129
let mut clean_cmd = Command::new("git");
98-
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir);
130+
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir);
99131
spawn_and_wait(clean_cmd);
100132

101133
let mut checkout_cmd = Command::new("git");
102-
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir);
134+
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir);
103135
spawn_and_wait(checkout_cmd);
104136
}
105137

106-
fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev: &str) {
138+
fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) {
107139
if cfg!(windows) {
108140
// Older windows doesn't have tar or curl by default. Fall back to using git.
109-
clone_repo(target_dir, &format!("https://github.com/{}/{}.git", username, repo), rev);
141+
clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev);
110142
return;
111143
}
112144

113-
let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", username, repo, rev);
145+
let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev);
114146
let archive_file = format!("{}.tar.gz", rev);
115147
let archive_dir = format!("{}-{}", repo, rev);
116148

117-
eprintln!("[DOWNLOAD] {}/{} from {}", username, repo, archive_url);
149+
eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
118150

119151
// Remove previous results if they exists
120152
let _ = std::fs::remove_file(&archive_file);
121153
let _ = std::fs::remove_dir_all(&archive_dir);
122-
let _ = std::fs::remove_dir_all(target_dir);
154+
let _ = std::fs::remove_dir_all(&download_dir);
123155

124156
// Download zip archive
125157
let mut download_cmd = Command::new("curl");
@@ -132,9 +164,9 @@ fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev:
132164
spawn_and_wait(unpack_cmd);
133165

134166
// Rename unpacked dir to the expected name
135-
std::fs::rename(archive_dir, target_dir).unwrap();
167+
std::fs::rename(archive_dir, &download_dir).unwrap();
136168

137-
init_git_repo(Path::new(target_dir));
169+
init_git_repo(&download_dir);
138170

139171
// Cleanup
140172
std::fs::remove_file(archive_file).unwrap();
@@ -175,6 +207,10 @@ fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
175207
}
176208

177209
fn apply_patches(crate_name: &str, target_dir: &Path) {
210+
if crate_name == "<none>" {
211+
return;
212+
}
213+
178214
for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
179215
eprintln!(
180216
"[PATCH] {:?} <- {:?}",

0 commit comments

Comments
 (0)