1313extern crate build_helper;
1414extern crate gcc;
1515
16- use std:: env;
16+ use std:: { env, fs } ;
1717use std:: path:: PathBuf ;
1818use std:: process:: Command ;
19- use build_helper:: run;
19+ use build_helper:: { run, rerun_if_changed_anything_in_dir } ;
2020
2121fn 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.
0 commit comments