]> git.lizzy.rs Git - rust.git/blob - src/build_helper/lib.rs
rustdoc: Hide `self: Box<Self>` in list of deref methods
[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 #![deny(warnings)]
12
13 extern crate filetime;
14
15 use std::fs::File;
16 use std::io;
17 use std::path::{Path, PathBuf};
18 use std::process::{Command, Stdio};
19 use std::{fs, env};
20
21 use filetime::FileTime;
22
23 /// A helper macro to `unwrap` a result except also print out details like:
24 ///
25 /// * The file/line of the panic
26 /// * The expression that failed
27 /// * The error itself
28 ///
29 /// This is currently used judiciously throughout the build system rather than
30 /// using a `Result` with `try!`, but this may change one day...
31 #[macro_export]
32 macro_rules! t {
33     ($e:expr) => (match $e {
34         Ok(e) => e,
35         Err(e) => panic!("{} failed with {}", stringify!($e), e),
36     })
37 }
38
39 pub fn run(cmd: &mut Command) {
40     println!("running: {:?}", cmd);
41     run_silent(cmd);
42 }
43
44 pub fn run_silent(cmd: &mut Command) {
45     let status = match cmd.status() {
46         Ok(status) => status,
47         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
48                                 cmd, e)),
49     };
50     if !status.success() {
51         fail(&format!("command did not execute successfully: {:?}\n\
52                        expected success, got: {}",
53                       cmd,
54                       status));
55     }
56 }
57
58 pub fn run_suppressed(cmd: &mut Command) {
59     let output = match cmd.output() {
60         Ok(status) => status,
61         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
62                                 cmd, e)),
63     };
64     if !output.status.success() {
65         fail(&format!("command did not execute successfully: {:?}\n\
66                        expected success, got: {}\n\n\
67                        stdout ----\n{}\n\
68                        stderr ----\n{}\n",
69                       cmd,
70                       output.status,
71                       String::from_utf8_lossy(&output.stdout),
72                       String::from_utf8_lossy(&output.stderr)));
73     }
74 }
75
76 pub fn gnu_target(target: &str) -> String {
77     match target {
78         "i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
79         "x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
80         "i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
81         "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
82         s => s.to_string(),
83     }
84 }
85
86 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
87     if target.contains("msvc") {
88         None
89     } else if target.contains("musl") {
90         Some(PathBuf::from("ar"))
91     } else if target.contains("openbsd") {
92         Some(PathBuf::from("ar"))
93     } else {
94         let parent = cc.parent().unwrap();
95         let file = cc.file_name().unwrap().to_str().unwrap();
96         for suffix in &["gcc", "cc", "clang"] {
97             if let Some(idx) = file.rfind(suffix) {
98                 let mut file = file[..idx].to_owned();
99                 file.push_str("ar");
100                 return Some(parent.join(&file));
101             }
102         }
103         Some(parent.join(file))
104     }
105 }
106
107 pub fn make(host: &str) -> PathBuf {
108     if host.contains("bitrig") || host.contains("dragonfly") ||
109         host.contains("freebsd") || host.contains("netbsd") ||
110         host.contains("openbsd") {
111         PathBuf::from("gmake")
112     } else {
113         PathBuf::from("make")
114     }
115 }
116
117 pub fn output(cmd: &mut Command) -> String {
118     let output = match cmd.stderr(Stdio::inherit()).output() {
119         Ok(status) => status,
120         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
121                                 cmd, e)),
122     };
123     if !output.status.success() {
124         panic!("command did not execute successfully: {:?}\n\
125                 expected success, got: {}",
126                cmd,
127                output.status);
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().unwrap()
134                        .map(|e| e.unwrap())
135                        .filter(|e| &*e.file_name() != ".git")
136                        .collect::<Vec<_>>();
137     while let Some(entry) = stack.pop() {
138         let path = entry.path();
139         if entry.file_type().unwrap().is_dir() {
140             stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
141         } else {
142             println!("cargo:rerun-if-changed={}", path.display());
143         }
144     }
145 }
146
147 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
148 pub fn mtime(path: &Path) -> FileTime {
149     fs::metadata(path).map(|f| {
150         FileTime::from_last_modification_time(&f)
151     }).unwrap_or(FileTime::zero())
152 }
153
154 /// Returns whether `dst` is up to date given that the file or files in `src`
155 /// are used to generate it.
156 ///
157 /// Uses last-modified time checks to verify this.
158 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
159     let threshold = mtime(dst);
160     let meta = match fs::metadata(src) {
161         Ok(meta) => meta,
162         Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
163     };
164     if meta.is_dir() {
165         dir_up_to_date(src, &threshold)
166     } else {
167         FileTime::from_last_modification_time(&meta) <= threshold
168     }
169 }
170
171 #[must_use]
172 pub struct NativeLibBoilerplate {
173     pub src_dir: PathBuf,
174     pub out_dir: PathBuf,
175 }
176
177 impl Drop for NativeLibBoilerplate {
178     fn drop(&mut self) {
179         t!(File::create(self.out_dir.join("rustbuild.timestamp")));
180     }
181 }
182
183 // Perform standard preparations for native libraries that are build only once for all stages.
184 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
185 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
186 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
187 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
188 // of scope, so all the build actions should be completed until then.
189 pub fn native_lib_boilerplate(src_name: &str,
190                               out_name: &str,
191                               link_name: &str,
192                               search_subdir: &str)
193                               -> Result<NativeLibBoilerplate, ()> {
194     let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
195     let src_dir = current_dir.join("..").join(src_name);
196     rerun_if_changed_anything_in_dir(&src_dir);
197
198     let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
199     let out_dir = PathBuf::from(out_dir).join(out_name);
200     t!(create_dir_racy(&out_dir));
201     if link_name.contains('=') {
202         println!("cargo:rustc-link-lib={}", link_name);
203     } else {
204         println!("cargo:rustc-link-lib=static={}", link_name);
205     }
206     println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
207
208     let timestamp = out_dir.join("rustbuild.timestamp");
209     if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(&src_dir, &timestamp) {
210         Ok(NativeLibBoilerplate { src_dir: src_dir, out_dir: out_dir })
211     } else {
212         Err(())
213     }
214 }
215
216 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str) -> Result<NativeLibBoilerplate, ()> {
217     let (link_name, search_path) = match &*env::var("TARGET").unwrap() {
218         "x86_64-unknown-linux-gnu" => (
219             format!("clang_rt.{}-x86_64", sanitizer_name),
220             "build/lib/linux",
221         ),
222         "x86_64-apple-darwin" => (
223             format!("dylib=clang_rt.{}_osx_dynamic", sanitizer_name),
224             "build/lib/darwin",
225         ),
226         _ => return Err(()),
227     };
228     native_lib_boilerplate("compiler-rt", sanitizer_name, &link_name, search_path)
229 }
230
231 fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
232     t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
233         let meta = t!(e.metadata());
234         if meta.is_dir() {
235             dir_up_to_date(&e.path(), threshold)
236         } else {
237             FileTime::from_last_modification_time(&meta) < *threshold
238         }
239     })
240 }
241
242 fn fail(s: &str) -> ! {
243     println!("\n\n{}\n\n", s);
244     std::process::exit(1);
245 }
246
247 fn create_dir_racy(path: &Path) -> io::Result<()> {
248     match fs::create_dir(path) {
249         Ok(()) => return Ok(()),
250         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
251         Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
252         Err(e) => return Err(e),
253     }
254     match path.parent() {
255         Some(p) => try!(create_dir_racy(p)),
256         None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
257     }
258     match fs::create_dir(path) {
259         Ok(()) => Ok(()),
260         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
261         Err(e) => Err(e),
262     }
263 }