From 2e5d925926e3953de7d9e9df46d8ab14810515d1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 18 Jul 2018 07:34:54 -0700 Subject: [PATCH] Don't build twice the sanitizers on Linux This commit is an attempted fix at #50887. It was noticed that on that issue we're building both x86_64 and i386 versions of libraries, but we only actually need the x86_64 versions! This hopes that the build race condition exhibited in #50887 is connected to building both architectures and/or building a lot of libraries, so this should help us build precisely what we need and no more. --- src/build_helper/lib.rs | 27 ++++++++++++++++++++------- src/librustc_asan/build.rs | 4 ++-- src/librustc_lsan/build.rs | 4 ++-- src/librustc_msan/build.rs | 4 ++-- src/librustc_tsan/build.rs | 4 ++-- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/build_helper/lib.rs b/src/build_helper/lib.rs index 4d767f0968976..00e5dee256eeb 100644 --- a/src/build_helper/lib.rs +++ b/src/build_helper/lib.rs @@ -15,6 +15,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; 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: /// @@ -181,7 +182,9 @@ pub struct NativeLibBoilerplate { impl Drop for NativeLibBoilerplate { fn drop(&mut self) { - t!(File::create(self.out_dir.join("rustbuild.timestamp"))); + if !thread::panicking() { + t!(File::create(self.out_dir.join("rustbuild.timestamp"))); + } } } @@ -225,24 +228,34 @@ pub fn native_lib_boilerplate( } } -pub fn sanitizer_lib_boilerplate(sanitizer_name: &str) -> Result { - let (link_name, search_path) = match &*env::var("TARGET").unwrap() { +pub fn sanitizer_lib_boilerplate(sanitizer_name: &str) + -> Result<(NativeLibBoilerplate, String), ()> +{ + let (link_name, search_path, dynamic) = 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!("dylib=clang_rt.{}_osx_dynamic", sanitizer_name), + format!("clang_rt.{}_osx_dynamic", sanitizer_name), "build/lib/darwin", + true, ), _ => return Err(()), }; - native_lib_boilerplate( + let to_link = if dynamic { + format!("dylib={}", link_name) + } else { + format!("static={}", link_name) + }; + let lib = native_lib_boilerplate( "libcompiler_builtins/compiler-rt", sanitizer_name, - &link_name, + &to_link, search_path, - ) + )?; + Ok((lib, link_name)) } fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool { diff --git a/src/librustc_asan/build.rs b/src/librustc_asan/build.rs index cb7721affe761..b8614c520e7cf 100644 --- a/src/librustc_asan/build.rs +++ b/src/librustc_asan/build.rs @@ -18,7 +18,7 @@ use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = match sanitizer_lib_boilerplate("asan") { + let (native, target) = match sanitizer_lib_boilerplate("asan") { Ok(native) => native, _ => return, }; @@ -29,7 +29,7 @@ fn main() { .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) .out_dir(&native.out_dir) - .build_target("asan") + .build_target(&target) .build(); } println!("cargo:rerun-if-env-changed=LLVM_CONFIG"); diff --git a/src/librustc_lsan/build.rs b/src/librustc_lsan/build.rs index 3d2ae480de6df..05e40cdceddda 100644 --- a/src/librustc_lsan/build.rs +++ b/src/librustc_lsan/build.rs @@ -18,7 +18,7 @@ use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = match sanitizer_lib_boilerplate("lsan") { + let (native, target) = match sanitizer_lib_boilerplate("lsan") { Ok(native) => native, _ => return, }; @@ -29,7 +29,7 @@ fn main() { .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) .out_dir(&native.out_dir) - .build_target("lsan") + .build_target(&target) .build(); } println!("cargo:rerun-if-env-changed=LLVM_CONFIG"); diff --git a/src/librustc_msan/build.rs b/src/librustc_msan/build.rs index 7e2a82dd0ab94..4abfc3585602f 100644 --- a/src/librustc_msan/build.rs +++ b/src/librustc_msan/build.rs @@ -18,7 +18,7 @@ use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = match sanitizer_lib_boilerplate("msan") { + let (native, target) = match sanitizer_lib_boilerplate("msan") { Ok(native) => native, _ => return, }; @@ -29,7 +29,7 @@ fn main() { .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) .out_dir(&native.out_dir) - .build_target("msan") + .build_target(&target) .build(); } println!("cargo:rerun-if-env-changed=LLVM_CONFIG"); diff --git a/src/librustc_tsan/build.rs b/src/librustc_tsan/build.rs index 641d9c3647d39..38595478c7433 100644 --- a/src/librustc_tsan/build.rs +++ b/src/librustc_tsan/build.rs @@ -18,7 +18,7 @@ use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = match sanitizer_lib_boilerplate("tsan") { + let (native, target) = match sanitizer_lib_boilerplate("tsan") { Ok(native) => native, _ => return, }; @@ -29,7 +29,7 @@ fn main() { .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) .out_dir(&native.out_dir) - .build_target("tsan") + .build_target(&target) .build(); } println!("cargo:rerun-if-env-changed=LLVM_CONFIG");