Skip to content

Commit aeadc81

Browse files
committed
Build compiler-rt and sanitizers only once
1 parent a7c8afd commit aeadc81

File tree

8 files changed

+123
-86
lines changed

8 files changed

+123
-86
lines changed

src/build_helper/lib.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
extern crate filetime;
1414

15-
use std::fs;
15+
use std::{fs, env};
1616
use std::process::{Command, Stdio};
1717
use std::path::{Path, PathBuf};
1818

@@ -166,6 +166,41 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool {
166166
}
167167
}
168168

169+
pub struct NativeLibBoilerplate {
170+
pub skip_build: bool,
171+
pub src_dir: PathBuf,
172+
pub out_dir: PathBuf,
173+
pub timestamp: PathBuf,
174+
}
175+
176+
pub fn native_lib_boilerplate(src_name: &str,
177+
out_name: &str,
178+
link_name: &str,
179+
timestamp_name: &str,
180+
search_subdir: &str)
181+
-> NativeLibBoilerplate {
182+
let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
183+
let src_dir = current_dir.join("..").join(src_name);
184+
rerun_if_changed_anything_in_dir(&src_dir);
185+
186+
let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
187+
let out_dir = PathBuf::from(out_dir).join(out_name);
188+
let _ = fs::create_dir_all(&out_dir);
189+
println!("cargo:rustc-link-lib=static={}", link_name);
190+
println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
191+
192+
let timestamp = out_dir.join(timestamp_name);
193+
let skip_build = up_to_date(Path::new("build.rs"), &timestamp) &&
194+
up_to_date(&src_dir, &timestamp);
195+
196+
NativeLibBoilerplate {
197+
skip_build: skip_build,
198+
src_dir: src_dir,
199+
out_dir: out_dir,
200+
timestamp: timestamp,
201+
}
202+
}
203+
169204
fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
170205
t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
171206
let meta = t!(e.metadata());

src/liballoc_jemalloc/build.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ extern crate build_helper;
1515
extern crate gcc;
1616

1717
use std::env;
18-
use std::fs::{self, File};
19-
use std::path::{Path, PathBuf};
18+
use std::fs::File;
19+
use std::path::PathBuf;
2020
use std::process::Command;
21-
use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date};
21+
use build_helper::{run, native_lib_boilerplate};
2222

