Skip to content

Commit d986227

Browse files
committed
Refactor and reduce usage of RelPath
1 parent f204181 commit d986227

File tree

9 files changed

+50
-55
lines changed

9 files changed

+50
-55
lines changed

build_system/abi_cafe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::path::{Dirs, RelPath};
1+
use crate::path::Dirs;
22
use crate::prepare::GitRepo;
33
use crate::utils::{spawn_and_wait, CargoProject, Compiler};
44
use crate::{build_sysroot, CodegenBackend, SysrootKind};
@@ -20,7 +20,7 @@ pub(crate) fn run(
2020
rustup_toolchain_name: Option<&str>,
2121
bootstrap_host_compiler: &Compiler,
2222
) {
23-
RelPath::DOWNLOAD.ensure_exists(dirs);
23+
std::fs::create_dir_all(&dirs.download_dir).unwrap();
2424
ABI_CAFE_REPO.fetch(dirs);
2525
ABI_CAFE_REPO.patch(dirs);
2626

build_system/bench.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::path::Path;
44
use std::process::Command;
55

6-
use crate::path::{Dirs, RelPath};
6+
use crate::path::Dirs;
77
use crate::prepare::GitRepo;
88
use crate::rustc_info::get_file_name;
99
use crate::utils::{spawn_and_wait, Compiler};
@@ -39,11 +39,11 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
3939
};
4040

4141
eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
42-
let cargo_clif = RelPath::DIST
43-
.to_path(dirs)
42+
let cargo_clif = dirs
43+
.dist_dir
4444
.join(get_file_name(&bootstrap_host_compiler.rustc, "cargo_clif", "bin").replace('_', "-"));
4545
let manifest_path = SIMPLE_RAYTRACER_REPO.source_dir().to_path(dirs).join("Cargo.toml");
46-
let target_dir = RelPath::BUILD.join("simple_raytracer").to_path(dirs);
46+
let target_dir = dirs.build_dir.join("simple_raytracer");
4747

4848
let clean_cmd = format!(
4949
"RUSTC=rustc cargo clean --manifest-path {manifest_path} --target-dir {target_dir}",
@@ -68,7 +68,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
6868
target_dir = target_dir.display(),
6969
);
7070

71-
let bench_compile_markdown = RelPath::DIST.to_path(dirs).join("bench_compile.md");
71+
let bench_compile_markdown = dirs.dist_dir.join("bench_compile.md");
7272

