]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Merge commit '5ff7b632a95bac6955611d85040859128902c580' into sync-rustfmt-subtree
[rust.git] / src / tools / compiletest / src / util.rs
1 use crate::common::Config;
2 use std::env;
3 use std::ffi::OsStr;
4 use std::path::PathBuf;
5
6 use tracing::*;
7
8 #[cfg(test)]
9 mod tests;
10
11 /// Conversion table from triple OS name to Rust SYSNAME
12 const OS_TABLE: &[(&str, &str)] = &[
13     ("android", "android"),
14     ("androideabi", "android"),
15     ("cuda", "cuda"),
16     ("darwin", "macos"),
17     ("dragonfly", "dragonfly"),
18     ("emscripten", "emscripten"),
19     ("freebsd", "freebsd"),
20     ("fuchsia", "fuchsia"),
21     ("haiku", "haiku"),
22     ("hermit", "hermit"),
23     ("illumos", "illumos"),
24     ("ios", "ios"),
25     ("l4re", "l4re"),
26     ("linux", "linux"),
27     ("mingw32", "windows"),
28     ("none", "none"),
29     ("netbsd", "netbsd"),
30     ("openbsd", "openbsd"),
31     ("redox", "redox"),
32     ("sgx", "sgx"),
33     ("solaris", "solaris"),
34     ("win32", "windows"),
35     ("windows", "windows"),
36     ("vxworks", "vxworks"),
37 ];
38
39 const ARCH_TABLE: &[(&str, &str)] = &[
40     ("aarch64", "aarch64"),
41     ("aarch64_be", "aarch64"),
42     ("amd64", "x86_64"),
43     ("arm", "arm"),
44     ("arm64", "aarch64"),
45     ("armv4t", "arm"),
46     ("armv5te", "arm"),
47     ("armv7", "arm"),
48     ("armv7s", "arm"),
49     ("asmjs", "asmjs"),
50     ("avr", "avr"),
51     ("bpfeb", "bpf"),
52     ("bpfel", "bpf"),
53     ("hexagon", "hexagon"),
54     ("i386", "x86"),
55     ("i586", "x86"),
56     ("i686", "x86"),
57     ("m68k", "m68k"),
58     ("mips", "mips"),
59     ("mips64", "mips64"),
60     ("mips64el", "mips64"),
61     ("mipsisa32r6", "mips"),
62     ("mipsisa32r6el", "mips"),
63     ("mipsisa64r6", "mips64"),
64     ("mipsisa64r6el", "mips64"),
65     ("mipsel", "mips"),
66     ("mipsisa32r6", "mips"),
67     ("mipsisa32r6el", "mips"),
68     ("mipsisa64r6", "mips64"),
69     ("mipsisa64r6el", "mips64"),
70     ("msp430", "msp430"),
71     ("nvptx64", "nvptx64"),
72     ("powerpc", "powerpc"),
73     ("powerpc64", "powerpc64"),
74     ("powerpc64le", "powerpc64"),
75     ("riscv64gc", "riscv64"),
76     ("s390x", "s390x"),
77     ("sparc", "sparc"),
78     ("sparc64", "sparc64"),
79     ("sparcv9", "sparc64"),
80     ("thumbv6m", "thumb"),
81     ("thumbv7em", "thumb"),
82     ("thumbv7m", "thumb"),
83     ("wasm32", "wasm32"),
84     ("x86_64", "x86_64"),
85     ("xcore", "xcore"),
86 ];
87
88 pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
89     "aarch64-apple-darwin",
90     "aarch64-fuchsia",
91     "aarch64-unknown-linux-gnu",
92     "x86_64-apple-darwin",
93     "x86_64-fuchsia",
94     "x86_64-unknown-freebsd",
95     "x86_64-unknown-linux-gnu",
96 ];
97
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",
104 ];
105
106 pub const MSAN_SUPPORTED_TARGETS: &[&str] =
107     &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
108
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",
115 ];
116
117 pub const HWASAN_SUPPORTED_TARGETS: &[&str] =
118     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
119
120 pub const MEMTAG_SUPPORTED_TARGETS: &[&str] =
121     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
122
123 const BIG_ENDIAN: &[&str] = &[
124     "aarch64_be",
125     "armebv7r",
126     "mips",
127     "mips64",
128     "mipsisa32r6",
129     "mipsisa64r6",
130     "powerpc",
131     "powerpc64",
132     "s390x",
133     "sparc",
134     "sparc64",
135     "sparcv9",
136 ];
137
138 static ASM_SUPPORTED_ARCHS: &[&str] = &[
139     "x86", "x86_64", "arm", "aarch64", "riscv32",
140     "riscv64",
141     // These targets require an additional asm_experimental_arch feature.
142     // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
143 ];
144
145 pub fn has_asm_support(triple: &str) -> bool {
146     ASM_SUPPORTED_ARCHS.contains(&get_arch(triple))
147 }
148
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";
154     }
155     let triple: Vec<_> = triple.split('-').collect();
156     for &(triple_os, os) in OS_TABLE {
157         if triple.contains(&triple_os) {
158             return os == name;
159         }
160     }
161     panic!("Cannot determine OS from triple");
162 }
163
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) {
169             return arch;
170         }
171     }
172     panic!("Cannot determine Architecture from triple");
173 }
174
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)
179 }
180
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 }
183 }
184
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")
188     {
189         "64bit"
190     } else if triple.starts_with("avr") {
191         "16bit"
192     } else {
193         "32bit"
194     }
195 }
196
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(),
204     }
205 }
206
207 pub fn lib_path_env_var() -> &'static str {
208     "PATH"
209 }
210 fn path_div() -> &'static str {
211     ";"
212 }
213
214 pub fn logv(config: &Config, s: String) {
215     debug!("{}", s);
216     if config.verbose {
217         println!("{}", s);
218     }
219 }
220
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;
224 }
225
226 impl PathBufExt for PathBuf {
227     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
228         if extension.as_ref().is_empty() {
229             self.clone()
230         } else {
231             let mut fname = self.file_name().unwrap().to_os_string();
232             if !extension.as_ref().to_str().unwrap().starts_with('.') {
233                 fname.push(".");
234             }
235             fname.push(extension);
236             self.with_file_name(fname)
237         }
238     }
239 }