diff --git a/build_sysroot/Cargo.toml b/build_sysroot/Cargo.toml deleted file mode 100644 index dca2ffdc24b..00000000000 --- a/build_sysroot/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -authors = ["bjorn3 "] -name = "sysroot" -version = "0.0.0" -resolver = "2" - -[dependencies] -core = { path = "./sysroot_src/library/core" } -compiler_builtins = "0.1" -alloc = { path = "./sysroot_src/library/alloc" } -std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] } -test = { path = "./sysroot_src/library/test" } -proc_macro = { path = "./sysroot_src/library/proc_macro" } - -[patch.crates-io] -rustc-std-workspace-core = { path = "./sysroot_src/library/rustc-std-workspace-core" } -rustc-std-workspace-alloc = { path = "./sysroot_src/library/rustc-std-workspace-alloc" } -rustc-std-workspace-std = { path = "./sysroot_src/library/rustc-std-workspace-std" } - -[profile.release] -debug = true diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh deleted file mode 100755 index 9d692d599f6..00000000000 --- a/build_sysroot/build_sysroot.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -# Requires the CHANNEL env var to be set to `debug` or `release.` - -set -e -cd $(dirname "$0") - -pushd ../ >/dev/null -source ./config.sh -popd >/dev/null - -# Cleanup for previous run -# v Clean target dir except for build scripts and incremental cache -rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true -rm Cargo.lock test_target/Cargo.lock 2>/dev/null || true -rm -r sysroot/ 2>/dev/null || true - -# Build libs -export RUSTFLAGS="$RUSTFLAGS -Z force-unstable-if-unmarked" -if [[ "$1" == "--release" ]]; then - sysroot_channel='release' - RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release -else - sysroot_channel='debug' - cargo build --target $TARGET_TRIPLE --features compiler_builtins/c -fi - -# Copy files to sysroot -mkdir -p sysroot/lib/rustlib/$TARGET_TRIPLE/lib/ -cp -r target/$TARGET_TRIPLE/$sysroot_channel/deps/* sysroot/lib/rustlib/$TARGET_TRIPLE/lib/ diff --git a/build_sysroot/src/lib.rs b/build_sysroot/src/lib.rs deleted file mode 100644 index 0c9ac1ac8e4..00000000000 --- a/build_sysroot/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -#![no_std] diff --git a/build_system/src/prepare.rs b/build_system/src/prepare.rs index 8e6183be629..56c13cc2138 100644 --- a/build_system/src/prepare.rs +++ b/build_system/src/prepare.rs @@ -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()), @@ -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)); } } @@ -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))?; @@ -47,8 +51,8 @@ fn prepare_libcore() -> Result<(), String> { // Even using --author is not enough. run_command(&[&"git", &"config", &"user.email", &"none@example.com"], 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| { @@ -73,27 +77,30 @@ fn build_raytracer(repo_dir: &Path) -> Result<(), String> { Ok(()) } -fn clone_and_setup(repo_url: &str, checkout_commit: &str, extra: Option) -> Result<(), String> +fn clone_and_setup(sysroot_path: &Path, repo_url: &str, checkout_commit: &str, extra: Option) -> 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(()) } @@ -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")?; @@ -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)?; } } diff --git a/build_system/src/utils.rs b/build_system/src/utils.rs index 145f40ec8ae..99fb61b0a01 100644 --- a/build_system/src/utils.rs +++ b/build_system/src/utils.rs @@ -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], cwd: Option<&Path>) -> Command { +fn get_command_inner(input: &[&dyn AsRef], cwd: Option<&Path>) -> Command { let (cmd, args) = match input { [] => panic!("empty command"), [cmd, args @ ..] => (cmd, args), @@ -16,37 +17,60 @@ fn run_command_inner(input: &[&dyn AsRef], cwd: Option<&Path>) -> Command command } -pub fn run_command(input: &[&dyn AsRef], cwd: Option<&Path>) -> Result { - run_command_inner(input, cwd).output() - .map_err(|e| format!( - "Command `{}` failed to run: {e:?}", +fn check_exit_status( + input: &[&dyn AsRef], + 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::>() .join(" "), + cwd.map(|cwd| format!(" (running in folder `{}`)", cwd.display())) + .unwrap_or_default(), + exit_status.code(), )) + } +} + +fn command_error(input: &[&dyn AsRef], cwd: &Option<&Path>, error: D) -> String { + format!( + "Command `{}`{} failed to run: {error:?}", + input.iter() + .map(|s| s.as_ref().to_str().unwrap()) + .collect::>() + .join(" "), + cwd.as_ref() + .map(|cwd| format!( + " (running in folder `{}`)", + cwd.display(), + )) + .unwrap_or_default(), + ) +} + +pub fn run_command(input: &[&dyn AsRef], cwd: Option<&Path>) -> Result { + 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], 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::>() - .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::>() - .join(" "), - ))?; + .map_err(|e| command_error(input, &cwd, e))?; + check_exit_status(input, cwd, exit_status)?; Ok(()) } @@ -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(()) } @@ -85,12 +109,14 @@ pub fn git_clone(to_clone: &str, dest: Option<&Path>) -> Result 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 }) }