xref: /sqlite-3.40.0/ext/wasm/GNUmakefile (revision 16603ecf)
1#######################################################################
2# This GNU makefile drives the build of the sqlite3 WASM
3# components. It is not part of the canonical build process.
4#
5# This build assumes a Linux platform and is not intended for
6# general-purpose client-level use, except for creating builds with
7# custom configurations. It is primarily intended for the sqlite
8# project's own development of the JS/WASM components.
9#
10# Primary targets:
11#
12#  default, all = build in dev mode
13#
14#  o0, o1, o2, o3, os, oz = full clean/rebuild with the -Ox level indicated
15#      by the target name. Rebuild is necessary for all components to get
16#      the desired optimization level.
17#
18#  dist = create end user deliverables. Add dist.build=oX to build
19#      with a specific optimization level, where oX is one of the
20#      above-listed o? target names.
21#
22#  clean = clean up
23########################################################################
24SHELL := $(shell which bash 2>/dev/null)
25MAKEFILE := $(lastword $(MAKEFILE_LIST))
26CLEAN_FILES :=
27DISTCLEAN_FILES := ./--dummy--
28default: all
29release: oz
30
31# Emscripten SDK home dir and related binaries...
32EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/src/emsdk $(HOME)/emsdk))
33emcc.bin ?= $(word 1,$(wildcard $(shell which emcc) $(EMSDK_HOME)/upstream/emscripten/emcc))
34ifeq (,$(emcc.bin))
35  $(error Cannot find emcc.)
36endif
37
38wasm-strip ?= $(shell which wasm-strip 2>/dev/null)
39ifeq (,$(filter clean,$(MAKECMDGOALS)))
40ifeq (,$(wasm-strip))
41  $(info WARNING: *******************************************************************)
42  $(info WARNING: builds using -O2/-O3/-Os/-Oz will minify WASM-exported names,)
43  $(info WARNING: breaking _All The Things_. The workaround for that is to build)
44  $(info WARNING: with -g3 (which explodes the file size) and then strip the debug)
45  $(info WARNING: info after compilation, using wasm-strip, to shrink the wasm file.)
46  $(info WARNING: wasm-strip was not found in the PATH so we cannot strip those.)
47  $(info WARNING: If this build uses any optimization level higher than -O1 then)
48  $(info WARNING: the ***resulting WASM binary WILL NOT BE USABLE***.)
49  $(info WARNING: wasm-strip is part of the wabt package:)
50  $(info WARNING:    https://github.com/WebAssembly/wabt)
51  $(info WARNING: on Ubuntu-like systems it can be installed with:)
52  $(info WARNING:    sudo apt install wabt)
53  $(info WARNING: *******************************************************************)
54endif
55endif # 'make clean' check
56
57ifeq (,$(wasm-strip))
58  maybe-wasm-strip = echo "not wasm-stripping"
59else
60  maybe-wasm-strip = $(wasm-strip)
61endif
62
63dir.top := ../..
64# Reminder: some Emscripten flags require absolute paths but we want
65# relative paths for most stuff simply to reduce noise. The
66# $(abspath...) GNU make function can transform relative paths to
67# absolute.
68dir.wasm := $(patsubst %/,%,$(dir $(MAKEFILE)))
69dir.api := api
70dir.jacc := jaccwabyt
71dir.common := common
72dir.fiddle := fiddle
73dir.tool := $(dir.top)/tool
74########################################################################
75# MAINTENANCE REMINDER: the output .js and .wasm files of emcc must be
76# in _this_ dir, rather than a subdir, or else parts of the generated
77# code get confused and cannot load property. Specifically, when X.js
78# loads X.wasm, whether or not X.js uses the correct path for X.wasm
79# depends on how it's loaded: an HTML script tag will resolve it
80# intuitively, whereas a Worker's call to importScripts() will not.
81# That's a fundamental incompatibility with how URL resolution in
82# JS happens between those two contexts. See:
83#
84# https://zzz.buzz/2017/03/14/relative-uris-in-web-development/
85#
86# We unfortunately have no way, from Worker-initiated code, to
87# automatically resolve the path from X.js to X.wasm.
88#
89# In case we ever find a solution to that which does not require
90# duplicating the X.js files only to swap out the path to X.wasm for
91# the loading-from-worker case...
92#
93# dir.dout = output dir for deliverables.
94dir.dout := $(dir.wasm)/jswasm
95# dir.tmp = output dir for intermediary build files, as opposed to
96# end-user deliverables.
97dir.tmp := $(dir.wasm)/bld
98#CLEAN_FILES += $(wildcard $(dir.dout)/*) $(wildcard $(dir.tmp)/*)
99ifeq (,$(wildcard $(dir.dout)))
100  dir._tmp := $(shell mkdir -p $(dir.dout))
101endif
102ifeq (,$(wildcard $(dir.tmp)))
103  dir._tmp := $(shell mkdir -p $(dir.tmp))
104endif
105
106cflags.common :=  -I. -I.. -I$(dir.top)
107CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~
108emcc_enable_bigint ?= 1
109sqlite3.c := $(dir.top)/sqlite3.c
110sqlite3.h := $(dir.top)/sqlite3.h
111SQLITE_OPT = \
112  -DSQLITE_ENABLE_FTS4 \
113  -DSQLITE_ENABLE_RTREE \
114  -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
115  -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \
116  -DSQLITE_ENABLE_STMTVTAB \
117  -DSQLITE_ENABLE_DBPAGE_VTAB \
118  -DSQLITE_ENABLE_DBSTAT_VTAB \
119  -DSQLITE_ENABLE_BYTECODE_VTAB \
120  -DSQLITE_ENABLE_OFFSET_SQL_FUNC \
121  -DSQLITE_OMIT_LOAD_EXTENSION \
122  -DSQLITE_OMIT_DEPRECATED \
123  -DSQLITE_OMIT_UTF16 \
124  -DSQLITE_OMIT_SHARED_CACHE \
125  -DSQLITE_OMIT_WAL \
126  -DSQLITE_THREADSAFE=0 \
127  -DSQLITE_TEMP_STORE=3 \
128  -DSQLITE_OS_KV_OPTIONAL=1 \
129  '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \
130  -DSQLITE_USE_URI=1 \
131  -DSQLITE_WASM_ENABLE_C_TESTS
132# ^^^ most flags are set in sqlite3-wasm.c but we need them
133# made explicit here for building speedtest1.c.
134
135ifneq (,$(filter release,$(MAKECMDGOALS)))
136emcc_opt ?= -Oz -flto
137else
138emcc_opt ?= -O0
139# ^^^^ build times for -O levels higher than 0 are painful at
140# dev-time.
141endif
142# When passing emcc_opt from the CLI, += and re-assignment have no
143# effect, so emcc_opt+=-g3 doesn't work. So...
144emcc_opt_full := $(emcc_opt) -g3
145# ^^^ ALWAYS use -g3. See below for why.
146#
147# ^^^ -flto improves runtime speed at -O0 considerably but doubles
148# build time.
149#
150# ^^^^ -O3, -Oz, -Os minify symbol names and there appears to be no
151# way around that except to use -g3, but -g3 causes the binary file
152# size to absolutely explode (approx. 5x larger). This minification
153# utterly breaks the resulting module, making it unsable except as
154# self-contained/self-referential-only code, as ALL of the exported
155# symbols get minified names.
156#
157# However, we have an option for using -Oz or -Os:
158#
159# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt
160# tools package (https://github.com/WebAssembly/wabt), to strip the
161# debugging symbols. That results in a small build with unmangled
162# symbol names. -Oz gives ever-so-slightly better compression than
163# -Os: not quite 1% in some completely unscientific tests. Runtime
164# speed for the unit tests is all over the place either way so it's
165# difficult to say whether -Os gives any speed benefit over -Oz.
166#
167# (Much later: -O2 consistently gives the best speeds.)
168########################################################################
169
170
171$(sqlite3.c) $(sqlite3.h):
172	$(MAKE) -C $(dir.top) sqlite3.c
173
174.PHONY: clean distclean
175clean:
176	-rm -f $(CLEAN_FILES)
177distclean: clean
178	-rm -f $(DISTCLEAN_FILES)
179
180ifeq (release,$(filter release,$(MAKECMDGOALS)))
181  ifeq (,$(wasm-strip))
182    $(error Cannot make release-quality binary because wasm-strip is not available. \
183            See notes in the warning above)
184  endif
185else
186  $(info Development build. Use '$(MAKE) release' for a smaller release build.)
187endif
188
189bin.version-info := $(dir.wasm)/bin.version-info
190# ^^^^ NOT in $(dir.tmp) because we need it to survive the cleanup
191# process for the dist build to work properly.
192$(bin.version-info): $(dir.wasm)/version-info.c $(sqlite3.h) $(MAKEFILE)
193	$(CC) -O0 -I$(dir.top) -o $@ $<
194DISTCLEAN_FILES += $(bin.version-info)
195
196bin.stripccomments := $(dir.tool)/stripccomments
197$(bin.stripccomments): $(bin.stripccomments).c $(MAKEFILE)
198	$(CC) -o $@ $<
199DISTCLEAN_FILES += $(bin.stripccomments)
200
201EXPORTED_FUNCTIONS.api.in := $(abspath $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api)
202EXPORTED_FUNCTIONS.api := $(dir.tmp)/EXPORTED_FUNCTIONS.api
203$(EXPORTED_FUNCTIONS.api): $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE)
204	cat $(EXPORTED_FUNCTIONS.api.in) > $@
205
206sqlite3-license-version.js := $(dir.tmp)/sqlite3-license-version.js
207sqlite3-license-version-header.js := $(dir.api)/sqlite3-license-version-header.js
208sqlite3-api-build-version.js := $(dir.tmp)/sqlite3-api-build-version.js
209# sqlite3-api.jses = the list of JS files which make up $(sqlite3-api.js), in
210# the order they need to be assembled.
211sqlite3-api.jses := $(sqlite3-license-version.js)
212sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.js
213sqlite3-api.jses += $(dir.common)/whwasmutil.js
214sqlite3-api.jses += $(dir.jacc)/jaccwabyt.js
215sqlite3-api.jses += $(dir.api)/sqlite3-api-glue.js
216sqlite3-api.jses += $(sqlite3-api-build-version.js)
217sqlite3-api.jses += $(dir.api)/sqlite3-api-oo1.js
218sqlite3-api.jses += $(dir.api)/sqlite3-api-worker1.js
219sqlite3-api.jses += $(dir.api)/sqlite3-api-opfs.js
220sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js
221
222# "External" API files which are part of our distribution
223# but not part of the sqlite3-api.js amalgamation.
224SOAP.js := $(dir.api)/sqlite3-opfs-async-proxy.js
225sqlite3-worker1.js := $(dir.api)/sqlite3-worker1.js
226sqlite3-worker1-promiser.js := $(dir.api)/sqlite3-worker1-promiser.js
227define CP_XAPI
228sqlite3-api.ext.jses += $$(dir.dout)/$$(notdir $(1))
229$$(dir.dout)/$$(notdir $(1)): $(1) $$(MAKEFILE)
230	cp $$< $$@
231endef
232$(foreach X,$(SOAP.js) $(sqlite3-worker1.js) $(sqlite3-worker1-promiser.js),\
233  $(eval $(call CP_XAPI,$(X))))
234all: $(sqlite3-api.ext.jses)
235
236sqlite3-api.js := $(dir.tmp)/sqlite3-api.js
237$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE)
238	@echo "Making $@..."
239	@for i in $(sqlite3-api.jses); do \
240		echo "/* BEGIN FILE: $$i */"; \
241		cat $$i; \
242		echo "/* END FILE: $$i */"; \
243	done > $@
244
245$(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE)
246	@echo "Making $@..."
247	@{ \
248  echo 'self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \
249	echo -n '  sqlite3.version = '; \
250  $(bin.version-info) --json; \
251  echo ';'; \
252	echo '});'; \
253  } > $@
254
255########################################################################
256# --post-js and --pre-js are emcc flags we use to append/prepend JS to
257# the generated emscripten module file.
258pre-js.js := $(dir.api)/pre-js.js
259post-js.js := $(dir.tmp)/post-js.js
260post-jses := \
261  $(dir.api)/post-js-header.js \
262  $(sqlite3-api.js) \
263  $(dir.api)/post-js-footer.js
264$(post-js.js): $(post-jses) $(MAKEFILE)
265	@echo "Making $@..."
266	@for i in $(post-jses); do \
267		echo "/* BEGIN FILE: $$i */"; \
268		cat $$i; \
269		echo "/* END FILE: $$i */"; \
270	done > $@
271extern-post-js.js := $(dir.api)/extern-post-js.js
272extern-pre-js.js := $(dir.api)/extern-pre-js.js
273pre-post-common.flags := \
274  --post-js=$(post-js.js) \
275  --extern-post-js=$(extern-post-js.js) \
276  --extern-pre-js=$(sqlite3-license-version.js)
277pre-post-jses.deps := $(post-js.js) \
278  $(extern-post-js.js) $(extern-pre-js.js) $(sqlite3-license-version.js)
279$(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js) $(MAKEFILE)
280	@echo "Making $@..."; { \
281    cat $(sqlite3-license-version-header.js); \
282    echo '/*'; \
283    echo '** This code was built from sqlite3 version...'; \
284    echo "** "; \
285    awk -e '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' \
286        -e '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \
287    echo '*/'; \
288   } > $@
289
290########################################################################
291# call-make-pre-js creates rules for pre-js-$(1).js. $1 = the base
292# name of the JS file on whose behalf this pre-js is for.
293define call-make-pre-js
294pre-post-$(1).flags ?=
295$$(dir.tmp)/pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE)
296	cp $$(pre-js.js) $$@
297	echo "Module[xInstantiateWasm].uri = '$(1).wasm';" >> $$@
298pre-post-$(1).deps := $$(pre-post-jses.deps) $$(dir.tmp)/pre-js-$(1).js
299pre-post-$(1).flags += --pre-js=$$(dir.tmp)/pre-js-$(1).js
300endef
301#$(error $(call call-make-pre-js,sqlite3-wasmfs))
302# /post-js and pre-js
303########################################################################
304
305########################################################################
306# emcc flags for .c/.o/.wasm/.js.
307emcc.flags :=
308#emcc.flags += -v # _very_ loud but also informative about what it's doing
309# -g3 is needed to keep -O2 and higher from creating broken JS via
310# minification.
311
312########################################################################
313# emcc flags for .c/.o.
314emcc.cflags :=
315emcc.cflags += -std=c99 -fPIC
316# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs.
317emcc.cflags += -I. -I$(dir.top)
318
319########################################################################
320# emcc flags specific to building the final .js/.wasm file...
321emcc.jsflags := -fPIC
322emcc.jsflags += --minify 0
323emcc.jsflags += --no-entry
324emcc.jsflags += -sMODULARIZE
325emcc.jsflags += -sSTRICT_JS
326emcc.jsflags += -sDYNAMIC_EXECUTION=0
327emcc.jsflags += -sNO_POLYFILL
328emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api)
329emcc.exportedRuntimeMethods := \
330    -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory
331    # FS ==> stdio/POSIX I/O proxies
332    # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
333emcc.jsflags += $(emcc.exportedRuntimeMethods)
334emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
335emcc.jsflags += -sIMPORTED_MEMORY
336emcc.environment := -sENVIRONMENT=web,worker
337emcc.jsflags += -sALLOW_MEMORY_GROWTH
338# emcc: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code
339#       slowly, see https://github.com/WebAssembly/design/issues/1271
340#       [-Wpthreads-mem-growth]
341emcc.jsflags += -sINITIAL_MEMORY=13107200
342#emcc.jsflags += -sINITIAL_MEMORY=64225280
343# ^^^^ 64MB is not enough for WASMFS/OPFS test runs using batch-runner.js
344emcc.jsflags += $(emcc.environment)
345#emcc.jsflags += -sTOTAL_STACK=4194304
346
347sqlite3.js.init-func := sqlite3InitModule
348# ^^^^ $(sqlite3.js.init-func) symbol name is hard-coded in
349# $(extern-post-js.js) as well as in numerous docs. If changed, it
350# needs to be globally modified in *.js and all related documentation.
351
352emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
353emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
354#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
355#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
356#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API
357#emcc.jsflags += -sABORTING_MALLOC
358emcc.jsflags += -sALLOW_TABLE_GROWTH
359# -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs
360emcc.jsflags += -Wno-limited-postlink-optimizations
361# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
362#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why
363# https://lld.llvm.org/WebAssembly.html
364emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0
365emcc.jsflags += -sLLD_REPORT_UNDEFINED
366#emcc.jsflags += --allow-undefined
367#emcc.jsflags += --import-undefined
368#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
369#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
370#emcc.jsflags += --unresolved-symbols=ignore-all
371emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
372# ^^^^ MEMORY64=1 fails to load, erroring with:
373#  invalid memory limits flags 0x5
374#    (enable via --experimental-wasm-memory64)
375#
376# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
377#
378#  new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n)
379#
380# because ptr is now a BigInt, so is invalid for passing to arguments
381# which have strict must-be-a-Number requirements.
382########################################################################
383
384
385########################################################################
386# -sSINGLE_FILE:
387# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
388# -sSINGLE_FILE=1 would be really nice but we have to build with -g
389# for -O2 and higher to work (else minification breaks the code) and
390# cannot wasm-strip the binary before it gets encoded into the JS
391# file. The result is that the generated JS file is, because of the -g
392# debugging info, _huge_.
393########################################################################
394
395sqlite3.js := $(dir.dout)/sqlite3.js
396sqlite3.wasm := $(dir.dout)/sqlite3.wasm
397sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
398# sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter
399# (predictably) results in a slightly faster binary, but we're close
400# enough to the target speed requirements that the 500ms makes a
401# difference. Thus we build all binaries against sqlite3-wasm.c
402# instead of building a shared copy of sqlite3-wasm.o.
403$(eval $(call call-make-pre-js,sqlite3))
404$(sqlite3.js):
405$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
406    $(EXPORTED_FUNCTIONS.api) \
407    $(pre-post-sqlite3.deps)
408	@echo "Building $@ ..."
409	$(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \
410    $(emcc.jsflags) $(pre-post-common.flags) $(pre-post-sqlite3.flags) \
411    $(cflags.common) $(SQLITE_OPT) $(sqlite3-wasm.c)
412	chmod -x $(sqlite3.wasm)
413	$(maybe-wasm-strip) $(sqlite3.wasm)
414	@ls -la $@ $(sqlite3.wasm)
415$(sqlite3.wasm): $(sqlite3.js)
416CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm)
417all: $(sqlite3.js)
418wasm: $(sqlite3.js)
419# End main Emscripten-based module build
420########################################################################
421
422########################################################################
423# batch-runner.js...
424dir.sql := sql
425speedtest1 := ../../speedtest1
426speedtest1.c := ../../test/speedtest1.c
427speedtest1.sql := $(dir.sql)/speedtest1.sql
428speedtest1.cliflags := --size 25 --big-transactions
429$(speedtest1):
430	$(MAKE) -C ../.. speedtest1
431$(speedtest1.sql): $(speedtest1) $(MAKEFILE)
432	$(speedtest1) $(speedtest1.cliflags) --script $@
433batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
434	bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
435	ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
436clean-batch:
437	rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
438# ^^^ we don't do this along with 'clean' because we clean/rebuild on
439# a regular basis with different -Ox flags and rebuilding the batch
440# pieces each time is an unnecessary time sink.
441batch: batch-runner.list
442all: batch
443# end batch-runner.js
444########################################################################
445# speedtest1.js...
446# speedtest1-common.eflags = emcc flags used by multiple builds of speedtest1
447# speedtest1.eflags = emcc flags used by main build of speedtest1
448speedtest1-common.eflags := $(emcc_opt_full)
449speedtest1.eflags :=
450speedtest1.eflags += -sENVIRONMENT=web
451speedtest1-common.eflags += -sINVOKE_RUN=0
452speedtest1-common.eflags += --no-entry
453#speedtest1-common.eflags += -flto
454speedtest1-common.eflags += -sABORTING_MALLOC
455speedtest1-common.eflags += -sINITIAL_MEMORY=128450560
456speedtest1-common.eflags += -sSTRICT_JS
457speedtest1-common.eflags += -sMODULARIZE
458speedtest1-common.eflags += -Wno-limited-postlink-optimizations
459EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
460speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
461speedtest1-common.eflags += $(emcc.exportedRuntimeMethods)
462speedtest1-common.eflags += -sALLOW_TABLE_GROWTH
463speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0
464speedtest1-common.eflags += --minify 0
465speedtest1-common.eflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
466speedtest1-common.eflags += -sWASM_BIGINT=$(emcc_enable_bigint)
467speedtest1-common.eflags += $(pre-post-common.flags)
468speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
469speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1
470# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
471# this error from emscripten:
472#
473# > native function `free` called after runtime exit (use
474# NO_EXIT_RUNTIME to keep it alive after main() exits))
475#
476# If it's 0 and it crashes, we get:
477#
478# > stdio streams had content in them that was not flushed. you should
479# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
480# when you printf etc.
481#
482# and pending output is not flushed because it didn't end with a
483# newline (by design). The lesser of the two evils seems to be
484# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
485# which runs speedtest1 multiple times.
486
487$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api)
488	@echo "Making $@ ..."
489	@{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@
490speedtest1.js := $(dir.dout)/speedtest1.js
491speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
492speedtest1.cflags := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM
493speedtest1.cses := $(speedtest1.c) $(sqlite3-wasm.c)
494$(eval $(call call-make-pre-js,speedtest1))
495$(speedtest1.js): $(MAKEFILE) $(speedtest1.cses) \
496    $(pre-post-speedtest1.deps) \
497    $(EXPORTED_FUNCTIONS.speedtest1)
498	@echo "Building $@ ..."
499	$(emcc.bin) \
500        $(speedtest1.eflags) $(speedtest1-common.eflags) $(speedtest1.cflags) \
501        $(pre-post-speedtest1.flags) \
502        $(SQLITE_OPT) \
503        $(speedtest1.exit-runtime0) \
504        -o $@ $(speedtest1.cses) -lm
505	$(maybe-wasm-strip) $(speedtest1.wasm)
506	ls -la $@ $(speedtest1.wasm)
507
508speedtest1: $(speedtest1.js)
509all: speedtest1
510CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
511# end speedtest1.js
512########################################################################
513
514########################################################################
515# fiddle_remote is the remote destination for the fiddle app. It
516# must be a [user@]HOST:/path for rsync.
517# Note that the target "should probably" contain a symlink of
518# index.html -> fiddle.html.
519fiddle_remote ?=
520ifeq (,$(fiddle_remote))
521ifneq (,$(wildcard /home/stephan))
522  fiddle_remote = wh:www/wh/sqlite3/.
523else ifneq (,$(wildcard /home/drh))
524  #fiddle_remote = if appropriate, add that user@host:/path here
525endif
526endif
527$(fiddle_files): default
528push-fiddle: $(fiddle_files)
529	@if [ x = "x$(fiddle_remote)" ]; then \
530		echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
531		exit 1; \
532	fi
533	rsync -va fiddle/ $(fiddle_remote)
534# end fiddle remote push
535########################################################################
536
537########################################################################
538# Convenience rules to rebuild with various -Ox levels. Much
539# experimentation shows -O2 to be the clear winner in terms of speed.
540# Note that build times with anything higher than -O0 are somewhat
541# painful.
542
543.PHONY: o0 o1 o2 o3 os oz
544o-xtra := -flto
545# ^^^^ -flto can have a considerably performance boost at -O0 but
546# doubles the build time and seems to have negligible effect on
547# higher optimization levels.
548o0: clean
549	$(MAKE) -e "emcc_opt=-O0"
550o1: clean
551	$(MAKE) -e "emcc_opt=-O1 $(o-xtra)"
552o2: clean
553	$(MAKE) -e "emcc_opt=-O2 $(o-xtra)"
554o3: clean
555	$(MAKE) -e "emcc_opt=-O3 $(o-xtra)"
556os: clean
557	@echo "WARNING: -Os can result in a build with mysteriously missing pieces!"
558	$(MAKE) -e "emcc_opt=-Os $(o-xtra)"
559oz: clean
560	$(MAKE) -e "emcc_opt=-Oz $(o-xtra)"
561
562########################################################################
563# Sub-makes...
564
565include fiddle.make
566
567ifneq (,$(filter wasmfs,$(MAKECMDGOALS)))
568# wasmfs build disabled 2022-10-19 per /chat
569# discussion. OPFS-over-wasmfs was initially a stopgap measure and a
570# convenient point of comparison for the OPFS sqlite3_vfs's
571# performance, but it currently doubles our deliverables for very
572# little, if any, benefit.
573#
574########################################################################
575# Some platforms do not support the WASMFS build. Raspberry Pi OS is one
576# of them. As such platforms are discovered, add their (uname -m) name
577# to PLATFORMS_WITH_NO_WASMFS to exclude the wasmfs build parts.
578PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here
579THIS_ARCH := $(shell /usr/bin/uname -m)
580ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS)))
581$(info This platform does not support the WASMFS build.)
582HAVE_WASMFS := 0
583else
584HAVE_WASMFS := 1
585include wasmfs.make
586endif
587endif
588# /wasmfs
589########################################################################
590
591########################################################################
592# Create deliverables:
593ifneq (,$(filter dist,$(MAKECMDGOALS)))
594include dist.make
595endif
596
597########################################################################
598# Push files to public wasm-testing.sqlite.org server
599wasm-testing.include = $(dir.dout) *.js *.html \
600    batch-runner.list $(dir.sql) $(dir.common) $(dir.fiddle) $(dir.jacc)
601wasm-testing.exclude = sql/speedtest1.sql
602wasm-testing.dir     = /jail/sites/wasm-testing
603wasm-testing.dest   ?= wasm-testing:$(wasm-testing.dir)
604# ---------------------^^^^^^^^^^^^ ssh alias
605push:
606	rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
607    $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
608    $(wasm-testing.include) $(wasm-testing.dest)
609	ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
610    echo "SSH failed: it's likely that stale content will be served via old gzip files."
611