]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
pin docs: add some forward references
[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 const BIG_ENDIAN: &'static [&'static str] = &[
103     "armebv7r",
104     "mips",
105     "mips64",
106     "mipsisa32r6",
107     "mipsisa64r6",
108     "powerpc",
109     "powerpc64",
110     "s390x",
111     "sparc",
112     "sparc64",
113     "sparcv9",
114 ];
115
116 pub fn matches_os(triple: &str, name: &str) -> bool {
117     // For the wasm32 bare target we ignore anything also ignored on emscripten
118     // and then we also recognize `wasm32-bare` as the os for the target
119     if triple == "wasm32-unknown-unknown" {
120         return name == "emscripten" || name == "wasm32-bare";
121     }
122     let triple: Vec<_> = triple.split('-').collect();
123     for &(triple_os, os) in OS_TABLE {
124         if triple.contains(&triple_os) {
125             return os == name;
126         }
127     }
128     panic!("Cannot determine OS from triple");
129 }
130
131 /// Determine the architecture from `triple`
132 pub fn get_arch(triple: &str) -> &'static str {
133     let triple: Vec<_> = triple.split('-').collect();
134     for &(triple_arch, arch) in ARCH_TABLE {
135         if triple.contains(&triple_arch) {
136             return arch;
137         }
138     }
139     panic!("Cannot determine Architecture from triple");
140 }
141
142 /// Determine the endianness from `triple`
143 pub fn is_big_endian(triple: &str) -> bool {
144     let triple_arch = triple.split('-').next().unwrap();
145     BIG_ENDIAN.contains(&triple_arch)
146 }
147
148 pub fn matches_env(triple: &str, name: &str) -> bool {
149     if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
150 }
151
152 pub fn get_pointer_width(triple: &str) -> &'static str {
153     if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") {
154         "64bit"
155     } else if triple.starts_with("avr") {
156         "16bit"
157     } else {
158         "32bit"
159     }
160 }
161
162 pub fn make_new_path(path: &str) -> String {
163     assert!(cfg!(windows));
164     // Windows just uses PATH as the library search path, so we have to
165     // maintain the current value while adding our own
166     match env::var(lib_path_env_var()) {
167         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
168         Err(..) => path.to_owned(),
169     }
170 }
171
172 pub fn lib_path_env_var() -> &'static str {
173     "PATH"
174 }
175 fn path_div() -> &'static str {
176     ";"
177 }
178
179 pub fn logv(config: &Config, s: String) {
180     debug!("{}", s);
181     if config.verbose {
182         println!("{}", s);
183     }
184 }
185
186 pub trait PathBufExt {
187     /// Append an extension to the path, even if it already has one.
188     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
189 }
190
191 impl PathBufExt for PathBuf {
192     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
193         if extension.as_ref().len() == 0 {
194             self.clone()
195         } else {
196             let mut fname = self.file_name().unwrap().to_os_string();
197             if !extension.as_ref().to_str().unwrap().starts_with(".") {
198                 fname.push(".");
199             }
200             fname.push(extension);
201             self.with_file_name(fname)
202         }
203     }
204 }