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.
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.
12 use std::path::{Path, PathBuf};
13 use std::process::{Command, Stdio};
14 use std::time::{SystemTime, UNIX_EPOCH};
18 /// A helper macro to `unwrap` a result except also print out details like:
20 /// * The file/line of the panic
21 /// * The expression that failed
22 /// * The error itself
24 /// This is currently used judiciously throughout the build system rather than
25 /// using a `Result` with `try!`, but this may change one day...
31 Err(e) => panic!("{} failed with {}", stringify!($e), e),
36 pub fn run(cmd: &mut Command) {
37 println!("running: {:?}", cmd);
41 pub fn run_silent(cmd: &mut Command) {
42 if !try_run_silent(cmd) {
43 std::process::exit(1);
47 pub fn try_run_silent(cmd: &mut Command) -> bool {
48 let status = match cmd.status() {
50 Err(e) => fail(&format!(
51 "failed to execute command: {:?}\nerror: {}",
55 if !status.success() {
57 "\n\ncommand did not execute successfully: {:?}\n\
58 expected success, got: {}\n\n",
65 pub fn run_suppressed(cmd: &mut Command) {
66 if !try_run_suppressed(cmd) {
67 std::process::exit(1);
71 pub fn try_run_suppressed(cmd: &mut Command) -> bool {
72 let output = match cmd.output() {
74 Err(e) => fail(&format!(
75 "failed to execute command: {:?}\nerror: {}",
79 if !output.status.success() {
81 "\n\ncommand did not execute successfully: {:?}\n\
82 expected success, got: {}\n\n\
87 String::from_utf8_lossy(&output.stdout),
88 String::from_utf8_lossy(&output.stderr)
91 output.status.success()
94 pub fn gnu_target(target: &str) -> String {
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(),
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")
108 PathBuf::from("gmake")
110 PathBuf::from("make")
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: {}",
122 if !output.status.success() {
124 "command did not execute successfully: {:?}\n\
125 expected success, got: {}",
129 String::from_utf8(output.stdout).unwrap()
132 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
133 let mut stack = dir.read_dir()
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()));
143 println!("cargo:rerun-if-changed={}", path.display());
148 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
149 pub fn mtime(path: &Path) -> SystemTime {
151 .and_then(|f| f.modified())
152 .unwrap_or(UNIX_EPOCH)
155 /// Returns whether `dst` is up to date given that the file or files in `src`
156 /// are used to generate it.
158 /// Uses last-modified time checks to verify this.
159 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
163 let threshold = mtime(dst);
164 let meta = match fs::metadata(src) {
166 Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
169 dir_up_to_date(src, threshold)
171 meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
176 pub struct NativeLibBoilerplate {
177 pub src_dir: PathBuf,
178 pub out_dir: PathBuf,
181 impl Drop for NativeLibBoilerplate {
183 if !thread::panicking() {
184 t!(File::create(self.out_dir.join("rustbuild.timestamp")));
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(
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);
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);
211 println!("cargo:rustc-link-lib=static={}", link_name);
214 "cargo:rustc-link-search=native={}",
215 out_dir.join(search_subdir).display()
218 let timestamp = out_dir.join("rustbuild.timestamp");
219 if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(&src_dir, ×tamp) {
220 Ok(NativeLibBoilerplate {
229 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
230 -> Result<(NativeLibBoilerplate, String), ()>
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),
238 "x86_64-apple-darwin" => (
239 format!("clang_rt.{}_osx_dynamic", sanitizer_name),
245 let to_link = if dynamic {
246 format!("dylib={}", link_name)
248 format!("static={}", link_name)
250 let lib = native_lib_boilerplate(
251 "libcompiler_builtins/compiler-rt",
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());
263 dir_up_to_date(&e.path(), threshold)
265 meta.modified().unwrap_or(UNIX_EPOCH) < threshold
270 fn fail(s: &str) -> ! {
271 println!("\n\n{}\n\n", s);
272 std::process::exit(1);