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