Skip to content

Commit b922a59

Browse files
committed
Refactor and reduce usage of RelPath
1 parent d5e2e23 commit b922a59

File tree

9 files changed

+43
-53
lines changed

9 files changed

+43
-53
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub(crate) fn build_sysroot(
2222

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

25-
let dist_dir = RelPath::DIST.to_path(dirs);
25+
let dist_dir = &dirs.dist_dir;
2626

27-
remove_dir_if_exists(&dist_dir);
28-
fs::create_dir_all(&dist_dir).unwrap();
27+
remove_dir_if_exists(dist_dir);
28+
fs::create_dir_all(dist_dir).unwrap();
2929
fs::create_dir_all(dist_dir.join("bin")).unwrap();
3030
fs::create_dir_all(dist_dir.join("lib")).unwrap();
3131

@@ -56,7 +56,7 @@ pub(crate) fn build_sysroot(
5656
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
5757
let wrapper_path = dist_dir.join(&wrapper_name);
5858
build_cargo_wrapper_cmd
59-
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
59+
.arg(dirs.source_dir.join("scripts").join(&format!("{wrapper}.rs")))
6060
.arg("-o")
6161
.arg(&wrapper_path)
6262
.arg("-Cstrip=debuginfo");
@@ -86,7 +86,7 @@ pub(crate) fn build_sysroot(
8686
&cg_clif_dylib_path,
8787
sysroot_kind,
8888
);
89-
host.install_into_sysroot(&dist_dir);
89+
host.install_into_sysroot(dist_dir);
9090

9191
if !is_native {
9292
build_sysroot_for_triple(
@@ -100,7 +100,7 @@ pub(crate) fn build_sysroot(
100100
&cg_clif_dylib_path,
101101
sysroot_kind,
102102
)
103-
.install_into_sysroot(&dist_dir);
103+
.install_into_sysroot(dist_dir);
104104
}
105105

106106
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
@@ -153,10 +153,10 @@ impl SysrootTarget {
153153
}
154154
}
155155

156-
static STDLIB_SRC: RelPath = RelPath::BUILD.join("stdlib");
156+
static STDLIB_SRC: RelPath = RelPath::build("stdlib");
157157
static STANDARD_LIBRARY: CargoProject =
158-
CargoProject::new(&STDLIB_SRC.join("library/sysroot"), "stdlib_target");
159-
static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
158+
CargoProject::new(&RelPath::build("stdlib/library/sysroot"), "stdlib_target");
159+
static RTSTARTUP_SYSROOT: RelPath = RelPath::build("rtstartup");
160160

161161
fn build_sysroot_for_triple(
162162
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: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub(crate) enum PathBase {
1818
Source,
1919
Download,
2020
Build,
21-
Dist,
2221
}
2322

2423
impl PathBase {
@@ -27,39 +26,31 @@ impl PathBase {
2726
PathBase::Source => dirs.source_dir.clone(),
2827
PathBase::Download => dirs.download_dir.clone(),
2928
PathBase::Build => dirs.build_dir.clone(),
30-
PathBase::Dist => dirs.dist_dir.clone(),
3129
}
3230
}
3331
}
3432

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

4139
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);
46-
47-
pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
48-
pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");
40+
pub(crate) const fn source(suffix: &'static str) -> RelPath {
41+
RelPath { base: PathBase::Source, suffix }
42+
}
4943

50-
pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
51-
RelPath::Join(self, suffix)
44+
pub(crate) const fn download(suffix: &'static str) -> RelPath {
45+
RelPath { base: PathBase::Download, suffix }
5246
}
5347

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-
}
48+
pub(crate) const fn build(suffix: &'static str) -> RelPath {
49+
RelPath { base: PathBase::Build, suffix }
5950
}
6051

61-
pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
62-
fs::create_dir_all(self.to_path(dirs)).unwrap();
52+
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
53+
self.base.to_path(dirs).join(self.suffix)
6354
}
6455

6556
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)