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