Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: Shared object #432

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn find_make() -> OsString {
}

fn cc_flags() -> Vec<&'static str> {
let mut result = vec!["-DRUST_BINDGEN", "-DSTATIC_JS_API"];
let mut result = vec!["-DRUST_BINDGEN", "-DJS_SHARED_LIBRARY"];

if env::var_os("CARGO_FEATURE_DEBUGMOZJS").is_some() {
result.extend(&["-DJS_GC_ZEAL", "-DDEBUG", "-DJS_DEBUG"]);
Expand Down Expand Up @@ -249,7 +249,7 @@ fn build_jsapi(build_dir: &Path) {
"cargo:rustc-link-search=native={}/js/src/build",
build_dir.display()
);
println!("cargo:rustc-link-lib=static=js_static"); // Must come before c++
println!("cargo:rustc-link-lib=dylib=mozjs-115"); // Must come before c++
if target.contains("windows") {
println!(
"cargo:rustc-link-search=native={}/dist/bin",
Expand All @@ -271,27 +271,46 @@ fn build_jsapi(build_dir: &Path) {

fn build_jsglue(build_dir: &Path) {
let mut build = cc::Build::new();
build.cpp(true);
// "-undefined" and "dynamic_lookup" to avoid compiler's complain about linking
build.cpp(true)
.shared_flag(true)
.flag("-undefined")
.flag("dynamic_lookup")
.flag_if_supported("-std=g++17"); // this seems mac specific, we already have gnu++ in cc_flags(), yet it only seems to be working with g++

for flag in cc_flags() {
build.flag_if_supported(flag);
}

let config = format!("{}/js/src/js-confdefs.h", build_dir.display());
if build.get_compiler().is_like_msvc() {
build.flag_if_supported("-std:c++17");
build.flag("-FI");
build
.flag_if_supported("-std:c++17")
.flag("-FI");
} else {
build.flag("-std=c++17");
build.flag("-include");
build
.flag("-std=c++17")
.flag("-include");
}
build
.flag(&config)
.file("src/jsglue.cpp")
.include(build_dir.join("dist/include"))
.include(build_dir.join("js/src"))
.out_dir(build_dir.join("glue"))
.compile("jsglue");

build.flag(&config)
.include(build_dir.join("dist/include"))
.include(build_dir.join("js/src"));

//We had static libs in /glue, make sure we have shared object at the same path
let glue_dir = build_dir.join("glue");
std::fs::create_dir_all(&glue_dir).expect("Failed to create glue directory");
let file = glue_dir.join(format!("{}.dylib", "libjsglue"));
let mut cmd = build.get_compiler().to_command();
cmd.arg("src/jsglue.cpp")
.arg("-o")
.arg(&file);

println!("cargo:rustc-link-lib=dylib=jsglue");
println!("cargo:rustc-link-search=native={}", glue_dir.display());

let status = cmd.status().expect("Failed to link the dynamic library");
assert!(status.success(), "Linking failed");
}

/// Invoke bindgen on the JSAPI headers to produce raw FFI bindings for use from
Expand Down
2 changes: 0 additions & 2 deletions mozjs-sys/makefile.cargo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ CONFIGURE_FLAGS := \
--disable-jemalloc \
--disable-js-shell \
--disable-tests \
--disable-export-js \
--disable-shared-js \
--build-backends=RecursiveMake

ifneq (,$(CARGO_FEATURE_STREAMS))
Expand Down
2 changes: 1 addition & 1 deletion mozjs-sys/mozjs/config/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ endif
ifeq ($(OS_ARCH),Darwin)
ifneq (,$(SHARED_LIBRARY))
_LOADER_PATH := @rpath
EXTRA_DSO_LDOPTS += -dynamiclib -install_name $(_LOADER_PATH)/$@ -compatibility_version 1 -current_version 1
EXTRA_DSO_LDOPTS += -undefined dynamic_lookup -dynamiclib -install_name $(_LOADER_PATH)/$@ -compatibility_version 1 -current_version 1
endif
endif

Expand Down
3 changes: 2 additions & 1 deletion mozjs-sys/mozjs/js/src/build/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ else:
Library("js")
USE_LIBS += ["mozglue"]

# We should remove this, we don't want both static and dynamic lib
FORCE_STATIC_LIB = True
STATIC_LIBRARY_NAME = "js_static"

Expand Down Expand Up @@ -105,4 +106,4 @@ GeneratedFile(
"/config/check_macroassembler_style.py",
"/config/check_js_opcode.py",
],
)
)
24 changes: 20 additions & 4 deletions mozjs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ fn cc_flags(bindgen: bool) -> Vec<&'static str> {
fn main() {
//let mut build = cxx_build::bridge("src/jsglue.rs"); // returns a cc::Build;
let mut build = cc::Build::new();
let outdir = env::var("DEP_MOZJS_OUTDIR").unwrap();
let include_path: PathBuf = [&outdir, "dist", "include"].iter().collect();
let out_dir = env::var("DEP_MOZJS_OUTDIR").unwrap();
let include_path: PathBuf = [&out_dir, "dist", "include"].iter().collect();

build
.cpp(true)
.file("src/jsglue.cpp")
.shared_flag(true)
.flag("-undefined")
.flag("dynamic_lookup")
.include(&include_path);

for flag in cc_flags(false) {
build.flag_if_supported(flag);
}

let confdefs_path: PathBuf = [&outdir, "js", "src", "js-confdefs.h"].iter().collect();
let confdefs_path: PathBuf = [&out_dir, "js", "src", "js-confdefs.h"].iter().collect();
let msvc = if build.get_compiler().is_like_msvc() {
build.flag(&format!("-FI{}", confdefs_path.to_string_lossy()));
build.define("WIN32", "");
Expand All @@ -65,7 +69,19 @@ fn main() {
false
};

build.compile("jsglue");
let out_dir_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let file = out_dir_path.join(format!("{}.dylib", "libjsglue"));
let mut cmd = build.get_compiler().to_command();
cmd.arg("src/jsglue.cpp")
.arg("-o")
.arg(&file);

println!("cargo:rustc-link-lib=dylib=jsglue");
println!("cargo:rustc-link-search=native={}", out_dir_path.display());

let status = cmd.status().expect("Failed to link the dynamic library");
assert!(status.success(), "Linking failed");

println!("cargo:rerun-if-changed=src/jsglue.cpp");
let mut builder = bindgen::Builder::default()
.header("./src/jsglue.cpp")
Expand Down
Loading