]> git.lizzy.rs Git - rust.git/blob - src/build_helper/lib.rs
Auto merge of #58304 - gnzlbg:simd_saturated, r=nagisa
[rust.git] / src / build_helper / lib.rs
1 #![deny(rust_2018_idioms)]
2
3 use std::fs::File;
4 use std::path::{Path, PathBuf};
5 use std::process::{Command, Stdio};
6 use std::time::{SystemTime, UNIX_EPOCH};
7 use std::{env, fs};
8 use std::thread;
9
10 /// A helper macro to `unwrap` a result except also print out details like:
11 ///
12 /// * The file/line of the panic
13 /// * The expression that failed
14 /// * The error itself
15 ///
16 /// This is currently used judiciously throughout the build system rather than
17 /// using a `Result` with `try!`, but this may change one day...
18 #[macro_export]
19 macro_rules! t {
20     ($e:expr) => {
21         match $e {
22             Ok(e) => e,
23             Err(e) => panic!("{} failed with {}", stringify!($e), e),
24         }
25     };
26 }
27
28 // Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
29 // break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
30 // shared library, which means that when our freshly built llvm-config goes to load it's
31 // associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
32 // compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
33 // the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
34 // perfect -- we might actually want to see something from Cargo's added library paths -- but
35 // for now it works.
36 pub fn restore_library_path() {
37     println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH_VAR");
38     println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH");
39     let key = env::var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
40     if let Some(env) = env::var_os("REAL_LIBRARY_PATH") {
41         env::set_var(&key, &env);
42     } else {
43         env::remove_var(&key);
44     }
45 }
46
47 pub fn run(cmd: &mut Command) {
48     println!("running: {:?}", cmd);
49     run_silent(cmd);
50 }
51
52 pub fn run_silent(cmd: &mut Command) {
53     if !try_run_silent(cmd) {
54         std::process::exit(1);
55     }
56 }
57
58 pub fn try_run_silent(cmd: &mut Command) -> bool {
59     let status = match cmd.status() {
60         Ok(status) => status,
61         Err(e) => fail(&format!(
62             "failed to execute command: {:?}\nerror: {}",
63             cmd, e
64         )),
65     };
66     if !status.success() {
67         println!(
68             "\n\ncommand did not execute successfully: {:?}\n\
69              expected success, got: {}\n\n",
70             cmd, status
71         );
72     }
73     status.success()
74 }
75
76 pub fn run_suppressed(cmd: &mut Command) {
77     if !try_run_suppressed(cmd) {
78         std::process::exit(1);
79     }
80 }
81
82 pub fn try_run_suppressed(cmd: &mut Command) -> bool {
83     let output = match cmd.output() {
84         Ok(status) => status,
85         Err(e) => fail(&format!(
86             "failed to execute command: {:?}\nerror: {}",
87             cmd, e
88         )),
89     };
90     if !output.status.success() {
91         println!(
92             "\n\ncommand did not execute successfully: {:?}\n\
93              expected success, got: {}\n\n\
94              stdout ----\n{}\n\
95              stderr ----\n{}\n\n",
96             cmd,
97             output.status,
98             String::from_utf8_lossy(&output.stdout),
99             String::from_utf8_lossy(&output.stderr)
100         );
101     }
102     output.status.success()
103 }
104
105 pub fn gnu_target(target: &str) -> &str {
106     match target {
107         "i686-pc-windows-msvc" => "i686-pc-win32",
108         "x86_64-pc-windows-msvc" => "x86_64-pc-win32",
109         "i686-pc-windows-gnu" => "i686-w64-mingw32",
110         "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32",
111         s => s,
112     }
113 }
114
115 pub fn make(host: &str) -> PathBuf {
116     if host.contains("bitrig") || host.contains("dragonfly") || host.contains("freebsd")
117         || host.contains("netbsd") || host.contains("openbsd")
118     {
119         PathBuf::from("gmake")
120     } else {
121         PathBuf::from("make")
122     }
123 }
124
125 pub fn output(cmd: &mut Command) -> String {
126     let output = match cmd.stderr(Stdio::inherit()).output() {
127         Ok(status) => status,
128         Err(e) => fail(&format!(
129             "failed to execute command: {:?}\nerror: {}",
130             cmd, e
131         )),
132     };
133     if !output.status.success() {
134         panic!(
135             "command did not execute successfully: {:?}\n\
136              expected success, got: {}",
137             cmd, output.status
138         );
139     }
140     String::from_utf8(output.stdout).unwrap()
141 }
142
143 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
144     let mut stack = dir.read_dir()
145         .unwrap()
146         .map(|e| e.unwrap())
147         .filter(|e| &*e.file_name() != ".git")
148         .collect::<Vec<_>>();
149     while let Some(entry) = stack.pop() {
150         let path = entry.path();
151         if entry.file_type().unwrap().is_dir() {
152             stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
153         } else {
154             println!("cargo:rerun-if-changed={}", path.display());
155         }
156     }
157 }
158
159 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
160 pub fn mtime(path: &Path) -> SystemTime {
161     fs::metadata(path)
162         .and_then(|f| f.modified())
163         .unwrap_or(UNIX_EPOCH)
164 }
165
166 /// Returns `true` if `dst` is up to date given that the file or files in `src`
167 /// are used to generate it.
168 ///
169 /// Uses last-modified time checks to verify this.
170 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
171     if !dst.exists() {
172         return false;
173     }
174     let threshold = mtime(dst);
175     let meta = match fs::metadata(src) {
176         Ok(meta) => meta,
177         Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
178     };
179     if meta.is_dir() {
180         dir_up_to_date(src, threshold)
181     } else {
182         meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
183     }
184 }
185
186 #[must_use]
187 pub struct NativeLibBoilerplate {
188     pub src_dir: PathBuf,
189     pub out_dir: PathBuf,
190 }
191
192 impl NativeLibBoilerplate {
193     /// On macOS we don't want to ship the exact filename that compiler-rt builds.
194     /// This conflicts with the system and ours is likely a wildly different
195     /// version, so they can't be substituted.
196     ///
197     /// As a result, we rename it here but we need to also use
198     /// `install_name_tool` on macOS to rename the commands listed inside of it to
199     /// ensure it's linked against correctly.
200     pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
201         if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
202             return
203         }
204
205         let dir = self.out_dir.join("build/lib/darwin");
206         let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
207         let src = dir.join(&format!("lib{}.dylib", name));
208         let new_name = format!("lib__rustc__{}.dylib", name);
209         let dst = dir.join(&new_name);
210
211         println!("{} => {}", src.display(), dst.display());
212         fs::rename(&src, &dst).unwrap();
213         let status = Command::new("install_name_tool")
214             .arg("-id")
215             .arg(format!("@rpath/{}", new_name))
216             .arg(&dst)
217             .status()
218             .expect("failed to execute `install_name_tool`");
219         assert!(status.success());
220     }
221 }
222
223 impl Drop for NativeLibBoilerplate {
224     fn drop(&mut self) {
225         if !thread::panicking() {
226             t!(File::create(self.out_dir.join("rustbuild.timestamp")));
227         }
228     }
229 }
230
231 // Perform standard preparations for native libraries that are build only once for all stages.
232 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
233 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
234 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
235 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
236 // of scope, so all the build actions should be completed until then.
237 pub fn native_lib_boilerplate(
238     src_dir: &Path,
239     out_name: &str,
240     link_name: &str,
241     search_subdir: &str,
242 ) -> Result<NativeLibBoilerplate, ()> {
243     rerun_if_changed_anything_in_dir(src_dir);
244
245     let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
246         env::var_os("OUT_DIR").unwrap());
247     let out_dir = PathBuf::from(out_dir).join(out_name);
248     t!(fs::create_dir_all(&out_dir));
249     if link_name.contains('=') {
250         println!("cargo:rustc-link-lib={}", link_name);
251     } else {
252         println!("cargo:rustc-link-lib=static={}", link_name);
253     }
254     println!(
255         "cargo:rustc-link-search=native={}",
256         out_dir.join(search_subdir).display()
257     );
258
259     let timestamp = out_dir.join("rustbuild.timestamp");
260     if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(src_dir, &timestamp) {
261         Ok(NativeLibBoilerplate {
262             src_dir: src_dir.to_path_buf(),
263             out_dir: out_dir,
264         })
265     } else {
266         Err(())
267     }
268 }
269
270 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
271     -> Result<(NativeLibBoilerplate, String), ()>
272 {
273     let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
274         "x86_64-unknown-linux-gnu" => (
275             format!("clang_rt.{}-x86_64", sanitizer_name),
276             "build/lib/linux",
277             false,
278         ),
279         "x86_64-apple-darwin" => (
280             format!("clang_rt.{}_osx_dynamic", sanitizer_name),
281             "build/lib/darwin",
282             true,
283         ),
284         _ => return Err(()),
285     };
286     let to_link = if apple {
287         format!("dylib=__rustc__{}", link_name)
288     } else {
289         format!("static={}", link_name)
290     };
291     // The source for `compiler-rt` comes from the `compiler-builtins` crate, so
292     // load our env var set by cargo to find the source code.
293     let dir = env::var_os("DEP_COMPILER_RT_COMPILER_RT").unwrap();
294     let lib = native_lib_boilerplate(
295         dir.as_ref(),
296         sanitizer_name,
297         &to_link,
298         search_path,
299     )?;
300     Ok((lib, link_name))
301 }
302
303 fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
304     t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
305         let meta = t!(e.metadata());
306         if meta.is_dir() {
307             dir_up_to_date(&e.path(), threshold)
308         } else {
309             meta.modified().unwrap_or(UNIX_EPOCH) < threshold
310         }
311     })
312 }
313
314 fn fail(s: &str) -> ! {
315     println!("\n\n{}\n\n", s);
316     std::process::exit(1);
317 }