]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/path.rs
Rollup merge of #94145 - ssomers:binary_heap_tests, r=jyn514
[rust.git] / compiler / rustc_codegen_cranelift / build_system / path.rs
1 use std::fs;
2 use std::path::PathBuf;
3
4 #[derive(Debug, Clone)]
5 pub(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)]
14 pub(crate) enum PathBase {
15     Source,
16     Download,
17     Build,
18     Dist,
19 }
20
21 impl 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)]
33 pub(crate) enum RelPath {
34     Base(PathBase),
35     Join(&'static RelPath, &'static str),
36 }
37
38 impl 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 }