2323
fn main() {
2424
// FIXME: This is a hack to support building targets that don't
@@ -59,20 +59,10 @@ fn main() {
5959
return;
6060
}
6161

62-
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
63-
let build_dir = PathBuf::from(build_dir).join("jemalloc");
64-
let _ = fs::create_dir_all(&build_dir);
65-
66-
if target.contains("windows") {
67-
println!("cargo:rustc-link-lib=static=jemalloc");
68-
} else {
69-
println!("cargo:rustc-link-lib=static=jemalloc_pic");
70-
}
71-
println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
72-
let src_dir = env::current_dir().unwrap().join("../jemalloc");
73-
rerun_if_changed_anything_in_dir(&src_dir);
74-
let timestamp = build_dir.join("rustbuild.timestamp");
75-
if up_to_date(&Path::new("build.rs"), &timestamp) && up_to_date(&src_dir, &timestamp) {
62+
let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
63+
let native = native_lib_boilerplate("jemalloc", "jemalloc", link_name,
64+
"rustbuild.timestamp", "lib");
65+
if native.skip_build {
7666
return
7767
}
7868

@@ -86,12 +76,12 @@ fn main() {
8676
.join(" ");
8777

8878
let mut cmd = Command::new("sh");
89-
cmd.arg(src_dir.join("configure")
90-
.to_str()
91-
.unwrap()
92-
.replace("C:\\", "/c/")
93-
.replace("\\", "/"))
94-
.current_dir(&build_dir)
79+
cmd.arg(native.src_dir.join("configure")
80+
.to_str()
81+
.unwrap()
82+
.replace("C:\\", "/c/")
83+
.replace("\\", "/"))
84+
.current_dir(&native.out_dir)
9585
.env("CC", compiler.path())
9686
.env("EXTRA_CFLAGS", cflags.clone())
9787
// jemalloc generates Makefile deps using GCC's "-MM" flag. This means
@@ -164,7 +154,7 @@ fn main() {
164154
run(&mut cmd);
165155

166156
let mut make = Command::new(build_helper::make(&host));
167-
make.current_dir(&build_dir)
157+
make.current_dir(&native.out_dir)
168158
.arg("build_lib_static");
169159

170160
// mingw make seems... buggy? unclear...
@@ -186,5 +176,5 @@ fn main() {
186176
.compile("libpthread_atfork_dummy.a");
187177
}
188178

189-
t!(File::create(&timestamp));
179+
t!(File::create(&native.timestamp));
190180
}

src/libcompiler_builtins/build.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate gcc;
3939
use std::collections::BTreeMap;
4040
use std::env;
4141
use std::path::Path;
42+
use build_helper::native_lib_boilerplate;
4243

4344
struct Sources {
4445
// SYMBOL -> PATH TO SOURCE
@@ -80,7 +81,17 @@ fn main() {
8081
return;
8182
}
8283

84+
// Can't reuse `sources` list for the freshness check becuse it doesn't contain header files.
85+
// Use the produced library itself as a timestamp.
86+
let out_name = "libcompiler-rt.a";
87+
let native = native_lib_boilerplate("compiler-rt", "compiler-rt", "compiler-rt",
88+
out_name, ".");
89+
if native.skip_build {
90+
return
91+
}
92+
8393
let cfg = &mut gcc::Config::new();
94+
cfg.out_dir(native.out_dir);
8495

8596
if target.contains("msvc") {
8697
// Don't pull in extra libraries on MSVC
@@ -405,8 +416,5 @@ fn main() {
405416
cfg.file(Path::new("../compiler-rt/lib/builtins").join(src));
406417
}
407418

408-
// Can't reuse `sources` list becuse it doesn't contain header files.
409-
build_helper::rerun_if_changed_anything_in_dir(Path::new("../compiler-rt"));
410-
411-
cfg.compile("libcompiler-rt.a");
419+
cfg.compile(out_name);
412420
}

src/librustc_asan/build.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[macro_use]
1112
extern crate build_helper;
1213
extern crate cmake;
1314

14-
use std::path::PathBuf;
1515
use std::env;
16+
use std::fs::File;
17+
use build_helper::native_lib_boilerplate;
1618

1719
use cmake::Config;
1820

1921
fn main() {
2022
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
21-
let dst = Config::new("../compiler-rt")
23+
let native = native_lib_boilerplate("compiler-rt", "asan", "clang_rt.asan-x86_64",
24+
"rustbuild.timestamp", "build/lib/linux");
25+
if native.skip_build {
26+
return
27+
}
28+
29+
Config::new(&native.src_dir)
2230
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
2331
.define("COMPILER_RT_BUILD_BUILTINS", "OFF")
2432
.define("COMPILER_RT_BUILD_XRAY", "OFF")
2533
.define("LLVM_CONFIG_PATH", llvm_config)
34+
.out_dir(&native.out_dir)
2635
.build_target("asan")
2736
.build();
2837

29-
println!("cargo:rustc-link-search=native={}",
30-
dst.join("build/lib/linux").display());
31-
println!("cargo:rustc-link-lib=static=clang_rt.asan-x86_64");
32-
33-
build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR")
34-
.unwrap())
35-
.join("../compiler-rt"));
38+
t!(File::create(&native.timestamp));
3639
}
3740
}

src/librustc_lsan/build.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[macro_use]
1112
extern crate build_helper;
1213
extern crate cmake;
1314

14-
use std::path::PathBuf;
1515
use std::env;
16+
use std::fs::File;
17+
use build_helper::native_lib_boilerplate;
1618

1719
use cmake::Config;
1820

1921
fn main() {
2022
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
21-
let dst = Config::new("../compiler-rt")
23+
let native = native_lib_boilerplate("compiler-rt", "lsan", "clang_rt.lsan-x86_64",
24+
"rustbuild.timestamp", "build/lib/linux");
25+
if native.skip_build {
26+
return
27+
}
28+
29+
Config::new(&native.src_dir)
2230
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
2331
.define("COMPILER_RT_BUILD_BUILTINS", "OFF")
2432
.define("COMPILER_RT_BUILD_XRAY", "OFF")
2533
.define("LLVM_CONFIG_PATH", llvm_config)
34+
.out_dir(&native.out_dir)
2635
.build_target("lsan")
2736
.build();
2837

29-
println!("cargo:rustc-link-search=native={}",
30-
dst.join("build/lib/linux").display());
31-
println!("cargo:rustc-link-lib=static=clang_rt.lsan-x86_64");
32-
33-
build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR")
34-
.unwrap())
35-
.join("../compiler-rt"));
38+
t!(File::create(&native.timestamp));
3639
}
3740
}

src/librustc_msan/build.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[macro_use]
1112
extern crate build_helper;
1213
extern crate cmake;
1314

14-
use std::path::PathBuf;
1515
use std::env;
16+
use std::fs::File;
17+
use build_helper::native_lib_boilerplate;
1618

1719
use cmake::Config;
1820

