3 use std::path::{Path, PathBuf};
4 use std::process::Command;
6 use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
7 use super::path::{Dirs, RelPath};
8 use super::rustc_info::{get_default_sysroot, get_rustc_version};
9 use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
11 pub(crate) fn prepare(dirs: &Dirs) {
12 RelPath::DOWNLOAD.ensure_fresh(dirs);
14 spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", "rustc", dirs));
16 prepare_sysroot(dirs);
17 spawn_and_wait(super::build_sysroot::STANDARD_LIBRARY.fetch("cargo", "rustc", dirs));
18 spawn_and_wait(super::tests::LIBCORE_TESTS.fetch("cargo", "rustc", dirs));
20 super::tests::RAND_REPO.fetch(dirs);
21 spawn_and_wait(super::tests::RAND.fetch("cargo", "rustc", dirs));
22 super::tests::REGEX_REPO.fetch(dirs);
23 spawn_and_wait(super::tests::REGEX.fetch("cargo", "rustc", dirs));
24 super::tests::PORTABLE_SIMD_REPO.fetch(dirs);
25 spawn_and_wait(super::tests::PORTABLE_SIMD.fetch("cargo", "rustc", dirs));
28 fn prepare_sysroot(dirs: &Dirs) {
29 let sysroot_src_orig = get_default_sysroot(Path::new("rustc")).join("lib/rustlib/src/rust");
30 assert!(sysroot_src_orig.exists());
32 eprintln!("[COPY] sysroot src");
34 // FIXME ensure builds error out or update the copy if any of the files copied here change
35 BUILD_SYSROOT.ensure_fresh(dirs);
36 copy_dir_recursively(&ORIG_BUILD_SYSROOT.to_path(dirs), &BUILD_SYSROOT.to_path(dirs));
38 fs::create_dir_all(SYSROOT_SRC.to_path(dirs).join("library")).unwrap();
40 &sysroot_src_orig.join("library"),
41 &SYSROOT_SRC.to_path(dirs).join("library"),
44 let rustc_version = get_rustc_version(Path::new("rustc"));
45 fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap();
47 eprintln!("[GIT] init");
48 init_git_repo(&SYSROOT_SRC.to_path(dirs));
50 apply_patches(dirs, "sysroot", &SYSROOT_SRC.to_path(dirs));
53 pub(crate) struct GitRepo {
56 patch_name: &'static str,
60 Github { user: &'static str, repo: &'static str },
64 pub(crate) const fn github(
68 patch_name: &'static str,
70 GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
73 pub(crate) const fn source_dir(&self) -> RelPath {
75 GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo),
79 pub(crate) fn fetch(&self, dirs: &Dirs) {
81 GitRepoUrl::Github { user, repo } => {
82 clone_repo_shallow_github(
84 &self.source_dir().to_path(dirs),
91 apply_patches(dirs, self.patch_name, &self.source_dir().to_path(dirs));
96 fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
97 eprintln!("[CLONE] {}", repo);
98 // Ignore exit code as the repo may already have been checked out
99 git_command(None, "clone").arg(repo).arg(download_dir).spawn().unwrap().wait().unwrap();
101 let mut clean_cmd = git_command(download_dir, "checkout");
102 clean_cmd.arg("--").arg(".");
103 spawn_and_wait(clean_cmd);
105 let mut checkout_cmd = git_command(download_dir, "checkout");
106 checkout_cmd.arg("-q").arg(rev);
107 spawn_and_wait(checkout_cmd);
110 fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: &str, rev: &str) {
112 // Older windows doesn't have tar or curl by default. Fall back to using git.
113 clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev);
117 let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev);
118 let archive_file = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}.tar.gz", rev));
119 let archive_dir = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}-{}", repo, rev));
121 eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
123 // Remove previous results if they exists
124 let _ = std::fs::remove_file(&archive_file);
125 let _ = std::fs::remove_dir_all(&archive_dir);
126 let _ = std::fs::remove_dir_all(&download_dir);
128 // Download zip archive
129 let mut download_cmd = Command::new("curl");
137 .arg("--connect-timeout")
139 .arg("--continue-at")
145 retry_spawn_and_wait(5, download_cmd);
147 // Unpack tar archive
148 let mut unpack_cmd = Command::new("tar");
149 unpack_cmd.arg("xf").arg(&archive_file).current_dir(RelPath::DOWNLOAD.to_path(dirs));
150 spawn_and_wait(unpack_cmd);
152 // Rename unpacked dir to the expected name
153 std::fs::rename(archive_dir, &download_dir).unwrap();
155 init_git_repo(&download_dir);
158 std::fs::remove_file(archive_file).unwrap();
161 fn init_git_repo(repo_dir: &Path) {
162 let mut git_init_cmd = git_command(repo_dir, "init");
163 git_init_cmd.arg("-q");
164 spawn_and_wait(git_init_cmd);
166 let mut git_add_cmd = git_command(repo_dir, "add");
167 git_add_cmd.arg(".");
168 spawn_and_wait(git_add_cmd);
170 let mut git_commit_cmd = git_command(repo_dir, "commit");
171 git_commit_cmd.arg("-m").arg("Initial commit").arg("-q");
172 spawn_and_wait(git_commit_cmd);
175 fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
176 let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
178 .map(|entry| entry.unwrap().path())
179 .filter(|path| path.extension() == Some(OsStr::new("patch")))
188 .starts_with(crate_name)
195 fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) {
196 if crate_name == "<none>" {
200 for patch in get_patches(dirs, crate_name) {
202 "[PATCH] {:?} <- {:?}",
203 target_dir.file_name().unwrap(),
204 patch.file_name().unwrap()
206 let mut apply_patch_cmd = git_command(target_dir, "am");
207 apply_patch_cmd.arg(patch).arg("-q");
208 spawn_and_wait(apply_patch_cmd);