]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Merge branch 'master' into feature/incorporate-tracing
[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 log::*;
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-linux-gnu",
91 ];
92
93 pub const LSAN_SUPPORTED_TARGETS: &'static [&'static str] =
94     &["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
95
96 pub const MSAN_SUPPORTED_TARGETS: &'static [&'static str] =
97     &["aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"];
98
99 pub const TSAN_SUPPORTED_TARGETS: &'static [&'static str] =
100     &["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
101
102 pub fn matches_os(triple: &str, name: &str) -> bool {
103     // For the wasm32 bare target we ignore anything also ignored on emscripten
104     // and then we also recognize `wasm32-bare` as the os for the target
105     if triple == "wasm32-unknown-unknown" {
106         return name == "emscripten" || name == "wasm32-bare";
107     }
108     let triple: Vec<_> = triple.split('-').collect();
109     for &(triple_os, os) in OS_TABLE {
110         if triple.contains(&triple_os) {
111             return os == name;
112         }
113     }
114     panic!("Cannot determine OS from triple");
115 }
116
117 /// Determine the architecture from `triple`
118 pub fn get_arch(triple: &str) -> &'static str {
119     let triple: Vec<_> = triple.split('-').collect();
120     for &(triple_arch, arch) in ARCH_TABLE {
121         if triple.contains(&triple_arch) {
122             return arch;
123         }
124     }
125     panic!("Cannot determine Architecture from triple");
126 }
127
128 pub fn matches_env(triple: &str, name: &str) -> bool {
129     if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
130 }
131
132 pub fn get_pointer_width(triple: &str) -> &'static str {
133     if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") {
134         "64bit"
135     } else if triple.starts_with("avr") {
136         "16bit"
137     } else {
138         "32bit"
139     }
140 }
141
142 pub fn make_new_path(path: &str) -> String {
143     assert!(cfg!(windows));
144     // Windows just uses PATH as the library search path, so we have to
145     // maintain the current value while adding our own
146     match env::var(lib_path_env_var()) {
147         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
148         Err(..) => path.to_owned(),
149     }
150 }
151
152 pub fn lib_path_env_var() -> &'static str {
153     "PATH"
154 }
155 fn path_div() -> &'static str {
156     ";"
157 }
158
159 pub fn logv(config: &Config, s: String) {
160     debug!("{}", s);
161     if config.verbose {
162         println!("{}", s);
163     }
164 }
165
166 pub trait PathBufExt {
167     /// Append an extension to the path, even if it already has one.
168     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
169 }
170
171 impl PathBufExt for PathBuf {
172     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
173         if extension.as_ref().len() == 0 {
174             self.clone()
175         } else {
176             let mut fname = self.file_name().unwrap().to_os_string();
177             if !extension.as_ref().to_str().unwrap().starts_with(".") {
178                 fname.push(".");
179             }
180             fname.push(extension);
181             self.with_file_name(fname)
182         }
183     }
184 }