Simplify directory creation

This commit is contained in:
Guillaume Gomez 2024-03-20 14:18:25 +01:00
parent 51d27a63b8
commit 9b17b3d184
5 changed files with 19 additions and 43 deletions

View File

@ -1,5 +1,5 @@
use crate::config::{Channel, ConfigInfo}; use crate::config::{Channel, ConfigInfo};
use crate::utils::{run_command, run_command_with_output_and_env, walk_dir}; use crate::utils::{create_dir, run_command, run_command_with_output_and_env, walk_dir};
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;
@ -103,6 +103,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> { pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
let start_dir = Path::new("build_sysroot"); let start_dir = Path::new("build_sysroot");
cleanup_sysroot_previous_build(&start_dir); cleanup_sysroot_previous_build(&start_dir);
// Builds libs // Builds libs
@ -136,13 +137,7 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
// Copy files to sysroot // Copy files to sysroot
let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple)); let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple));
fs::create_dir_all(&sysroot_path).map_err(|error| { create_dir(&sysroot_path)?;
format!(
"Failed to create directory `{}`: {:?}",
sysroot_path.display(),
error
)
})?;
let copier = |dir_to_copy: &Path| { let copier = |dir_to_copy: &Path| {
// FIXME: should not use shell command! // FIXME: should not use shell command!
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ()) run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
@ -155,13 +150,7 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
// Copy the source files to the sysroot (Rust for Linux needs this). // Copy the source files to the sysroot (Rust for Linux needs this).
let sysroot_src_path = start_dir.join("sysroot/lib/rustlib/src/rust"); let sysroot_src_path = start_dir.join("sysroot/lib/rustlib/src/rust");
fs::create_dir_all(&sysroot_src_path).map_err(|error| { create_dir(&sysroot_src_path)?;
format!(
"Failed to create directory `{}`: {:?}",
sysroot_src_path.display(),
error
)
})?;
run_command( run_command(
&[ &[
&"cp", &"cp",
@ -216,12 +205,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
// We voluntarily ignore the error. // We voluntarily ignore the error.
let _ = fs::remove_dir_all("target/out"); let _ = fs::remove_dir_all("target/out");
let gccjit_target = "target/out/gccjit"; let gccjit_target = "target/out/gccjit";
fs::create_dir_all(gccjit_target).map_err(|error| { create_dir(gccjit_target)?;
format!(
"Failed to create directory `{}`: {:?}",
gccjit_target, error
)
})?;
println!("[BUILD] sysroot"); println!("[BUILD] sysroot");
build_sysroot(&env, &args.config_info)?; build_sysroot(&env, &args.config_info)?;

View File

@ -1,5 +1,5 @@
use crate::utils::{ use crate::utils::{
create_symlink, get_os_name, run_command_with_output, rustc_version_info, split_args, create_dir, create_symlink, get_os_name, run_command_with_output, rustc_version_info, split_args,
}; };
use std::collections::HashMap; use std::collections::HashMap;
use std::env as std_env; use std::env as std_env;
@ -228,13 +228,7 @@ fn download_gccjit_if_needed(&mut self) -> Result<(), String> {
let output_dir = output_dir.join(&commit); let output_dir = output_dir.join(&commit);
if !output_dir.is_dir() { if !output_dir.is_dir() {
std::fs::create_dir_all(&output_dir).map_err(|err| { create_dir(&output_dir)?;
format!(
"failed to create folder `{}`: {:?}",
output_dir.display(),
err,
)
})?;
} }
let output_dir = output_dir.canonicalize().map_err(|err| { let output_dir = output_dir.canonicalize().map_err(|err| {
format!( format!(

View File

@ -1,6 +1,6 @@
use crate::rustc_info::get_rustc_path; use crate::rustc_info::get_rustc_path;
use crate::utils::{ use crate::utils::{
cargo_install, git_clone_root_dir, remove_file, run_command, run_command_with_output, walk_dir, cargo_install, create_dir, git_clone_root_dir, remove_file, run_command, run_command_with_output, walk_dir,
}; };
use std::fs; use std::fs;
@ -41,13 +41,7 @@ fn prepare_libcore(
} }
let sysroot_library_dir = sysroot_dir.join("library"); let sysroot_library_dir = sysroot_dir.join("library");
fs::create_dir_all(&sysroot_library_dir).map_err(|error| { create_dir(&sysroot_library_dir)?;
format!(
"Failed to create folder `{}`: {:?}",
sysroot_library_dir.display(),
error,
)
})?;
run_command( run_command(
&[&"cp", &"-r", &rustlib_dir.join("library"), &sysroot_dir], &[&"cp", &"-r", &rustlib_dir.join("library"), &sysroot_dir],

View File

@ -1,13 +1,13 @@
use crate::build; use crate::build;
use crate::config::{Channel, ConfigInfo}; use crate::config::{Channel, ConfigInfo};
use crate::utils::{ use crate::utils::{
get_toolchain, git_clone, git_clone_root_dir, remove_file, run_command, run_command_with_env, create_dir, get_toolchain, git_clone, git_clone_root_dir, remove_file, run_command, run_command_with_env,
run_command_with_output_and_env, rustc_version_info, split_args, walk_dir, run_command_with_output_and_env, rustc_version_info, split_args, walk_dir,
}; };
use std::collections::{BTreeSet, HashMap}; use std::collections::{BTreeSet, HashMap};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::{create_dir_all, remove_dir_all, File}; use std::fs::{remove_dir_all, File};
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
@ -211,8 +211,7 @@ fn build_if_no_backend(env: &Env, args: &TestArg) -> Result<(), String> {
fn clean(_env: &Env, args: &TestArg) -> Result<(), String> { fn clean(_env: &Env, args: &TestArg) -> Result<(), String> {
let _ = std::fs::remove_dir_all(&args.config_info.cargo_target_dir); let _ = std::fs::remove_dir_all(&args.config_info.cargo_target_dir);
let path = Path::new(&args.config_info.cargo_target_dir).join("gccjit"); let path = Path::new(&args.config_info.cargo_target_dir).join("gccjit");
std::fs::create_dir_all(&path) create_dir(&path)
.map_err(|error| format!("failed to create folder `{}`: {:?}", path.display(), error))
} }
fn mini_tests(env: &Env, args: &TestArg) -> Result<(), String> { fn mini_tests(env: &Env, args: &TestArg) -> Result<(), String> {
@ -715,8 +714,7 @@ fn test_projects(env: &Env, args: &TestArg) -> Result<(), String> {
}; };
let projects_path = Path::new("projects"); let projects_path = Path::new("projects");
create_dir_all(projects_path) create_dir(projects_path)?;
.map_err(|err| format!("Failed to create directory `projects`: {}", err))?;
let nb_parts = args.nb_parts.unwrap_or(0); let nb_parts = args.nb_parts.unwrap_or(0);
if nb_parts > 0 { if nb_parts > 0 {

View File

@ -307,6 +307,12 @@ pub fn git_clone(
git_clone_inner(to_clone, dest, shallow_clone, repo_name) git_clone_inner(to_clone, dest, shallow_clone, repo_name)
} }
pub fn create_dir<P: AsRef<Path>>(path: P) -> Result<(), String> {
fs::create_dir_all(&path).map_err(|error| {
format!("Failed to create directory `{}`: {:?}", path.as_ref().display(), error)
})
}
/// This function differs from `git_clone` in how it handles *where* the repository will be cloned. /// This function differs from `git_clone` in how it handles *where* the repository will be cloned.
/// In `git_clone`, it is cloned in the provided path. In this function, the path you provide is /// In `git_clone`, it is cloned in the provided path. In this function, the path you provide is
/// the parent folder. So if you pass "a" as folder and try to clone "b.git", it will be cloned into /// the parent folder. So if you pass "a" as folder and try to clone "b.git", it will be cloned into