Snapshot of upstream SQLite 3.41.0
[sqlcipher.git] / ext / wasm / wasmfs.make
blob020014f3e8c25a56ac0d4ad3b9510659971a484d
1 #!/usr/bin/make
2 #^^^^ help emacs select makefile mode
4 # This is a sub-make for building a standalone wasmfs-based
5 # sqlite3.wasm. It is intended to be "include"d from the main
6 # GNUMakefile.
7 ########################################################################
8 MAKEFILE.wasmfs := $(lastword $(MAKEFILE_LIST))
10 # Maintenance reminder: these particular files cannot be built into a
11 # subdirectory because loading of the auxiliary
12 # sqlite3-wasmfs.worker.js file it creates fails if sqlite3-wasmfs.js
13 # is loaded from any directory other than the one in which the
14 # containing HTML lives. Similarly, they cannot be loaded from a
15 # Worker to an Emscripten quirk regarding loading nested Workers.
16 dir.wasmfs := $(dir.wasm)
17 sqlite3-wasmfs.js := $(dir.wasmfs)/sqlite3-wasmfs.js
18 sqlite3-wasmfs.mjs := $(dir.wasmfs)/sqlite3-wasmfs.mjs
19 sqlite3-wasmfs.wasm := $(dir.wasmfs)/sqlite3-wasmfs.wasm
21 CLEAN_FILES += $(sqlite3-wasmfs.js) $(sqlite3-wasmfs.wasm) \
22 $(subst .js,.worker.js,$(sqlite3-wasmfs.js)) \
23 $(sqlite3-wasmfs.mjs) \
24 $(subst .mjs,.worker.mjs,$(sqlite3-wasmfs.mjs))
26 ########################################################################
27 # emcc flags for .c/.o.
28 cflags.sqlite3-wasmfs :=
29 cflags.sqlite3-wasmfs += -std=c99 -fPIC
30 cflags.sqlite3-wasmfs += -pthread
31 cflags.sqlite3-wasmfs += $(cflags.speedtest1)
32 cflags.sqlite3-wasmfs += $(SQLITE_OPT) -DSQLITE_ENABLE_WASMFS
34 ########################################################################
35 # emcc flags specific to building the final .js/.wasm file...
36 emcc.flags.sqlite3-wasmfs := -fPIC
37 emcc.flags.sqlite3-wasmfs += --no-entry
38 emcc.flags.sqlite3-wasmfs += --minify 0
39 emcc.flags.sqlite3-wasmfs += -sMODULARIZE
40 emcc.flags.sqlite3-wasmfs += -sEXPORT_NAME=$(sqlite3.js.init-func)
41 emcc.flags.sqlite3-wasmfs += -sSTRICT_JS
42 emcc.flags.sqlite3-wasmfs += -sDYNAMIC_EXECUTION=0
43 emcc.flags.sqlite3-wasmfs += -sNO_POLYFILL
44 emcc.flags.sqlite3-wasmfs += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
45 emcc.flags.sqlite3-wasmfs += -sEXPORTED_FUNCTIONS=@$(abspath $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api)
46 emcc.flags.sqlite3-wasmfs += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory,allocateUTF8OnStack
47 # wasmMemory ==> for -sIMPORTED_MEMORY
48 # allocateUTF8OnStack ==> wasmfs internals
49 emcc.flags.sqlite3-wasmfs += -sUSE_CLOSURE_COMPILER=0
50 emcc.flags.sqlite3-wasmfs += -Wno-limited-postlink-optimizations
51 # ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
52 emcc.flags.sqlite3-wasmfs += -sALLOW_TABLE_GROWTH
53 emcc.flags.sqlite3-wasmfs += -sSTACK_SIZE=512KB
54 emcc.flags.sqlite3-wasmfs += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
55 emcc.flags.sqlite3-wasmfs += -sMEMORY64=0
56 emcc.flags.sqlite3-wasmfs += -sIMPORTED_MEMORY
57 emcc.flags.sqlite3-wasmfs += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.128)
58 # ^^^^ 64MB is not enough for WASMFS/OPFS test runs using batch-runner.js
59 sqlite3-wasmfs.fsflags := -pthread -sWASMFS \
60 -sPTHREAD_POOL_SIZE=2 -sENVIRONMENT=web,worker \
61 -sERROR_ON_UNDEFINED_SYMBOLS=0 -sLLD_REPORT_UNDEFINED
62 # ^^^^^ why undefined symbols are necessary for the wasmfs build is anyone's guess.
63 emcc.flags.sqlite3-wasmfs += $(sqlite3-wasmfs.fsflags)
64 #emcc.flags.sqlite3-wasmfs += -sALLOW_MEMORY_GROWTH
65 #^^^ using ALLOW_MEMORY_GROWTH produces a warning from emcc:
66 # USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly,
67 # see https://github.com/WebAssembly/design/issues/1271 [-Wpthreads-mem-growth]
68 # And, indeed, it runs slowly if memory is permitted to grow.
69 emcc.flags.sqlite3-wasmfs.vanilla :=
70 emcc.flags.sqlite3-wasmfs.esm := -sEXPORT_ES6 -sUSE_ES6_IMPORT_META
71 $(eval $(call call-make-pre-js,sqlite3-wasmfs,vanilla))
72 $(eval $(call call-make-pre-js,sqlite3-wasmfs,esm))
73 Xemcc.flags.sqlite3-wasmfs.vanilla += \
74 $(pre-post-common.flags.vanilla) \
75 $(pre-post-sqlite3-wasmfs.flags.vanilla)
76 Xemcc.flags.sqlite3-wasmfs.esm += \
77 $(pre-post-common.flags.esm) \
78 $(pre-post-sqlite3-wasmfs.flags.esm)
79 $(sqlite3-wasmfs.js) $(sqlite3-wasmfs.mjs): $(sqlite3-wasm.c) \
80 $(EXPORTED_FUNCTIONS.api) $(MAKEFILE) $(MAKEFILE.wasmfs)
81 $(sqlite3-wasmfs.js): $(pre-post-sqlite3-wasmfs.deps.vanilla)
82 $(sqlite3-wasmfs.mjs): $(pre-post-sqlite3-wasmfs.deps.esm)
83 # SQLITE3-WASMFS.xJS.RECIPE is the wasmfs-specific counterpart
84 # of SQLITE3.xJS.RECIPE from the main makefile.
85 define SQLITE3-WASMFS.xJS.RECIPE
86 @echo "Building $@ ..."
87 $(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \
88 $(cflags.sqlite3-wasmfs) \
89 $(emcc.flags.sqlite3-wasmfs) $(emcc.flags.sqlite3-wasmfs.$(1)) \
90 $(pre-post-sqlite3-wasmfs.flags.$(1)) \
91 $(sqlite3-wasm.c)
92 @$(call SQLITE3.xJS.ESM-EXPORT-DEFAULT,$(1))
93 chmod -x $(sqlite3-wasmfs.wasm)
94 $(maybe-wasm-strip) $(sqlite3-wasmfs.wasm)
95 @ls -la $(sqlite3-wasmfs.wasm) sqlite3-wasmfs*js
96 endef
97 $(sqlite3-wasmfs.js):
98 $(call SQLITE3-WASMFS.xJS.RECIPE,vanilla)
99 $(sqlite3-wasmfs.mjs): $(sqlite3-wasmfs.js)
100 $(call SQLITE3-WASMFS.xJS.RECIPE,esm)
101 $(sqlite3-wasmfs.wasm): $(sqlite3-wasmfs.js)
102 wasmfs: $(sqlite3-wasmfs.js) $(sqlite3-wasmfs.mjs)
103 #all: wasmfs
105 ########################################################################
106 # speedtest1 for wasmfs.
107 speedtest1-wasmfs.js := $(dir.wasmfs)/speedtest1-wasmfs.js
108 speedtest1-wasmfs.wasm := $(subst .js,.wasm,$(speedtest1-wasmfs.js))
109 emcc.flags.speedtest1-wasmfs := $(sqlite3-wasmfs.fsflags)
110 emcc.flags.speedtest1-wasmfs += $(SQLITE_OPT) -DSQLITE_ENABLE_WASMFS
111 emcc.flags.speedtest1-wasmfs += -sALLOW_MEMORY_GROWTH=0
112 emcc.flags.speedtest1-wasmfs += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.128)
113 #$(eval $(call call-make-pre-js,speedtest1-wasmfs,vanilla))
114 $(speedtest1-wasmfs.js): $(speedtest1.cses) $(sqlite3-wasmfs.js) \
115 $(MAKEFILE) $(MAKEFILE.wasmfs) \
116 $(pre-post-sqlite3-wasmfs.deps) \
117 $(EXPORTED_FUNCTIONS.speedtest1)
118 @echo "Building $@ ..."
119 $(emcc.bin) \
120 $(emcc.speedtest1.common) $(emcc.flags.speedtest1-wasmfs) \
121 $(pre-post-sqlite3-wasmfs.flags.vanilla) \
122 $(cflags.sqlite3-wasmfs) \
123 -o $@ $(speedtest1.cses) -lm
124 $(maybe-wasm-strip) $(speedtest1-wasmfs.wasm)
125 ls -la $@ $(speedtest1-wasmfs.wasm)
127 #speedtest1: $(speedtest1-wasmfs.js)
128 wasmfs: $(speedtest1-wasmfs.js)
129 CLEAN_FILES += $(speedtest1-wasmfs.js) $(speedtest1-wasmfs.wasm) \
130 $(subst .js,.worker.js,$(speedtest1-wasmfs.js))
131 # end speedtest1.js
132 ########################################################################