@@ -138,6 +138,7 @@ fn main() {
138138
139139fn build_spidermonkey ( build_dir : & Path ) {
140140 let target = env:: var ( "TARGET" ) . unwrap ( ) ;
141+ let cargo_manifest_dir = PathBuf :: from ( env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
141142 let make;
142143
143144 #[ cfg( windows) ]
@@ -210,24 +211,37 @@ fn build_spidermonkey(build_dir: &Path) {
210211 }
211212
212213 cppflags. push ( get_cc_rs_env_os ( "CPPFLAGS" ) . unwrap_or_default ( ) ) ;
213- cmd. env ( "CPPFLAGS" , cppflags) ;
214214
215215 if let Some ( makeflags) = env:: var_os ( "CARGO_MAKEFLAGS" ) {
216216 cmd. env ( "MAKEFLAGS" , makeflags) ;
217217 }
218218
219219 let mut cxxflags = vec ! [ ] ;
220220
221+ if env:: var_os ( "CARGO_FEATURE_MIMALLOC" ) . is_some ( ) {
222+ let mut flags = vec ! [ ] ;
223+ // let include_dir = env::var("DEP_DFMALLOC_INCLUDE_DIR").expect("Required variable not set with feature dfmalloc");
224+ // flags.push(format!("-I{}", &include_dir.replace("\\", "/")));
225+ let header_dir = cargo_manifest_dir. join ( "src/custom_alloc" ) . to_str ( ) . expect ( "utf-8" ) . to_string ( ) ;
226+ flags. push ( format ! ( "-I{}" , header_dir) ) ;
227+ println ! ( "cargo:rerun-if-changed={}" , header_dir) ;
228+
229+ // flags.push("-DJS_USE_CUSTOM_ALLOCATOR".to_string());
230+ cppflags. extend ( flags. iter ( ) . map ( |s| OsString :: from ( s) ) ) ;
231+ cxxflags. extend ( flags) ;
232+ }
233+
221234 if target. contains ( "apple" ) || target. contains ( "freebsd" ) || target. contains ( "ohos" ) {
222235 cxxflags. push ( String :: from ( "-stdlib=libc++" ) ) ;
223236 }
224237
238+ cmd. env ( "CPPFLAGS" , cppflags) ;
239+
225240 let base_cxxflags = env:: var ( "CXXFLAGS" ) . unwrap_or_default ( ) ;
226241 let mut cxxflags = cxxflags. join ( " " ) ;
227242 cxxflags. push_str ( & base_cxxflags) ;
228243 cmd. env ( "CXXFLAGS" , cxxflags) ;
229244
230- let cargo_manifest_dir = PathBuf :: from ( env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
231245 let result = cmd
232246 . args ( & [ "-R" , "-f" ] )
233247 . arg ( cargo_manifest_dir. join ( "makefile.cargo" ) )
@@ -292,6 +306,11 @@ fn build(build_dir: &Path, target: BuildTarget) {
292306
293307 build. flag ( include_file_flag ( build. get_compiler ( ) . is_like_msvc ( ) ) ) ;
294308 build. flag ( & js_config_path ( build_dir) ) ;
309+ let cargo_manifest_dir = PathBuf :: from ( env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
310+
311+ let header_dir = cargo_manifest_dir. join ( "src/custom_alloc" ) . to_str ( ) . expect ( "utf-8" ) . to_string ( ) ;
312+ build. include ( & header_dir) ;
313+ println ! ( "cargo:rerun-if-changed={}" , header_dir) ;
295314
296315 for path in target. include_paths ( build_dir) {
297316 build. include ( path) ;
@@ -313,6 +332,10 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
313332 config &= !CodegenConfig :: DESTRUCTORS ;
314333 config &= !CodegenConfig :: METHODS ;
315334
335+ let cargo_manifest_dir = PathBuf :: from ( env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ;
336+
337+ let header_dir = cargo_manifest_dir. join ( "src/custom_alloc" ) . to_str ( ) . expect ( "utf-8" ) . to_string ( ) ;
338+
316339 let mut builder = bindgen:: builder ( )
317340 . rust_target ( minimum_rust_target ( ) )
318341 . header ( target. path ( ) )
@@ -324,6 +347,7 @@ fn build_bindings(build_dir: &Path, target: BuildTarget) {
324347 . size_t_is_usize ( true )
325348 . enable_cxx_namespaces ( )
326349 . with_codegen_config ( config)
350+ . clang_arg ( format ! ( "-I{}" , header_dir) )
327351 . clang_args ( cc_flags ( true ) ) ;
328352
329353 if env:: var ( "TARGET" ) . unwrap ( ) . contains ( "wasi" ) {
@@ -432,6 +456,8 @@ fn link_static_lib_binaries(build_dir: &Path) {
432456 // needing to use the WASI-SDK's clang for linking, which is annoying.
433457 println ! ( "cargo:rustc-link-lib=stdc++" )
434458 }
459+ // FIXME: guard me, or remove me or whatever.
460+ // println!("cargo:rustc-link-lib=mimalloc");
435461
436462 if target. contains ( "wasi" ) {
437463 println ! ( "cargo:rustc-link-lib=wasi-emulated-getpid" ) ;
@@ -463,6 +489,9 @@ fn should_build_from_source() -> bool {
463489 } else if env:: var_os ( "CARGO_FEATURE_INTL" ) . is_none ( ) {
464490 println ! ( "intl feature is disabled. Building from source directly." ) ;
465491 true
492+ } else if env:: var_os ( "CARGO_FEATURE_MIMALLOC" ) . is_some ( ) {
493+ println ! ( "mimalloc feature is enabled. Building from source directly." ) ;
494+ true
466495 } else {
467496 false
468497 }
0 commit comments