Skip to content

Commit 9b17b3d

Browse files
Simplify directory creation
1 parent 51d27a6 commit 9b17b3d

File tree

5 files changed

+19
-43
lines changed

5 files changed

+19
-43
lines changed

build_system/src/build.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::config::{Channel, ConfigInfo};
2-
use crate::utils::{run_command, run_command_with_output_and_env, walk_dir};
2+
use crate::utils::{create_dir, run_command, run_command_with_output_and_env, walk_dir};
33
use std::collections::HashMap;
44
use std::ffi::OsStr;
55
use std::fs;
@@ -103,6 +103,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
103103

104104
pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
105105
let start_dir = Path::new("build_sysroot");
106+
106107
cleanup_sysroot_previous_build(&start_dir);
107108

108109
// Builds libs
@@ -136,13 +137,7 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
136137

137138
// Copy files to sysroot
138139
let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple));
139-
fs::create_dir_all(&sysroot_path).map_err(|error| {
140-
format!(
141-
"Failed to create directory `{}`: {:?}",
142-
sysroot_path.display(),
143-
error
144-
)
145-
})?;
140+
create_dir(&sysroot_path)?;
146141
let copier = |dir_to_copy: &Path| {
147142
// FIXME: should not use shell command!
148143
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
155150

156151
// Copy the source files to the sysroot (Rust for Linux needs this).
157152
let sysroot_src_path = start_dir.join("sysroot/lib/rustlib/src/rust");
158-
fs::create_dir_all(&sysroot_src_path).map_err(|error| {
159-
format!(
160-
"Failed to create directory `{}`: {:?}",
161-
sysroot_src_path.display(),
162-
error
163-
)
164-
})?;
153+
create_dir(&sysroot_src_path)?;
165154
run_command(
166155
&[
167156
&"cp",
@@ -216,12 +205,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
216205
// We voluntarily ignore the error.
217206
let _ = fs::remove_dir_all("target/out");
218207
let gccjit_target = "target/out/gccjit";
219-
fs::create_dir_all(gccjit_target).map_err(|error| {
220-
format!(
221-
"Failed to create directory `{}`: {:?}",
222-
gccjit_target, error
223-
)
224-
})?;
208+
create_dir(gccjit_target)?;
225209

226210
println!("[BUILD] sysroot");
227211
build_sysroot(&env, &args.config_info)?;

build_system/src/config.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
create_symlink, get_os_name, run_command_with_output, rustc_version_info, split_args,
2+
create_dir, create_symlink, get_os_name, run_command_with_output, rustc_version_info, split_args,
33
};
44
use std::collections::HashMap;
55
use std::env as std_env;
@@ -228,13 +228,7 @@ impl ConfigInfo {
228228

229229
let output_dir = output_dir.join(&commit);
230230
if !output_dir.is_dir() {
231-
std::fs::create_dir_all(&output_dir).map_err(|err| {
232-
format!(
233-
"failed to create folder `{}`: {:?}",
234-
output_dir.display(),
235-
err,
236-
)
237-
})?;
231+
create_dir(&output_dir)?;
238232
}
239233
let output_dir = output_dir.canonicalize().map_err(|err| {
240234
format!(

build_system/src/prepare.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::rustc_info::get_rustc_path;
22
use crate::utils::{
3-
cargo_install, git_clone_root_dir, remove_file, run_command, run_command_with_output, walk_dir,
3+
cargo_install, create_dir, git_clone_root_dir, remove_file, run_command, run_command_with_output, walk_dir,
44
};
55

66
use std::fs;
@@ -41,13 +41,7 @@ fn prepare_libcore(
4141
}
4242

4343
let sysroot_library_dir = sysroot_dir.join("library");
44-
fs::create_dir_all(&sysroot_library_dir).map_err(|error| {
45-
format!(
46-
"Failed to create folder `{}`: {:?}",
47-
sysroot_library_dir.display(),
48-
error,
49-
)
50-
})?;
44+
create_dir(&sysroot_library_dir)?;
5145

5246
run_command(
5347
&[&"cp", &"-r", &rustlib_dir.join("library"), &sysroot_dir],

build_system/src/test.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::build;
22
use crate::config::{Channel, ConfigInfo};
33
use crate::utils::{
4-
get_toolchain, git_clone, git_clone_root_dir, remove_file, run_command, run_command_with_env,
4+
create_dir, get_toolchain, git_clone, git_clone_root_dir, remove_file, run_command, run_command_with_env,
55
run_command_with_output_and_env, rustc_version_info, split_args, walk_dir,
66
};
77

88
use std::collections::{BTreeSet, HashMap};
99
use std::ffi::OsStr;
10-
use std::fs::{create_dir_all, remove_dir_all, File};
10+
use std::fs::{remove_dir_all, File};
1111
use std::io::{BufRead, BufReader};
1212
use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
@@ -211,8 +211,7 @@ fn build_if_no_backend(env: &Env, args: &TestArg) -> Result<(), String> {
211211
fn clean(_env: &Env, args: &TestArg) -> Result<(), String> {
212212
let _ = std::fs::remove_dir_all(&args.config_info.cargo_target_dir);
213213
let path = Path::new(&args.config_info.cargo_target_dir).join("gccjit");
214-
std::fs::create_dir_all(&path)
215-
.map_err(|error| format!("failed to create folder `{}`: {:?}", path.display(), error))
214+
create_dir(&path)
216215
}
217216

218217
fn mini_tests(env: &Env, args: &TestArg) -> Result<(), String> {
@@ -715,8 +714,7 @@ fn test_projects(env: &Env, args: &TestArg) -> Result<(), String> {
715714
};
716715

717716
let projects_path = Path::new("projects");
718-
create_dir_all(projects_path)
719-
.map_err(|err| format!("Failed to create directory `projects`: {}", err))?;
717+
create_dir(projects_path)?;
720718

721719
let nb_parts = args.nb_parts.unwrap_or(0);
722720
if nb_parts > 0 {

build_system/src/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ pub fn git_clone(
307307
git_clone_inner(to_clone, dest, shallow_clone, repo_name)
308308
}
309309

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

0 commit comments

Comments
 (0)