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