7373
let bench_compile = hyperfine_command(
7474
1,
@@ -92,7 +92,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
9292

9393
eprintln!("[BENCH RUN] ebobby/simple-raytracer");
9494

95-
let bench_run_markdown = RelPath::DIST.to_path(dirs).join("bench_run.md");
95+
let bench_run_markdown = dirs.dist_dir.join("bench_run.md");
9696

9797
let raytracer_cg_llvm = Path::new(".").join(get_file_name(
9898
&bootstrap_host_compiler.rustc,
@@ -120,7 +120,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
120120
],
121121
&bench_run_markdown,
122122
);
123-
bench_run.current_dir(RelPath::BUILD.to_path(dirs));
123+
bench_run.current_dir(&dirs.build_dir);
124124
spawn_and_wait(bench_run);
125125

126126
if let Some(gha_step_summary) = gha_step_summary.as_mut() {

build_system/build_backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::rustc_info::get_file_name;
66
use crate::shared_utils::{rustflags_from_env, rustflags_to_cmd_env};
77
use crate::utils::{CargoProject, Compiler, LogGroup};
88

9-
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
9+
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::source("."), "cg_clif");
1010

1111
pub(crate) fn build_backend(
1212
dirs: &Dirs,

build_system/build_sysroot.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::utils::{
1010
};
1111
use crate::{config, CodegenBackend, SysrootKind};
1212

13-
static DIST_DIR: RelPath = RelPath::DIST;
14-
static BIN_DIR: RelPath = RelPath::DIST.join("bin");
15-
static LIB_DIR: RelPath = RelPath::DIST.join("lib");
13+
static DIST_DIR: RelPath = RelPath::dist(".");
14+
static BIN_DIR: RelPath = RelPath::dist("bin");
15+
static LIB_DIR: RelPath = RelPath::dist("lib");
1616

1717
pub(crate) fn build_sysroot(
1818
dirs: &Dirs,
@@ -26,9 +26,10 @@ pub(crate) fn build_sysroot(
2626

2727
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
2828

29-
DIST_DIR.ensure_fresh(dirs);
30-
BIN_DIR.ensure_exists(dirs);
31-
LIB_DIR.ensure_exists(dirs);
29+
remove_dir_if_exists(&dirs.dist_dir);
30+
fs::create_dir_all(&dirs.dist_dir).unwrap();
31+
fs::create_dir_all(BIN_DIR.to_path(dirs)).unwrap();
32+
fs::create_dir_all(LIB_DIR.to_path(dirs)).unwrap();
3233

3334
let is_native = bootstrap_host_compiler.triple == target_triple;
3435

@@ -58,7 +59,7 @@ pub(crate) fn build_sysroot(
5859
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
5960
let wrapper_path = DIST_DIR.to_path(dirs).join(&wrapper_name);
6061
build_cargo_wrapper_cmd
61-
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
62+
.arg(dirs.source_dir.join("scripts").join(&format!("{wrapper}.rs")))
6263
.arg("-o")
6364
.arg(&wrapper_path)
6465
.arg("-Cstrip=debuginfo");
@@ -116,10 +117,8 @@ pub(crate) fn build_sysroot(
116117

117118
let mut target_compiler = {
118119
let dirs: &Dirs = &dirs;
119-
let rustc_clif =
120-
RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustc-clif"));
121-
let rustdoc_clif =
122-
RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif"));
120+
let rustc_clif = dirs.dist_dir.join(wrapper_base_name.replace("____", "rustc-clif"));
121+
let rustdoc_clif = dirs.dist_dir.join(wrapper_base_name.replace("____", "rustdoc-clif"));
123122

124123
Compiler {
125124
cargo: bootstrap_host_compiler.cargo.clone(),
@@ -158,10 +157,10 @@ impl SysrootTarget {
158157
}
159158
}
160159

161-
static STDLIB_SRC: RelPath = RelPath::BUILD.join("stdlib");
160+
static STDLIB_SRC: RelPath = RelPath::build("stdlib");
162161
static STANDARD_LIBRARY: CargoProject =
163-
CargoProject::new(&STDLIB_SRC.join("library/sysroot"), "stdlib_target");
164-
static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
162+
CargoProject::new(&RelPath::build("stdlib/library/sysroot"), "stdlib_target");
163+
static RTSTARTUP_SYSROOT: RelPath = RelPath::build("rtstartup");
165164

166165
fn build_sysroot_for_triple(
167166
dirs: &Dirs,

build_system/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,11 @@ fn main() {
185185
frozen,
186186
};
187187

188-
path::RelPath::BUILD.ensure_exists(&dirs);
188+
std::fs::create_dir_all(&dirs.build_dir).unwrap();
189189

190190
{
191191
// Make sure we always explicitly specify the target dir
192-
let target =
193-
path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
192+
let target = dirs.build_dir.join("target_dir_should_be_set_explicitly");
194193
env::set_var("CARGO_TARGET_DIR", &target);
195194
let _ = std::fs::remove_file(&target);
196195
std::fs::File::create(target).unwrap();

build_system/path.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,30 @@ impl PathBase {
3333
}
3434

3535
#[derive(Debug, Copy, Clone)]
36-
pub(crate) enum RelPath {
37-
Base(PathBase),
38-
Join(&'static RelPath, &'static str),
36+
pub(crate) struct RelPath {
37+
base: PathBase,
38+
suffix: &'static str,
3939
}
4040

4141
impl RelPath {
42-
pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
43-
pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download);
44-
pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
45-
pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);
42+
pub(crate) const fn source(suffix: &'static str) -> RelPath {
43+
RelPath { base: PathBase::Source, suffix }
44+
}
4645

47-
pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
48-
pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");
46+
pub(crate) const fn download(suffix: &'static str) -> RelPath {
47+
RelPath { base: PathBase::Download, suffix }
48+
}
4949

50-
pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
51-
RelPath::Join(self, suffix)
50+
pub(crate) const fn build(suffix: &'static str) -> RelPath {
51+
RelPath { base: PathBase::Build, suffix }
5252
}
5353

54-
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
55-
match self {
56-
RelPath::Base(base) => base.to_path(dirs),
57-
RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix),
58-
}
54+
pub(crate) const fn dist(suffix: &'static str) -> RelPath {
55+
RelPath { base: PathBase::Dist, suffix }
5956
}
6057

61-
pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
62-
fs::create_dir_all(self.to_path(dirs)).unwrap();
58+
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
59+
self.base.to_path(dirs).join(self.suffix)
6360
}
6461

6562
pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {

build_system/prepare.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::path::{Dirs, RelPath};
88
use crate::utils::{copy_dir_recursively, remove_dir_if_exists, spawn_and_wait};
99

1010
pub(crate) fn prepare(dirs: &Dirs) {
11-
RelPath::DOWNLOAD.ensure_exists(dirs);
11+
std::fs::create_dir_all(&dirs.download_dir).unwrap();
1212
crate::tests::RAND_REPO.fetch(dirs);
1313
crate::tests::REGEX_REPO.fetch(dirs);
1414
}
@@ -79,13 +79,13 @@ impl GitRepo {
7979

8080
fn download_dir(&self, dirs: &Dirs) -> PathBuf {
8181
match self.url {
82-
GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo).to_path(dirs),
82+
GitRepoUrl::Github { user: _, repo } => RelPath::download(repo).to_path(dirs),
8383
}
8484
}
8585

8686
pub(crate) const fn source_dir(&self) -> RelPath {
8787
match self.url {
88-
GitRepoUrl::Github { user: _, repo } => RelPath::BUILD.join(repo),
88+
GitRepoUrl::Github { user: _, repo } => RelPath::build(repo),
8989
}
9090
}
9191

@@ -117,7 +117,7 @@ impl GitRepo {
117117
}
118118

119119
let source_lockfile =
120-
RelPath::PATCHES.to_path(dirs).join(format!("{}-lock.toml", self.patch_name));
120+
dirs.source_dir.join("patches").join(format!("{}-lock.toml", self.patch_name));
121121
let target_lockfile = download_dir.join("Cargo.lock");
122122
if source_lockfile.exists() {
123123
assert!(!target_lockfile.exists());
@@ -178,7 +178,7 @@ fn init_git_repo(repo_dir: &Path) {
178178
}
179179

180180
fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
181-
let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
181+
let mut patches: Vec<_> = fs::read_dir(dirs.source_dir.join("patches"))
182182
.unwrap()
183183
.map(|entry| entry.unwrap().path())
184184
.filter(|path| path.extension() == Some(OsStr::new("patch")))

build_system/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::shared_utils::rustflags_from_env;
1010
use crate::utils::{spawn_and_wait, CargoProject, Compiler, LogGroup};
1111
use crate::{build_sysroot, config, CodegenBackend, SysrootKind};
1212

13-
static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
13+
static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::build("example");
1414

1515
struct TestCase {
1616
config: &'static str,
@@ -129,11 +129,11 @@ pub(crate) static REGEX_REPO: GitRepo = GitRepo::github(
129129

130130
static REGEX: CargoProject = CargoProject::new(&REGEX_REPO.source_dir(), "regex_target");
131131

132-
static PORTABLE_SIMD_SRC: RelPath = RelPath::BUILD.join("portable-simd");
132+
static PORTABLE_SIMD_SRC: RelPath = RelPath::build("portable-simd");
133133

134134
static PORTABLE_SIMD: CargoProject = CargoProject::new(&PORTABLE_SIMD_SRC, "portable-simd_target");
135135

136-
static LIBCORE_TESTS_SRC: RelPath = RelPath::BUILD.join("coretests");
136+
static LIBCORE_TESTS_SRC: RelPath = RelPath::build("coretests");
137137

138138
static LIBCORE_TESTS: CargoProject = CargoProject::new(&LIBCORE_TESTS_SRC, "coretests_target");
139139

@@ -162,7 +162,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
162162
&LIBCORE_TESTS_SRC.to_path(&runner.dirs),
163163
);
164164

165-
let source_lockfile = RelPath::PATCHES.to_path(&runner.dirs).join("coretests-lock.toml");
165+
let source_lockfile = runner.dirs.source_dir.join("patches/coretests-lock.toml");
166166
let target_lockfile = LIBCORE_TESTS_SRC.to_path(&runner.dirs).join("Cargo.lock");
167167
fs::copy(source_lockfile, target_lockfile).unwrap();
168168

build_system/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl CargoProject {
8080
}
8181

8282
pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
83-
RelPath::BUILD.join(self.target).to_path(dirs)
83+
dirs.build_dir.join(self.target)
8484
}
8585

8686
#[must_use]

0 commit comments

Comments
 (0)