1 use crate::common::Config;
4 use std::path::PathBuf;
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"),
18 ("dragonfly", "dragonfly"),
19 ("emscripten", "emscripten"),
20 ("freebsd", "freebsd"),
21 ("fuchsia", "fuchsia"),
27 ("mingw32", "windows"),
30 ("openbsd", "openbsd"),
33 ("solaris", "solaris"),
35 ("windows", "windows"),
36 ("vxworks", "vxworks"),
39 const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
40 ("aarch64", "aarch64"),
49 ("hexagon", "hexagon"),
55 ("mips64el", "mips64"),
56 ("mipsisa32r6", "mips"),
57 ("mipsisa32r6el", "mips"),
58 ("mipsisa64r6", "mips64"),
59 ("mipsisa64r6el", "mips64"),
61 ("mipsisa32r6", "mips"),
62 ("mipsisa32r6el", "mips"),
63 ("mipsisa64r6", "mips64"),
64 ("mipsisa64r6el", "mips64"),
66 ("nvptx64", "nvptx64"),
67 ("powerpc", "powerpc"),
68 ("powerpc64", "powerpc64"),
69 ("powerpc64le", "powerpc64"),
70 ("riscv64gc", "riscv64"),
73 ("sparc64", "sparc64"),
74 ("sparcv9", "sparc64"),
75 ("thumbv6m", "thumb"),
76 ("thumbv7em", "thumb"),
77 ("thumbv7m", "thumb"),
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";
89 let triple: Vec<_> = triple.split('-').collect();
90 for &(triple_os, os) in OS_TABLE {
91 if triple.contains(&triple_os) {
95 panic!("Cannot determine OS from triple");
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) {
106 panic!("Cannot determine Architecture from triple");
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 }
113 pub fn get_pointer_width(triple: &str) -> &'static str {
114 if (triple.contains("64") && !triple.ends_with("gnux32")) || triple.starts_with("s390x") {
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(),
131 pub fn lib_path_env_var() -> &'static str {
134 fn path_div() -> &'static str {
138 pub fn logv(config: &Config, s: String) {
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;
150 impl PathBufExt for PathBuf {
151 fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
152 if extension.as_ref().len() == 0 {
155 let mut fname = self.file_name().unwrap().to_os_string();
156 if !extension.as_ref().to_str().unwrap().starts_with(".") {
159 fname.push(extension);
160 self.with_file_name(fname)