summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_cranelift/build_system/path.rs
blob: e93981f1d64d369b28672c0e61ff5a533c271a23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub(crate) struct Dirs {
    pub(crate) source_dir: PathBuf,
    pub(crate) download_dir: PathBuf,
    pub(crate) build_dir: PathBuf,
    pub(crate) dist_dir: PathBuf,
}

#[doc(hidden)]
#[derive(Debug, Copy, Clone)]
pub(crate) enum PathBase {
    Source,
    Download,
    Build,
    Dist,
}

impl PathBase {
    fn to_path(self, dirs: &Dirs) -> PathBuf {
        match self {
            PathBase::Source => dirs.source_dir.clone(),
            PathBase::Download => dirs.download_dir.clone(),
            PathBase::Build => dirs.build_dir.clone(),
            PathBase::Dist => dirs.dist_dir.clone(),
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum RelPath {
    Base(PathBase),
    Join(&'static RelPath, &'static str),
}

impl RelPath {
    pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source);
    pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download);
    pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build);
    pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist);

    pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts");
    pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
    pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches");

    pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath {
        RelPath::Join(self, suffix)
    }

    pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf {
        match self {
            RelPath::Base(base) => base.to_path(dirs),
            RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix),
        }
    }

    pub(crate) fn ensure_exists(&self, dirs: &Dirs) {
        fs::create_dir_all(self.to_path(dirs)).unwrap();
    }

    pub(crate) fn ensure_fresh(&self, dirs: &Dirs) {
        let path = self.to_path(dirs);
        if path.exists() {
            fs::remove_dir_all(&path).unwrap();
        }
        fs::create_dir_all(path).unwrap();
    }
}