Skip to content

Commit fdca75c

Browse files
committed
Refactor and reduce usage of RelPath
1 parent 5763c09 commit fdca75c

File tree

8 files changed

+35
-49
lines changed

8 files changed

+35
-49
lines changed

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::{Compiler, spawn_and_wait};
@@ -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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ 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-
ensure_empty_dir(&dist_dir);
27+
ensure_empty_dir(dist_dir);
2828
fs::create_dir_all(dist_dir.join("bin")).unwrap();
2929
fs::create_dir_all(dist_dir.join("lib")).unwrap();
3030

@@ -55,7 +55,7 @@ pub(crate) fn build_sysroot(
5555
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
5656
let wrapper_path = dist_dir.join(&wrapper_name);
5757
build_cargo_wrapper_cmd
58-
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
58+
.arg(dirs.source_dir.join("scripts").join(&format!("{wrapper}.rs")))
5959
.arg("-o")
6060
.arg(&wrapper_path)
6161
.arg("-Cstrip=debuginfo");
@@ -85,7 +85,7 @@ pub(crate) fn build_sysroot(
8585
&cg_clif_dylib_path,
8686
sysroot_kind,
8787
);
88-
host.install_into_sysroot(&dist_dir);
88+
host.install_into_sysroot(dist_dir);
8989

9090
if !is_native {
9191
build_sysroot_for_triple(
@@ -99,7 +99,7 @@ pub(crate) fn build_sysroot(
9999
&cg_clif_dylib_path,
100100
sysroot_kind,
101101
)
102-
.install_into_sysroot(&dist_dir);
102+
.install_into_sysroot(dist_dir);
103103
}
104104

105105
let mut target_compiler = {
@@ -143,10 +143,10 @@ impl SysrootTarget {
143143
}
144144
}
145145

146-
static STDLIB_SRC: RelPath = RelPath::BUILD.join("stdlib");
146+
static STDLIB_SRC: RelPath = RelPath::build("stdlib");
147147
static STANDARD_LIBRARY: CargoProject =
148-
CargoProject::new(&STDLIB_SRC.join("library/sysroot"), "stdlib_target");
149-
static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
148+
CargoProject::new(&RelPath::build("stdlib/library/sysroot"), "stdlib_target");
149+
static RTSTARTUP_SYSROOT: RelPath = RelPath::build("rtstartup");
150150

151151
fn build_sysroot_for_triple(
152152
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: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fs;
21
use std::path::PathBuf;
32

43
#[derive(Debug, Clone)]
@@ -15,45 +14,33 @@ pub(crate) struct Dirs {
1514
pub(crate) enum PathBase {
1615
Source,
1716
Build,
18-
Dist,
1917
}
2018

2119
impl PathBase {
2220
fn to_path(self, dirs: &Dirs) -> PathBuf {
2321
match self {
2422
PathBase::Source => dirs.source_dir.clone(),
2523
PathBase::Build => dirs.build_dir.clone(),
26-
PathBase::Dist => dirs.dist_dir.clone(),
2724
}
2825
}
2926
}
3027

3128
#[derive(Debug, Copy, Clone)]
32-
pub(crate) enum RelPath {
33-
Base(PathBase),
34-
Join(&'static RelPath, &'static str),
29+
pub(crate) struct RelPath {
30+
base: PathBase,
31+
suffix: &'static str,
3532
}
3633

3734
impl RelPath {
38-
pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
39-
pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
40-
pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);
41-
42-
pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
43-
pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");
44-
45-
pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
46-
RelPath::Join(self, suffix)
35+
pub(crate) const fn source(suffix: &'static str) -> RelPath {
36+
RelPath { base: PathBase::Source, suffix }
4737
}
4838

49-
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
50-
match self {
51-
RelPath::Base(base) => base.to_path(dirs),
52-
RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix),
53-
}
39+
pub(crate) const fn build(suffix: &'static str) -> RelPath {
40+
RelPath { base: PathBase::Build, suffix }
5441
}
5542

56-
pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
57-
fs::create_dir_all(self.to_path(dirs)).unwrap();
43+
pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
44+
self.base.to_path(dirs).join(self.suffix)
5845
}
5946
}

build_system/prepare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl GitRepo {
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

@@ -130,7 +130,7 @@ impl GitRepo {
130130
}
131131

132132
let source_lockfile =
133-
RelPath::PATCHES.to_path(dirs).join(format!("{}-lock.toml", self.patch_name));
133+
dirs.source_dir.join("patches").join(format!("{}-lock.toml", self.patch_name));
134134
let target_lockfile = download_dir.join("Cargo.lock");
135135
if source_lockfile.exists() {
136136
assert!(!target_lockfile.exists());
@@ -191,7 +191,7 @@ fn init_git_repo(repo_dir: &Path) {
191191
}
192192

193193
fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
194-
let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
194+
let mut patches: Vec<_> = fs::read_dir(dirs.source_dir.join("patches"))
195195
.unwrap()
196196
.map(|entry| entry.unwrap().path())
197197
.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::{CargoProject, Compiler, LogGroup, ensure_empty_dir, spawn_and_wait};
1111
use crate::{CodegenBackend, SysrootKind, build_sysroot, config};
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
@@ -93,7 +93,7 @@ impl CargoProject {
9393
}
9494

9595
pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf {
96-
RelPath::BUILD.join(self.target).to_path(dirs)
96+
dirs.build_dir.join(self.target)
9797
}
9898

9999
#[must_use]

0 commit comments

Comments
 (0)