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