]> git.lizzy.rs Git - rust.git/blob - src/build_helper/lib.rs
1cbb8e49bfa15e0579ebeb0e3b37aa0d0bda10db
[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) -> String {
95     match target {
96         "i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
97         "x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
98         "i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
99         "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
100         s => s.to_string(),
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 Drop for NativeLibBoilerplate {
182     fn drop(&mut self) {
183         if !thread::panicking() {
184             t!(File::create(self.out_dir.join("rustbuild.timestamp")));
185         }
186     }
187 }
188
189 // Perform standard preparations for native libraries that are build only once for all stages.
190 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
191 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
192 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
193 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
194 // of scope, so all the build actions should be completed until then.
195 pub fn native_lib_boilerplate(
196     src_name: &str,
197     out_name: &str,
198     link_name: &str,
199     search_subdir: &str,
200 ) -> Result<NativeLibBoilerplate, ()> {
201     let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
202     let src_dir = current_dir.join("..").join(src_name);
203     rerun_if_changed_anything_in_dir(&src_dir);
204
205     let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
206     let out_dir = PathBuf::from(out_dir).join(out_name);
207     t!(fs::create_dir_all(&out_dir));
208     if link_name.contains('=') {
209         println!("cargo:rustc-link-lib={}", link_name);
210     } else {
211         println!("cargo:rustc-link-lib=static={}", link_name);
212     }
213     println!(
214         "cargo:rustc-link-search=native={}",
215         out_dir.join(search_subdir).display()
216     );
217
218     let timestamp = out_dir.join("rustbuild.timestamp");
219     if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(&src_dir, &timestamp) {
220         Ok(NativeLibBoilerplate {
221             src_dir: src_dir,
222             out_dir: out_dir,
223         })
224     } else {
225         Err(())
226     }
227 }
228
229 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
230     -> Result<(NativeLibBoilerplate, String), ()>
231 {
232     let (link_name, search_path, dynamic) = match &*env::var("TARGET").unwrap() {
233         "x86_64-unknown-linux-gnu" => (
234             format!("clang_rt.{}-x86_64", sanitizer_name),
235             "build/lib/linux",
236             false,
237         ),
238         "x86_64-apple-darwin" => (
239             format!("clang_rt.{}_osx_dynamic", sanitizer_name),
240             "build/lib/darwin",
241             true,
242         ),
243         _ => return Err(()),
244     };
245     let to_link = if dynamic {
246         format!("dylib={}", link_name)
247     } else {
248         format!("static={}", link_name)
249     };
250     let lib = native_lib_boilerplate(
251         "libcompiler_builtins/compiler-rt",
252         sanitizer_name,
253         &to_link,
254         search_path,
255     )?;
256     Ok((lib, link_name))
257 }
258
259 fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
260     t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
261         let meta = t!(e.metadata());
262         if meta.is_dir() {
263             dir_up_to_date(&e.path(), threshold)
264         } else {
265             meta.modified().unwrap_or(UNIX_EPOCH) < threshold
266         }
267     })
268 }
269
270 fn fail(s: &str) -> ! {
271     println!("\n\n{}\n\n", s);
272     std::process::exit(1);
273 }