1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Minor WASM build cleanups. Enable custom Module.instantiateWasm() when not in WASMFS mode (where it doesn't work). Add sqlite3.debugModule URL param to enable some module-init-time debugging output.

FossilOrigin-Name: 50f678846a2b3c3d0818f0bae89f2ee86252a2e6a9c7029ebaae3953ca0fa14c
This commit is contained in:
stephan
2022-10-30 09:47:33 +00:00
parent af9cee12c1
commit 1fc6ffccc5
9 changed files with 73 additions and 48 deletions

View File

@ -106,7 +106,7 @@ endif
cflags.common := -I. -I.. -I$(dir.top)
CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~
emcc_enable_bigint ?= 1
emcc.WASM_BIGINT ?= 1
sqlite3.c := $(dir.top)/sqlite3.c
sqlite3.h := $(dir.top)/sqlite3.h
SQLITE_OPT = \
@ -295,7 +295,11 @@ define call-make-pre-js
pre-post-$(1).flags ?=
$$(dir.tmp)/pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE)
cp $$(pre-js.js) $$@
echo "Module[xInstantiateWasm].uri = '$(1).wasm';" >> $$@
@if [ sqlite3-wasmfs = $(1) ]; then \
echo "delete Module[xNameOfInstantiateWasm] /*for WASMFS build*/;"; \
elif [ sqlite3 != $(1) ]; then \
echo "Module[xNameOfInstantiateWasm].uri = '$(1).wasm';"; \
fi >> $$@
pre-post-$(1).deps := $$(pre-post-jses.deps) $$(dir.tmp)/pre-js-$(1).js
pre-post-$(1).flags += --pre-js=$$(dir.tmp)/pre-js-$(1).js
endef
@ -388,14 +392,16 @@ emcc.jsflags += -sLLD_REPORT_UNDEFINED
#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
#emcc.jsflags += --unresolved-symbols=ignore-all
emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
# ^^^^ MEMORY64=1 fails to load, erroring with:
emcc.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
########################################################################
# -sMEMORY64=1 fails to load, erroring with:
# invalid memory limits flags 0x5
# (enable via --experimental-wasm-memory64)
#
# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
#
# new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n)
# new Uint8Array(wasm.heap8u().buffer, ptr, n)
#
# because ptr is now a BigInt, so is invalid for passing to arguments
# which have strict must-be-a-Number requirements.
@ -405,10 +411,10 @@ emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
########################################################################
# -sSINGLE_FILE:
# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
# -sSINGLE_FILE=1 would be really nice but we have to build with -g
# -sSINGLE_FILE=1 would be really nice but we have to build with -g3
# for -O2 and higher to work (else minification breaks the code) and
# cannot wasm-strip the binary before it gets encoded into the JS
# file. The result is that the generated JS file is, because of the -g
# file. The result is that the generated JS file is, because of the -g3
# debugging info, _huge_.
########################################################################
@ -468,11 +474,12 @@ all: batch
speedtest1-common.eflags := $(emcc_opt_full)
speedtest1.eflags :=
speedtest1.eflags += -sENVIRONMENT=web
speedtest1.eflags += -sALLOW_MEMORY_GROWTH
speedtest1.eflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
speedtest1-common.eflags += -sINVOKE_RUN=0
speedtest1-common.eflags += --no-entry
#speedtest1-common.eflags += -flto
speedtest1-common.eflags += -sABORTING_MALLOC
speedtest1-common.eflags += -sINITIAL_MEMORY=128450560
speedtest1-common.eflags += -sSTRICT_JS
speedtest1-common.eflags += -sMODULARIZE
speedtest1-common.eflags += -Wno-limited-postlink-optimizations
@ -483,7 +490,7 @@ speedtest1-common.eflags += -sALLOW_TABLE_GROWTH
speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0
speedtest1-common.eflags += --minify 0
speedtest1-common.eflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
speedtest1-common.eflags += -sWASM_BIGINT=$(emcc_enable_bigint)
speedtest1-common.eflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
speedtest1-common.eflags += $(pre-post-common.flags)
speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1

