]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/build_system/path.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / build_system / path.rs
CommitLineData
9c376795
FG
1use std::fs;
2use std::path::PathBuf;
3
4#[derive(Debug, Clone)]
5pub(crate) struct Dirs {
6 pub(crate) source_dir: PathBuf,
7 pub(crate) download_dir: PathBuf,
8 pub(crate) build_dir: PathBuf,
9 pub(crate) dist_dir: PathBuf,
10}
11
12#[doc(hidden)]
13#[derive(Debug, Copy, Clone)]
14pub(crate) enum PathBase {
15 Source,
16 Download,
17 Build,
18 Dist,
19}
20
21impl PathBase {
22 fn to_path(self, dirs: &Dirs) -> PathBuf {
23 match self {
24 PathBase::Source => dirs.source_dir.clone(),
25 PathBase::Download => dirs.download_dir.clone(),
26 PathBase::Build => dirs.build_dir.clone(),
27 PathBase::Dist => dirs.dist_dir.clone(),
28 }
29 }
30}
31
32#[derive(Debug, Copy, Clone)]
33pub(crate) enum RelPath {
34 Base(PathBase),
35 Join(&'static RelPath, &'static str),
36}
37
38impl RelPath {
39 pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
40 pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download);
41 pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
42 pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);
43
44 pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
45 pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
46 pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");
47
48 pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
49 RelPath::Join(self, suffix)
50 }
51
52 pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
53 match self {
54 RelPath::Base(base) => base.to_path(dirs),
55 RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix),
56 }
57 }
58
59 pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
60 fs::create_dir_all(self.to_path(dirs)).unwrap();
61 }
62
63 pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
64 let path = self.to_path(dirs);
65 if path.exists() {
66 fs::remove_dir_all(&path).unwrap();
67 }
68 fs::create_dir_all(path).unwrap();
69 }
70}