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) -> &str {
96 "i686-pc-windows-msvc" => "i686-pc-win32",
97 "x86_64-pc-windows-msvc" => "x86_64-pc-win32",
98 "i686-pc-windows-gnu" => "i686-w64-mingw32",
99 "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32",
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 NativeLibBoilerplate {
182 /// On OSX we don't want to ship the exact filename that compiler-rt builds.
183 /// This conflicts with the system and ours is likely a wildly different
184 /// version, so they can't be substituted.
186 /// As a result, we rename it here but we need to also use
187 /// `install_name_tool` on OSX to rename the commands listed inside of it to
188 /// ensure it's linked against correctly.
189 pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
190 if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
194 let dir = self.out_dir.join("build/lib/darwin");
195 let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
196 let src = dir.join(&format!("lib{}.dylib", name));
197 let new_name = format!("lib__rustc__{}.dylib", name);
198 let dst = dir.join(&new_name);
200 println!("{} => {}", src.display(), dst.display());
201 fs::rename(&src, &dst).unwrap();
202 let status = Command::new("install_name_tool")
204 .arg(format!("@rpath/{}", new_name))
207 .expect("failed to execute `install_name_tool`");
208 assert!(status.success());
212 impl Drop for NativeLibBoilerplate {
214 if !thread::panicking() {
215 t!(File::create(self.out_dir.join("rustbuild.timestamp")));
220 // Perform standard preparations for native libraries that are build only once for all stages.
221 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
222 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
223 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
224 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
225 // of scope, so all the build actions should be completed until then.
226 pub fn native_lib_boilerplate(
231 ) -> Result<NativeLibBoilerplate, ()> {
232 rerun_if_changed_anything_in_dir(src_dir);
234 let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
235 env::var_os("OUT_DIR").unwrap());
236 let out_dir = PathBuf::from(out_dir).join(out_name);
237 t!(fs::create_dir_all(&out_dir));
238 if link_name.contains('=') {
239 println!("cargo:rustc-link-lib={}", link_name);
241 println!("cargo:rustc-link-lib=static={}", link_name);
244 "cargo:rustc-link-search=native={}",
245 out_dir.join(search_subdir).display()
248 let timestamp = out_dir.join("rustbuild.timestamp");
249 if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(src_dir, ×tamp) {
250 Ok(NativeLibBoilerplate {
251 src_dir: src_dir.to_path_buf(),
259 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
260 -> Result<(NativeLibBoilerplate, String), ()>
262 let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
263 "x86_64-unknown-linux-gnu" => (
264 format!("clang_rt.{}-x86_64", sanitizer_name),
268 "x86_64-apple-darwin" => (
269 format!("clang_rt.{}_osx_dynamic", sanitizer_name),
275 let to_link = if apple {
276 format!("dylib=__rustc__{}", link_name)
278 format!("static={}", link_name)
280 // The source for `compiler-rt` comes from the `compiler-builtins` crate, so
281 // load our env var set by cargo to find the source code.
282 let dir = env::var_os("DEP_COMPILER_RT_COMPILER_RT").unwrap();
283 let lib = native_lib_boilerplate(
292 fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
293 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
294 let meta = t!(e.metadata());
296 dir_up_to_date(&e.path(), threshold)
298 meta.modified().unwrap_or(UNIX_EPOCH) < threshold
303 fn fail(s: &str) -> ! {
304 println!("\n\n{}\n\n", s);
305 std::process::exit(1);