From 43893f726d481cc9184d5a23311d21b4fcdd4909 Mon Sep 17 00:00:00 2001 From: Arthur Silva Date: Wed, 16 Mar 2022 16:20:09 +0100 Subject: [PATCH] Add crate feature that controls whether custom libcxx is used (#924) The `use_custom_libcxx` feature is enabled by default. When this feature is disabled, the crate will link with the system libcxx instead. --- Cargo.toml | 4 ++++ build.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 4b5316be..ab4f047e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,6 +75,10 @@ exclude = [ "!v8/tools/testrunner/utils/dump_build_config.py", ] +[features] +default = ["use_custom_libcxx"] +use_custom_libcxx = [] + [dependencies] lazy_static = "1.4.0" libc = "0.2.93" diff --git a/build.rs b/build.rs index 19bcf30e..44019dd6 100644 --- a/build.rs +++ b/build.rs @@ -109,6 +109,10 @@ fn build_v8() { vec!["is_debug=false".to_string()] }; + if cfg!(not(feature = "use_custom_libcxx")) { + gn_args.push("use_custom_libcxx=false".to_string()); + } + if !is_debug() { gn_args.push("v8_enable_handle_zapping=false".to_string()); } @@ -398,6 +402,28 @@ fn download_static_lib_binaries() { fn print_link_flags() { println!("cargo:rustc-link-lib=static=rusty_v8"); + let should_dyn_link_libcxx = cfg!(not(feature = "use_custom_libcxx")) + || env::var("GN_ARGS").map_or(false, |gn_args| { + gn_args + .split_whitespace() + .any(|ba| ba == "use_custom_libcxx=false") + }); + + if should_dyn_link_libcxx { + // Based on https://github.com/alexcrichton/cc-rs/blob/fba7feded71ee4f63cfe885673ead6d7b4f2f454/src/lib.rs#L2462 + let target = env::var("TARGET").unwrap(); + if target.contains("apple") + || target.contains("freebsd") + || target.contains("openbsd") + { + println!("cargo:rustc-link-lib=dylib=c++"); + } else if target.contains("linux") { + println!("cargo:rustc-link-lib=dylib=stdc++"); + } else if target.contains("android") { + println!("cargo:rustc-link-lib=dylib=c++_shared"); + } + } + if cfg!(target_os = "windows") { println!("cargo:rustc-link-lib=dylib=winmm"); println!("cargo:rustc-link-lib=dylib=dbghelp");