cxx_llvm_build_common/
lib.rs

1pub mod constants {
2    pub const CMAKE_BUILD_MODE: &str = "MinSizeRelAssert";
3
4    #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
5    pub const CMAKE_BUILD_TARGET: &str = "macosx-x86_64";
6    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
7    pub const CMAKE_BUILD_TARGET: &str = "macosx-arm64";
8    #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
9    pub const CMAKE_BUILD_TARGET: &str = "linux-x86_64";
10    #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
11    pub const CMAKE_BUILD_TARGET: &str = "linux-aarch64";
12
13    #[allow(non_snake_case)]
14    pub fn NINJA_BUILD_DIR() -> String {
15        format!("Ninja-{CMAKE_BUILD_MODE}")
16    }
17}
18
19pub mod errors {
20    pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
21    pub type BoxResult<T> = Result<T, BoxError>;
22}
23
24pub mod traits {
25    pub trait BuildExt: crate::traits::sealed::BuildExt {
26        fn llvm_common_compiler(&mut self) -> &mut cc::Build;
27        fn llvm_common_defines(&mut self) -> &mut cc::Build;
28        fn llvm_common_flags(&mut self) -> &mut cc::Build;
29    }
30
31    impl BuildExt for cc::Build {
32        fn llvm_common_compiler(&mut self) -> &mut cc::Build {
33            self.compiler("clang++")
34        }
35
36        fn llvm_common_defines(&mut self) -> &mut cc::Build {
37            self.define("_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", None)
38                .define("_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS", None)
39                .define("RUST_CXX_NO_EXCEPTIONS", None)
40        }
41
42        fn llvm_common_flags(&mut self) -> &mut cc::Build {
43            self.flag_if_supported("-std=gnu++20")
44                .flag_if_supported("-Werror")
45                .flag_if_supported("-Wall")
46                .flag_if_supported("-Wextra")
47                .flag_if_supported("-Wthread-safety")
48                .flag_if_supported("-Wthread-safety-beta")
49                .flag_if_supported("-pedantic")
50                .flag_if_supported("-Wno-ambiguous-reversed-operator")
51                .flag_if_supported("-Wno-deprecated-anon-enum-enum-conversion")
52                .flag_if_supported("-Wno-deprecated-builtins")
53                .flag_if_supported("-Wno-deprecated-declarations")
54                .flag_if_supported("-Wno-dollar-in-identifier-extension")
55                .flag_if_supported("-Wno-nested-anon-types")
56                .flag_if_supported("-Wno-unused-parameter")
57                .flag_if_supported("-fno-exceptions")
58                .flag_if_supported("-fno-rtti")
59        }
60    }
61
62    pub(crate) mod sealed {
63        pub trait BuildExt {}
64        impl BuildExt for cc::Build {
65        }
66    }
67}
68
69pub mod util {
70    use crate::errors::*;
71    use std::path::PathBuf;
72
73    pub fn get_path_from_env(var: &str, should_ignore: &mut bool) -> BoxResult<Option<PathBuf>> {
74        if let Ok(path) = std::env::var(var) {
75            if path.is_empty() {
76                *should_ignore = true;
77                return Ok(None);
78            }
79            let path = PathBuf::from(path);
80            if !path.is_absolute() {
81                return Err(format!(
82                    "`{var}` specified but given path:\n\t\"{}\"\nis not absolute",
83                    path.display()
84                )
85                .into());
86            }
87            if !path.exists() {
88                return Err(format!(
89                    "`{var}` specified but given path:\n\t\"{}\"\ndoes not exist",
90                    path.display()
91                )
92                .into());
93            }
94            Ok(Some(path))
95        } else {
96            Ok(None)
97        }
98    }
99}
100
101pub mod prelude {
102    pub use crate::{constants::*, errors::*, traits::*, util::*};
103}