]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Fix font color for help button in ayu and dark themes
[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: &'static [(&'static str, &'static str)] = &[
13     ("android", "android"),
14     ("androideabi", "android"),
15     ("cloudabi", "cloudabi"),
16     ("cuda", "cuda"),
17     ("darwin", "macos"),
18     ("dragonfly", "dragonfly"),
19     ("emscripten", "emscripten"),
20     ("freebsd", "freebsd"),
21     ("fuchsia", "fuchsia"),
22     ("haiku", "haiku"),
23     ("hermit", "hermit"),
24     ("illumos", "illumos"),
25     ("ios", "ios"),
26     ("l4re", "l4re"),
27     ("linux", "linux"),
28     ("mingw32", "windows"),
29     ("none", "none"),
30     ("netbsd", "netbsd"),
31     ("openbsd", "openbsd"),
32     ("redox", "redox"),
33     ("sgx", "sgx"),
34     ("solaris", "solaris"),
35     ("win32", "windows"),
36     ("windows", "windows"),
37     ("vxworks", "vxworks"),
38 ];
39
40 const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
41     ("aarch64", "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     ("hexagon", "hexagon"),
52     ("i386", "x86"),
53     ("i586", "x86"),
54     ("i686", "x86"),
55     ("mips", "mips"),
56     ("mips64", "mips64"),
57     ("mips64el", "mips64"),
58     ("mipsisa32r6", "mips"),
59     ("mipsisa32r6el", "mips"),
60     ("mipsisa64r6", "mips64"),
61     ("mipsisa64r6el", "mips64"),
62     ("mipsel", "mips"),
63     ("mipsisa32r6", "mips"),
64     ("mipsisa32r6el", "mips"),
65     ("mipsisa64r6", "mips64"),
66     ("mipsisa64r6el", "mips64"),
67     ("msp430", "msp430"),
68     ("nvptx64", "nvptx64"),
69     ("powerpc", "powerpc"),
70     ("powerpc64", "powerpc64"),
71     ("powerpc64le", "powerpc64"),
72     ("riscv64gc", "riscv64"),
73     ("s390x", "s390x"),
74     ("sparc", "sparc"),
75     ("sparc64", "sparc64"),
76     ("sparcv9", "sparc64"),
77     ("thumbv6m", "thumb"),
78     ("thumbv7em", "thumb"),
79     ("thumbv7m", "thumb"),
80     ("wasm32", "wasm32"),
81     ("x86_64", "x86_64"),
82     ("xcore", "xcore"),
83 ];
84
85 pub const ASAN_SUPPORTED_TARGETS: &'static [&'static str] = &[
86     "aarch64-fuchsia",
87     "aarch64-unknown-linux-gnu",
88     "x86_64-apple-darwin",
89     "x86_64-fuchsia",
90     "x86_64-unknown-freebsd",
91     "x86_64-unknown-linux-gnu",
92 ];
93
94 pub const LSAN_SUPPORTED_TARGETS: &'static [&'static str] =
95     &["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
96
97 pub const MSAN_SUPPORTED_TARGETS: &'static [&'static str] =
98     &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
99
100 pub const TSAN_SUPPORTED_TARGETS: &'static [&'static str] = &[
101     "aarch64-unknown-linux-gnu",
102     "x86_64-apple-darwin",
103     "x86_64-unknown-freebsd",
104     "x86_64-unknown-linux-gnu",
105 ];
106
107 const BIG_ENDIAN: &'static [&'static str] = &[
108     "armebv7r",
109     "mips",
110     "mips64",
111     "mipsisa32r6",
112     "mipsisa64r6",
113     "powerpc",
114     "powerpc64",
115     "s390x",
116     "sparc",
117     "sparc64",
118     "sparcv9",
119 ];
120
121 pub fn matches_os(triple: &str, name: &str) -> bool {
122     // For the wasm32 bare target we ignore anything also ignored on emscripten
123     // and then we also recognize `wasm32-bare` as the os for the target
124     if triple == "wasm32-unknown-unknown" {
125         return name == "emscripten" || name == "wasm32-bare";
126     }
127     let triple: Vec<_> = triple.split('-').collect();
128     for &(triple_os, os) in OS_TABLE {
129         if triple.contains(&triple_os) {
130             return os == name;
131         }
132     }
133     panic!("Cannot determine OS from triple");
134 }
135
136 /// Determine the architecture from `triple`
137 pub fn get_arch(triple: &str) -> &'static str {
138     let triple: Vec<_> = triple.split('-').collect();
139     for &(triple_arch, arch) in ARCH_TABLE {
140         if triple.contains(&triple_arch) {
141             return arch;
142         }
143     }
144     panic!("Cannot determine Architecture from triple");
145 }
146
147 /// Determine the endianness from `triple`
148 pub fn is_big_endian(triple: &str) -> bool {
149     let triple_arch = triple.split('-').next().unwrap();
150     BIG_ENDIAN.contains(&triple_arch)
151 }
152
153 pub fn matches_env(triple: &str, name: &str) -> bool {
154     if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
155 }
156
157 pub fn get_pointer_width(triple: &str) -> &'static str {
158     if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") {
159         "64bit"
160     } else if triple.starts_with("avr") {
161         "16bit"
162     } else {
163         "32bit"
164     }
165 }
166
167 pub fn make_new_path(path: &str) -> String {
168     assert!(cfg!(windows));
169     // Windows just uses PATH as the library search path, so we have to
170     // maintain the current value while adding our own
171     match env::var(lib_path_env_var()) {
172         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
173         Err(..) => path.to_owned(),
174     }
175 }
176
177 pub fn lib_path_env_var() -> &'static str {
178     "PATH"
179 }
180 fn path_div() -> &'static str {
181     ";"
182 }
183
184 pub fn logv(config: &Config, s: String) {
185     debug!("{}", s);
186     if config.verbose {
187         println!("{}", s);
188     }
189 }
190
191 pub trait PathBufExt {
192     /// Append an extension to the path, even if it already has one.
193     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
194 }
195
196 impl PathBufExt for PathBuf {
197     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
198         if extension.as_ref().len() == 0 {
199             self.clone()
200         } else {
201             let mut fname = self.file_name().unwrap().to_os_string();
202             if !extension.as_ref().to_str().unwrap().starts_with(".") {
203                 fname.push(".");
204             }
205             fname.push(extension);
206             self.with_file_name(fname)
207         }
208     }
209 }