Skip to content

Commit 066d5f9

Browse files
committed
Rewrite prepare_sysroot_src.sh in rust
1 parent fe6a289 commit 066d5f9

File tree

7 files changed

+97
-56
lines changed

7 files changed

+97
-56
lines changed

build_sysroot/prepare_sysroot_src.sh

Lines changed: 0 additions & 39 deletions
This file was deleted.

build_system/prepare.rs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use std::ffi::OsStr;
22
use std::ffi::OsString;
33
use std::fs;
4+
use std::path::Path;
5+
use std::path::PathBuf;
46
use std::process::Command;
57

8+
use crate::rustc_info::get_rustc_path;
9+
use crate::utils::copy_dir_recursively;
610
use crate::utils::spawn_and_wait;
711

812
pub(crate) fn prepare() {
9-
// FIXME implement in rust
10-
let prepare_sysroot_cmd = Command::new("./build_sysroot/prepare_sysroot_src.sh");
11-
spawn_and_wait(prepare_sysroot_cmd);
13+
prepare_sysroot();
1214

1315
eprintln!("[INSTALL] hyperfine");
1416
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
@@ -18,15 +20,7 @@ pub(crate) fn prepare() {
1820
"https://github.com/rust-random/rand.git",
1921
"0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
2022
);
21-
22-
eprintln!("[PATCH] rand");
23-
for patch in get_patches("crate_patches", "rand") {
24-
let mut patch_arg = OsString::from("../crate_patches/");
25-
patch_arg.push(patch);
26-
let mut apply_patch_cmd = Command::new("git");
27-
apply_patch_cmd.arg("am").arg(patch_arg).current_dir("rand");
28-
spawn_and_wait(apply_patch_cmd);
29-
}
23+
apply_patches("crate_patches", "rand", Path::new("rand"));
3024

3125
clone_repo(
3226
"regex",
@@ -47,17 +41,64 @@ pub(crate) fn prepare() {
4741
fs::copy("simple-raytracer/target/debug/main", "simple-raytracer/raytracer_cg_llvm").unwrap();
4842
}
4943

50-
fn clone_repo(name: &str, repo: &str, commit: &str) {
44+
fn prepare_sysroot() {
45+
let rustc_path = get_rustc_path();
46+
let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
47+
let sysroot_src = PathBuf::from("build_sysroot").canonicalize().unwrap().join("sysroot_src");
48+
49+
assert!(sysroot_src_orig.exists());
50+
51+
if sysroot_src.exists() {
52+
fs::remove_dir_all(&sysroot_src).unwrap();
53+
}
54+
fs::create_dir_all(sysroot_src.join("library")).unwrap();
55+
eprintln!("[COPY] sysroot src");
56+
copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library"));
57+
58+
eprintln!("[GIT] init");
59+
let mut git_init_cmd = Command::new("git");
60+
git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src);
61+
spawn_and_wait(git_init_cmd);
62+
63+
let mut git_add_cmd = Command::new("git");
64+
git_add_cmd.arg("add").arg(".").current_dir(&sysroot_src);
65+
spawn_and_wait(git_add_cmd);
66+
67+
let mut git_commit_cmd = Command::new("git");
68+
git_commit_cmd
69+
.arg("commit")
70+
.arg("-m")
71+
.arg("Initial commit")
72+
.arg("-q")
73+
.current_dir(&sysroot_src);
74+
spawn_and_wait(git_commit_cmd);
75+
76+
apply_patches("patches", "sysroot", &sysroot_src);
77+
78+
clone_repo(
79+
"build_sysroot/compiler-builtins",
80+
"https://github.com/rust-lang/compiler-builtins.git",
81+
"0.1.45",
82+
);
83+
84+
apply_patches(
85+
"crate_patches",
86+
"compiler-builtins",
87+
Path::new("build_sysroot/compiler-builtins"),
88+
);
89+
}
90+
91+
fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
5192
eprintln!("[CLONE] {}", repo);
5293
// Ignore exit code as the repo may already have been checked out
53-
Command::new("git").arg("clone").arg(repo).spawn().unwrap().wait().unwrap();
94+
Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap();
5495

5596
let mut clean_cmd = Command::new("git");
56-
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(name);
97+
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir);
5798
spawn_and_wait(clean_cmd);
5899

59100
let mut checkout_cmd = Command::new("git");
60-
checkout_cmd.arg("checkout").arg(commit).current_dir(name);
101+
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir);
61102
spawn_and_wait(checkout_cmd);
62103
}
63104

@@ -67,8 +108,20 @@ fn get_patches(patch_dir: &str, crate_name: &str) -> Vec<OsString> {
67108
.map(|entry| entry.unwrap().path())
68109
.filter(|path| path.extension() == Some(OsStr::new("patch")))
69110
.map(|path| path.file_name().unwrap().to_owned())
70-
.filter(|file_name| file_name.to_str().unwrap().split("-").nth(1).unwrap() == crate_name)
111+
.filter(|file_name| {
112+
file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
113+
})
71114
.collect();
72115
patches.sort();
73116
patches
74117
}
118+
119+
fn apply_patches(patch_dir: &str, crate_name: &str, target_dir: &Path) {
120+
for patch in get_patches(patch_dir, crate_name) {
121+
eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
122+
let patch_arg = Path::new(patch_dir).join(patch).canonicalize().unwrap();
123+
let mut apply_patch_cmd = Command::new("git");
124+
apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
125+
spawn_and_wait(apply_patch_cmd);
126+
}
127+
}

build_system/rustc_info.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ pub(crate) fn get_host_triple() -> String {
1717
.to_owned()
1818
}
1919

20+
pub(crate) fn get_rustc_path() -> PathBuf {
21+
let rustc_path = Command::new("rustup")
22+
.stderr(Stdio::inherit())
23+
.args(&["which", "rustc"])
24+
.output()
25+
.unwrap()
26+
.stdout;
27+
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
28+
}
29+
2030
pub(crate) fn get_default_sysroot() -> PathBuf {
2131
let default_sysroot = Command::new("rustc")
2232
.stderr(Stdio::inherit())

build_system/utils.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
1111
}
1212
}
1313

14+
#[track_caller]
1415
pub(crate) fn spawn_and_wait(mut cmd: Command) {
1516
if !cmd.spawn().unwrap().wait().unwrap().success() {
1617
process::exit(1);
1718
}
1819
}
20+
21+
pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {
22+
for entry in fs::read_dir(from).unwrap() {
23+
let entry = entry.unwrap();
24+
let filename = entry.file_name();
25+
if filename == "." || filename == ".." {
26+
continue;
27+
}
28+
if entry.metadata().unwrap().is_dir() {
29+
fs::create_dir(to.join(&filename)).unwrap();
30+
copy_dir_recursively(&from.join(&filename), &to.join(&filename));
31+
} else {
32+
fs::copy(from.join(&filename), to.join(&filename)).unwrap();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)