1921
fn main() {
2022
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
21-
let dst = Config::new("../compiler-rt")
23+
let native = native_lib_boilerplate("compiler-rt", "msan", "clang_rt.msan-x86_64",
24+
"rustbuild.timestamp", "build/lib/linux");
25+
if native.skip_build {
26+
return
27+
}
28+
29+
Config::new(&native.src_dir)
2230
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
2331
.define("COMPILER_RT_BUILD_BUILTINS", "OFF")
2432
.define("COMPILER_RT_BUILD_XRAY", "OFF")
2533
.define("LLVM_CONFIG_PATH", llvm_config)
34+
.out_dir(&native.out_dir)
2635
.build_target("msan")
2736
.build();
2837

29-
println!("cargo:rustc-link-search=native={}",
30-
dst.join("build/lib/linux").display());
31-
println!("cargo:rustc-link-lib=static=clang_rt.msan-x86_64");
32-
33-
build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR")
34-
.unwrap())
35-
.join("../compiler-rt"));
38+
t!(File::create(&native.timestamp));
3639
}
3740
}

src/librustc_tsan/build.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[macro_use]
1112
extern crate build_helper;
1213
extern crate cmake;
1314

14-
use std::path::PathBuf;
1515
use std::env;
16+
use std::fs::File;
17+
use build_helper::native_lib_boilerplate;
1618

1719
use cmake::Config;
1820

1921
fn main() {
2022
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
21-
let dst = Config::new("../compiler-rt")
23+
let native = native_lib_boilerplate("compiler-rt", "tsan", "clang_rt.tsan-x86_64",
24+
"rustbuild.timestamp", "build/lib/linux");
25+
if native.skip_build {
26+
return
27+
}
28+
29+
Config::new(&native.src_dir)
2230
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
2331
.define("COMPILER_RT_BUILD_BUILTINS", "OFF")
2432
.define("COMPILER_RT_BUILD_XRAY", "OFF")
2533
.define("LLVM_CONFIG_PATH", llvm_config)
34+
.out_dir(&native.out_dir)
2635
.build_target("tsan")
2736
.build();
2837

29-
println!("cargo:rustc-link-search=native={}",
30-
dst.join("build/lib/linux").display());
31-
println!("cargo:rustc-link-lib=static=clang_rt.tsan-x86_64");
32-
33-
build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR")
34-
.unwrap())
35-
.join("../compiler-rt"));
38+
t!(File::create(&native.timestamp));
3639
}
3740
}

src/libstd/build.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ extern crate build_helper;
1515
extern crate gcc;
1616

1717
use std::env;
18-
use std::fs::{self, File};
19-
use std::path::{Path, PathBuf};
18+
use std::fs::File;
2019
use std::process::Command;
21-
use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date};
20+
use build_helper::{run, native_lib_boilerplate};
2221

2322
fn main() {
2423
let target = env::var("TARGET").expect("TARGET was not set");
@@ -68,16 +67,9 @@ fn main() {
6867
}
6968

7069
fn build_libbacktrace(host: &str, target: &str) {
71-
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
72-
let build_dir = PathBuf::from(build_dir).join("libbacktrace");
73-
let _ = fs::create_dir_all(&build_dir);
74-
75-
println!("cargo:rustc-link-lib=static=backtrace");
76-
println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
77-
let src_dir = env::current_dir().unwrap().join("../libbacktrace");
78-
rerun_if_changed_anything_in_dir(&src_dir);
79-
let timestamp = build_dir.join("rustbuild.timestamp");
80-
if up_to_date(&Path::new("build.rs"), &timestamp) && up_to_date(&src_dir, &timestamp) {
70+
let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace",
71+
"rustbuild.timestamp", ".libs");
72+
if native.skip_build {
8173
return
8274
}
8375

@@ -88,10 +80,10 @@ fn build_libbacktrace(host: &str, target: &str) {
8880
.collect::<Vec<_>>().join(" ");
8981
cflags.push_str(" -fvisibility=hidden");
9082
run(Command::new("sh")
91-
.current_dir(&build_dir)
92-
.arg(src_dir.join("configure").to_str().unwrap()
93-
.replace("C:\\", "/c/")
94-
.replace("\\", "/"))
83+
.current_dir(&native.out_dir)
84+
.arg(native.src_dir.join("configure").to_str().unwrap()
85+
.replace("C:\\", "/c/")
86+
.replace("\\", "/"))
9587
.arg("--with-pic")
9688
.arg("--disable-multilib")
9789
.arg("--disable-shared")
@@ -104,9 +96,9 @@ fn build_libbacktrace(host: &str, target: &str) {
10496
.env("CFLAGS", cflags));
10597

10698
run(Command::new(build_helper::make(host))
107-
.current_dir(&build_dir)
108-
.arg(format!("INCDIR={}", src_dir.display()))
99+
.current_dir(&native.out_dir)
100+
.arg(format!("INCDIR={}", native.src_dir.display()))
109101
.arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
110102

111-
t!(File::create(&timestamp));
103+
t!(File::create(&native.timestamp));
112104
}

0 commit comments

Comments
 (0)