nrustc_build/
lib.rs

1#![allow(unused)]
2
3use std::fmt;
4use std::marker::PhantomData;
5use std::path::Path;
6
7pub mod const_global {
8    pub const RUSTC: super::Rustc = super::Rustc::new();
9}
10
11pub mod static_global {
12    pub static RUSTC: super::Rustc = super::Rustc::new();
13}
14
15#[derive(Debug, Clone, Copy)]
16pub struct Rustc {
17    _marker: PhantomData<usize>,
18}
19
20impl Rustc {
21    
22    #[inline]
23    pub(crate) const fn new() -> Rustc {
24        Rustc {
25            _marker: PhantomData,
26        }
27    }
28
29    pub fn println<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
30        let out = msg();
31        println!("{}", out)
32    }
33
34    pub fn println_cargo<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
35        let out = msg();
36        self.println(|| {
37            format!("cargo::{}", out)
38        })
39    }
40
41    pub fn println_clippy<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
42        let out = msg();
43        self.println_cargo(|| {
44            format!("clippy-{}", out)
45        })
46    }
47
48    pub fn println_rustc<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
49        let out = msg();
50        self.println_cargo(|| {
51            format!("rustc-{}", out)
52        })
53    }
54
55    pub fn rerun_if_changed<P: AsRef<Path>>(&self, path: P) {
56        let path2: &Path = path.as_ref();
57        let disp = path2.display();
58        self.println_cargo(|| {
59            format!("rerun-if-changed={}", disp)
60        })
61    }
62
63    pub unsafe fn rustc_check_cfg_raw<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
64        let out = msg();
65        self.println_rustc(|| {
66            format!("check-cfg={}", out)
67        })
68    }
69
70    pub fn rustc_check_cfg<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
71        unsafe {
72            let out = msg();
73            let out_str = out.to_string();
74            self.rustc_check_cfg_raw(|| {
75                if out_str.starts_with("cfg(") && out_str.ends_with(")") {
76                    out_str
77                } else {
78                    format!("cfg({})", out_str)
79                }
80            })
81        }
82    }
83
84    pub fn rustc_check_cfg_wrap_key(&self, cfg: &str) {
85        self.rustc_check_cfg(|| format!("cfg({})", cfg))
86    }
87
88    pub fn rustc_check_cfg_wrap_key_value(&self, cfg: &str, value: &str) {
89        self.rustc_check_cfg(|| format!("cfg({}, values(\"{}\"))", cfg, value))
90    }
91
92    pub fn rustc_check_cfg_wrap_key_values(&self, cfg: &str, values: &[&str]) {
93        let mut iter = values.into_iter();
94
95        while let Some(value) = iter.next() {
96            self.rustc_check_cfg_wrap_key_value(cfg, value);
97        }
98    }
99
100    pub fn rustc_cfg<F: FnOnce() -> D, D: fmt::Display>(&self, msg: F) {
101        self.println_rustc(|| {
102            let out = msg();
103            format!("cfg={}", out)
104        })
105    }
106
107    pub fn rustc_cfg_wrap_key(&self, cfg: &str) {
108        self.rustc_cfg(|| {
109            cfg
110        })
111    }
112
113    pub fn rustc_cfg_wrap_key_if_env_set(&self, cfg: &str, env: &str) {
114        let envu = env.to_uppercase();
115        if std::env::var_os(&envu).is_some() {
116            self.rustc_cfg_wrap_key(cfg);
117        }
118    }
119
120    pub fn rustc_cfg_wrap_key_if_env_has_value(&self, cfg: &str, env: &str, env_value: &str) {
121        let envu = env.to_uppercase();
122        let vall = env_value.to_lowercase();
123
124        if let Ok(envval) = std::env::var(envu) {
125            if envval.to_lowercase() == vall {
126                self.rustc_cfg_wrap_key(cfg);
127            }
128        }
129    }
130
131    pub fn rustc_cfg_wrap_key_value(&self, cfg: &str, value: &str) {
132        self.rustc_cfg(|| format!("{}=\"{}\"", cfg, value))
133    }
134    
135    pub fn rustc_cfg_wrap_key_value_if_env_set(&self, cfg: &str, value: &str, env: &str) {
136        let envu = env.to_uppercase();
137        if std::env::var_os(&envu).is_some() {
138            self.rustc_cfg_wrap_key_value(cfg, value);
139        }
140    }
141
142    pub fn rustc_cfg_wrap_key_value_if_env_has_value(&self, cfg: &str, value: &str, env: &str, env_value: &str) {
143        let envu = env.to_uppercase();
144        let vall = env_value.to_lowercase();
145
146        if let Ok(envval) = std::env::var(envu) {
147            if envval.to_lowercase() == vall {
148                self.rustc_cfg_wrap_key_value(cfg, value)
149            }
150        }
151    }
152
153    pub fn rustc_cfg_wrap_key_values(&self, cfg: &str, values: &[&str]) {
154        let mut iter = values.into_iter();
155        
156        while let Some(value) = iter.next() {
157            self.rustc_cfg_wrap_key_value(cfg, value);
158        }
159    }
160
161    pub fn rustc_cfg_wrap_key_values_if_env_set(&self, cfg: &str, values: &[&str], env: &str) {
162        let envu = env.to_uppercase();
163
164        if std::env::var_os(&envu).is_some() {
165            self.rustc_cfg_wrap_key_values(cfg, values);
166        }
167    }
168
169    pub fn rustc_cfg_wrap_key_values_if_env_has_value(&self, cfg: &str, values: &[&str], env: &str, env_value: &str) {
170        let envu = env.to_uppercase();
171        let vall = env_value.to_lowercase();
172
173        if let Ok(envval) = std::env::var(envu) {
174            if envval.to_lowercase() == vall {
175                self.rustc_cfg_wrap_key_values(cfg, values);
176            }
177        }
178    }
179}