]> git.lizzy.rs Git - rust.git/blob - src/build_helper/lib.rs
Auto merge of #40245 - cuviper:maybe-not-pie, 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;
16 use std::process::{Command, Stdio};
17 use std::path::{Path, PathBuf};
18
19 use filetime::FileTime;
20
21 /// A helper macro to `unwrap` a result except also print out details like:
22 ///
23 /// * The file/line of the panic
24 /// * The expression that failed
25 /// * The error itself
26 ///
27 /// This is currently used judiciously throughout the build system rather than
28 /// using a `Result` with `try!`, but this may change one day...
29 #[macro_export]
30 macro_rules! t {
31     ($e:expr) => (match $e {
32         Ok(e) => e,
33         Err(e) => panic!("{} failed with {}", stringify!($e), e),
34     })
35 }
36
37 pub fn run(cmd: &mut Command) {
38     println!("running: {:?}", cmd);
39     run_silent(cmd);
40 }
41
42 pub fn run_silent(cmd: &mut Command) {
43     let status = match cmd.status() {
44         Ok(status) => status,
45         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
46                                 cmd, e)),
47     };
48     if !status.success() {
49         fail(&format!("command did not execute successfully: {:?}\n\
50                        expected success, got: {}",
51                       cmd,
52                       status));
53     }
54 }
55
56 pub fn run_suppressed(cmd: &mut Command) {
57     let output = match cmd.output() {
58         Ok(status) => status,
59         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
60                                 cmd, e)),
61     };
62     if !output.status.success() {
63         fail(&format!("command did not execute successfully: {:?}\n\
64                        expected success, got: {}\n\n\
65                        stdout ----\n{}\n\
66                        stderr ----\n{}\n",
67                       cmd,
68                       output.status,
69                       String::from_utf8_lossy(&output.stdout),
70                       String::from_utf8_lossy(&output.stderr)));
71     }
72 }
73
74 pub fn gnu_target(target: &str) -> String {
75     match target {
76         "i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
77         "x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
78         "i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
79         "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
80         s => s.to_string(),
81     }
82 }
83
84 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
85     if target.contains("msvc") {
86         None
87     } else if target.contains("musl") {
88         Some(PathBuf::from("ar"))
89     } else if target.contains("openbsd") {
90         Some(PathBuf::from("ar"))
91     } else {
92         let parent = cc.parent().unwrap();
93         let file = cc.file_name().unwrap().to_str().unwrap();
94         for suffix in &["gcc", "cc", "clang"] {
95             if let Some(idx) = file.rfind(suffix) {
96                 let mut file = file[..idx].to_owned();
97                 file.push_str("ar");
98                 return Some(parent.join(&file));
99             }
100         }
101         Some(parent.join(file))
102     }
103 }
104
105 pub fn make(host: &str) -> PathBuf {
106     if host.contains("bitrig") || host.contains("dragonfly") ||
107         host.contains("freebsd") || host.contains("netbsd") ||
108         host.contains("openbsd") {
109         PathBuf::from("gmake")
110     } else {
111         PathBuf::from("make")
112     }
113 }
114
115 pub fn output(cmd: &mut Command) -> String {
116     let output = match cmd.stderr(Stdio::inherit()).output() {
117         Ok(status) => status,
118         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
119                                 cmd, e)),
120     };
121     if !output.status.success() {
122         panic!("command did not execute successfully: {:?}\n\
123                 expected success, got: {}",
124                cmd,
125                output.status);
126     }
127     String::from_utf8(output.stdout).unwrap()
128 }
129
130 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
131     let mut stack = dir.read_dir().unwrap()
132                        .map(|e| e.unwrap())
133                        .filter(|e| &*e.file_name() != ".git")
134                        .collect::<Vec<_>>();
135     while let Some(entry) = stack.pop() {
136         let path = entry.path();
137         if entry.file_type().unwrap().is_dir() {
138             stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
139         } else {
140             println!("cargo:rerun-if-changed={}", path.display());
141         }
142     }
143 }
144
145 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
146 pub fn mtime(path: &Path) -> FileTime {
147     fs::metadata(path).map(|f| {
148         FileTime::from_last_modification_time(&f)
149     }).unwrap_or(FileTime::zero())
150 }
151
152 /// Returns whether `dst` is up to date given that the file or files in `src`
153 /// are used to generate it.
154 ///
155 /// Uses last-modified time checks to verify this.
156 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
157     let threshold = mtime(dst);
158     let meta = match fs::metadata(src) {
159         Ok(meta) => meta,
160         Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
161     };
162     if meta.is_dir() {
163         dir_up_to_date(src, &threshold)
164     } else {
165         FileTime::from_last_modification_time(&meta) <= threshold
166     }
167 }
168
169 fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
170     t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
171         let meta = t!(e.metadata());
172         if meta.is_dir() {
173             dir_up_to_date(&e.path(), threshold)
174         } else {
175             FileTime::from_last_modification_time(&meta) < *threshold
176         }
177     })
178 }
179
180 fn fail(s: &str) -> ! {
181     println!("\n\n{}\n\n", s);
182     std::process::exit(1);
183 }