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