]> git.lizzy.rs Git - rust.git/blob - build_system/prepare.rs
Move all downloaded repos to the downloads/ dir
[rust.git] / build_system / prepare.rs
1 use std::env;
2 use std::ffi::OsStr;
3 use std::fs;
4 use std::path::{Path, PathBuf};
5 use std::process::Command;
6
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};
9
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
37 pub(crate) fn prepare() {
38     if Path::new("download").exists() {
39         std::fs::remove_dir_all(Path::new("download")).unwrap();
40     }
41     std::fs::create_dir_all(Path::new("download")).unwrap();
42
43     prepare_sysroot();
44
45     // FIXME maybe install this only locally?
46     eprintln!("[INSTALL] hyperfine");
47     Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
48
49     ABI_CAFE.fetch();
50     RAND.fetch();
51     REGEX.fetch();
52     PORTABLE_SIMD.fetch();
53     SIMPLE_RAYTRACER.fetch();
54
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);
58     fs::copy(
59         SIMPLE_RAYTRACER
60             .source_dir()
61             .join("target")
62             .join("debug")
63             .join(get_file_name("main", "bin")),
64         SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")),
65     )
66     .unwrap();
67 }
68
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");
73
74     assert!(sysroot_src_orig.exists());
75
76     if sysroot_src.exists() {
77         fs::remove_dir_all(&sysroot_src).unwrap();
78     }
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"));
82
83     let rustc_version = get_rustc_version();
84     fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap();
85
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);
90
91     init_git_repo(&sysroot_src);
92
93     apply_patches("sysroot", &sysroot_src);
94 }
95
96 pub(crate) struct GitRepo {
97     url: GitRepoUrl,
98     rev: &'static str,
99     patch_name: &'static str,
100 }
101
102 enum GitRepoUrl {
103     Github { user: &'static str, repo: &'static str },
104 }
105
106 impl GitRepo {
107     const fn github(
108         user: &'static str,
109         repo: &'static str,
110         rev: &'static str,
111         patch_name: &'static str,
112     ) -> GitRepo {
113         GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
114     }
115
116     pub(crate) fn source_dir(&self) -> PathBuf {
117         match self.url {
118             GitRepoUrl::Github { user: _, repo } => {
119                 std::env::current_dir().unwrap().join("download").join(repo)
120             }
121         }
122     }
123
124     fn fetch(&self) {
125         match self.url {
126             GitRepoUrl::Github { user, repo } => {
127                 clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev);
128             }
129         }
130         apply_patches(self.patch_name, &self.source_dir());
131     }
132 }
133
134 #[allow(dead_code)]
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();
139
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);
143
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);
147 }
148
149 fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) {
150     if cfg!(windows) {
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);
153         return;
154     }
155
156     let downloads_dir = std::env::current_dir().unwrap().join("download");
157
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));
161
162     eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
163
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);
168
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);
173
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);
178
179     // Rename unpacked dir to the expected name
180     std::fs::rename(archive_dir, &download_dir).unwrap();
181
182     init_git_repo(&download_dir);
183
184     // Cleanup
185     std::fs::remove_file(archive_file).unwrap();
186 }
187
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);
192
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);
196
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);
200 }
201
202 fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
203     let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches"))
204         .unwrap()
205         .map(|entry| entry.unwrap().path())
206         .filter(|path| path.extension() == Some(OsStr::new("patch")))
207         .filter(|path| {
208             path.file_name()
209                 .unwrap()
210                 .to_str()
211                 .unwrap()
212                 .split_once("-")
213                 .unwrap()
214                 .1
215                 .starts_with(crate_name)
216         })
217         .collect();
218     patches.sort();
219     patches
220 }
221
222 fn apply_patches(crate_name: &str, target_dir: &Path) {
223     if crate_name == "<none>" {
224         return;
225     }
226
227     for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
228         eprintln!(
229             "[PATCH] {:?} <- {:?}",
230             target_dir.file_name().unwrap(),
231             patch.file_name().unwrap()
232         );
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);
236     }
237 }