13
13
extern crate build_helper;
14
14
extern crate gcc;
15
15
16
- use std:: env;
16
+ use std:: { env, fs } ;
17
17
use std:: path:: PathBuf ;
18
18
use std:: process:: Command ;
19
- use build_helper:: run;
19
+ use build_helper:: { run, rerun_if_changed_anything_in_dir } ;
20
20
21
21
fn main ( ) {
22
22
println ! ( "cargo:rustc-cfg=cargobuild" ) ;
23
23
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
24
24
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
-
30
25
// FIXME: This is a hack to support building targets that don't
31
26
// support jemalloc alongside hosts that do. The jemalloc build is
32
27
// controlled by a feature of the std crate, and if that feature
@@ -35,6 +30,7 @@ fn main() {
35
30
// that the feature set used by std is the same across all
36
31
// targets, which means we have to build the alloc_jemalloc crate
37
32
// for targets like emscripten, even if we don't use it.
33
+ let target = env:: var ( "TARGET" ) . expect ( "TARGET was not set" ) ;
38
34
if target. contains ( "rumprun" ) || target. contains ( "bitrig" ) || target. contains ( "openbsd" ) ||
39
35
target. contains ( "msvc" ) || target. contains ( "emscripten" ) || target. contains ( "fuchsia" ) ||
40
36
target. contains ( "redox" ) {
@@ -57,6 +53,28 @@ fn main() {
57
53
return ;
58
54
}
59
55
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) ;
60
78
let compiler = gcc:: Config :: new ( ) . get_compiler ( ) ;
61
79
// only msvc returns None for ar so unwrap is okay
62
80
let ar = build_helper:: cc2ar ( compiler. path ( ) , & target) . unwrap ( ) ;
@@ -66,23 +84,8 @@ fn main() {
66
84
. collect :: < Vec < _ > > ( )
67
85
. join ( " " ) ;
68
86
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
-
84
87
let mut cmd = Command :: new ( "sh" ) ;
85
- cmd. arg ( src_dir. join ( "../jemalloc/ configure" )
88
+ cmd. arg ( src_dir. join ( "configure" )
86
89
. to_str ( )
87
90
. unwrap ( )
88
91
. replace ( "C:\\ " , "/c/" )
@@ -158,6 +161,7 @@ fn main() {
158
161
}
159
162
160
163
run ( & mut cmd) ;
164
+
161
165
let mut make = Command :: new ( build_helper:: make ( & host) ) ;
162
166
make. current_dir ( & build_dir)
163
167
. arg ( "build_lib_static" ) ;
@@ -170,18 +174,6 @@ fn main() {
170
174
171
175
run ( & mut make) ;
172
176
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
-
185
177
// The pthread_atfork symbols is used by jemalloc on android but the really
186
178
// old android we're building on doesn't have them defined, so just make
187
179
// sure the symbols are available.
0 commit comments