]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use crate::common::Config; |
85aaf69f | 2 | use std::env; |
dfeec247 | 3 | use std::ffi::OsStr; |
94b46f34 | 4 | use std::path::PathBuf; |
1a4d82fc | 5 | |
48663c56 XL |
6 | use log::*; |
7 | ||
416331ca XL |
8 | #[cfg(test)] |
9 | mod tests; | |
10 | ||
1a4d82fc | 11 | /// Conversion table from triple OS name to Rust SYSNAME |
32a655c1 SL |
12 | const OS_TABLE: &'static [(&'static str, &'static str)] = &[ |
13 | ("android", "android"), | |
0531ce1d | 14 | ("androideabi", "android"), |
2c00a5a8 | 15 | ("cloudabi", "cloudabi"), |
9fa01778 | 16 | ("cuda", "cuda"), |
32a655c1 SL |
17 | ("darwin", "macos"), |
18 | ("dragonfly", "dragonfly"), | |
0531ce1d | 19 | ("emscripten", "emscripten"), |
32a655c1 | 20 | ("freebsd", "freebsd"), |
0531ce1d | 21 | ("fuchsia", "fuchsia"), |
32a655c1 | 22 | ("haiku", "haiku"), |
b7449926 | 23 | ("hermit", "hermit"), |
ba9703b0 | 24 | ("illumos", "illumos"), |
32a655c1 | 25 | ("ios", "ios"), |
0531ce1d | 26 | ("l4re", "l4re"), |
32a655c1 SL |
27 | ("linux", "linux"), |
28 | ("mingw32", "windows"), | |
9fa01778 | 29 | ("none", "none"), |
32a655c1 SL |
30 | ("netbsd", "netbsd"), |
31 | ("openbsd", "openbsd"), | |
0531ce1d | 32 | ("redox", "redox"), |
9fa01778 | 33 | ("sgx", "sgx"), |
0531ce1d | 34 | ("solaris", "solaris"), |
32a655c1 SL |
35 | ("win32", "windows"), |
36 | ("windows", "windows"), | |
416331ca | 37 | ("vxworks", "vxworks"), |
32a655c1 | 38 | ]; |
c34b1796 | 39 | |
32a655c1 SL |
40 | const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[ |
41 | ("aarch64", "aarch64"), | |
42 | ("amd64", "x86_64"), | |
43 | ("arm", "arm"), | |
44 | ("arm64", "aarch64"), | |
0531ce1d XL |
45 | ("armv4t", "arm"), |
46 | ("armv5te", "arm"), | |
47 | ("armv7", "arm"), | |
48 | ("armv7s", "arm"), | |
49 | ("asmjs", "asmjs"), | |
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 | |
abe05a73 XL |
84 | pub fn matches_os(triple: &str, name: &str) -> bool { |
85 | // For the wasm32 bare target we ignore anything also ignored on emscripten | |
86 | // and then we also recognize `wasm32-bare` as the os for the target | |
87 | if triple == "wasm32-unknown-unknown" { | |
94b46f34 | 88 | return name == "emscripten" || name == "wasm32-bare"; |
abe05a73 | 89 | } |
0531ce1d | 90 | let triple: Vec<_> = triple.split('-').collect(); |
85aaf69f | 91 | for &(triple_os, os) in OS_TABLE { |
0531ce1d | 92 | if triple.contains(&triple_os) { |
abe05a73 | 93 | return os == name; |
1a4d82fc JJ |
94 | } |
95 | } | |
96 | panic!("Cannot determine OS from triple"); | |
97 | } | |
0731742a XL |
98 | |
99 | /// Determine the architecture from `triple` | |
c34b1796 | 100 | pub fn get_arch(triple: &str) -> &'static str { |
0531ce1d | 101 | let triple: Vec<_> = triple.split('-').collect(); |
c34b1796 | 102 | for &(triple_arch, arch) in ARCH_TABLE { |
0531ce1d | 103 | if triple.contains(&triple_arch) { |
5bcae85e | 104 | return arch; |
c34b1796 AL |
105 | } |
106 | } | |
107 | panic!("Cannot determine Architecture from triple"); | |
108 | } | |
223e47cc | 109 | |
e1599b0c | 110 | pub fn matches_env(triple: &str, name: &str) -> bool { |
dfeec247 | 111 | if let Some(env) = triple.split('-').nth(3) { env.starts_with(name) } else { false } |
d9579d0f AL |
112 | } |
113 | ||
041b39d2 | 114 | pub fn get_pointer_width(triple: &str) -> &'static str { |
abe05a73 | 115 | if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") { |
041b39d2 XL |
116 | "64bit" |
117 | } else { | |
118 | "32bit" | |
119 | } | |
120 | } | |
121 | ||
1a4d82fc | 122 | pub fn make_new_path(path: &str) -> String { |
c34b1796 | 123 | assert!(cfg!(windows)); |
223e47cc LB |
124 | // Windows just uses PATH as the library search path, so we have to |
125 | // maintain the current value while adding our own | |
85aaf69f | 126 | match env::var(lib_path_env_var()) { |
5bcae85e SL |
127 | Ok(curr) => format!("{}{}{}", path, path_div(), curr), |
128 | Err(..) => path.to_owned(), | |
223e47cc LB |
129 | } |
130 | } | |
131 | ||
5bcae85e SL |
132 | pub fn lib_path_env_var() -> &'static str { |
133 | "PATH" | |
134 | } | |
135 | fn path_div() -> &'static str { | |
136 | ";" | |
137 | } | |
223e47cc | 138 | |
1a4d82fc JJ |
139 | pub fn logv(config: &Config, s: String) { |
140 | debug!("{}", s); | |
5bcae85e SL |
141 | if config.verbose { |
142 | println!("{}", s); | |
143 | } | |
223e47cc | 144 | } |
94b46f34 XL |
145 | |
146 | pub trait PathBufExt { | |
147 | /// Append an extension to the path, even if it already has one. | |
148 | fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf; | |
149 | } | |
150 | ||
151 | impl PathBufExt for PathBuf { | |
152 | fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf { | |
153 | if extension.as_ref().len() == 0 { | |
154 | self.clone() | |
155 | } else { | |
156 | let mut fname = self.file_name().unwrap().to_os_string(); | |
157 | if !extension.as_ref().to_str().unwrap().starts_with(".") { | |
158 | fname.push("."); | |
159 | } | |
160 | fname.push(extension); | |
161 | self.with_file_name(fname) | |
162 | } | |
163 | } | |
164 | } |