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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
pub mod constants {
    pub const CMAKE_BUILD_MODE: &str = "MinSizeRelAssert";

    #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
    pub const CMAKE_BUILD_TARGET: &str = "macosx-x86_64";
    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
    pub const CMAKE_BUILD_TARGET: &str = "macosx-arm64";
    #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
    pub const CMAKE_BUILD_TARGET: &str = "linux-x86_64";
    #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
    pub const CMAKE_BUILD_TARGET: &str = "linux-aarch64";

    #[allow(non_snake_case)]
    pub fn NINJA_BUILD_DIR() -> String {
        format!("Ninja-{CMAKE_BUILD_MODE}")
    }
}

pub mod errors {
    pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
    pub type BoxResult<T> = Result<T, BoxError>;
}

pub mod traits {
    pub trait BuildExt: crate::traits::sealed::BuildExt {
        fn llvm_common_compiler(&mut self) -> &mut cc::Build;
        fn llvm_common_defines(&mut self) -> &mut cc::Build;
        fn llvm_common_flags(&mut self) -> &mut cc::Build;
    }

    impl BuildExt for cc::Build {
        fn llvm_common_compiler(&mut self) -> &mut cc::Build {
            self.compiler("clang++")
        }

        fn llvm_common_defines(&mut self) -> &mut cc::Build {
            self.define("_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", None)
                .define("_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS", None)
                .define("RUST_CXX_NO_EXCEPTIONS", None)
        }

        fn llvm_common_flags(&mut self) -> &mut cc::Build {
            self.flag_if_supported("-std=gnu++20")
                .flag_if_supported("-Werror")
                .flag_if_supported("-Wall")
                .flag_if_supported("-Wextra")
                .flag_if_supported("-Wthread-safety")
                .flag_if_supported("-Wthread-safety-beta")
                .flag_if_supported("-pedantic")
                .flag_if_supported("-Wno-ambiguous-reversed-operator")
                .flag_if_supported("-Wno-deprecated-anon-enum-enum-conversion")
                .flag_if_supported("-Wno-deprecated-builtins")
                .flag_if_supported("-Wno-deprecated-declarations")
                .flag_if_supported("-Wno-dollar-in-identifier-extension")
                .flag_if_supported("-Wno-nested-anon-types")
                .flag_if_supported("-Wno-unused-parameter")
                .flag_if_supported("-fno-exceptions")
                .flag_if_supported("-fno-rtti")
        }
    }

    pub(crate) mod sealed {
        pub trait BuildExt {}
        impl BuildExt for cc::Build {
        }
    }
}

pub mod util {
    use crate::errors::*;
    use std::path::PathBuf;

    pub fn get_path_from_env(var: &str, should_ignore: &mut bool) -> BoxResult<Option<PathBuf>> {
        if let Ok(path) = std::env::var(var) {
            if path.is_empty() {
                *should_ignore = true;
                return Ok(None);
            }
            let path = PathBuf::from(path);
            if !path.is_absolute() {
                return Err(format!(
                    "`{var}` specified but given path:\n\t\"{}\"\nis not absolute",
                    path.display()
                )
                .into());
            }
            if !path.exists() {
                return Err(format!(
                    "`{var}` specified but given path:\n\t\"{}\"\ndoes not exist",
                    path.display()
                )
                .into());
            }
            return Ok(Some(path));
        } else {
            return Ok(None);
        }
    }
}

pub mod prelude {
    pub use crate::{constants::*, errors::*, traits::*, util::*};
}