]> git.lizzy.rs Git - rust.git/blob - build_system/prepare.rs
Merge commit '370c397ec9169809e5ad270079712e0043514240' into sync_cg_clif-2022-03-20
[rust.git] / build_system / prepare.rs
1 use std::env;
2 use std::ffi::OsStr;
3 use std::ffi::OsString;
4 use std::fs;
5 use std::path::Path;
6 use std::process::Command;
7
8 use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
9 use super::utils::{copy_dir_recursively, spawn_and_wait};
10
11 pub(crate) fn prepare() {
12     prepare_sysroot();
13
14     eprintln!("[INSTALL] hyperfine");
15     Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
16
17     clone_repo_shallow_github(
18         "rand",
19         "rust-random",
20         "rand",
21         "0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
22     );
23     apply_patches("rand", Path::new("rand"));
24
25     clone_repo_shallow_github(
26         "regex",
27         "rust-lang",
28         "regex",
29         "341f207c1071f7290e3f228c710817c280c8dca1",
30     );
31
32     clone_repo_shallow_github(
33         "portable-simd",
34         "rust-lang",
35         "portable-simd",
36         "b8d6b6844602f80af79cd96401339ec594d472d8",
37     );
38     apply_patches("portable-simd", Path::new("portable-simd"));
39
40     clone_repo_shallow_github(
41         "simple-raytracer",
42         "ebobby",
43         "simple-raytracer",
44         "804a7a21b9e673a482797aa289a18ed480e4d813",
45     );
46
47     eprintln!("[LLVM BUILD] simple-raytracer");
48     let mut build_cmd = Command::new("cargo");
49     build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
50     spawn_and_wait(build_cmd);
51     fs::copy(
52         Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
53         // FIXME use get_file_name here too once testing is migrated to rust
54         "simple-raytracer/raytracer_cg_llvm",
55     )
56     .unwrap();
57 }
58
59 fn prepare_sysroot() {
60     let rustc_path = get_rustc_path();
61     let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
62     let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src");
63
64     assert!(sysroot_src_orig.exists());
65
66     if sysroot_src.exists() {
67         fs::remove_dir_all(&sysroot_src).unwrap();
68     }
69     fs::create_dir_all(sysroot_src.join("library")).unwrap();
70     eprintln!("[COPY] sysroot src");
71     copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library"));
72
73     let rustc_version = get_rustc_version();
74     fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap();
75
76     eprintln!("[GIT] init");
77     let mut git_init_cmd = Command::new("git");
78     git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src);
79     spawn_and_wait(git_init_cmd);
80
81     init_git_repo(&sysroot_src);
82
83     apply_patches("sysroot", &sysroot_src);
84 }
85
86 #[allow(dead_code)]
87 fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
88     eprintln!("[CLONE] {}", repo);
89     // Ignore exit code as the repo may already have been checked out
90     Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap();
91
92     let mut clean_cmd = Command::new("git");
93     clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir);
94     spawn_and_wait(clean_cmd);
95
96     let mut checkout_cmd = Command::new("git");
97     checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir);
98     spawn_and_wait(checkout_cmd);
99 }
100
101 fn clone_repo_shallow_github(target_dir: &str, username: &str, repo: &str, rev: &str) {
102     if cfg!(windows) {
103         // Older windows doesn't have tar or curl by default. Fall back to using git.
104         clone_repo(target_dir, &format!("https://github.com/{}/{}.git", username, repo), rev);
105         return;
106     }
107
108     let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", username, repo, rev);
109     let archive_file = format!("{}.tar.gz", rev);
110     let archive_dir = format!("{}-{}", repo, rev);
111
112     eprintln!("[DOWNLOAD] {}/{} from {}", username, repo, archive_url);
113
114     // Remove previous results if they exists
115     let _ = std::fs::remove_file(&archive_file);
116     let _ = std::fs::remove_dir_all(&archive_dir);
117     let _ = std::fs::remove_dir_all(target_dir);
118
119     // Download zip archive
120     let mut download_cmd = Command::new("curl");
121     download_cmd.arg("--location").arg("--output").arg(&archive_file).arg(archive_url);
122     spawn_and_wait(download_cmd);
123
124     // Unpack tar archive
125     let mut unpack_cmd = Command::new("tar");
126     unpack_cmd.arg("xf").arg(&archive_file);
127     spawn_and_wait(unpack_cmd);
128
129     // Rename unpacked dir to the expected name
130     std::fs::rename(archive_dir, target_dir).unwrap();
131
132     init_git_repo(Path::new(target_dir));
133
134     // Cleanup
135     std::fs::remove_file(archive_file).unwrap();
136 }
137
138 fn init_git_repo(repo_dir: &Path) {
139     let mut git_init_cmd = Command::new("git");
140     git_init_cmd.arg("init").arg("-q").current_dir(repo_dir);
141     spawn_and_wait(git_init_cmd);
142
143     let mut git_add_cmd = Command::new("git");
144     git_add_cmd.arg("add").arg(".").current_dir(repo_dir);
145     spawn_and_wait(git_add_cmd);
146
147     let mut git_commit_cmd = Command::new("git");
148     git_commit_cmd.arg("commit").arg("-m").arg("Initial commit").arg("-q").current_dir(repo_dir);
149     spawn_and_wait(git_commit_cmd);
150 }
151
152 fn get_patches(crate_name: &str) -> Vec<OsString> {
153     let mut patches: Vec<_> = fs::read_dir("patches")
154         .unwrap()
155         .map(|entry| entry.unwrap().path())
156         .filter(|path| path.extension() == Some(OsStr::new("patch")))
157         .map(|path| path.file_name().unwrap().to_owned())
158         .filter(|file_name| {
159             file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
160         })
161         .collect();
162     patches.sort();
163     patches
164 }
165
166 fn apply_patches(crate_name: &str, target_dir: &Path) {
167     for patch in get_patches(crate_name) {
168         eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
169         let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
170         let mut apply_patch_cmd = Command::new("git");
171         apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
172         spawn_and_wait(apply_patch_cmd);
173     }
174 }