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