4 use std::path::{Path, PathBuf};
5 use std::process::Command;
7 use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
8 use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};
10 pub(crate) const ABI_CAFE: GitRepo = GitRepo::github(
13 "4c6dc8c9c687e2b3a760ff2176ce236872b37212",
17 pub(crate) const RAND: GitRepo =
18 GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand");
20 pub(crate) const REGEX: GitRepo =
21 GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex");
23 pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github(
26 "d5cd4a8112d958bd3a252327e0d069a6363249bd",
30 pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github(
33 "804a7a21b9e673a482797aa289a18ed480e4d813",
37 pub(crate) fn prepare() {
38 if Path::new("download").exists() {
39 std::fs::remove_dir_all(Path::new("download")).unwrap();
41 std::fs::create_dir_all(Path::new("download")).unwrap();
45 // FIXME maybe install this only locally?
46 eprintln!("[INSTALL] hyperfine");
47 Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
52 PORTABLE_SIMD.fetch();
53 SIMPLE_RAYTRACER.fetch();
55 eprintln!("[LLVM BUILD] simple-raytracer");
56 let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir());
57 spawn_and_wait(build_cmd);
63 .join(get_file_name("main", "bin")),
64 SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")),
69 fn prepare_sysroot() {
70 let rustc_path = get_rustc_path();
71 let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
72 let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src");
74 assert!(sysroot_src_orig.exists());
76 if sysroot_src.exists() {
77 fs::remove_dir_all(&sysroot_src).unwrap();
79 fs::create_dir_all(sysroot_src.join("library")).unwrap();
80 eprintln!("[COPY] sysroot src");
81 copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library"));
83 let rustc_version = get_rustc_version();
84 fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap();
86 eprintln!("[GIT] init");
87 let mut git_init_cmd = Command::new("git");
88 git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src);
89 spawn_and_wait(git_init_cmd);
91 init_git_repo(&sysroot_src);
93 apply_patches("sysroot", &sysroot_src);
96 pub(crate) struct GitRepo {
99 patch_name: &'static str,
103 Github { user: &'static str, repo: &'static str },
111 patch_name: &'static str,
113 GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
116 pub(crate) fn source_dir(&self) -> PathBuf {
118 GitRepoUrl::Github { user: _, repo } => {
119 std::env::current_dir().unwrap().join("download").join(repo)
126 GitRepoUrl::Github { user, repo } => {
127 clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev);
130 apply_patches(self.patch_name, &self.source_dir());
135 fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
136 eprintln!("[CLONE] {}", repo);
137 // Ignore exit code as the repo may already have been checked out
138 Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap();
140 let mut clean_cmd = Command::new("git");
141 clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir);
142 spawn_and_wait(clean_cmd);
144 let mut checkout_cmd = Command::new("git");
145 checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir);
146 spawn_and_wait(checkout_cmd);
149 fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) {
151 // Older windows doesn't have tar or curl by default. Fall back to using git.
152 clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev);
156 let downloads_dir = std::env::current_dir().unwrap().join("download");
158 let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev);
159 let archive_file = downloads_dir.join(format!("{}.tar.gz", rev));
160 let archive_dir = downloads_dir.join(format!("{}-{}", repo, rev));
162 eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
164 // Remove previous results if they exists
165 let _ = std::fs::remove_file(&archive_file);
166 let _ = std::fs::remove_dir_all(&archive_dir);
167 let _ = std::fs::remove_dir_all(&download_dir);
169 // Download zip archive
170 let mut download_cmd = Command::new("curl");
171 download_cmd.arg("--location").arg("--output").arg(&archive_file).arg(archive_url);
172 spawn_and_wait(download_cmd);
174 // Unpack tar archive
175 let mut unpack_cmd = Command::new("tar");
176 unpack_cmd.arg("xf").arg(&archive_file).current_dir(downloads_dir);
177 spawn_and_wait(unpack_cmd);
179 // Rename unpacked dir to the expected name
180 std::fs::rename(archive_dir, &download_dir).unwrap();
182 init_git_repo(&download_dir);
185 std::fs::remove_file(archive_file).unwrap();
188 fn init_git_repo(repo_dir: &Path) {
189 let mut git_init_cmd = Command::new("git");
190 git_init_cmd.arg("init").arg("-q").current_dir(repo_dir);
191 spawn_and_wait(git_init_cmd);
193 let mut git_add_cmd = Command::new("git");
194 git_add_cmd.arg("add").arg(".").current_dir(repo_dir);
195 spawn_and_wait(git_add_cmd);
197 let mut git_commit_cmd = Command::new("git");
198 git_commit_cmd.arg("commit").arg("-m").arg("Initial commit").arg("-q").current_dir(repo_dir);
199 spawn_and_wait(git_commit_cmd);
202 fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
203 let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches"))
205 .map(|entry| entry.unwrap().path())
206 .filter(|path| path.extension() == Some(OsStr::new("patch")))
215 .starts_with(crate_name)
222 fn apply_patches(crate_name: &str, target_dir: &Path) {
223 if crate_name == "<none>" {
227 for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
229 "[PATCH] {:?} <- {:?}",
230 target_dir.file_name().unwrap(),
231 patch.file_name().unwrap()
233 let mut apply_patch_cmd = Command::new("git");
234 apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir);
235 spawn_and_wait(apply_patch_cmd);