Skip to content

Commit 384f64a

Browse files
committed
Automate timestamp creation and build skipping for native libraries
Add comments
1 parent 60fb577 commit 384f64a

File tree

8 files changed

+54
-78
lines changed

8 files changed

+54
-78
lines changed

src/build_helper/lib.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
extern crate filetime;
1414

1515
use std::{fs, env};
16+
use std::fs::File;
1617
use std::process::{Command, Stdio};
1718
use std::path::{Path, PathBuf};
1819

@@ -148,19 +149,29 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool {
148149
}
149150
}
150151

152+
#[must_use]
151153
pub struct NativeLibBoilerplate {
152-
pub skip_build: bool,
153154
pub src_dir: PathBuf,
154155
pub out_dir: PathBuf,
155-
pub timestamp: PathBuf,
156156
}
157157

158+
impl Drop for NativeLibBoilerplate {
159+
fn drop(&mut self) {
160+
t!(File::create(self.out_dir.join("rustbuild.timestamp")));
161+
}
162+
}
163+
164+
// Perform standard preparations for native libraries that are build only once for all stages.
165+
// Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
166+
// updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
167+
// If Err is returned, then everything is up-to-date and further build actions can be skipped.
168+
// Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
169+
// of scope, so all the build actions should be completed until then.
158170
pub fn native_lib_boilerplate(src_name: &str,
159171
out_name: &str,
160172
link_name: &str,
161-
timestamp_name: &str,
162173
search_subdir: &str)
163-
-> NativeLibBoilerplate {
174+
-> Result<NativeLibBoilerplate, ()> {
164175
let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
165176
let src_dir = current_dir.join("..").join(src_name);
166177
rerun_if_changed_anything_in_dir(&src_dir);
@@ -171,15 +182,11 @@ pub fn native_lib_boilerplate(src_name: &str,
171182
println!("cargo:rustc-link-lib=static={}", link_name);
172183
println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display());
173184

174-
let timestamp = out_dir.join(timestamp_name);
175-
let skip_build = up_to_date(Path::new("build.rs"), &timestamp) &&
176-
up_to_date(&src_dir, &timestamp);
177-
178-
NativeLibBoilerplate {
179-
skip_build: skip_build,
180-
src_dir: src_dir,
181-
out_dir: out_dir,
182-
timestamp: timestamp,
185+
let timestamp = out_dir.join("rustbuild.timestamp");
186+
if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(&src_dir, &timestamp) {
187+
Ok(NativeLibBoilerplate { src_dir: src_dir, out_dir: out_dir })
188+
} else {
189+
Err(())
183190
}
184191
}
185192

src/liballoc_jemalloc/build.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
#![deny(warnings)]
1212

13-
#[macro_use]
1413
extern crate build_helper;
1514
extern crate gcc;
1615

1716
use std::env;
18-
use std::fs::File;
1917
use std::path::PathBuf;
2018
use std::process::Command;
2119
use build_helper::{run, native_lib_boilerplate};
@@ -60,11 +58,10 @@ fn main() {
6058
}
6159

6260
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 {
66-
return
67-
}
61+
let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") {
62+
Ok(native) => native,
63+
_ => return,
64+
};
6865

6966
let compiler = gcc::Config::new().get_compiler();
7067
// only msvc returns None for ar so unwrap is okay
@@ -175,6 +172,4 @@ fn main() {
175172
.file("pthread_atfork_dummy.c")
176173
.compile("libpthread_atfork_dummy.a");
177174
}
178-
179-
t!(File::create(&native.timestamp));
180175
}

src/libcompiler_builtins/build.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,13 @@ fn main() {
8282
}
8383

8484
// 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-
}
85+
let native = match native_lib_boilerplate("compiler-rt", "compiler-rt", "compiler-rt", ".") {
86+
Ok(native) => native,
87+
_ => return,
88+
};
9289

9390
let cfg = &mut gcc::Config::new();
94-
cfg.out_dir(native.out_dir);
91+
cfg.out_dir(&native.out_dir);
9592

9693
if target.contains("msvc") {
9794
// Don't pull in extra libraries on MSVC
@@ -416,5 +413,5 @@ fn main() {
416413
cfg.file(Path::new("../compiler-rt/lib/builtins").join(src));
417414
}
418415

419-
cfg.compile(out_name);
416+
cfg.compile("libcompiler-rt.a");
420417
}

src/librustc_asan/build.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[macro_use]
1211
extern crate build_helper;
1312
extern crate cmake;
1413

1514
use std::env;
16-
use std::fs::File;
1715
use build_helper::native_lib_boilerplate;
1816

1917
use cmake::Config;
2018

