Skip to content

Commit

Permalink
Correctly set path
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 19, 2023
1 parent bce234d commit 0fdf7c6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 92 deletions.
21 changes: 0 additions & 21 deletions build_sysroot/Cargo.toml

This file was deleted.

30 changes: 0 additions & 30 deletions build_sysroot/build_sysroot.sh

This file was deleted.

1 change: 0 additions & 1 deletion build_sysroot/src/lib.rs

This file was deleted.

40 changes: 24 additions & 16 deletions build_system/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::utils::{cargo_install, git_clone, run_command, run_command_with_outpu
use std::fs;
use std::path::Path;

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

let rustlib_dir = parent.join("../lib/rustlib/src/rust");
let rustlib_dir =
parent
.join("../lib/rustlib/src/rust")
.canonicalize()
.map_err(|e| format!("Failed to canonicalize path: {e:?}"))?;
if !rustlib_dir.is_dir() {
return Err("Please install `rust-src` component".to_owned());
}

let sysroot_dir = Path::new("build_sysroot/sysroot_src");
let sysroot_dir = sysroot_path.join("sysroot_src");
if sysroot_dir.is_dir() {
if let Err(e) = fs::remove_dir_all(sysroot_dir) {
if let Err(e) = fs::remove_dir_all(&sysroot_dir) {
return Err(format!("Failed to remove `{}`: {:?}", sysroot_dir.display(), e));
}
}
Expand All @@ -34,7 +38,7 @@ fn prepare_libcore() -> Result<(), String> {
sysroot_library_dir.display(),
))?;

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

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

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

fn clone_and_setup<F>(repo_url: &str, checkout_commit: &str, extra: Option<F>) -> Result<(), String>
fn clone_and_setup<F>(sysroot_path: &Path, repo_url: &str, checkout_commit: &str, extra: Option<F>) -> Result<(), String>
where
F: Fn(&Path) -> Result<(), String>,
{
let clone_result = git_clone(repo_url, None)?;
let clone_result = git_clone(repo_url, Some(sysroot_path))?;
if !clone_result.ran_clone {
println!("`{}` has already been cloned", clone_result.repo_name);
}
let repo_path = Path::new(&clone_result.repo_name);
run_command(&[&"git", &"checkout", &"--", &"."], Some(repo_path))?;
run_command(&[&"git", &"checkout", &checkout_commit], Some(repo_path))?;
let repo_path = sysroot_path.join(&clone_result.repo_name);
run_command_with_output(&[&"git", &"checkout", &"--", &"."], Some(&repo_path))?;
run_command_with_output(&[&"git", &"checkout", &checkout_commit], Some(&repo_path))?;
let filter = format!("-{}-", clone_result.repo_name);
walk_dir("crate_patches", |_| Ok(()), |file_path| {
let s = file_path.as_os_str().to_str().unwrap();
if s.contains(&filter) && s.ends_with(".patch") {
run_command(&[&"git", &"am", &s], Some(repo_path))?;
run_command_with_output(
&[&"git", &"am", &file_path.canonicalize().unwrap()],
Some(&repo_path),
)?;
}
Ok(())
})?;
if let Some(extra) = extra {
extra(repo_path)?;
extra(&repo_path)?;
}
Ok(())
}
Expand Down Expand Up @@ -136,7 +143,8 @@ pub fn run() -> Result<(), String> {
Some(a) => a,
None => return Ok(()),
};
prepare_libcore()?;
let sysroot_path = Path::new("build_sysroot");
prepare_libcore(sysroot_path)?;

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

for (repo_url, checkout_commit, cb) in to_clone {
clone_and_setup(repo_url, checkout_commit, *cb)?;
clone_and_setup(sysroot_path, repo_url, checkout_commit, *cb)?;
}
}

Expand Down
74 changes: 50 additions & 24 deletions build_system/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs;
use std::path::Path;
use std::process::{Command, Output};
use std::process::{Command, ExitStatus, Output};

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

pub fn run_command(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Result<Output, String> {
run_command_inner(input, cwd).output()
.map_err(|e| format!(
"Command `{}` failed to run: {e:?}",
fn check_exit_status(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
exit_status: ExitStatus,
) -> Result<(), String> {
if exit_status.success() {
Ok(())
} else {
Err(format!(
"Command `{}`{} exited with status {:?}",
input.iter()
.map(|s| s.as_ref().to_str().unwrap())
.collect::<Vec<_>>()
.join(" "),
cwd.map(|cwd| format!(" (running in folder `{}`)", cwd.display()))
.unwrap_or_default(),
exit_status.code(),
))
}
}

fn command_error<D: Debug>(input: &[&dyn AsRef<OsStr>], cwd: &Option<&Path>, error: D) -> String {
format!(
"Command `{}`{} failed to run: {error:?}",
input.iter()
.map(|s| s.as_ref().to_str().unwrap())
.collect::<Vec<_>>()
.join(" "),
cwd.as_ref()
.map(|cwd| format!(
" (running in folder `{}`)",
cwd.display(),
))
.unwrap_or_default(),
)
}

pub fn run_command(input: &[&dyn AsRef<OsStr>], cwd: Option<&Path>) -> Result<Output, String> {
let output = get_command_inner(input, cwd)
.output()
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, output.status)?;
Ok(output)
}

pub fn run_command_with_output(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
) -> Result<(), String> {
run_command_inner(input, cwd).spawn()
.map_err(|e| format!(
"Command `{}` failed to run: {e:?}",
input.iter()
.map(|s| s.as_ref().to_str().unwrap())
.collect::<Vec<_>>()
.join(" "),
))?
let exit_status = get_command_inner(input, cwd).spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| format!(
"Failed to wait for command `{}` to run: {e:?}",
input.iter()
.map(|s| s.as_ref().to_str().unwrap())
.collect::<Vec<_>>()
.join(" "),
))?;
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status)?;
Ok(())
}

Expand All @@ -69,7 +93,7 @@ pub fn cargo_install(to_install: &str) -> Result<(), String> {
{
return Ok(());
}
run_command(&[&"cargo", &"install", &to_install], None)?;
run_command_with_output(&[&"cargo", &"install", &to_install], None)?;
Ok(())
}

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

let dest = dest.unwrap_or_else(|| Path::new(&repo_name));
let dest = dest
.map(|dest| dest.join(&repo_name))
.unwrap_or_else(|| Path::new(&repo_name).into());
if dest.is_dir() {
return Ok(CloneResult { ran_clone: false, repo_name });
}

run_command(&[&"git", &"clone", &to_clone, &dest], None)?;
run_command_with_output(&[&"git", &"clone", &to_clone, &dest], None)?;
Ok(CloneResult { ran_clone: true, repo_name })
}

Expand Down

0 comments on commit 0fdf7c6

Please sign in to comment.