Skip to content

Commit 6c2ef52

Browse files
committed
rustbuild: Build jemalloc and libbacktrace only once (take 2)
1 parent 1a2428f commit 6c2ef52

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed

src/bootstrap/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ impl Build {
482482
//
483483
// These variables are primarily all read by
484484
// src/bootstrap/bin/{rustc.rs,rustdoc.rs}
485-
cargo.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
485+
cargo.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
486+
.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
486487
.env("RUSTC_REAL", self.compiler_path(compiler))
487488
.env("RUSTC_STAGE", stage.to_string())
488489
.env("RUSTC_DEBUGINFO", self.config.rust_debuginfo.to_string())
@@ -746,10 +747,15 @@ impl Build {
746747
}
747748
}
748749

750+
/// Directory for libraries built from C/C++ code and shared between stages.
751+
fn native_dir(&self, target: &str) -> PathBuf {
752+
self.out.join(target).join("native")
753+
}
754+
749755
/// Root output directory for rust_test_helpers library compiled for
750756
/// `target`
751757
fn test_helpers_out(&self, target: &str) -> PathBuf {
752-
self.out.join(target).join("rust-test-helpers")
758+
self.native_dir(target).join("rust-test-helpers")
753759
}
754760

755761
/// Adds the compiler's directory of dynamic libraries to `cmd`'s dynamic

src/build_helper/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ pub fn output(cmd: &mut Command) -> String {
8888
String::from_utf8(output.stdout).unwrap()
8989
}
9090

