xref: /sqlite-3.40.0/ext/wasm/GNUmakefile (revision cd0df83c)
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
188version-info := $(dir.wasm)/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$(version-info): $(dir.wasm)/version-info.c $(sqlite3.h) $(MAKEFILE)
192	$(CC) -O0 -I$(dir.top) -o $@ $<
193DISTCLEAN_FILES += $(version-info)
194
195stripccomments := $(dir.tool)/stripccomments
196$(stripccomments): $(stripccomments).c $(MAKEFILE)
197	$(CC) -o $@ $<
198DISTCLEAN_FILES += $(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): $(version-info) $(MAKEFILE)
245	@echo "Making $@..."
246	@{ \
247  echo 'self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \
248	echo -n '  sqlite3.version = '; \
249  $(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 ?=
294pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE)
295	cp $$(pre-js.js) $$@
296	echo "Module[xInstantiateWasm].uri = '$(1).wasm';" >> $$@
297CLEAN_FILES += pre-js-$(1).js
298pre-post-$(1).deps := $$(pre-post-jses.deps) pre-js-$(1).js
299pre-post-$(1).flags += --pre-js=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# -g 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 $(extern-post-js.js)
349
350emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
351emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
352#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
353#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
354#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API
355#emcc.jsflags += -sABORTING_MALLOC
356emcc.jsflags += -sALLOW_TABLE_GROWTH
357emcc.jsflags += -Wno-limited-postlink-optimizations
358# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
359#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why
360# https://lld.llvm.org/WebAssembly.html
361emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0
362emcc.jsflags += -sLLD_REPORT_UNDEFINED
363#emcc.jsflags += --allow-undefined
364#emcc.jsflags += --import-undefined
365#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
366#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
367#emcc.jsflags += --unresolved-symbols=ignore-all
368emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
369# ^^^^ MEMORY64=1 fails to load, erroring with:
370#  invalid memory limits flags 0x5
371#    (enable via --experimental-wasm-memory64)
372#
373# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
374#
375#  new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n)
376#
377# because ptr is now a BigInt, so is invalid for passing to arguments
378# which have strict must-be-a-Number requirements.
379########################################################################
380
381
382########################################################################
383# -sSINGLE_FILE:
384# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
385# -sSINGLE_FILE=1 would be really nice but we have to build with -g
386# for -O2 and higher to work (else minification breaks the code) and
387# cannot wasm-strip the binary before it gets encoded into the JS
388# file. The result is that the generated JS file is, because of the -g
389# debugging info, _huge_.
390########################################################################
391
392sqlite3.js := $(dir.dout)/sqlite3.js
393sqlite3.wasm := $(dir.dout)/sqlite3.wasm
394sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
395# sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter
396# (predictably) results in a slightly faster binary, but we're close
397# enough to the target speed requirements that the 500ms makes a
398# difference. Thus we build all binaries against sqlite3-wasm.c
399# instead of building a shared copy of sqlite3-wasm.o.
400$(eval $(call call-make-pre-js,sqlite3))
401$(sqlite3.js):
402$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
403    $(EXPORTED_FUNCTIONS.api) \
404    $(pre-post-sqlite3.deps)
405	@echo "Building $@ ..."
406	$(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \
407    $(emcc.jsflags) $(pre-post-common.flags) $(pre-post-sqlite3.flags) \
408    $(cflags.common) $(SQLITE_OPT) $(sqlite3-wasm.c)
409	chmod -x $(sqlite3.wasm)
410	$(maybe-wasm-strip) $(sqlite3.wasm)
411	@ls -la $@ $(sqlite3.wasm)
412$(sqlite3.wasm): $(sqlite3.js)
413CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm)
414all: $(sqlite3.js)
415wasm: $(sqlite3.js)
416# End main Emscripten-based module build
417########################################################################
418
419########################################################################
420# batch-runner.js...
421dir.sql := sql
422speedtest1 := ../../speedtest1
423speedtest1.c := ../../test/speedtest1.c
424speedtest1.sql := $(dir.sql)/speedtest1.sql
425speedtest1.cliflags := --size 25 --big-transactions
426$(speedtest1):
427	$(MAKE) -C ../.. speedtest1
428$(speedtest1.sql): $(speedtest1) $(MAKEFILE)
429	$(speedtest1) $(speedtest1.cliflags) --script $@
430batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
431	bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
432	ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
433clean-batch:
434	rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
435# ^^^ we don't do this along with 'clean' because we clean/rebuild on
436# a regular basis with different -Ox flags and rebuilding the batch
437# pieces each time is an unnecessary time sink.
438batch: batch-runner.list
439all: batch
440# end batch-runner.js
441########################################################################
442# speedtest1.js...
443# speedtest1-common.eflags = emcc flags used by multiple builds of speedtest1
444# speedtest1.eflags = emcc flags used by main build of speedtest1
445speedtest1-common.eflags := $(emcc_opt_full)
446speedtest1.eflags :=
447speedtest1.eflags += -sENVIRONMENT=web
448speedtest1-common.eflags += -sINVOKE_RUN=0
449speedtest1-common.eflags += --no-entry
450#speedtest1-common.eflags += -flto
451speedtest1-common.eflags += -sABORTING_MALLOC
452speedtest1-common.eflags += -sINITIAL_MEMORY=128450560
453speedtest1-common.eflags += -sSTRICT_JS
454speedtest1-common.eflags += -sMODULARIZE
455speedtest1-common.eflags += -Wno-limited-postlink-optimizations
456EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
457speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
458speedtest1-common.eflags += $(emcc.exportedRuntimeMethods)
459speedtest1-common.eflags += -sALLOW_TABLE_GROWTH
460speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0
461speedtest1-common.eflags += --minify 0
462speedtest1-common.eflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
463speedtest1-common.eflags += -sWASM_BIGINT=$(emcc_enable_bigint)
464speedtest1-common.eflags += $(pre-post-common.flags)
465speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
466speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1
467# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
468# this error from emscripten:
469#
470# > native function `free` called after runtime exit (use
471# NO_EXIT_RUNTIME to keep it alive after main() exits))
472#
473# If it's 0 and it crashes, we get:
474#
475# > stdio streams had content in them that was not flushed. you should
476# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
477# when you printf etc.
478#
479# and pending output is not flushed because it didn't end with a
480# newline (by design). The lesser of the two evils seems to be
481# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
482# which runs speedtest1 multiple times.
483
484$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api)
485	@echo "Making $@ ..."
486	@{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@
487speedtest1.js := $(dir.dout)/speedtest1.js
488speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
489speedtest1.cflags := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM
490speedtest1.cses := $(speedtest1.c) $(sqlite3-wasm.c)
491$(eval $(call call-make-pre-js,speedtest1))
492$(speedtest1.js): $(MAKEFILE) $(speedtest1.cses) \
493    $(pre-post-speedtest1.deps) \
494    $(EXPORTED_FUNCTIONS.speedtest1)
495	@echo "Building $@ ..."
496	$(emcc.bin) \
497        $(speedtest1.eflags) $(speedtest1-common.eflags) $(speedtest1.cflags) \
498        $(pre-post-speedtest1.flags) \
499        $(SQLITE_OPT) \
500        $(speedtest1.exit-runtime0) \
501        -o $@ $(speedtest1.cses) -lm
502	$(maybe-wasm-strip) $(speedtest1.wasm)
503	ls -la $@ $(speedtest1.wasm)
504
505speedtest1: $(speedtest1.js)
506all: speedtest1
507CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
508# end speedtest1.js
509########################################################################
510
511########################################################################
512# fiddle_remote is the remote destination for the fiddle app. It
513# must be a [user@]HOST:/path for rsync.
514# Note that the target "should probably" contain a symlink of
515# index.html -> fiddle.html.
516fiddle_remote ?=
517ifeq (,$(fiddle_remote))
518ifneq (,$(wildcard /home/stephan))
519  fiddle_remote = wh:www/wh/sqlite3/.
520else ifneq (,$(wildcard /home/drh))
521  #fiddle_remote = if appropriate, add that user@host:/path here
522endif
523endif
524$(fiddle_files): default
525push-fiddle: $(fiddle_files)
526	@if [ x = "x$(fiddle_remote)" ]; then \
527		echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
528		exit 1; \
529	fi
530	rsync -va fiddle/ $(fiddle_remote)
531# end fiddle remote push
532########################################################################
533
534########################################################################
535# Convenience rules to rebuild with various -Ox levels. Much
536# experimentation shows -O2 to be the clear winner in terms of speed.
537# Note that build times with anything higher than -O0 are somewhat
538# painful.
539
540.PHONY: o0 o1 o2 o3 os oz
541o-xtra := -flto
542# ^^^^ -flto can have a considerably performance boost at -O0 but
543# doubles the build time and seems to have negligible effect on
544# higher optimization levels.
545o0: clean
546	$(MAKE) -e "emcc_opt=-O0 $(o-xtra)"
547o1: clean
548	$(MAKE) -e "emcc_opt=-O1 $(o-xtra)"
549o2: clean
550	$(MAKE) -e "emcc_opt=-O2 $(o-xtra)"
551o3: clean
552	$(MAKE) -e "emcc_opt=-O3 $(o-xtra)"
553os: clean
554	@echo "WARNING: -Os can result in a build with mysteriously missing pieces!"
555	$(MAKE) -e "emcc_opt=-Os $(o-xtra)"
556oz: clean
557	$(MAKE) -e "emcc_opt=-Oz $(o-xtra)"
558
559########################################################################
560# Sub-makes...
561
562include fiddle.make
563
564ifneq (,$(filter wasmfs,$(MAKECMDGOALS)))
565# wasmfs build disabled 2022-10-19 per /chat
566# discussion. OPFS-over-wasmfs was initially a stopgap measure and a
567# convenient point of comparison for the OPFS sqlite3_vfs's
568# performance, but it currently doubles our deliverables for very
569# little, if any, benefit.
570#
571########################################################################
572# Some platforms do not support the WASMFS build. Raspberry Pi OS is one
573# of them. As such platforms are discovered, add their (uname -m) name
574# to PLATFORMS_WITH_NO_WASMFS to exclude the wasmfs build parts.
575PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here
576THIS_ARCH := $(shell /usr/bin/uname -m)
577ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS)))
578$(info This platform does not support the WASMFS build.)
579HAVE_WASMFS := 0
580else
581HAVE_WASMFS := 1
582include wasmfs.make
583endif
584endif
585# /wasmfs
586########################################################################
587
588########################################################################
589# Create deliverables:
590ifneq (,$(filter dist,$(MAKECMDGOALS)))
591include dist.make
592endif
593
594########################################################################
595# Push files to public wasm-testing.sqlite.org server
596wasm-testing.include = *.wasm *.js *.html \
597    batch-runner.list sql common fiddle jaccwabyt
598wasm-testing.exclude = sql/speedtest1.sql
599wasm-testing.dir     = /jail/sites/wasm-testing
600wasm-testing.dest   ?= wasm-testing:$(wasm-testing.dir)
601# ---------------------^^^^^^^^^^^^ ssh alias
602push:
603	rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
604    $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
605    $(wasm-testing.include) $(wasm-testing.dest)
606	ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
607    echo "SSH failed: it's likely that stale content will be served via old gzip files."
608