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