]> git.proxmox.com Git - rustc.git/blame - src/tools/compiletest/src/util.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / compiletest / src / util.rs
CommitLineData
dfeec247 1use crate::common::Config;
85aaf69f 2use std::env;
dfeec247 3use std::ffi::OsStr;
94b46f34 4use std::path::PathBuf;
1a4d82fc 5
3dfed10e 6use tracing::*;
48663c56 7
416331ca
XL
8#[cfg(test)]
9mod tests;
10
1a4d82fc 11/// Conversion table from triple OS name to Rust SYSNAME
3dfed10e 12const 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"),
923072b8 34 ("watchos", "watchos"),
32a655c1
SL
35 ("win32", "windows"),
36 ("windows", "windows"),
416331ca 37 ("vxworks", "vxworks"),
32a655c1 38];
c34b1796 39
3dfed10e 40const ARCH_TABLE: &[(&str, &str)] = &[
32a655c1 41 ("aarch64", "aarch64"),
5869c6ff 42 ("aarch64_be", "aarch64"),
32a655c1
SL
43 ("amd64", "x86_64"),
44 ("arm", "arm"),
45 ("arm64", "aarch64"),
0531ce1d
XL
46 ("armv4t", "arm"),
47 ("armv5te", "arm"),
48 ("armv7", "arm"),
49 ("armv7s", "arm"),
50 ("asmjs", "asmjs"),
f035d41b 51 ("avr", "avr"),
17df50a5
XL
52 ("bpfeb", "bpf"),
53 ("bpfel", "bpf"),
32a655c1
SL
54 ("hexagon", "hexagon"),
55 ("i386", "x86"),
56 ("i586", "x86"),
57 ("i686", "x86"),
c295e0f8 58 ("m68k", "m68k"),
32a655c1 59 ("mips", "mips"),
0531ce1d
XL
60 ("mips64", "mips64"),
61 ("mips64el", "mips64"),
532ac7d7
XL
62 ("mipsisa32r6", "mips"),
63 ("mipsisa32r6el", "mips"),
64 ("mipsisa64r6", "mips64"),
65 ("mipsisa64r6el", "mips64"),
0531ce1d 66 ("mipsel", "mips"),
532ac7d7
XL
67 ("mipsisa32r6", "mips"),
68 ("mipsisa32r6el", "mips"),
69 ("mipsisa64r6", "mips64"),
70 ("mipsisa64r6el", "mips64"),
32a655c1 71 ("msp430", "msp430"),
532ac7d7 72 ("nvptx64", "nvptx64"),
32a655c1
SL
73 ("powerpc", "powerpc"),
74 ("powerpc64", "powerpc64"),
0531ce1d 75 ("powerpc64le", "powerpc64"),
dfeec247 76 ("riscv64gc", "riscv64"),
32a655c1
SL
77 ("s390x", "s390x"),
78 ("sparc", "sparc"),
0531ce1d
XL
79 ("sparc64", "sparc64"),
80 ("sparcv9", "sparc64"),
81 ("thumbv6m", "thumb"),
82 ("thumbv7em", "thumb"),
83 ("thumbv7m", "thumb"),
84 ("wasm32", "wasm32"),
32a655c1
SL
85 ("x86_64", "x86_64"),
86 ("xcore", "xcore"),
32a655c1 87];
1a4d82fc 88
3dfed10e 89pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
5869c6ff 90 "aarch64-apple-darwin",
f035d41b
XL
91 "aarch64-fuchsia",
92 "aarch64-unknown-linux-gnu",
93 "x86_64-apple-darwin",
94 "x86_64-fuchsia",
3dfed10e 95 "x86_64-unknown-freebsd",
f035d41b
XL
96 "x86_64-unknown-linux-gnu",
97];
98
5869c6ff 99pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[
c295e0f8
XL
100 // FIXME: currently broken, see #88132
101 // "aarch64-apple-darwin",
5869c6ff
XL
102 "aarch64-unknown-linux-gnu",
103 "x86_64-apple-darwin",
104 "x86_64-unknown-linux-gnu",
105];
f035d41b 106
3dfed10e
XL
107pub const MSAN_SUPPORTED_TARGETS: &[&str] =
108 &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
f035d41b 109
3dfed10e 110pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[
5869c6ff 111 "aarch64-apple-darwin",
3dfed10e
XL
112 "aarch64-unknown-linux-gnu",
113 "x86_64-apple-darwin",
114 "x86_64-unknown-freebsd",
115 "x86_64-unknown-linux-gnu",
116];
117
6a06907d
XL
118pub const HWASAN_SUPPORTED_TARGETS: &[&str] =
119 &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
120
5099ac24
FG
121pub const MEMTAG_SUPPORTED_TARGETS: &[&str] =
122 &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
123
3dfed10e 124const BIG_ENDIAN: &[&str] = &[
5869c6ff 125 "aarch64_be",
3dfed10e
XL
126 "armebv7r",
127 "mips",
128 "mips64",
129 "mipsisa32r6",
130 "mipsisa64r6",
131 "powerpc",
132 "powerpc64",
133 "s390x",
134 "sparc",
135 "sparc64",
136 "sparcv9",
137];
f035d41b 138
cdc7bbd5 139static ASM_SUPPORTED_ARCHS: &[&str] = &[
3c0e092e
XL
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",
cdc7bbd5
XL
144];
145
146pub fn has_asm_support(triple: &str) -> bool {
147 ASM_SUPPORTED_ARCHS.contains(&get_arch(triple))
148}
149
abe05a73
XL
150pub 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" {
94b46f34 154 return name == "emscripten" || name == "wasm32-bare";
abe05a73 155 }
0531ce1d 156 let triple: Vec<_> = triple.split('-').collect();
85aaf69f 157 for &(triple_os, os) in OS_TABLE {
0531ce1d 158 if triple.contains(&triple_os) {
abe05a73 159 return os == name;
1a4d82fc
JJ
160 }
161 }
162 panic!("Cannot determine OS from triple");
163}
0731742a
XL
164
165/// Determine the architecture from `triple`
c34b1796 166pub fn get_arch(triple: &str) -> &'static str {
0531ce1d 167 let triple: Vec<_> = triple.split('-').collect();
c34b1796 168 for &(triple_arch, arch) in ARCH_TABLE {
0531ce1d 169 if triple.contains(&triple_arch) {
5bcae85e 170 return arch;
c34b1796
AL
171 }
172 }
173 panic!("Cannot determine Architecture from triple");
174}
223e47cc 175
3dfed10e
XL
176/// Determine the endianness from `triple`
177pub fn is_big_endian(triple: &str) -> bool {
178 let triple_arch = triple.split('-').next().unwrap();
179 BIG_ENDIAN.contains(&triple_arch)
180}
181
e1599b0c 182pub fn matches_env(triple: &str, name: &str) -> bool {
dfeec247 183 if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
d9579d0f
AL
184}
185
041b39d2 186pub fn get_pointer_width(triple: &str) -> &'static str {
5869c6ff
XL
187 if (triple.contains("64") && !triple.ends_with("gnux32") && !triple.ends_with("gnu_ilp32"))
188 || triple.starts_with("s390x")
189 {
041b39d2 190 "64bit"
f035d41b
XL
191 } else if triple.starts_with("avr") {
192 "16bit"
041b39d2
XL
193 } else {
194 "32bit"
195 }
196}
197
1a4d82fc 198pub fn make_new_path(path: &str) -> String {
c34b1796 199 assert!(cfg!(windows));
223e47cc
LB
200 // Windows just uses PATH as the library search path, so we have to
201 // maintain the current value while adding our own
85aaf69f 202 match env::var(lib_path_env_var()) {
5bcae85e
SL
203 Ok(curr) => format!("{}{}{}", path, path_div(), curr),
204 Err(..) => path.to_owned(),
223e47cc
LB
205 }
206}
207
5bcae85e
SL
208pub fn lib_path_env_var() -> &'static str {
209 "PATH"
210}
211fn path_div() -> &'static str {
212 ";"
213}
223e47cc 214
1a4d82fc
JJ
215pub fn logv(config: &Config, s: String) {
216 debug!("{}", s);
5bcae85e
SL
217 if config.verbose {
218 println!("{}", s);
219 }
223e47cc 220}
94b46f34
XL
221
222pub 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
227impl PathBufExt for PathBuf {
228 fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
3dfed10e 229 if extension.as_ref().is_empty() {
94b46f34
XL
230 self.clone()
231 } else {
232 let mut fname = self.file_name().unwrap().to_os_string();
3dfed10e 233 if !extension.as_ref().to_str().unwrap().starts_with('.') {
94b46f34
XL
234 fname.push(".");
235 }
236 fname.push(extension);
237 self.with_file_name(fname)
238 }
239 }
240}