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