91+
pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
92+
let mut stack = dir.read_dir().unwrap()
93+
.map(|e| e.unwrap())
94+
.filter(|e| &*e.file_name() != ".git")
95+
.collect::<Vec<_>>();
96+
while let Some(entry) = stack.pop() {
97+
let path = entry.path();
98+
if entry.file_type().unwrap().is_dir() {
99+
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
100+
} else {
101+
println!("cargo:rerun-if-changed={}", path.display());
102+
}
103+
}
104+
}
105+
91106
fn fail(s: &str) -> ! {
92107
println!("\n\n{}\n\n", s);
93108
std::process::exit(1);

src/liballoc_jemalloc/build.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@
1313
extern crate build_helper;
1414
extern crate gcc;
1515

16-
use std::env;
16+
use std::{env, fs};
1717
use std::path::PathBuf;
1818
use std::process::Command;
19-
use build_helper::run;
19+
use build_helper::{run, rerun_if_changed_anything_in_dir};
2020

2121
fn main() {
2222
println!("cargo:rustc-cfg=cargobuild");
2323
println!("cargo:rerun-if-changed=build.rs");
2424

25-
let target = env::var("TARGET").expect("TARGET was not set");
26-
let host = env::var("HOST").expect("HOST was not set");
27-
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
28-
let src_dir = env::current_dir().unwrap();
29-
3025
// FIXME: This is a hack to support building targets that don't
3126
// support jemalloc alongside hosts that do. The jemalloc build is
3227
// controlled by a feature of the std crate, and if that feature
@@ -35,6 +30,7 @@ fn main() {
3530
// that the feature set used by std is the same across all
3631
// targets, which means we have to build the alloc_jemalloc crate
3732
// for targets like emscripten, even if we don't use it.
33+
let target = env::var("TARGET").expect("TARGET was not set");
3834
if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
3935
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
4036
target.contains("redox") {
@@ -57,6 +53,28 @@ fn main() {
5753
return;
5854
}
5955

56+
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
57+
let build_dir = PathBuf::from(build_dir).join("jemalloc");
58+
let _ = fs::create_dir_all(&build_dir);
59+
60+
if target.contains("windows") {
61+
println!("cargo:rustc-link-lib=static=jemalloc");
62+
} else {
63+
println!("cargo:rustc-link-lib=static=jemalloc_pic");
64+
}
65+
println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
66+
if target.contains("android") {
67+
println!("cargo:rustc-link-lib=gcc");
68+
} else if !target.contains("windows") && !target.contains("musl") {
69+
println!("cargo:rustc-link-lib=pthread");
70+
}
71+
if !cfg!(stage0) {
72+
return
73+
}
74+
75+
let host = env::var("HOST").expect("HOST was not set");
76+
let src_dir = env::current_dir().unwrap().join("../jemalloc");
77+
rerun_if_changed_anything_in_dir(&src_dir);
6078
let compiler = gcc::Config::new().get_compiler();
6179
// only msvc returns None for ar so unwrap is okay
6280
let ar = build_helper::cc2ar(compiler.path(), &target).unwrap();
@@ -66,23 +84,8 @@ fn main() {
6684
.collect::<Vec<_>>()
6785
.join(" ");
6886

69-
let mut stack = src_dir.join("../jemalloc")
70-
.read_dir()
71-
.unwrap()
72-
.map(|e| e.unwrap())
73-
.filter(|e| &*e.file_name() != ".git")
74-
.collect::<Vec<_>>();
75-
while let Some(entry) = stack.pop() {
76-
let path = entry.path();
77-
if entry.file_type().unwrap().is_dir() {
78-
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
79-
} else {
80-
println!("cargo:rerun-if-changed={}", path.display());
81-
}
82-
}
83-
8487
let mut cmd = Command::new("sh");
85-
cmd.arg(src_dir.join("../jemalloc/configure")
88+
cmd.arg(src_dir.join("configure")
8689
.to_str()
8790
.unwrap()
8891
.replace("C:\\", "/c/")
@@ -158,6 +161,7 @@ fn main() {
158161
}
159162

160163
run(&mut cmd);
164+
161165
let mut make = Command::new(build_helper::make(&host));
162166
make.current_dir(&build_dir)
163167
.arg("build_lib_static");
@@ -170,18 +174,6 @@ fn main() {
170174

171175
run(&mut make);
172176

173-
if target.contains("windows") {
174-
println!("cargo:rustc-link-lib=static=jemalloc");
175-
} else {
176-
println!("cargo:rustc-link-lib=static=jemalloc_pic");
177-
}
178-
println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
179-
if target.contains("android") {
180-
println!("cargo:rustc-link-lib=gcc");
181-
} else if !target.contains("windows") && !target.contains("musl") {
182-
println!("cargo:rustc-link-lib=pthread");
183-
}
184-
185177
// The pthread_atfork symbols is used by jemalloc on android but the really
186178
// old android we're building on doesn't have them defined, so just make
187179
// sure the symbols are available.

src/libstd/build.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
extern crate gcc;
1414
extern crate build_helper;
1515

16-
use std::env;
16+
use std::{env, fs};
1717
use std::path::PathBuf;
1818
use std::process::Command;
19-
20-
use build_helper::run;
19+
use build_helper::{run, rerun_if_changed_anything_in_dir};
2120

2221
fn main() {
2322
println!("cargo:rustc-cfg=cargobuild");
@@ -66,24 +65,18 @@ fn main() {
6665
}
6766

6867
fn build_libbacktrace(host: &str, target: &str) {
69-
let src_dir = env::current_dir().unwrap().join("../libbacktrace");
70-
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
68+
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
69+
let build_dir = PathBuf::from(build_dir).join("libbacktrace");
70+
let _ = fs::create_dir_all(&build_dir);
7171

7272
println!("cargo:rustc-link-lib=static=backtrace");
7373
println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
74-
75-
let mut stack = src_dir.read_dir().unwrap()
76-
.map(|e| e.unwrap())
77-
.collect::<Vec<_>>();
78-
while let Some(entry) = stack.pop() {
79-
let path = entry.path();
80-
if entry.file_type().unwrap().is_dir() {
81-
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
82-
} else {
83-
println!("cargo:rerun-if-changed={}", path.display());
84-
}
74+
if !cfg!(stage0) {
75+
return
8576
}
8677

78+
let src_dir = env::current_dir().unwrap().join("../libbacktrace");
79+
rerun_if_changed_anything_in_dir(&src_dir);
8780
let compiler = gcc::Config::new().get_compiler();
8881
// only msvc returns None for ar so unwrap is okay
8982
let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
@@ -105,6 +98,7 @@ fn build_libbacktrace(host: &str, target: &str) {
10598
.env("AR", &ar)
10699
.env("RANLIB", format!("{} s", ar.display()))
107100
.env("CFLAGS", cflags));
101+
108102
run(Command::new(build_helper::make(host))
109103
.current_dir(&build_dir)
110104
.arg(format!("INCDIR={}", src_dir.display()))

0 commit comments

Comments
 (0)