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::path::{Path, PathBuf};
18 use std::process::{Command, Stdio};
21 use filetime::FileTime;
23 /// A helper macro to `unwrap` a result except also print out details like:
25 /// * The file/line of the panic
26 /// * The expression that failed
27 /// * The error itself
29 /// This is currently used judiciously throughout the build system rather than
30 /// using a `Result` with `try!`, but this may change one day...
33 ($e:expr) => (match $e {
35 Err(e) => panic!("{} failed with {}", stringify!($e), e),
39 pub fn run(cmd: &mut Command) {
40 println!("running: {:?}", cmd);
44 pub fn run_silent(cmd: &mut Command) {
45 let status = match cmd.status() {
47 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
50 if !status.success() {
51 fail(&format!("command did not execute successfully: {:?}\n\
52 expected success, got: {}",
58 pub fn run_suppressed(cmd: &mut Command) {
59 let output = match cmd.output() {
61 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
64 if !output.status.success() {
65 fail(&format!("command did not execute successfully: {:?}\n\
66 expected success, got: {}\n\n\
71 String::from_utf8_lossy(&output.stdout),
72 String::from_utf8_lossy(&output.stderr)));
76 pub fn gnu_target(target: &str) -> String {
78 "i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
79 "x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
80 "i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
81 "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
86 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
87 if target.contains("msvc") {
89 } else if target.contains("musl") {
90 Some(PathBuf::from("ar"))
91 } else if target.contains("openbsd") {
92 Some(PathBuf::from("ar"))
94 let parent = cc.parent().unwrap();
95 let file = cc.file_name().unwrap().to_str().unwrap();
96 for suffix in &["gcc", "cc", "clang"] {
97 if let Some(idx) = file.rfind(suffix) {
98 let mut file = file[..idx].to_owned();
100 return Some(parent.join(&file));
103 Some(parent.join(file))
107 pub fn make(host: &str) -> PathBuf {
108 if host.contains("bitrig") || host.contains("dragonfly") ||
109 host.contains("freebsd") || host.contains("netbsd") ||
110 host.contains("openbsd") {
111 PathBuf::from("gmake")
113 PathBuf::from("make")
117 pub fn output(cmd: &mut Command) -> String {
118 let output = match cmd.stderr(Stdio::inherit()).output() {
119 Ok(status) => status,
120 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
123 if !output.status.success() {
124 panic!("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().unwrap()
135 .filter(|e| &*e.file_name() != ".git")
136 .collect::<Vec<_>>();
137 while let Some(entry) = stack.pop() {
138 let path = entry.path();
139 if entry.file_type().unwrap().is_dir() {
140 stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
142 println!("cargo:rerun-if-changed={}", path.display());
147 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
148 pub fn mtime(path: &Path) -> FileTime {
149 fs::metadata(path).map(|f| {
150 FileTime::from_last_modification_time(&f)
151 }).unwrap_or(FileTime::zero())
154 /// Returns whether `dst` is up to date given that the file or files in `src`
155 /// are used to generate it.
157 /// Uses last-modified time checks to verify this.
158 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
159 let threshold = mtime(dst);
160 let meta = match fs::metadata(src) {
162 Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
165 dir_up_to_date(src, &threshold)
167 FileTime::from_last_modification_time(&meta) <= threshold
172 pub struct NativeLibBoilerplate {
173 pub src_dir: PathBuf,
174 pub out_dir: PathBuf,
177 impl Drop for NativeLibBoilerplate {
179 t!(File::create(self.out_dir.join("rustbuild.timestamp")));
183 // Perform standard preparations for native libraries that are build only once for all stages.
184 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
185 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
186 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
187 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
188 // of scope, so all the build actions should be completed until then.
189 pub fn native_lib_boilerplate(src_name: &str,
193 -> Result<NativeLibBoilerplate, ()> {
194 let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
195 let src_dir = current_dir.join("..").join(src_name);
196 rerun_if_changed_anything_in_dir(&src_dir);
198 let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
199 let out_dir = PathBuf::from(out_dir).join(out_name);
200 t!(create_dir_racy(&out_dir));
201 if link_name.contains('=') {
202 println!("cargo:rustc-link-lib={}", link_name);
204 println!("cargo:rustc-link-lib=static={}", link_name);
206 println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
208 let timestamp = out_dir.join("rustbuild.timestamp");
209 if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(&src_dir, ×tamp) {
210 Ok(NativeLibBoilerplate { src_dir: src_dir, out_dir: out_dir })
216 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str) -> Result<NativeLibBoilerplate, ()> {
217 let (link_name, search_path) = match &*env::var("TARGET").unwrap() {
218 "x86_64-unknown-linux-gnu" => (
219 format!("clang_rt.{}-x86_64", sanitizer_name),
222 "x86_64-apple-darwin" => (
223 format!("dylib=clang_rt.{}_osx_dynamic", sanitizer_name),
228 native_lib_boilerplate("compiler-rt", sanitizer_name, &link_name, search_path)
231 fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
232 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
233 let meta = t!(e.metadata());
235 dir_up_to_date(&e.path(), threshold)
237 FileTime::from_last_modification_time(&meta) < *threshold
242 fn fail(s: &str) -> ! {
243 println!("\n\n{}\n\n", s);
244 std::process::exit(1);
247 fn create_dir_racy(path: &Path) -> io::Result<()> {
248 match fs::create_dir(path) {
249 Ok(()) => return Ok(()),
250 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
251 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
252 Err(e) => return Err(e),
254 match path.parent() {
255 Some(p) => try!(create_dir_racy(p)),
256 None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
258 match fs::create_dir(path) {
260 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),