]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Rollup merge of #97837 - sunfishcode:sunfishcode/proc-self-mem, r=m-ou-se
[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     ("watchos", "watchos"),
35     ("win32", "windows"),
36     ("windows", "windows"),
37     ("vxworks", "vxworks"),
38 ];
39
40 const ARCH_TABLE: &[(&str, &str)] = &[
41     ("aarch64", "aarch64"),
42     ("aarch64_be", "aarch64"),
43     ("amd64", "x86_64"),
44     ("arm", "arm"),
45     ("arm64", "aarch64"),
46     ("armv4t", "arm"),
47     ("armv5te", "arm"),
48     ("armv7", "arm"),
49     ("armv7s", "arm"),
50     ("asmjs", "asmjs"),
51     ("avr", "avr"),
52     ("bpfeb", "bpf"),
53     ("bpfel", "bpf"),
54     ("hexagon", "hexagon"),
55     ("i386", "x86"),
56     ("i586", "x86"),
57     ("i686", "x86"),
58     ("m68k", "m68k"),
59     ("mips", "mips"),
60     ("mips64", "mips64"),
61     ("mips64el", "mips64"),
62     ("mipsisa32r6", "mips"),
63     ("mipsisa32r6el", "mips"),
64     ("mipsisa64r6", "mips64"),
65     ("mipsisa64r6el", "mips64"),
66     ("mipsel", "mips"),
67     ("mipsisa32r6", "mips"),
68     ("mipsisa32r6el", "mips"),
69     ("mipsisa64r6", "mips64"),
70     ("mipsisa64r6el", "mips64"),
71     ("msp430", "msp430"),
72     ("nvptx64", "nvptx64"),
73     ("powerpc", "powerpc"),
74     ("powerpc64", "powerpc64"),
75     ("powerpc64le", "powerpc64"),
76     ("riscv64gc", "riscv64"),
77     ("s390x", "s390x"),
78     ("sparc", "sparc"),
79     ("sparc64", "sparc64"),
80     ("sparcv9", "sparc64"),
81     ("thumbv6m", "thumb"),
82     ("thumbv7em", "thumb"),
83     ("thumbv7m", "thumb"),
84     ("wasm32", "wasm32"),
85     ("x86_64", "x86_64"),
86     ("xcore", "xcore"),
87 ];
88
89 pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
90     "aarch64-apple-darwin",
91     "aarch64-fuchsia",
92     "aarch64-unknown-linux-gnu",
93     "x86_64-apple-darwin",
94     "x86_64-fuchsia",
95     "x86_64-unknown-freebsd",
96     "x86_64-unknown-linux-gnu",
97 ];
98
99 pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[
100     // FIXME: currently broken, see #88132
101     // "aarch64-apple-darwin",
102     "aarch64-unknown-linux-gnu",
103     "x86_64-apple-darwin",
104     "x86_64-unknown-linux-gnu",
105 ];
106
107 pub const MSAN_SUPPORTED_TARGETS: &[&str] =
108     &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
109
110 pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[
111     "aarch64-apple-darwin",
112     "aarch64-unknown-linux-gnu",
113     "x86_64-apple-darwin",
114     "x86_64-unknown-freebsd",
115     "x86_64-unknown-linux-gnu",
116 ];
117
118 pub const HWASAN_SUPPORTED_TARGETS: &[&str] =
119     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
120
121 pub const MEMTAG_SUPPORTED_TARGETS: &[&str] =
122     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
123
124 const BIG_ENDIAN: &[&str] = &[
125     "aarch64_be",
126     "armebv7r",
127     "mips",
128     "mips64",
129     "mipsisa32r6",
130     "mipsisa64r6",
131     "powerpc",
132     "powerpc64",
133     "s390x",
134     "sparc",
135     "sparc64",
136     "sparcv9",
137 ];
138
139 static ASM_SUPPORTED_ARCHS: &[&str] = &[
140     "x86", "x86_64", "arm", "aarch64", "riscv32",
141     "riscv64",
142     // These targets require an additional asm_experimental_arch feature.
143     // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
144 ];
145
146 pub fn has_asm_support(triple: &str) -> bool {
147     ASM_SUPPORTED_ARCHS.contains(&get_arch(triple))
148 }
149
150 pub fn matches_os(triple: &str, name: &str) -> bool {
151     // For the wasm32 bare target we ignore anything also ignored on emscripten
152     // and then we also recognize `wasm32-bare` as the os for the target
153     if triple == "wasm32-unknown-unknown" {
154         return name == "emscripten" || name == "wasm32-bare";
155     }
156     let triple: Vec<_> = triple.split('-').collect();
157     for &(triple_os, os) in OS_TABLE {
158         if triple.contains(&triple_os) {
159             return os == name;
160         }
161     }
162     panic!("Cannot determine OS from triple");
163 }
164
165 /// Determine the architecture from `triple`
166 pub fn get_arch(triple: &str) -> &'static str {
167     let triple: Vec<_> = triple.split('-').collect();
168     for &(triple_arch, arch) in ARCH_TABLE {
169         if triple.contains(&triple_arch) {
170             return arch;
171         }
172     }
173     panic!("Cannot determine Architecture from triple");
174 }
175
176 /// Determine the endianness from `triple`
177 pub fn is_big_endian(triple: &str) -> bool {
178     let triple_arch = triple.split('-').next().unwrap();
179     BIG_ENDIAN.contains(&triple_arch)
180 }
181
182 pub fn matches_env(triple: &str, name: &str) -> bool {
183     if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
184 }
185
186 pub fn get_pointer_width(triple: &str) -> &'static str {
187     if (triple.contains("64") && !triple.ends_with("gnux32") && !triple.ends_with("gnu_ilp32"))
188         || triple.starts_with("s390x")
189     {
190         "64bit"
191     } else if triple.starts_with("avr") {
192         "16bit"
193     } else {
194         "32bit"
195     }
196 }
197
198 pub fn make_new_path(path: &str) -> String {
199     assert!(cfg!(windows));
200     // Windows just uses PATH as the library search path, so we have to
201     // maintain the current value while adding our own
202     match env::var(lib_path_env_var()) {
203         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
204         Err(..) => path.to_owned(),
205     }
206 }
207
208 pub fn lib_path_env_var() -> &'static str {
209     "PATH"
210 }
211 fn path_div() -> &'static str {
212     ";"
213 }
214
215 pub fn logv(config: &Config, s: String) {
216     debug!("{}", s);
217     if config.verbose {
218         println!("{}", s);
219     }
220 }
221
222 pub trait PathBufExt {
223     /// Append an extension to the path, even if it already has one.
224     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
225 }
226
227 impl PathBufExt for PathBuf {
228     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
229         if extension.as_ref().is_empty() {
230             self.clone()
231         } else {
232             let mut fname = self.file_name().unwrap().to_os_string();
233             if !extension.as_ref().to_str().unwrap().starts_with('.') {
234                 fname.push(".");
235             }
236             fname.push(extension);
237             self.with_file_name(fname)
238         }
239     }
240 }