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