Skip to content

Commit 0fdf7c6

Browse files
Correctly set path
1 parent bce234d commit 0fdf7c6

File tree

5 files changed

+74
-92
lines changed

5 files changed

+74
-92
lines changed

build_sysroot/Cargo.toml

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

build_sysroot/build_sysroot.sh

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

build_sysroot/src/lib.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

build_system/src/prepare.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::utils::{cargo_install, git_clone, run_command, run_command_with_outpu
44
use std::fs;
55
use std::path::Path;
66

7-
fn prepare_libcore() -> Result<(), String> {
7+
fn prepare_libcore(sysroot_path: &Path) -> Result<(), String> {
88
let rustc_path = match get_rustc_path() {
99
Some(path) => path,
1010
None => return Err("`rustc` path not found".to_owned()),
@@ -15,14 +15,18 @@ fn prepare_libcore() -> Result<(), String> {
1515
None => return Err(format!("No parent for `{}`", rustc_path.display())),
1616
};
1717

18-
let rustlib_dir = parent.join("../lib/rustlib/src/rust");
18+
let rustlib_dir =
19+
parent
20+
.join("../lib/rustlib/src/rust")
21+
.canonicalize()
22+
.map_err(|e| format!("Failed to canonicalize path: {e:?}"))?;
1923
if !rustlib_dir.is_dir() {
2024
return Err("Please install `rust-src` component".to_owned());
2125
}
2226

23-
let sysroot_dir = Path::new("build_sysroot/sysroot_src");
27+
let sysroot_dir = sysroot_path.join("sysroot_src");
2428
if sysroot_dir.is_dir() {
25-
if let Err(e) = fs::remove_dir_all(sysroot_dir) {
29+
if let Err(e) = fs::remove_dir_all(&sysroot_dir) {
2630
return Err(format!("Failed to remove `{}`: {:?}", sysroot_dir.display(), e));
2731
}
2832
}
@@ -34,7 +38,7 @@ fn prepare_libcore() -> Result<(), String> {
3438
sysroot_library_dir.display(),
3539
))?;
3640

37-
run_command(&[&"cp", &"-r", &rustlib_dir, &sysroot_library_dir], None)?;
41+
run_command(&[&"cp", &"-r", &rustlib_dir.join("library"), &sysroot_dir], None)?;
3842

3943
println!("[GIT] init (cwd): `{}`", sysroot_dir.display());
4044
run_command_with_output(&[&"git", &"init"], Some(&sysroot_dir))?;
@@ -47,8 +51,8 @@ fn prepare_libcore() -> Result<(), String> {
4751
// Even using --author is not enough.
4852
run_command(&[&"git", &"config", &"user.email", &"[email protected]"], Some(&sysroot_dir))?;
4953
run_command(&[&"git", &"config", &"user.name", &"None"], Some(&sysroot_dir))?;
50-
run_command(&[&"git", &"config", &"core.autocrlf=false"], Some(&sysroot_dir))?;
51-
run_command(&[&"git", &"config", &"commit.gpgSign=false"], Some(&sysroot_dir))?;
54+
run_command(&[&"git", &"config", &"core.autocrlf", &"false"], Some(&sysroot_dir))?;
55+
run_command(&[&"git", &"config", &"commit.gpgSign", &"false"], Some(&sysroot_dir))?;
5256
run_command(&[&"git", &"commit", &"-m", &"Initial commit", &"-q"], Some(&sysroot_dir))?;
5357

5458
walk_dir("patches", |_| Ok(()), |file_path: &Path| {
@@ -73,27 +77,30 @@ fn build_raytracer(repo_dir: &Path) -> Result<(), String> {
7377
Ok(())
7478
}
7579

76-
fn clone_and_setup<F>(repo_url: &str, checkout_commit: &str, extra: Option<F>) -> Result<(), String>
80+
fn clone_and_setup<F>(sysroot_path: &Path, repo_url: &str, checkout_commit: &str, extra: Option<F>) -> Result<(), String>
7781
where
7882
F: Fn(&Path) -> Result<(), String>,
7983
{
80-
let clone_result = git_clone(repo_url, None)?;
84+
let clone_result = git_clone(repo_url, Some(sysroot_path))?;
8185
if !clone_result.ran_clone {
8286
println!("`{}` has already been cloned", clone_result.repo_name);
8387
}
84-
let repo_path = Path::new(&clone_result.repo_name);
85-
run_command(&[&"git", &"checkout", &"--", &"."], Some(repo_path))?;
86-
run_command(&[&"git", &"checkout", &checkout_commit], Some(repo_path))?;
88+
let repo_path = sysroot_path.join(&clone_result.repo_name);
89+
run_command_with_output(&[&"git", &"checkout", &"--", &"."], Some(&repo_path))?;
90+
run_command_with_output(&[&"git", &"checkout", &checkout_commit], Some(&repo_path))?;
8791
let filter = format!("-{}-", clone_result.repo_name);
8892
walk_dir("crate_patches", |_| Ok(()), |file_path| {
8993
let s = file_path.as_os_str().to_str().unwrap();
9094
if s.contains(&filter) && s.ends_with(".patch") {
91-
run_command(&[&"git", &"am", &s], Some(repo_path))?;
95+
run_command_with_output(
96+
&[&"git", &"am", &file_path.canonicalize().unwrap()],
97+
Some(&repo_path),
98+
)?;
9299
}
93100
Ok(())
94101
})?;
95102
if let Some(extra) = extra {
96-
extra(repo_path)?;
103+
extra(&repo_path)?;
97104
}
98105
Ok(())
99106
}
@@ -136,7 +143,8 @@ pub fn run() -> Result<(), String> {
136143
Some(a) => a,
137144
None => return Ok(()),
138145
};
139-
prepare_libcore()?;
146+
let sysroot_path = Path::new("build_sysroot");
147+
prepare_libcore(sysroot_path)?;
140148

141149
if !args.only_libcore {
142150
cargo_install("hyperfine")?;
@@ -148,7 +156,7 @@ pub fn run() -> Result<(), String> {
148156
];
149157

150158
for (repo_url, checkout_commit, cb) in to_clone {
151-
clone_and_setup(repo_url, checkout_commit, *cb)?;
159+
clone_and_setup(sysroot_path, repo_url, checkout_commit, *cb)?;
152160
}
153161
}
154162

build_system/src/utils.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::ffi::OsStr;
2+
use std::fmt::Debug;
23
use std::fs;
34
use std::path::Path;
4-
use std::process::{Command, Output};
5+
use std::process::{Command, ExitStatus, Output};
56

6-
fn run_command_inner(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Command {
7+
fn get_command_inner(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Command {
78
let (cmd, args) = match input {
89
[] => panic!("empty command"),
910
[cmd, args @ ..] => (cmd, args),
@@ -16,37 +17,60 @@ fn run_command_inner(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Command
1617
command
1718
}
1819

19-
pub fn run_command(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Result<Output, String> {
20-
run_command_inner(input, cwd).output()
21-
.map_err(|e| format!(
22-
"Command `{}` failed to run: {e:?}",
20+
fn check_exit_status(
21+
input: &[&dyn AsRef<OsStr>],
22+
cwd: Option<&Path>,
23+
exit_status: ExitStatus,
24+
) -> Result<(), String> {
25+
if exit_status.success() {
26+
Ok(())
27+
} else {
28+
Err(format!(
29+
"Command `{}`{} exited with status {:?}",
2330
input.iter()
2431
.map(|s| s.as_ref().to_str().unwrap())
2532
.collect::<Vec<_>>()
2633
.join(" "),
34+
cwd.map(|cwd| format!(" (running in folder `{}`)", cwd.display()))
35+
.unwrap_or_default(),
36+
exit_status.code(),
2737
))
38+
}
39+
}
40+
41+
fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String {
42+
format!(
43+
"Command `{}`{} failed to run: {error:?}",
44+
input.iter()
45+
.map(|s| s.as_ref().to_str().unwrap())
46+
.collect::<Vec<_>>()
47+
.join(" "),
48+
cwd.as_ref()
49+
.map(|cwd| format!(
50+
" (running in folder `{}`)",
51+
cwd.display(),
52+
))
53+
.unwrap_or_default(),
54+
)
55+
}
56+
57+
pub fn run_command(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Result<Output, String> {
58+
let output = get_command_inner(input, cwd)
59+
.output()
60+
.map_err(|e| command_error(input, &cwd, e))?;
61+
check_exit_status(input, cwd, output.status)?;
62+
Ok(output)
2863
}
2964

3065
pub fn run_command_with_output(
3166
input: &[&dyn AsRef<OsStr>],
3267
cwd: Option<&Path>,
3368
) -> Result<(), String> {
34-
run_command_inner(input, cwd).spawn()
35-
.map_err(|e| format!(
36-
"Command `{}` failed to run: {e:?}",
37-
input.iter()
38-
.map(|s| s.as_ref().to_str().unwrap())
39-
.collect::<Vec<_>>()
40-
.join(" "),
41-
))?
69+
let exit_status = get_command_inner(input, cwd).spawn()
70+
.map_err(|e| command_error(input, &cwd, e))?
4271
.wait()
43-
.map_err(|e| format!(
44-
"Failed to wait for command `{}` to run: {e:?}",
45-
input.iter()
46-
.map(|s| s.as_ref().to_str().unwrap())
47-
.collect::<Vec<_>>()
48-
.join(" "),
49-
))?;
72+
.map_err(|e| command_error(input, &cwd, e))?;
73+
check_exit_status(input, cwd, exit_status)?;
5074
Ok(())
5175
}
5276

@@ -69,7 +93,7 @@ pub fn cargo_install(to_install: &str) -> Result<(), String> {
6993
{
7094
return Ok(());
7195
}
72-
run_command(&[&"cargo", &"install", &to_install], None)?;
96+
run_command_with_output(&[&"cargo", &"install", &to_install], None)?;
7397
Ok(())
7498
}
7599

@@ -85,12 +109,14 @@ pub fn git_clone(to_clone: &str, dest: Option<&Path>) -> Result<CloneResult, Str
85109
None => repo_name.to_owned(),
86110
};
87111

88-
let dest = dest.unwrap_or_else(|| Path::new(&repo_name));
112+
let dest = dest
113+
.map(|dest| dest.join(&repo_name))
114+
.unwrap_or_else(|| Path::new(&repo_name).into());
89115
if dest.is_dir() {
90116
return Ok(CloneResult { ran_clone: false, repo_name });
91117
}
92118

93-
run_command(&[&"git", &"clone", &to_clone, &dest], None)?;
119+
run_command_with_output(&[&"git", &"clone", &to_clone, &dest], None)?;
94120
Ok(CloneResult { ran_clone: true, repo_name })
95121
}
96122

0 commit comments

Comments
 (0)