]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use crate::common::Config; |
85aaf69f | 2 | use std::env; |
dfeec247 | 3 | use std::ffi::OsStr; |
94b46f34 | 4 | use std::path::PathBuf; |
1a4d82fc | 5 | |
3dfed10e | 6 | use tracing::*; |
48663c56 | 7 | |
416331ca XL |
8 | #[cfg(test)] |
9 | mod tests; | |
10 | ||
1a4d82fc | 11 | /// Conversion table from triple OS name to Rust SYSNAME |
3dfed10e | 12 | const OS_TABLE: &[(&str, &str)] = &[ |
32a655c1 | 13 | ("android", "android"), |
0531ce1d | 14 | ("androideabi", "android"), |
9fa01778 | 15 | ("cuda", "cuda"), |
32a655c1 SL |
16 | ("darwin", "macos"), |
17 | ("dragonfly", "dragonfly"), | |
0531ce1d | 18 | ("emscripten", "emscripten"), |
32a655c1 | 19 | ("freebsd", "freebsd"), |
0531ce1d | 20 | ("fuchsia", "fuchsia"), |
32a655c1 | 21 | ("haiku", "haiku"), |
b7449926 | 22 | ("hermit", "hermit"), |
ba9703b0 | 23 | ("illumos", "illumos"), |
32a655c1 | 24 | ("ios", "ios"), |
0531ce1d | 25 | ("l4re", "l4re"), |
32a655c1 SL |
26 | ("linux", "linux"), |
27 | ("mingw32", "windows"), | |
9fa01778 | 28 | ("none", "none"), |
32a655c1 SL |
29 | ("netbsd", "netbsd"), |
30 | ("openbsd", "openbsd"), | |
0531ce1d | 31 | ("redox", "redox"), |
9fa01778 | 32 | ("sgx", "sgx"), |
0531ce1d | 33 | ("solaris", "solaris"), |
32a655c1 SL |
34 | ("win32", "windows"), |
35 | ("windows", "windows"), | |
416331ca | 36 | ("vxworks", "vxworks"), |
32a655c1 | 37 | ]; |
c34b1796 | 38 | |
3dfed10e | 39 | const ARCH_TABLE: &[(&str, &str)] = &[ |
32a655c1 | 40 | ("aarch64", "aarch64"), |
5869c6ff | 41 | ("aarch64_be", "aarch64"), |
32a655c1 SL |
42 | ("amd64", "x86_64"), |
43 | ("arm", "arm"), | |
44 | ("arm64", "aarch64"), | |
0531ce1d XL |
45 | ("armv4t", "arm"), |
46 | ("armv5te", "arm"), | |
47 | ("armv7", "arm"), | |
48 | ("armv7s", "arm"), | |
49 | ("asmjs", "asmjs"), | |
f035d41b | 50 | ("avr", "avr"), |
32a655c1 SL |
51 | ("hexagon", "hexagon"), |
52 | ("i386", "x86"), | |
53 | ("i586", "x86"), | |
54 | ("i686", "x86"), | |
55 | ("mips", "mips"), | |
0531ce1d XL |
56 | ("mips64", "mips64"), |
57 | ("mips64el", "mips64"), | |
532ac7d7 XL |
58 | ("mipsisa32r6", "mips"), |
59 | ("mipsisa32r6el", "mips"), | |
60 | ("mipsisa64r6", "mips64"), | |
61 | ("mipsisa64r6el", "mips64"), | |
0531ce1d | 62 | ("mipsel", "mips"), |
532ac7d7 XL |
63 | ("mipsisa32r6", "mips"), |
64 | ("mipsisa32r6el", "mips"), | |
65 | ("mipsisa64r6", "mips64"), | |
66 | ("mipsisa64r6el", "mips64"), | |
32a655c1 | 67 | ("msp430", "msp430"), |
532ac7d7 | 68 | ("nvptx64", "nvptx64"), |
32a655c1 SL |
69 | ("powerpc", "powerpc"), |
70 | ("powerpc64", "powerpc64"), | |
0531ce1d | 71 | ("powerpc64le", "powerpc64"), |
dfeec247 | 72 | ("riscv64gc", "riscv64"), |
32a655c1 SL |
73 | ("s390x", "s390x"), |
74 | ("sparc", "sparc"), | |
0531ce1d XL |
75 | ("sparc64", "sparc64"), |
76 | ("sparcv9", "sparc64"), | |
77 | ("thumbv6m", "thumb"), | |
78 | ("thumbv7em", "thumb"), | |
79 | ("thumbv7m", "thumb"), | |
80 | ("wasm32", "wasm32"), | |
32a655c1 SL |
81 | ("x86_64", "x86_64"), |
82 | ("xcore", "xcore"), | |
32a655c1 | 83 | ]; |
1a4d82fc | 84 | |
3dfed10e | 85 | pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[ |
5869c6ff | 86 | "aarch64-apple-darwin", |
f035d41b XL |
87 | "aarch64-fuchsia", |
88 | "aarch64-unknown-linux-gnu", | |
89 | "x86_64-apple-darwin", | |
90 | "x86_64-fuchsia", | |
3dfed10e | 91 | "x86_64-unknown-freebsd", |
f035d41b XL |
92 | "x86_64-unknown-linux-gnu", |
93 | ]; | |
94 | ||
5869c6ff XL |
95 | pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[ |
96 | "aarch64-apple-darwin", | |
97 | "aarch64-unknown-linux-gnu", | |
98 | "x86_64-apple-darwin", | |
99 | "x86_64-unknown-linux-gnu", | |
100 | ]; | |
f035d41b | 101 | |
3dfed10e XL |
102 | pub const MSAN_SUPPORTED_TARGETS: &[&str] = |
103 | &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"]; | |
f035d41b | 104 | |
3dfed10e | 105 | pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[ |
5869c6ff | 106 | "aarch64-apple-darwin", |
3dfed10e XL |
107 | "aarch64-unknown-linux-gnu", |
108 | "x86_64-apple-darwin", | |
109 | "x86_64-unknown-freebsd", | |
110 | "x86_64-unknown-linux-gnu", | |
111 | ]; | |
112 | ||
6a06907d XL |
113 | pub const HWASAN_SUPPORTED_TARGETS: &[&str] = |
114 | &["aarch64-linux-android", "aarch64-unknown-linux-gnu"]; | |
115 | ||
3dfed10e | 116 | const BIG_ENDIAN: &[&str] = &[ |
5869c6ff | 117 | "aarch64_be", |
3dfed10e XL |
118 | "armebv7r", |
119 | "mips", | |
120 | "mips64", | |
121 | "mipsisa32r6", | |
122 | "mipsisa64r6", | |
123 | "powerpc", | |
124 | "powerpc64", | |
125 | "s390x", | |
126 | "sparc", | |
127 | "sparc64", | |
128 | "sparcv9", | |
129 | ]; | |
f035d41b | 130 | |
abe05a73 XL |
131 | pub fn matches_os(triple: &str, name: &str) -> bool { |
132 | // For the wasm32 bare target we ignore anything also ignored on emscripten | |
133 | // and then we also recognize `wasm32-bare` as the os for the target | |
134 | if triple == "wasm32-unknown-unknown" { | |
94b46f34 | 135 | return name == "emscripten" || name == "wasm32-bare"; |
abe05a73 | 136 | } |
0531ce1d | 137 | let triple: Vec<_> = triple.split('-').collect(); |
85aaf69f | 138 | for &(triple_os, os) in OS_TABLE { |
0531ce1d | 139 | if triple.contains(&triple_os) { |
abe05a73 | 140 | return os == name; |
1a4d82fc JJ |
141 | } |
142 | } | |
143 | panic!("Cannot determine OS from triple"); | |
144 | } | |
0731742a XL |
145 | |
146 | /// Determine the architecture from `triple` | |
c34b1796 | 147 | pub fn get_arch(triple: &str) -> &'static str { |
0531ce1d | 148 | let triple: Vec<_> = triple.split('-').collect(); |
c34b1796 | 149 | for &(triple_arch, arch) in ARCH_TABLE { |
0531ce1d | 150 | if triple.contains(&triple_arch) { |
5bcae85e | 151 | return arch; |
c34b1796 AL |
152 | } |
153 | } | |
154 | panic!("Cannot determine Architecture from triple"); | |
155 | } | |
223e47cc | 156 | |
3dfed10e XL |
157 | /// Determine the endianness from `triple` |
158 | pub fn is_big_endian(triple: &str) -> bool { | |
159 | let triple_arch = triple.split('-').next().unwrap(); | |
160 | BIG_ENDIAN.contains(&triple_arch) | |
161 | } | |
162 | ||
e1599b0c | 163 | pub fn matches_env(triple: &str, name: &str) -> bool { |
dfeec247 | 164 | if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false } |
d9579d0f AL |
165 | } |
166 | ||
041b39d2 | 167 | pub fn get_pointer_width(triple: &str) -> &'static str { |
5869c6ff XL |
168 | if (triple.contains("64") && !triple.ends_with("gnux32") && !triple.ends_with("gnu_ilp32")) |
169 | || triple.starts_with("s390x") | |
170 | { | |
041b39d2 | 171 | "64bit" |
f035d41b XL |
172 | } else if triple.starts_with("avr") { |
173 | "16bit" | |
041b39d2 XL |
174 | } else { |
175 | "32bit" | |
176 | } | |
177 | } | |
178 | ||
1a4d82fc | 179 | pub fn make_new_path(path: &str) -> String { |
c34b1796 | 180 | assert!(cfg!(windows)); |
223e47cc LB |
181 | // Windows just uses PATH as the library search path, so we have to |
182 | // maintain the current value while adding our own | |
85aaf69f | 183 | match env::var(lib_path_env_var()) { |
5bcae85e SL |
184 | Ok(curr) => format!("{}{}{}", path, path_div(), curr), |
185 | Err(..) => path.to_owned(), | |
223e47cc LB |
186 | } |
187 | } | |
188 | ||
5bcae85e SL |
189 | pub fn lib_path_env_var() -> &'static str { |
190 | "PATH" | |
191 | } | |
192 | fn path_div() -> &'static str { | |
193 | ";" | |
194 | } | |
223e47cc | 195 | |
1a4d82fc JJ |
196 | pub fn logv(config: &Config, s: String) { |
197 | debug!("{}", s); | |
5bcae85e SL |
198 | if config.verbose { |
199 | println!("{}", s); | |
200 | } | |
223e47cc | 201 | } |
94b46f34 XL |
202 | |
203 | pub trait PathBufExt { | |
204 | /// Append an extension to the path, even if it already has one. | |
205 | fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf; | |
206 | } | |
207 | ||
208 | impl PathBufExt for PathBuf { | |
209 | fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf { | |
3dfed10e | 210 | if extension.as_ref().is_empty() { |
94b46f34 XL |
211 | self.clone() |
212 | } else { | |
213 | let mut fname = self.file_name().unwrap().to_os_string(); | |
3dfed10e | 214 | if !extension.as_ref().to_str().unwrap().starts_with('.') { |
94b46f34 XL |
215 | fname.push("."); |
216 | } | |
217 | fname.push(extension); | |
218 | self.with_file_name(fname) | |
219 | } | |
220 | } | |
221 | } |