2119
fn main() {
2220
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
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-
}
21+
let native = match native_lib_boilerplate("compiler-rt", "asan", "clang_rt.asan-x86_64",
22+
"build/lib/linux") {
23+
Ok(native) => native,
24+
_ => return,
25+
};
2826

2927
Config::new(&native.src_dir)
3028
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
@@ -34,7 +32,5 @@ fn main() {
3432
.out_dir(&native.out_dir)
3533
.build_target("asan")
3634
.build();
37-
38-
t!(File::create(&native.timestamp));
3935
}
4036
}

src/librustc_lsan/build.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[macro_use]
1211
extern crate build_helper;
1312
extern crate cmake;
1413

1514
use std::env;
16-
use std::fs::File;
1715
use build_helper::native_lib_boilerplate;
1816

1917
use cmake::Config;
2018

2119
fn main() {
2220
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
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-
}
21+
let native = match native_lib_boilerplate("compiler-rt", "lsan", "clang_rt.lsan-x86_64",
22+
"build/lib/linux") {
23+
Ok(native) => native,
24+
_ => return,
25+
};
2826

2927
Config::new(&native.src_dir)
3028
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
@@ -34,7 +32,5 @@ fn main() {
3432
.out_dir(&native.out_dir)
3533
.build_target("lsan")
3634
.build();
37-
38-
t!(File::create(&native.timestamp));
3935
}
4036
}

src/librustc_msan/build.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[macro_use]
1211
extern crate build_helper;
1312
extern crate cmake;
1413

1514
use std::env;
16-
use std::fs::File;
1715
use build_helper::native_lib_boilerplate;
1816

1917
use cmake::Config;
2018

2119
fn main() {
2220
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
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-
}
21+
let native = match native_lib_boilerplate("compiler-rt", "msan", "clang_rt.msan-x86_64",
22+
"build/lib/linux") {
23+
Ok(native) => native,
24+
_ => return,
25+
};
2826

2927
Config::new(&native.src_dir)
3028
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
@@ -34,7 +32,5 @@ fn main() {
3432
.out_dir(&native.out_dir)
3533
.build_target("msan")
3634
.build();
37-
38-
t!(File::create(&native.timestamp));
3935
}
4036
}

src/librustc_tsan/build.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[macro_use]
1211
extern crate build_helper;
1312
extern crate cmake;
1413

1514
use std::env;
16-
use std::fs::File;
1715
use build_helper::native_lib_boilerplate;
1816

1917
use cmake::Config;
2018

2119
fn main() {
2220
if let Some(llvm_config) = env::var_os("LLVM_CONFIG") {
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-
}
21+
let native = match native_lib_boilerplate("compiler-rt", "tsan", "clang_rt.tsan-x86_64",
22+
"build/lib/linux") {
23+
Ok(native) => native,
24+
_ => return,
25+
};
2826

2927
Config::new(&native.src_dir)
3028
.define("COMPILER_RT_BUILD_SANITIZERS", "ON")
@@ -34,7 +32,5 @@ fn main() {
3432
.out_dir(&native.out_dir)
3533
.build_target("tsan")
3634
.build();
37-
38-
t!(File::create(&native.timestamp));
3935
}
4036
}

src/libstd/build.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
#![deny(warnings)]
1212

13-
#[macro_use]
1413
extern crate build_helper;
1514
extern crate gcc;
1615

1716
use std::env;
18-
use std::fs::File;
1917
use std::process::Command;
2018
use build_helper::{run, native_lib_boilerplate};
2119

@@ -24,7 +22,7 @@ fn main() {
2422
let host = env::var("HOST").expect("HOST was not set");
2523
if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
2624
!target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") {
27-
build_libbacktrace(&host, &target);
25+
let _ = build_libbacktrace(&host, &target);
2826
}
2927

3028
if target.contains("linux") {
@@ -66,12 +64,8 @@ fn main() {
6664
}
6765
}
6866

69-
fn build_libbacktrace(host: &str, target: &str) {
70-
let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace",
71-
"rustbuild.timestamp", ".libs");
72-
if native.skip_build {
73-
return
74-
}
67+
fn build_libbacktrace(host: &str, target: &str) -> Result<(), ()> {
68+
let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace", ".libs")?;
7569

7670
let compiler = gcc::Config::new().get_compiler();
7771
// only msvc returns None for ar so unwrap is okay
@@ -99,6 +93,5 @@ fn build_libbacktrace(host: &str, target: &str) {
9993
.current_dir(&native.out_dir)
10094
.arg(format!("INCDIR={}", native.src_dir.display()))
10195
.arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
102-
103-
t!(File::create(&native.timestamp));
96+
Ok(())
10497
}

0 commit comments

Comments
 (0)