]> git.lizzy.rs Git - rust.git/blobdiff - src/build_helper/lib.rs
Format the world
[rust.git] / src / build_helper / lib.rs
index f035a7119188a1aa3c415e09096e20b8e896104f..3f42533238a89dc0cfcba06e016f63dee2d209e1 100644 (file)
@@ -1,9 +1,9 @@
 use std::fs::File;
 use std::path::{Path, PathBuf};
 use std::process::{Command, Stdio};
+use std::thread;
 use std::time::{SystemTime, UNIX_EPOCH};
 use std::{env, fs};
-use std::thread;
 
 /// A helper macro to `unwrap` a result except also print out details like:
 ///
@@ -21,6 +21,13 @@ macro_rules! t {
             Err(e) => panic!("{} failed with {}", stringify!($e), e),
         }
     };
+    // it can show extra info in the second parameter
+    ($e:expr, $extra:expr) => {
+        match $e {
+            Ok(e) => e,
+            Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
+        }
+    };
 }
 
 // Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
@@ -57,10 +64,7 @@ pub fn run(cmd: &mut Command) {
 pub fn try_run(cmd: &mut Command) -> bool {
     let status = match cmd.status() {
         Ok(status) => status,
-        Err(e) => fail(&format!(
-            "failed to execute command: {:?}\nerror: {}",
-            cmd, e
-        )),
+        Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
     };
     if !status.success() {
         println!(
@@ -81,10 +85,7 @@ pub fn run_suppressed(cmd: &mut Command) {
 pub fn try_run_suppressed(cmd: &mut Command) -> bool {
     let output = match cmd.output() {
         Ok(status) => status,
-        Err(e) => fail(&format!(
-            "failed to execute command: {:?}\nerror: {}",
-            cmd, e
-        )),
+        Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
     };
     if !output.status.success() {
         println!(
@@ -112,8 +113,10 @@ pub fn gnu_target(target: &str) -> &str {
 }
 
 pub fn make(host: &str) -> PathBuf {
-    if host.contains("dragonfly") || host.contains("freebsd")
-        || host.contains("netbsd") || host.contains("openbsd")
+    if host.contains("dragonfly")
+        || host.contains("freebsd")
+        || host.contains("netbsd")
+        || host.contains("openbsd")
     {
         PathBuf::from("gmake")
     } else {
@@ -124,10 +127,7 @@ pub fn make(host: &str) -> PathBuf {
 pub fn output(cmd: &mut Command) -> String {
     let output = match cmd.stderr(Stdio::inherit()).output() {
         Ok(status) => status,
-        Err(e) => fail(&format!(
-            "failed to execute command: {:?}\nerror: {}",
-            cmd, e
-        )),
+        Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
     };
     if !output.status.success() {
         panic!(
@@ -140,7 +140,8 @@ pub fn output(cmd: &mut Command) -> String {
 }
 
 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
-    let mut stack = dir.read_dir()
+    let mut stack = dir
+        .read_dir()
         .unwrap()
         .map(|e| e.unwrap())
         .filter(|e| &*e.file_name() != ".git")
@@ -157,9 +158,7 @@ pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
 
 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
 pub fn mtime(path: &Path) -> SystemTime {
-    fs::metadata(path)
-        .and_then(|f| f.modified())
-        .unwrap_or(UNIX_EPOCH)
+    fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)
 }
 
 /// Returns `true` if `dst` is up to date given that the file or files in `src`
@@ -198,7 +197,7 @@ impl NativeLibBoilerplate {
     /// ensure it's linked against correctly.
     pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
         if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
-            return
+            return;
         }
 
         let dir = self.out_dir.join("build/lib/darwin");
@@ -241,8 +240,8 @@ pub fn native_lib_boilerplate(
 ) -> Result<NativeLibBoilerplate, ()> {
     rerun_if_changed_anything_in_dir(src_dir);
 
-    let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
-        env::var_os("OUT_DIR").unwrap());
+    let out_dir =
+        env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(|| env::var_os("OUT_DIR").unwrap());
     let out_dir = PathBuf::from(out_dir).join(out_name);
     t!(fs::create_dir_all(&out_dir));
     if link_name.contains('=') {
@@ -250,36 +249,26 @@ pub fn native_lib_boilerplate(
     } else {
         println!("cargo:rustc-link-lib=static={}", link_name);
     }
-    println!(
-        "cargo:rustc-link-search=native={}",
-        out_dir.join(search_subdir).display()
-    );
+    println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
 
     let timestamp = out_dir.join("rustbuild.timestamp");
     if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(src_dir, &timestamp) {
-        Ok(NativeLibBoilerplate {
-            src_dir: src_dir.to_path_buf(),
-            out_dir,
-        })
+        Ok(NativeLibBoilerplate { src_dir: src_dir.to_path_buf(), out_dir })
     } else {
         Err(())
     }
 }
 
-pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
-    -> Result<(NativeLibBoilerplate, String), ()>
-{
+pub fn sanitizer_lib_boilerplate(
+    sanitizer_name: &str,
+) -> Result<(NativeLibBoilerplate, String), ()> {
     let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
-        "x86_64-unknown-linux-gnu" => (
-            format!("clang_rt.{}-x86_64", sanitizer_name),
-            "build/lib/linux",
-            false,
-        ),
-        "x86_64-apple-darwin" => (
-            format!("clang_rt.{}_osx_dynamic", sanitizer_name),
-            "build/lib/darwin",
-            true,
-        ),
+        "x86_64-unknown-linux-gnu" => {
+            (format!("clang_rt.{}-x86_64", sanitizer_name), "build/lib/linux", false)
+        }
+        "x86_64-apple-darwin" => {
+            (format!("clang_rt.{}_osx_dynamic", sanitizer_name), "build/lib/darwin", true)
+        }
         _ => return Err(()),
     };
     let to_link = if apple {
@@ -290,12 +279,7 @@ pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
     // This env var is provided by rustbuild to tell us where `compiler-rt`
     // lives.
     let dir = env::var_os("RUST_COMPILER_RT_ROOT").unwrap();
-    let lib = native_lib_boilerplate(
-        dir.as_ref(),
-        sanitizer_name,
-        &to_link,
-        search_path,
-    )?;
+    let lib = native_lib_boilerplate(dir.as_ref(), sanitizer_name, &to_link, search_path)?;
     Ok((lib, link_name))
 }