]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/util.rs
Auto merge of #105716 - chriswailes:ndk-update-redux, r=pietroalbini
[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 use std::process::Command;
6
7 use tracing::*;
8
9 #[cfg(test)]
10 mod tests;
11
12 pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
13     "aarch64-apple-darwin",
14     "aarch64-unknown-fuchsia",
15     "aarch64-linux-android",
16     "aarch64-unknown-linux-gnu",
17     "arm-linux-androideabi",
18     "armv7-linux-androideabi",
19     "i686-linux-android",
20     "i686-unknown-linux-gnu",
21     "x86_64-apple-darwin",
22     "x86_64-unknown-fuchsia",
23     "x86_64-linux-android",
24     "x86_64-unknown-freebsd",
25     "x86_64-unknown-linux-gnu",
26 ];
27
28 // FIXME(rcvalle): More targets are likely supported.
29 pub const CFI_SUPPORTED_TARGETS: &[&str] = &[
30     "aarch64-apple-darwin",
31     "aarch64-unknown-fuchsia",
32     "aarch64-linux-android",
33     "aarch64-unknown-freebsd",
34     "aarch64-unknown-linux-gnu",
35     "x86_64-apple-darwin",
36     "x86_64-unknown-fuchsia",
37     "x86_64-pc-solaris",
38     "x86_64-unknown-freebsd",
39     "x86_64-unknown-illumos",
40     "x86_64-unknown-linux-gnu",
41     "x86_64-unknown-linux-musl",
42     "x86_64-unknown-netbsd",
43 ];
44
45 pub const KCFI_SUPPORTED_TARGETS: &[&str] = &["aarch64-linux-none", "x86_64-linux-none"];
46
47 pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[
48     // FIXME: currently broken, see #88132
49     // "aarch64-apple-darwin",
50     "aarch64-unknown-linux-gnu",
51     "x86_64-apple-darwin",
52     "x86_64-unknown-linux-gnu",
53 ];
54
55 pub const MSAN_SUPPORTED_TARGETS: &[&str] =
56     &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
57
58 pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[
59     "aarch64-apple-darwin",
60     "aarch64-unknown-linux-gnu",
61     "x86_64-apple-darwin",
62     "x86_64-unknown-freebsd",
63     "x86_64-unknown-linux-gnu",
64 ];
65
66 pub const HWASAN_SUPPORTED_TARGETS: &[&str] =
67     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
68
69 pub const MEMTAG_SUPPORTED_TARGETS: &[&str] =
70     &["aarch64-linux-android", "aarch64-unknown-linux-gnu"];
71
72 pub const SHADOWCALLSTACK_SUPPORTED_TARGETS: &[&str] = &["aarch64-linux-android"];
73
74 pub fn make_new_path(path: &str) -> String {
75     assert!(cfg!(windows));
76     // Windows just uses PATH as the library search path, so we have to
77     // maintain the current value while adding our own
78     match env::var(lib_path_env_var()) {
79         Ok(curr) => format!("{}{}{}", path, path_div(), curr),
80         Err(..) => path.to_owned(),
81     }
82 }
83
84 pub fn lib_path_env_var() -> &'static str {
85     "PATH"
86 }
87 fn path_div() -> &'static str {
88     ";"
89 }
90
91 pub fn logv(config: &Config, s: String) {
92     debug!("{}", s);
93     if config.verbose {
94         println!("{}", s);
95     }
96 }
97
98 pub trait PathBufExt {
99     /// Append an extension to the path, even if it already has one.
100     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;
101 }
102
103 impl PathBufExt for PathBuf {
104     fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
105         if extension.as_ref().is_empty() {
106             self.clone()
107         } else {
108             let mut fname = self.file_name().unwrap().to_os_string();
109             if !extension.as_ref().to_str().unwrap().starts_with('.') {
110                 fname.push(".");
111             }
112             fname.push(extension);
113             self.with_file_name(fname)
114         }
115     }
116 }
117
118 /// The name of the environment variable that holds dynamic library locations.
119 pub fn dylib_env_var() -> &'static str {
120     if cfg!(windows) {
121         "PATH"
122     } else if cfg!(target_os = "macos") {
123         "DYLD_LIBRARY_PATH"
124     } else if cfg!(target_os = "haiku") {
125         "LIBRARY_PATH"
126     } else {
127         "LD_LIBRARY_PATH"
128     }
129 }
130
131 /// Adds a list of lookup paths to `cmd`'s dynamic library lookup path.
132 /// If the dylib_path_var is already set for this cmd, the old value will be overwritten!
133 pub fn add_dylib_path(cmd: &mut Command, paths: impl Iterator<Item = impl Into<PathBuf>>) {
134     let path_env = env::var_os(dylib_env_var());
135     let old_paths = path_env.as_ref().map(env::split_paths);
136     let new_paths = paths.map(Into::into).chain(old_paths.into_iter().flatten());
137     cmd.env(dylib_env_var(), env::join_paths(new_paths).unwrap());
138 }