From e01ecebd7bd45524c9fdd5c1e685b154d61a3bfc Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Fri, 17 Nov 2023 20:18:40 +0100 Subject: [PATCH 1/5] apply patch to avoid linking issue --- mozjs-sys/makefile.cargo | 2 -- mozjs-sys/mozjs/config/rules.mk | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/mozjs-sys/makefile.cargo b/mozjs-sys/makefile.cargo index b7cac9478b9..a2dd96eebc5 100644 --- a/mozjs-sys/makefile.cargo +++ b/mozjs-sys/makefile.cargo @@ -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)) diff --git a/mozjs-sys/mozjs/config/rules.mk b/mozjs-sys/mozjs/config/rules.mk index 5cd9def5ea8..f0c646a3cd9 100644 --- a/mozjs-sys/mozjs/config/rules.mk +++ b/mozjs-sys/mozjs/config/rules.mk @@ -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 From f965ded8b229e11371b1cd6bda6f4a492fc7f500 Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Fri, 24 Nov 2023 15:08:04 +0100 Subject: [PATCH 2/5] produce shared object for glue code --- mozjs-sys/build.rs | 46 ++++++++++++++++++-------- mozjs-sys/mozjs/js/src/build/moz.build | 13 -------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/mozjs-sys/build.rs b/mozjs-sys/build.rs index 20466425dec..3c0b1e177f4 100644 --- a/mozjs-sys/build.rs +++ b/mozjs-sys/build.rs @@ -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"]); @@ -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", @@ -271,7 +271,11 @@ fn build_jsapi(build_dir: &Path) { fn build_jsglue(build_dir: &Path) { let mut build = cc::Build::new(); - build.cpp(true); + build.cpp(true) + .shared_flag(true) + .flag("-undefined") + .flag("dynamic_lookup") + .flag_if_supported("-std=g++17"); for flag in cc_flags() { build.flag_if_supported(flag); @@ -279,19 +283,33 @@ fn build_jsglue(build_dir: &Path) { 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("jsglue.dylib"); + 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 diff --git a/mozjs-sys/mozjs/js/src/build/moz.build b/mozjs-sys/mozjs/js/src/build/moz.build index 61162adb2a2..2da4b07d155 100644 --- a/mozjs-sys/mozjs/js/src/build/moz.build +++ b/mozjs-sys/mozjs/js/src/build/moz.build @@ -41,8 +41,6 @@ else: Library("js") USE_LIBS += ["mozglue"] -FORCE_STATIC_LIB = True -STATIC_LIBRARY_NAME = "js_static" if CONFIG["JS_HAS_INTL_API"]: USE_LIBS += [ @@ -91,18 +89,7 @@ if CONFIG["MOZ_NEEDS_LIBATOMIC"]: OS_LIBS += CONFIG["REALTIME_LIBS"] -NO_EXPAND_LIBS = True - DIST_INSTALL = True # Run SpiderMonkey style checker after linking the static library. This avoids # running the script for no-op builds. -GeneratedFile( - "spidermonkey_checks", - script="/config/run_spidermonkey_checks.py", - inputs=[ - "!%sjs_static.%s" % (CONFIG["LIB_PREFIX"], CONFIG["LIB_SUFFIX"]), - "/config/check_macroassembler_style.py", - "/config/check_js_opcode.py", - ], -) From 3c5faf5ac23b29497fb49db954e49f664d90abcb Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Fri, 24 Nov 2023 15:34:45 +0100 Subject: [PATCH 3/5] produce shared object for mozjs glue code --- mozjs-sys/build.rs | 5 +++-- mozjs/build.rs | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/mozjs-sys/build.rs b/mozjs-sys/build.rs index 3c0b1e177f4..3416df86b98 100644 --- a/mozjs-sys/build.rs +++ b/mozjs-sys/build.rs @@ -271,11 +271,12 @@ fn build_jsapi(build_dir: &Path) { fn build_jsglue(build_dir: &Path) { let mut build = cc::Build::new(); + // "-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"); + .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); @@ -299,7 +300,7 @@ fn build_jsglue(build_dir: &Path) { //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("jsglue.dylib"); + let file = glue_dir.join(format!("{}.dylib", "jsglue")); let mut cmd = build.get_compiler().to_command(); cmd.arg("src/jsglue.cpp") .arg("-o") diff --git a/mozjs/build.rs b/mozjs/build.rs index ccf2cbd4373..0b31ce57dcd 100644 --- a/mozjs/build.rs +++ b/mozjs/build.rs @@ -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", ""); @@ -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", "jsglue")); + 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") From 5d75603fb5c494cfee73659f0b81e1e42f75f40c Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Mon, 27 Nov 2023 09:56:16 +0100 Subject: [PATCH 4/5] fix error libjsglue library not found --- mozjs-sys/build.rs | 2 +- mozjs/build.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mozjs-sys/build.rs b/mozjs-sys/build.rs index 3416df86b98..a131f5aa7c1 100644 --- a/mozjs-sys/build.rs +++ b/mozjs-sys/build.rs @@ -300,7 +300,7 @@ fn build_jsglue(build_dir: &Path) { //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", "jsglue")); + let file = glue_dir.join(format!("{}.dylib", "libjsglue")); let mut cmd = build.get_compiler().to_command(); cmd.arg("src/jsglue.cpp") .arg("-o") diff --git a/mozjs/build.rs b/mozjs/build.rs index 0b31ce57dcd..9a331e2bb73 100644 --- a/mozjs/build.rs +++ b/mozjs/build.rs @@ -70,7 +70,7 @@ fn main() { }; let out_dir_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let file = out_dir_path.join(format!("{}.dylib", "jsglue")); + let file = out_dir_path.join(format!("{}.dylib", "libjsglue")); let mut cmd = build.get_compiler().to_command(); cmd.arg("src/jsglue.cpp") .arg("-o") From 82644b863f0bc223d01a4adc5e0e5112e18388d1 Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Tue, 28 Nov 2023 14:33:49 +0100 Subject: [PATCH 5/5] cleanup --- mozjs-sys/mozjs/config/rules.mk | 2 +- mozjs-sys/mozjs/js/src/build/moz.build | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mozjs-sys/mozjs/config/rules.mk b/mozjs-sys/mozjs/config/rules.mk index f0c646a3cd9..91d672f543a 100644 --- a/mozjs-sys/mozjs/config/rules.mk +++ b/mozjs-sys/mozjs/config/rules.mk @@ -279,7 +279,7 @@ endif ifeq ($(OS_ARCH),Darwin) ifneq (,$(SHARED_LIBRARY)) _LOADER_PATH := @rpath -EXTRA_DSO_LDOPTS += -undefined dynamic_lookup -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 diff --git a/mozjs-sys/mozjs/js/src/build/moz.build b/mozjs-sys/mozjs/js/src/build/moz.build index 2da4b07d155..4af3a218388 100644 --- a/mozjs-sys/mozjs/js/src/build/moz.build +++ b/mozjs-sys/mozjs/js/src/build/moz.build @@ -41,6 +41,9 @@ 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" if CONFIG["JS_HAS_INTL_API"]: USE_LIBS += [ @@ -89,7 +92,18 @@ if CONFIG["MOZ_NEEDS_LIBATOMIC"]: OS_LIBS += CONFIG["REALTIME_LIBS"] +NO_EXPAND_LIBS = True + DIST_INSTALL = True # Run SpiderMonkey style checker after linking the static library. This avoids # running the script for no-op builds. +GeneratedFile( + "spidermonkey_checks", + script="/config/run_spidermonkey_checks.py", + inputs=[ + "!%sjs_static.%s" % (CONFIG["LIB_PREFIX"], CONFIG["LIB_SUFFIX"]), + "/config/check_macroassembler_style.py", + "/config/check_js_opcode.py", + ], +) \ No newline at end of file