1 use crate::common::Config;
4 use std::path::PathBuf;
11 /// Conversion table from triple OS name to Rust SYSNAME
12 const OS_TABLE: &[(&str, &str)] = &[
13 ("android", "android"),
14 ("androideabi", "android"),
17 ("dragonfly", "dragonfly"),
18 ("emscripten", "emscripten"),
19 ("freebsd", "freebsd"),
20 ("fuchsia", "fuchsia"),
23 ("illumos", "illumos"),
27 ("mingw32", "windows"),
30 ("openbsd", "openbsd"),
33 ("solaris", "solaris"),
35 ("windows", "windows"),
36 ("vxworks", "vxworks"),
39 const ARCH_TABLE: &[(&str, &str)] = &[
40 ("aarch64", "aarch64"),
41 ("aarch64_be", "aarch64"),
53 ("hexagon", "hexagon"),
60 ("mips64el", "mips64"),
61 ("mipsisa32r6", "mips"),
62 ("mipsisa32r6el", "mips"),
63 ("mipsisa64r6", "mips64"),
64 ("mipsisa64r6el", "mips64"),
66 ("mipsisa32r6", "mips"),
67 ("mipsisa32r6el", "mips"),
68 ("mipsisa64r6", "mips64"),
69 ("mipsisa64r6el", "mips64"),
71 ("nvptx64", "nvptx64"),
72 ("powerpc", "powerpc"),
73 ("powerpc64", "powerpc64"),
74 ("powerpc64le", "powerpc64"),
75 ("riscv64gc", "riscv64"),
78 ("sparc64", "sparc64"),
79 ("sparcv9", "sparc64"),
80 ("thumbv6m", "thumb"),
81 ("thumbv7em", "thumb"),
82 ("thumbv7m", "thumb"),
88 pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
89 "aarch64-apple-darwin",
91 "aarch64-unknown-linux-gnu",
92 "x86_64-apple-darwin",
94 "x86_64-unknown-freebsd",
95 "x86_64-unknown-linux-gnu",
98 pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[
99 // FIXME: currently broken, see #88132
100 // "aarch64-apple-darwin",
101 "aarch64-unknown-linux-gnu",
102 "x86_64-apple-darwin",
103 "x86_64-unknown-linux-gnu",
106 pub const MSAN_SUPPORTED_TARGETS: &[&str] =
107 &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
109 pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[
110 "aarch64-apple-darwin",
111 "aarch64-unknown-linux-gnu",
112 "x86_64-apple-darwin",
113 "x86_64-unknown-freebsd",
114 "x86_64-unknown-linux-gnu",
117 pub const HWASAN_SUPPORTED_TARGETS: &[&str] =
118 &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
120 pub const MEMTAG_SUPPORTED_TARGETS: &[&str] =
121 &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
123 const BIG_ENDIAN: &[&str] = &[
138 static ASM_SUPPORTED_ARCHS: &[&str] = &[
139 "x86", "x86_64", "arm", "aarch64", "riscv32",
141 // These targets require an additional asm_experimental_arch feature.
142 // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
145 pub fn has_asm_support(triple: &str) -> bool {
146 ASM_SUPPORTED_ARCHS.contains(&get_arch(triple))
149 pub fn matches_os(triple: &str, name: &str) -> bool {
150 // For the wasm32 bare target we ignore anything also ignored on emscripten
151 // and then we also recognize `wasm32-bare` as the os for the target
152 if triple == "wasm32-unknown-unknown" {
153 return name == "emscripten" || name == "wasm32-bare";
155 let triple: Vec<_> = triple.split('-').collect();
156 for &(triple_os, os) in OS_TABLE {
157 if triple.contains(&triple_os) {
161 panic!("Cannot determine OS from triple");
164 /// Determine the architecture from `triple`
165 pub fn get_arch(triple: &str) -> &'static str {
166 let triple: Vec<_> = triple.split('-').collect();
167 for &(triple_arch, arch) in ARCH_TABLE {
168 if triple.contains(&triple_arch) {
172 panic!("Cannot determine Architecture from triple");
175 /// Determine the endianness from `triple`
176 pub fn is_big_endian(triple: &str) -> bool {
177 let triple_arch = triple.split('-').next().unwrap();
178 BIG_ENDIAN.contains(&triple_arch)
181 pub fn matches_env(triple: &str, name: &str) -> bool {
182 if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
185 pub fn get_pointer_width(triple: &str) -> &'static str {
186 if (triple.contains("64") && !triple.ends_with("gnux32") && !triple.ends_with("gnu_ilp32"))
187 || triple.starts_with("s390x")
190 } else if triple.starts_with("avr") {
197 pub fn make_new_path(path: &str) -> String {
198 assert!(cfg!(windows));
199 // Windows just uses PATH as the library search path, so we have to
200 // maintain the current value while adding our own
201 match env::var(lib_path_env_var()) {
202 Ok(curr) => format!("{}{}{}", path, path_div(), curr),
203 Err(..) => path.to_owned(),
207 pub fn lib_path_env_var() -> &'static str {
210 fn path_div() -> &'static str {
214 pub fn logv(config: &Config, s: String) {
221 pub trait PathBufExt {
222 /// Append an extension to the path, even if it already has one.
223 fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
226 impl PathBufExt for PathBuf {
227 fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
228 if extension.as_ref().is_empty() {
231 let mut fname = self.file_name().unwrap().to_os_string();
232 if !extension.as_ref().to_str().unwrap().starts_with('.') {
235 fname.push(extension);
236 self.with_file_name(fname)