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