xref: /sqlite-3.40.0/ext/wasm/GNUmakefile (revision a002cc17)
1# This GNU makefile exists primarily to simplify/speed up development
2# of the sqlite3 WASM components. It is not part of the canonical
3# build process.
4#
5# Maintenance notes: the fiddle build is currently performed in the
6# top-level ../../Makefile.in. It may be moved into this file at some
7# point, as GNU Make has been deemed acceptable for the WASM-related
8# components (whereas POSIX Make is required for the more conventional
9# components).
10SHELL := $(shell which bash 2>/dev/null)
11all:
12
13.PHONY: fiddle
14ifneq (,$(wildcard /home/stephan))
15  fiddle_opt ?= -O0
16else
17  fiddle_opt = -Os
18endif
19fiddle:
20	$(MAKE) -C ../.. fiddle -e emcc_opt=$(fiddle_opt)
21all: fiddle
22
23clean:
24	$(MAKE) -C ../../ clean-fiddle
25	-rm -f $(CLEAN_FILES)
26
27MAKEFILE := $(lastword $(MAKEFILE_LIST))
28dir.top := ../..
29# Reminder: some Emscripten flags require absolute paths
30dir.wasm := $(patsubst %/,%,$(dir $(abspath $(MAKEFILE))))
31dir.api := api
32dir.jacc := jaccwabyt
33dir.common := common
34CLEAN_FILES := *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~
35
36sqlite3.c := $(dir.top)/sqlite3.c
37$(sqlite3.c):
38	$(MAKE) -C $(dir.top) sqlite3.c
39
40SQLITE_OPT = \
41  -DSQLITE_ENABLE_FTS4 \
42  -DSQLITE_ENABLE_RTREE \
43  -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
44  -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \
45  -DSQLITE_ENABLE_STMTVTAB \
46  -DSQLITE_ENABLE_DBPAGE_VTAB \
47  -DSQLITE_ENABLE_DBSTAT_VTAB \
48  -DSQLITE_ENABLE_BYTECODE_VTAB \
49  -DSQLITE_ENABLE_OFFSET_SQL_FUNC \
50  -DSQLITE_OMIT_LOAD_EXTENSION \
51  -DSQLITE_OMIT_DEPRECATED \
52  -DSQLITE_OMIT_UTF16 \
53  -DSQLITE_OMIT_SHARED_CACHE \
54  -DSQLITE_THREADSAFE=0 \
55  -DSQLITE_TEMP_STORE=3
56#SQLITE_OPT += -DSQLITE_ENABLE_MEMSYS5
57# ^^^ MEMSYS5 is hypothetically useful for non-Emscripten builds but
58# requires adding more infrastructure and fixing one spot in the
59# sqlite3 internals which calls malloc() early on.
60
61# SQLITE_OMIT_LOAD_EXTENSION: if this is true, sqlite3_vfs::xDlOpen
62# and friends may be NULL.
63
64emcc_opt ?= -O0
65.PHONY: release
66release:
67	$(MAKE) 'emcc_opt=-Os -g3 -flto'
68# ^^^^^ target-specific vars, e.g.:
69#   release: emcc_opt=...
70# apparently only work for file targets, not PHONY targets?
71#
72# ^^^ -flto improves runtime speed at -O0 considerably but doubles
73# build time.
74#
75# ^^^^ -O3, -Oz, -Os minify symbol names and there appears to be no
76# way around that except to use -g3, but -g3 causes the binary file
77# size to absolutely explode (approx. 5x larger). This minification
78# utterly breaks the resulting module, making it unsable except as
79# self-contained/self-referential-only code, as ALL of the exported
80# symbols get minified names.
81#
82# However, we have an option for using -Oz or -Os:
83#
84# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt
85# tools package (https://github.com/WebAssembly/wabt), to strip the
86# debugging symbols. That results in a small build with unmangled
87# symbol names. -Oz gives ever-so-slightly better compression than
88# -Os: not quite 1% in some completely unscientific tests. Runtime
89# speed for the unit tests is all over the place either way so it's
90# difficult to say whether -Os gives any speed benefit over -Oz.
91########################################################################
92
93# Emscripten SDK home dir and related binaries...
94EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/src/emsdk $(HOME)/emsdk))
95emcc.bin ?= $(word 1,$(wildcard $(shell which emcc) $(EMSDK_HOME)/upstream/emscripten/emcc))
96ifeq (,$(emcc.bin))
97  $(error Cannot find emcc.)
98endif
99
100wasm-strip ?= $(shell which wasm-strip 2>/dev/null)
101ifeq (,$(filter clean,$(MAKECMDGOALS)))
102ifeq (,$(wasm-strip))
103  $(info WARNING: *******************************************************************)
104  $(info WARNING: builds using -O3/-Os/-Oz will minify WASM-exported names,)
105  $(info WARNING: breaking _All The Things_. The workaround for that is to build)
106  $(info WARNING: with -g3 (which explodes the file size) and then strip the debug)
107  $(info WARNING: info after compilation, using wasm-strip, to shrink the wasm file.)
108  $(info WARNING: wasm-strip was not found in the PATH so we cannot strip those.)
109  $(info WARNING: If this build uses any optimization level higher than -O2 then)
110  $(info WARNING: the ***resulting WASM binary WILL NOT BE USABLE***.)
111  $(info WARNING: wasm-strip is part of the wabt package:)
112  $(info WARNING:    https://github.com/WebAssembly/wabt)
113  $(info WARNING: on Ubuntu-like systems it can be installed with:)
114  $(info WARNING:    sudo apt install wabt)
115  $(info WARNING: *******************************************************************)
116endif
117endif # 'make clean' check
118
119ifeq (release,$(filter release,$(MAKECMDGOALS)))
120  ifeq (,$(wasm-strip))
121    $(error Cannot make release-quality binary because wasm-strip is not available. \
122            See notes in the warning above)
123  endif
124else
125  $(info Development build. Use '$(MAKE) release' for a smaller release build.)
126endif
127
128EXPORTED_FUNCTIONS.api.in := $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api \
129    $(dir.jacc)/jaccwabyt_test.exports
130
131EXPORTED_FUNCTIONS.api: $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE)
132	cat $(EXPORTED_FUNCTIONS.api.in) > $@
133CLEAN_FILES += EXPORTED_FUNCTIONS.api
134
135sqlite3-api.jses := \
136  $(dir.api)/sqlite3-api-prologue.js \
137  $(dir.common)/whwasmutil.js \
138  $(dir.jacc)/jaccwabyt.js \
139  $(dir.api)/sqlite3-api-glue.js \
140  $(dir.api)/sqlite3-api-oo1.js \
141  $(dir.api)/sqlite3-api-worker1.js
142#sqlite3-api.jses += $(dir.api)/sqlite3-api-opfs.js
143sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js
144
145sqlite3-api.js := sqlite3-api.js
146CLEAN_FILES += $(sqlite3-api.js)
147$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE)
148	@echo "Making $@..."
149	@for i in $(sqlite3-api.jses); do \
150		echo "/* BEGIN FILE: $$i */"; \
151		cat $$i; \
152		echo "/* END FILE: $$i */"; \
153	done > $@
154
155post-js.js := post-js.js
156CLEAN_FILES += $(post-js.js)
157post-jses := \
158  $(dir.api)/post-js-header.js \
159  $(sqlite3-api.js) \
160  $(dir.api)/post-js-footer.js
161
162$(post-js.js): $(post-jses) $(MAKEFILE)
163	@echo "Making $@..."
164	@for i in $(post-jses); do \
165		echo "/* BEGIN FILE: $$i */"; \
166		cat $$i; \
167		echo "/* END FILE: $$i */"; \
168	done > $@
169
170
171########################################################################
172# emcc flags for .c/.o/.wasm/.js.
173emcc.flags =
174#emcc.flags += -v # _very_ loud but also informative about what it's doing
175# -g is needed to keep -O2 and higher from creating broken JS via
176# minification.
177emcc.flags += -g
178
179########################################################################
180# emcc flags for .c/.o.
181emcc.cflags :=
182emcc.cflags += -std=c99 -fPIC
183# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs.
184emcc.cflags += -I. -I$(dir.top) # $(SQLITE_OPT)
185
186########################################################################
187# emcc flags specific to building the final .js/.wasm file...
188emcc.jsflags := -fPIC
189emcc.jsflags := --minify 0
190emcc.jsflags += --no-entry
191emcc.jsflags += -sMODULARIZE
192emcc.jsflags += -sSTRICT_JS
193emcc.jsflags += -sDYNAMIC_EXECUTION=0
194emcc.jsflags += -sNO_POLYFILL
195emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api
196emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory,allocateUTF8OnStack
197                # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
198                # allocateUTF8OnStack => for kvvp internals
199emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
200emcc.jsflags += -sIMPORTED_MEMORY
201emcc.environment := -sENVIRONMENT=web
202ENABLE_WASMFS ?= 0
203ifneq (0,$(ENABLE_WASMFS))
204  emcc.cflags += -pthread
205  emcc.jsflags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2
206  emcc.cflags += '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"'
207  emcc.environment := $(emcc.environment),worker
208  emcc.jsflags += -sINITIAL_MEMORY=128450560
209else
210  emcc.jsflags += -sALLOW_MEMORY_GROWTH
211  # emcc: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code
212  #       slowly, see https://github.com/WebAssembly/design/issues/1271
213  #       [-Wpthreads-mem-growth]
214  emcc.jsflags += -sINITIAL_MEMORY=13107200
215  #emcc.jsflags += -sINITIAL_MEMORY=64225280
216  # ^^^^ 64MB is not enough for WASMFS/OPFS test runs using batch-runner.js
217endif
218emcc.jsflags += $(emcc.environment)
219#emcc.jsflags += -sTOTAL_STACK=4194304
220emcc.jsflags += -sEXPORT_NAME=sqlite3InitModule
221emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
222emcc.jsflags += --post-js=$(post-js.js)
223#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
224#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
225#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API
226#emcc.jsflags += -sABORTING_MALLOC
227emcc.jsflags += -sALLOW_TABLE_GROWTH
228emcc.jsflags += -Wno-limited-postlink-optimizations
229# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
230#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why
231# https://lld.llvm.org/WebAssembly.html
232emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0
233emcc.jsflags += -sLLD_REPORT_UNDEFINED
234#emcc.jsflags += --allow-undefined
235emcc.jsflags += --import-undefined
236#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
237#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
238#emcc.jsflags += --unresolved-symbols=ignore-all
239enable_bigint ?= 1
240ifneq (0,$(enable_bigint))
241emcc.jsflags += -sWASM_BIGINT
242endif
243emcc.jsflags += -sMEMORY64=0
244# ^^^^ MEMORY64=1 fails to load, erroring with:
245#  invalid memory limits flags 0x5
246#    (enable via --experimental-wasm-memory64)
247#
248# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
249#
250#  new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n)
251#
252# because ptr is now a BigInt, so is invalid for passing to arguments
253# which have strict must-be-a-Number requirements.
254########################################################################
255
256
257########################################################################
258# -sSINGLE_FILE:
259# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
260# -sSINGLE_FILE=1 would be really nice but we have to build with -g
261# for -O2 and higher to work (else minification breaks the code) and
262# cannot wasm-strip the binary before it gets encoded into the JS
263# file. The result is that the generated JS file is, because of the -g
264# debugging info, _huge_.
265########################################################################
266
267########################################################################
268# Maintenance reminder: the output .js and .wasm files of emcc must be
269# in _this_ dir, rather than a subdir, or else parts of the generated
270# code get confused and cannot load property (namely, the
271# sqlite3.worker.js generated in conjunction with -sWASMFS).
272sqlite3.js := sqlite3.js
273sqlite3.wasm := sqlite3.wasm
274sqlite3-wasm.o := $(dir.api)/sqlite3-wasm.o
275$(sqlite3-wasm.o): emcc.cflags += $(SQLITE_OPT)
276$(sqlite3-wasm.o): $(dir.top)/sqlite3.c
277$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT)
278sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
279jaccwabyt_test.c := $(dir.jacc)/jaccwabyt_test.c
280# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test apps,
281# so we don't really want to include it in release builds. However, we
282# want to test the release builds with those apps, so we cannot simply
283# elide that file in release builds. That component is critical to the
284# VFS bindings so needs to be tested along with the core APIs.
285ifneq (,$(filter -sWASMFS,$(emcc.jsflags)))
286  $(sqlite3-wasm.o): emcc.cflags+=-DSQLITE_WASM_OPFS
287endif
288define WASM_C_COMPILE
289$(1).o := $$(subst .c,.o,$(1))
290sqlite3.wasm.obj += $$($(1).o)
291$$($(1).o): $$(MAKEFILE) $(1)
292	$$(emcc.bin) $$(emcc_opt) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@
293CLEAN_FILES += $$($(1).o)
294endef
295$(foreach c,$(sqlite3-wasm.c) $(jaccwabyt_test.c),$(eval $(call WASM_C_COMPILE,$(c))))
296$(sqlite3.js):
297$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
298    EXPORTED_FUNCTIONS.api \
299    $(post-js.js)
300	$(emcc.bin) -o $(sqlite3.js) $(emcc_opt) $(emcc.flags) $(emcc.jsflags) $(sqlite3.wasm.obj)
301	chmod -x $(sqlite3.wasm)
302ifneq (,$(wasm-strip))
303	$(wasm-strip) $(sqlite3.wasm)
304endif
305	@ls -la $@ $(sqlite3.wasm)
306
307CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm)
308all: $(sqlite3.js)
309wasm: $(sqlite3.js)
310# End main Emscripten-based module build
311########################################################################
312
313########################################################################
314# batch-runner.js...
315dir.sql := sql
316speedtest1 := ../../speedtest1
317speedtest1.c := ../../test/speedtest1.c
318speedtest1.sql := $(dir.sql)/speedtest1.sql
319$(speedtest1):
320	$(MAKE) -C ../.. speedtest1
321$(speedtest1.sql): $(speedtest1)
322	$(speedtest1) --script $@
323batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
324	bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
325	ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
326clean-batch:
327	rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
328# ^^^ we don't do this along with 'clean' because we clean/rebuild on
329# a regular basis with different -Ox flags and rebuilding the batch
330# pieces each time is an unnecessary time sink.
331batch: batch-runner.list
332all: batch
333# end batch-runner.js
334########################################################################
335# speedtest1.js...
336emcc.speedtest1-flags := -g $(emcc_opt)
337ifneq (0,$(ENABLE_WASMFS))
338  emcc.speedtest1-flags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2
339  emcc.speedtest1-flags += -DSQLITE_WASM_OPFS
340endif
341emcc.speedtest1-flags += -sINVOKE_RUN=0
342#emcc.speedtest1-flags += --no-entry
343emcc.speedtest1-flags += -flto
344emcc.speedtest1-flags += -sABORTING_MALLOC
345emcc.speedtest1-flags += -sINITIAL_MEMORY=128450560
346emcc.speedtest1-flags += -sSTRICT_JS
347emcc.speedtest1-flags += $(emcc.environment)
348emcc.speedtest1-flags += -sMODULARIZE
349emcc.speedtest1-flags += -sEXPORT_NAME=sqlite3Speedtest1InitModule
350emcc.speedtest1-flags += -Wno-limited-postlink-optimizations
351emcc.speedtest1-flags += -sEXPORTED_FUNCTIONS=_main,_malloc,_free,_sqlite3_wasm_vfs_unlink,_sqlite3_wasm_init_opfs
352emcc.speedtest1-flags += -sDYNAMIC_EXECUTION=0
353emcc.speedtest1-flags += --minify 0
354
355speedtest1.js := speedtest1.js
356speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
357$(speedtest1.js): emcc.cflags+=
358# speedtest1 notes re. sqlite3-wasm.o vs sqlite3-wasm.c: building against
359# the latter (predictably) results in a slightly faster binary, but we're
360# close enough to the target speed requirements that the 500ms makes a
361# difference.
362$(speedtest1.js): $(speedtest1.c) $(sqlite3-wasm.c) $(MAKEFILE)
363	$(emcc.bin) \
364        $(emcc.speedtest1-flags) \
365        -I. -I$(dir.top) \
366        -DSQLITE_THREADSAFE=0 \
367        -DSQLITE_TEMP_STORE=3 \
368        -DSQLITE_OMIT_UTF16 \
369        -DSQLITE_OMIT_DEPRECATED \
370        -DSQLITE_OMIT_SHARED_CACHE \
371        '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \
372        -DSQLITE_SPEEDTEST1_WASM \
373        -o $@ $(speedtest1.c) $(sqlite3-wasm.c) -lm
374ifneq (,$(wasm-strip))
375	$(wasm-strip) $(speedtest1.wasm)
376endif
377	ls -la $@ $(speedtest1.wasm)
378
379speedtest1: $(speedtest1.js)
380all: $(speedtest1.js)
381CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
382# end speedtest1.js
383########################################################################
384
385########################################################################
386# fiddle_remote is the remote destination for the fiddle app. It
387# must be a [user@]HOST:/path for rsync.
388# Note that the target "should probably" contain a symlink of
389# index.html -> fiddle.html.
390fiddle_remote ?=
391ifeq (,$(fiddle_remote))
392ifneq (,$(wildcard /home/stephan))
393  fiddle_remote = wh:www/wh/sqlite3/.
394else ifneq (,$(wildcard /home/drh))
395  #fiddle_remote = if appropriate, add that user@host:/path here
396endif
397endif
398$(fiddle_files): default
399push-fiddle: $(fiddle_files)
400	@if [ x = "x$(fiddle_remote)" ]; then \
401		echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
402		exit 1; \
403	fi
404	rsync -va fiddle/ $(fiddle_remote)
405# end fiddle remote push
406########################################################################
407
408include kvvfs.make
409