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.
13 extern crate filetime;
16 use std::process::{Command, Stdio};
17 use std::path::{Path, PathBuf};
19 use filetime::FileTime;
21 /// A helper macro to `unwrap` a result except also print out details like:
23 /// * The file/line of the panic
24 /// * The expression that failed
25 /// * The error itself
27 /// This is currently used judiciously throughout the build system rather than
28 /// using a `Result` with `try!`, but this may change one day...
31 ($e:expr) => (match $e {
33 Err(e) => panic!("{} failed with {}", stringify!($e), e),
37 pub fn run(cmd: &mut Command) {
38 println!("running: {:?}", cmd);
42 pub fn run_silent(cmd: &mut Command) {
43 let status = match cmd.status() {
45 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
48 if !status.success() {
49 fail(&format!("command did not execute successfully: {:?}\n\
50 expected success, got: {}",
56 pub fn run_suppressed(cmd: &mut Command) {
57 let output = match cmd.output() {
59 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
62 if !output.status.success() {
63 fail(&format!("command did not execute successfully: {:?}\n\
64 expected success, got: {}\n\n\
69 String::from_utf8_lossy(&output.stdout),
70 String::from_utf8_lossy(&output.stderr)));
74 pub fn gnu_target(target: &str) -> String {
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(),
84 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
85 if target.contains("msvc") {
87 } else if target.contains("musl") {
88 Some(PathBuf::from("ar"))
89 } else if target.contains("openbsd") {
90 Some(PathBuf::from("ar"))
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();
98 return Some(parent.join(&file));
101 Some(parent.join(file))
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")
111 PathBuf::from("make")
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: {}",
121 if !output.status.success() {
122 panic!("command did not execute successfully: {:?}\n\
123 expected success, got: {}",
127 String::from_utf8(output.stdout).unwrap()
130 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
131 let mut stack = dir.read_dir().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()));
140 println!("cargo:rerun-if-changed={}", path.display());
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())
152 /// Returns whether `dst` is up to date given that the file or files in `src`
153 /// are used to generate it.
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) {
160 Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
163 dir_up_to_date(src, &threshold)
165 FileTime::from_last_modification_time(&meta) <= threshold
169 pub struct NativeLibBoilerplate {
170 pub skip_build: bool,
171 pub src_dir: PathBuf,
172 pub out_dir: PathBuf,
173 pub timestamp: PathBuf,
176 pub fn native_lib_boilerplate(src_name: &str,
179 timestamp_name: &str,
181 -> NativeLibBoilerplate {
182 let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
183 let src_dir = current_dir.join("..").join(src_name);
184 rerun_if_changed_anything_in_dir(&src_dir);
186 let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
187 let out_dir = PathBuf::from(out_dir).join(out_name);
188 let _ = fs::create_dir_all(&out_dir);
189 println!("cargo:rustc-link-lib=static={}", link_name);
190 println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
192 let timestamp = out_dir.join(timestamp_name);
193 let skip_build = up_to_date(Path::new("build.rs"), ×tamp) &&
194 up_to_date(&src_dir, ×tamp);
196 NativeLibBoilerplate {
197 skip_build: skip_build,
200 timestamp: timestamp,
204 fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
205 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
206 let meta = t!(e.metadata());
208 dir_up_to_date(&e.path(), threshold)
210 FileTime::from_last_modification_time(&meta) < *threshold
215 fn fail(s: &str) -> ! {
216 println!("\n\n{}\n\n", s);
217 std::process::exit(1);