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;
17 use std::process::{Command, Stdio};
18 use std::path::{Path, PathBuf};
20 use filetime::FileTime;
22 /// A helper macro to `unwrap` a result except also print out details like:
24 /// * The file/line of the panic
25 /// * The expression that failed
26 /// * The error itself
28 /// This is currently used judiciously throughout the build system rather than
29 /// using a `Result` with `try!`, but this may change one day...
32 ($e:expr) => (match $e {
34 Err(e) => panic!("{} failed with {}", stringify!($e), e),
38 pub fn run(cmd: &mut Command) {
39 println!("running: {:?}", cmd);
43 pub fn run_silent(cmd: &mut Command) {
44 let status = match cmd.status() {
46 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
49 if !status.success() {
50 fail(&format!("command did not execute successfully: {:?}\n\
51 expected success, got: {}",
57 pub fn run_suppressed(cmd: &mut Command) {
58 let output = match cmd.output() {
60 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
63 if !output.status.success() {
64 fail(&format!("command did not execute successfully: {:?}\n\
65 expected success, got: {}\n\n\
70 String::from_utf8_lossy(&output.stdout),
71 String::from_utf8_lossy(&output.stderr)));
75 pub fn gnu_target(target: &str) -> String {
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(),
85 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
86 if target.contains("msvc") {
88 } else if target.contains("musl") {
89 Some(PathBuf::from("ar"))
90 } else if target.contains("openbsd") {
91 Some(PathBuf::from("ar"))
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();
99 return Some(parent.join(&file));
102 Some(parent.join(file))
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")
112 PathBuf::from("make")
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: {}",
122 if !output.status.success() {
123 panic!("command did not execute successfully: {:?}\n\
124 expected success, got: {}",
128 String::from_utf8(output.stdout).unwrap()
131 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
132 let mut stack = dir.read_dir().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()));
141 println!("cargo:rerun-if-changed={}", path.display());
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())
153 /// Returns whether `dst` is up to date given that the file or files in `src`
154 /// are used to generate it.
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) {
161 Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
164 dir_up_to_date(src, &threshold)
166 FileTime::from_last_modification_time(&meta) <= threshold
171 pub struct NativeLibBoilerplate {
172 pub src_dir: PathBuf,
173 pub out_dir: PathBuf,
176 impl Drop for NativeLibBoilerplate {
178 t!(File::create(self.out_dir.join("rustbuild.timestamp")));
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,
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);
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());
203 let timestamp = out_dir.join("rustbuild.timestamp");
204 if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(&src_dir, ×tamp) {
205 Ok(NativeLibBoilerplate { src_dir: src_dir, out_dir: out_dir })
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());
215 dir_up_to_date(&e.path(), threshold)
217 FileTime::from_last_modification_time(&meta) < *threshold
222 fn fail(s: &str) -> ! {
223 println!("\n\n{}\n\n", s);
224 std::process::exit(1);