]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/prepare.rs
Rollup merge of #93692 - mfrw:mfrw/document-keyword-in, r=dtolnay
[rust.git] / compiler / rustc_codegen_cranelift / 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(
18         "rand",
19         "https://github.com/rust-random/rand.git",
20         "0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
21     );
22     apply_patches("rand", Path::new("rand"));
23
24     clone_repo(
25         "regex",
26         "https://github.com/rust-lang/regex.git",
27         "341f207c1071f7290e3f228c710817c280c8dca1",
28     );
29
30     clone_repo(
31         "portable-simd",
32         "https://github.com/rust-lang/portable-simd",
33         "b8d6b6844602f80af79cd96401339ec594d472d8",
34     );
35     apply_patches("portable-simd", Path::new("portable-simd"));
36
37     clone_repo(
38         "simple-raytracer",
39         "https://github.com/ebobby/simple-raytracer",
40         "804a7a21b9e673a482797aa289a18ed480e4d813",
41     );
42
43     eprintln!("[LLVM BUILD] simple-raytracer");
44     let mut build_cmd = Command::new("cargo");
45     build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
46     spawn_and_wait(build_cmd);
47     fs::copy(
48         Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
49         // FIXME use get_file_name here too once testing is migrated to rust
50         "simple-raytracer/raytracer_cg_llvm",
51     )
52     .unwrap();
53 }
54
55 fn prepare_sysroot() {
56     let rustc_path = get_rustc_path();
57     let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
58     let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src");
59
60     assert!(sysroot_src_orig.exists());
61
62     if sysroot_src.exists() {
63         fs::remove_dir_all(&sysroot_src).unwrap();
64     }
65     fs::create_dir_all(sysroot_src.join("library")).unwrap();
66     eprintln!("[COPY] sysroot src");
67     copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library"));
68
69     let rustc_version = get_rustc_version();
70     fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap();
71
72     eprintln!("[GIT] init");
73     let mut git_init_cmd = Command::new("git");
74     git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src);
75     spawn_and_wait(git_init_cmd);
76
77     let mut git_add_cmd = Command::new("git");
78     git_add_cmd.arg("add").arg(".").current_dir(&sysroot_src);
79     spawn_and_wait(git_add_cmd);
80
81     let mut git_commit_cmd = Command::new("git");
82     git_commit_cmd
83         .arg("commit")
84         .arg("-m")
85         .arg("Initial commit")
86         .arg("-q")
87         .current_dir(&sysroot_src);
88     spawn_and_wait(git_commit_cmd);
89
90     apply_patches("sysroot", &sysroot_src);
91
92     clone_repo(
93         "build_sysroot/compiler-builtins",
94         "https://github.com/rust-lang/compiler-builtins.git",
95         "0.1.70",
96     );
97     apply_patches("compiler-builtins", Path::new("build_sysroot/compiler-builtins"));
98 }
99
100 fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
101     eprintln!("[CLONE] {}", repo);
102     // Ignore exit code as the repo may already have been checked out
103     Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap();
104
105     let mut clean_cmd = Command::new("git");
106     clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir);
107     spawn_and_wait(clean_cmd);
108
109     let mut checkout_cmd = Command::new("git");
110     checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir);
111     spawn_and_wait(checkout_cmd);
112 }
113
114 fn get_patches(crate_name: &str) -> Vec<OsString> {
115     let mut patches: Vec<_> = fs::read_dir("patches")
116         .unwrap()
117         .map(|entry| entry.unwrap().path())
118         .filter(|path| path.extension() == Some(OsStr::new("patch")))
119         .map(|path| path.file_name().unwrap().to_owned())
120         .filter(|file_name| {
121             file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
122         })
123         .collect();
124     patches.sort();
125     patches
126 }
127
128 fn apply_patches(crate_name: &str, target_dir: &Path) {
129     for patch in get_patches(crate_name) {
130         eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
131         let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
132         let mut apply_patch_cmd = Command::new("git");
133         apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
134         spawn_and_wait(apply_patch_cmd);
135     }
136 }