]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Recognise riscv64 in compiletest
[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     ("ios", "ios"),
25     ("l4re", "l4re"),
26     ("linux", "linux"),
27     ("mingw32", "windows"),
28     ("none", "none"),
29     ("netbsd", "netbsd"),
30     ("openbsd", "openbsd"),
31     ("redox", "redox"),
32     ("sgx", "sgx"),
33     ("solaris", "solaris"),
34     ("win32", "windows"),
35     ("windows", "windows"),
36     ("vxworks", "vxworks"),
37 ];
38
39 const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
40     ("aarch64", "aarch64"),
41     ("amd64", "x86_64"),
42     ("arm", "arm"),
43     ("arm64", "aarch64"),
44     ("armv4t", "arm"),
45     ("armv5te", "arm"),
46     ("armv7", "arm"),
47     ("armv7s", "arm"),
48     ("asmjs", "asmjs"),
49     ("hexagon", "hexagon"),
50     ("i386", "x86"),
51     ("i586", "x86"),
52     ("i686", "x86"),
53     ("mips", "mips"),
54     ("mips64", "mips64"),
55     ("mips64el", "mips64"),
56     ("mipsisa32r6", "mips"),
57     ("mipsisa32r6el", "mips"),
58     ("mipsisa64r6", "mips64"),
59     ("mipsisa64r6el", "mips64"),
60     ("mipsel", "mips"),
61     ("mipsisa32r6", "mips"),
62     ("mipsisa32r6el", "mips"),
63     ("mipsisa64r6", "mips64"),
64     ("mipsisa64r6el", "mips64"),
65     ("msp430", "msp430"),
66     ("nvptx64", "nvptx64"),
67     ("powerpc", "powerpc"),
68     ("powerpc64", "powerpc64"),
69     ("powerpc64le", "powerpc64"),
70     ("riscv64gc", "riscv64"),
71     ("s390x", "s390x"),
72     ("sparc", "sparc"),
73     ("sparc64", "sparc64"),
74     ("sparcv9", "sparc64"),
75     ("thumbv6m", "thumb"),
76     ("thumbv7em", "thumb"),
77     ("thumbv7m", "thumb"),
78     ("wasm32", "wasm32"),
79     ("x86_64", "x86_64"),
80     ("xcore", "xcore"),
81 ];
82
83 pub fn matches_os(triple: &str, name: &str) -> bool {
84     // For the wasm32 bare target we ignore anything also ignored on emscripten
85     // and then we also recognize `wasm32-bare` as the os for the target
86     if triple == "wasm32-unknown-unknown" {
87         return name == "emscripten" || name == "wasm32-bare";
88     }
89     let triple: Vec<_> = triple.split('-').collect();
90     for &(triple_os, os) in OS_TABLE {
91         if triple.contains(&triple_os) {
92             return os == name;
93         }
94     }
95     panic!("Cannot determine OS from triple");
96 }
97
98 /// Determine the architecture from `triple`
99 pub fn get_arch(triple: &str) -> &'static str {
100     let triple: Vec<_> = triple.split('-').collect();
101     for &(triple_arch, arch) in ARCH_TABLE {
102         if triple.contains(&triple_arch) {
103             return arch;
104         }
105     }
106     panic!("Cannot determine Architecture from triple");
107 }
108
109 pub fn matches_env(triple: &str, name: &str) -> bool {
110     if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false }
111 }
112
113 pub fn get_pointer_width(triple: &str) -> &'static str {
114     if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") {
115         "64bit"
116     } else {
117         "32bit"
118     }
119 }
120
121 pub fn make_new_path(path: &str) -> String {
122     assert!(cfg!(windows));
123     // Windows just uses PATH as the library search path, so we have to
124     // maintain the current value while adding our own
125     match env::var(lib_path_env_var()) {
126         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
127         Err(..) => path.to_owned(),
128     }
129 }
130
131 pub fn lib_path_env_var() -> &'static str {
132     "PATH"
133 }
134 fn path_div() -> &'static str {
135     ";"
136 }
137
138 pub fn logv(config: &Config, s: String) {
139     debug!("{}", s);
140     if config.verbose {
141         println!("{}", s);
142     }
143 }
144
145 pub trait PathBufExt {
146     /// Append an extension to the path, even if it already has one.
147     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
148 }
149
150 impl PathBufExt for PathBuf {
151     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
152         if extension.as_ref().len() == 0 {
153             self.clone()
154         } else {
155             let mut fname = self.file_name().unwrap().to_os_string();
156             if !extension.as_ref().to_str().unwrap().starts_with(".") {
157                 fname.push(".");
158             }
159             fname.push(extension);
160             self.with_file_name(fname)
161         }
162     }
163 }