View File

@ -36,6 +36,11 @@
location: self.location,
urlParams: new URL(self.location.href).searchParams
});
initModuleState.debugModule =
(new URL(self.location.href).searchParams).has('sqlite3.debugModule')
? (...args)=>console.warn('sqlite3.debugModule:',...args)
: ()=>{};
if(initModuleState.urlParams.has('sqlite3.dir')){
initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/';
}else if(initModuleState.moduleScript){
@ -43,7 +48,6 @@
li.pop();
initModuleState.sqlite3Dir = li.join('/') + '/';
}
//console.warn("initModuleState =",initModuleState);
self.sqlite3InitModule = (...args)=>{
//console.warn("Using replaced sqlite3InitModule()",self.location);
@ -79,6 +83,7 @@
src.pop();
sim.scriptDir = src.join('/') + '/';
}
initModuleState.debugModule('sqlite3InitModuleState =',initModuleState);
if(0){
console.warn("Replaced sqlite3InitModule()");
console.warn("self.location.href =",self.location.href);

View File

@ -5,7 +5,7 @@
environment must have been set up already but it will not have
loaded its WASM when the code in this file is run. The function it
installs will be run after the WASM module is loaded, at which
point the sqlite3 WASM API bits will be set up.
point the sqlite3 JS API bits will get set up.
*/
if(!Module.postRun) Module.postRun = [];
Module.postRun.push(function(Module/*the Emscripten-style module object*/){

View File

@ -8,6 +8,7 @@
// See notes in extern-post-js.js
const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
delete self.sqlite3InitModuleState;
sqlite3InitModuleState.debugModule('self.location =',self.location);
/**
This custom locateFile() tries to figure out where to load `path`
@ -30,12 +31,6 @@ delete self.sqlite3InitModuleState;
Module['locateFile'] = function(path, prefix) {
let theFile;
const up = this.urlParams;
if(0){
console.warn("locateFile(",arguments[0], ',', arguments[1],")",
'self.location =',self.location,
'sqlite3InitModuleState.scriptDir =',this.scriptDir,
'up.entries() =',Array.from(up.entries()));
}
if(up.has(path)){
theFile = up.get(path);
}else if(this.sqlite3Dir){
@ -45,22 +40,37 @@ Module['locateFile'] = function(path, prefix) {
}else{
theFile = prefix + path;
}
sqlite3InitModuleState.debugModule(
"locateFile(",arguments[0], ',', arguments[1],")",
'sqlite3InitModuleState.scriptDir =',this.scriptDir,
'up.entries() =',Array.from(up.entries()),
"result =", theFile
);
return theFile;
}.bind(sqlite3InitModuleState);
/**
Bug warning: this xInstantiateWasm bit must remain disabled
until this bug is resolved or wasmfs won't work:
Bug warning: a custom Module.instantiateWasm() does not work
in WASMFS builds:
https://github.com/emscripten-core/emscripten/issues/17951
In such builds we must disable this.
*/
const xInstantiateWasm = 1
? 'emscripten-bug-17951'
: 'instantiateWasm';
Module[xInstantiateWasm] = function callee(imports,onSuccess){
const xNameOfInstantiateWasm = true
? 'instantiateWasm'
: 'emscripten-bug-17951';
Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){
imports.env.foo = function(){};
console.warn("instantiateWasm() uri =",callee.uri, self.location.href);
const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'});
const uri = Module.locateFile(
callee.uri, (
('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/)
? '' : scriptDirectory)
);
sqlite3InitModuleState.debugModule(
"instantiateWasm() uri =", uri
);
const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
const loadWasm = WebAssembly.instantiateStreaming
? async ()=>{
return WebAssembly.instantiateStreaming(wfetch(), imports)
@ -79,10 +89,12 @@ Module[xInstantiateWasm] = function callee(imports,onSuccess){
It is literally impossible to reliably get the name of _this_ script
at runtime, so impossible to derive X.wasm from script name
X.js. Thus we need, at build-time, to redefine
Module[xInstantiateWasm].uri by appending it to a build-specific
Module[xNameOfInstantiateWasm].uri by appending it to a build-specific
copy of this file with the name of the wasm file. This is apparently
why Emscripten hard-codes the name of the wasm file into their glue
scripts.
*/
Module[xInstantiateWasm].uri = 'sqlite3.wasm';
/* END FILE: api/pre-js.js */
Module[xNameOfInstantiateWasm].uri = 'sqlite3.wasm';
/* END FILE: api/pre-js.js, noting that the build process may add a
line after this one to change the above .uri to a build-specific
one. */

View File

@ -1028,7 +1028,7 @@ int sqlite3_wasm_init_wasmfs(const char *zMountPoint){
#else
SQLITE_WASM_KEEP
int sqlite3_wasm_init_wasmfs(const char *zUnused){
emscripten_console_warn("WASMFS OPFS is not compiled in.");
//emscripten_console_warn("WASMFS OPFS is not compiled in.");
if(zUnused){/*unused*/}
return SQLITE_NOTFOUND;
}

View File

@ -33,7 +33,7 @@ fiddle.emcc-flags = \
-sENVIRONMENT=web,worker \
-sMODULARIZE \
-sDYNAMIC_EXECUTION=0 \
-sWASM_BIGINT=$(emcc_enable_bigint) \
-sWASM_BIGINT=$(emcc.WASM_BIGINT) \
-sEXPORT_NAME=$(sqlite3.js.init-func) \
-Wno-limited-postlink-optimizations \
$(sqlite3.js.flags.--post-js) \

View File

@ -11,7 +11,8 @@ MAKEFILE.wasmfs := $(lastword $(MAKEFILE_LIST))
# subdirectory because loading of the auxiliary
# sqlite3-wasmfs.worker.js file it creates fails if sqlite3-wasmfs.js
# is loaded from any directory other than the one in which the
# containing HTML lives.
# containing HTML lives. Similarly, they cannot be loaded from a
# Worker to an Emscripten quirk regarding loading nested Workers.
dir.wasmfs := $(dir.wasm)
sqlite3-wasmfs.js := $(dir.wasmfs)/sqlite3-wasmfs.js
sqlite3-wasmfs.wasm := $(dir.wasmfs)/sqlite3-wasmfs.wasm
@ -65,7 +66,7 @@ sqlite3-wasmfs.jsflags += $(sqlite3-wasmfs.fsflags)
#^^^ using ALLOW_MEMORY_GROWTH produces a warning from emcc:
# USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly,
# see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
sqlite3-wasmfs.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
sqlite3-wasmfs.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
$(eval $(call call-make-pre-js,sqlite3-wasmfs))
sqlite3-wasmfs.jsflags += $(pre-post-common.flags) $(pre-post-sqlite3-wasmfs.flags)
$(sqlite3-wasmfs.js): $(sqlite3-wasm.c) \
@ -83,13 +84,13 @@ wasmfs: $(sqlite3-wasmfs.js)
all: wasmfs
########################################################################
# speedtest1 for wasmfs. Re. sqlite3-wasm.o vs sqlite3-wasm.c:
# building against the latter (predictably) results in a slightly
# faster binary.
# speedtest1 for wasmfs.
speedtest1-wasmfs.js := $(dir.wasmfs)/speedtest1-wasmfs.js
speedtest1-wasmfs.wasm := $(subst .js,.wasm,$(speedtest1-wasmfs.js))
speedtest1-wasmfs.eflags := $(sqlite3-wasmfs.fsflags)
speedtest1-wasmfs.eflags += $(SQLITE_OPT) -DSQLITE_WASM_WASMFS
speedtest1-wasmfs.eflags += -sALLOW_MEMORY_GROWTH=0
speedtest1-wasmfs.eflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.128)
$(eval $(call call-make-pre-js,speedtest1-wasmfs))
$(speedtest1-wasmfs.js): $(speedtest1.cses) $(sqlite3-wasmfs.js) \
$(MAKEFILE) $(MAKEFILE.wasmfs) \