Skip to content

Commit

Permalink
Switch to separate setting for loading shared libraries globally
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Aug 31, 2022
1 parent 87d8462 commit 2d053d6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/cpp-exceptions-test/meta.yaml
Expand Up @@ -6,6 +6,7 @@ source:
path: src
build:
sharedlibrary: true
global: true
script: |
em++ -c throw.cpp -o throw.o ${SIDE_MODULE_CFLAGS} -fexceptions
em++ -c catch.cpp -o catch.o ${SIDE_MODULE_CFLAGS} -fexceptions
Expand Down
1 change: 1 addition & 0 deletions packages/sharedlib-test/meta.yaml
Expand Up @@ -8,6 +8,7 @@ source:

build:
sharedlibrary: true
global: true
script: |
emcc -c main.c -o main.o ${SIDE_MODULE_CFLAGS}
emcc -c dep.c -o dep.o ${SIDE_MODULE_CFLAGS}
Expand Down
1 change: 1 addition & 0 deletions packages/sqlite3/meta.yaml
Expand Up @@ -6,6 +6,7 @@ source:
path: empty
build:
sharedlibrary: true
global: true
script: |
mkdir dist
export DISTDIR=$(pwd)/dist
Expand Down
3 changes: 3 additions & 0 deletions pyodide-build/pyodide_build/buildall.py
Expand Up @@ -44,6 +44,7 @@ class BasePackage:
meta: dict[str, Any]
library: bool
shared_library: bool
global_: bool
run_dependencies: list[str]
host_dependencies: list[str]
dependencies: set[str] # run + host dependencies
Expand Down Expand Up @@ -99,6 +100,7 @@ def __init__(self, pkgdir: Path):

self.library = self.meta["build"].get("library", False)
self.shared_library = self.meta["build"].get("sharedlibrary", False)
self.global_ = self.meta["build"].get("global", False)

assert self.name == pkgdir.name, f"{self.name} != {pkgdir.name}"

Expand Down Expand Up @@ -496,6 +498,7 @@ def generate_packagedata(
}
if pkg.shared_library:
pkg_entry["shared_library"] = True
pkg_entry["global"] = pkg.global_
pkg_entry["install_dir"] = "lib" if pkg.cpython_dynlib else "dynlib"

pkg_entry["depends"] = [
Expand Down
3 changes: 3 additions & 0 deletions pyodide-build/pyodide_build/io.py
Expand Up @@ -22,6 +22,7 @@
},
"build": {
"exports": str | list, # list[str]
"global": bool,
"backend-flags": str,
"cflags": str,
"cxxflags": str,
Expand Down Expand Up @@ -151,6 +152,8 @@ def _check_config_build(config: dict[str, Any]) -> Iterator[str]:
yield "build/library and build/sharedlibrary cannot both be true."

allowed_keys = {"library", "sharedlibrary", "script", "cross-script"}
if not library:
allowed_keys |= {"global"}
typ = "library" if library else "sharedlibrary"
for key in build_metadata.keys():
if key not in PACKAGE_CONFIG_SPEC["build"]:
Expand Down
10 changes: 8 additions & 2 deletions src/js/load-package.ts
Expand Up @@ -226,7 +226,7 @@ async function installPackage(
source: channel === DEFAULT_CHANNEL ? "pyodide" : channel,
});
for (const dynlib of dynlibs) {
await loadDynlib(dynlib);
await loadDynlib(dynlib, pkg.global);
}
}

Expand Down Expand Up @@ -269,7 +269,7 @@ const acquireDynlibLock = createLock();
* @param shared Is this a shared library or not?
* @private
*/
async function loadDynlib(lib: string) {
async function loadDynlib(lib: string, global: boolean) {
const node = Module.FS.lookupPath(lib).node;
let byteArray;
if (node.mount.type == Module.FS.filesystems.MEMFS) {
Expand Down Expand Up @@ -301,6 +301,12 @@ async function loadDynlib(lib: string) {
});
Module.preloadedWasm[lib] = module;
Module.preloadedWasm[lib.split("/").pop()!] = module;
if (global) {
Module.loadDynamicLibrary(lib, {
global: true,
nodelete: true,
});
}
} catch (e: any) {
if (e && e.message && e.message.includes("need to see wasm magic number")) {
console.warn(
Expand Down

0 comments on commit 2d053d6

Please sign in to comment.