xref: /sqlite-3.40.0/ext/wasm/GNUmakefile (revision 71de8e02)
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)
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
221sqlite3-api.js := $(dir.tmp)/sqlite3-api.js
222$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE)
223	@echo "Making $@..."
224	@for i in $(sqlite3-api.jses); do \
225		echo "/* BEGIN FILE: $$i */"; \
226		cat $$i; \
227		echo "/* END FILE: $$i */"; \
228	done > $@
229
230$(sqlite3-api-build-version.js): $(version-info) $(MAKEFILE)
231	@echo "Making $@..."
232	@{ \
233  echo 'self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \
234	echo -n '  sqlite3.version = '; \
235  $(version-info) --json; \
236  echo ';'; \
237	echo '});'; \
238  } > $@
239
240########################################################################
241# --post-js and --pre-js are emcc flags we use to append/prepend JS to
242# the generated emscripten module file.
243pre-js.js := $(dir.api)/pre-js.js
244post-js.js := $(dir.tmp)/post-js.js
245post-jses := \
246  $(dir.api)/post-js-header.js \
247  $(sqlite3-api.js) \
248  $(dir.api)/post-js-footer.js
249$(post-js.js): $(post-jses) $(MAKEFILE)
250	@echo "Making $@..."
251	@for i in $(post-jses); do \
252		echo "/* BEGIN FILE: $$i */"; \
253		cat $$i; \
254		echo "/* END FILE: $$i */"; \
255	done > $@
256extern-post-js.js := $(dir.api)/extern-post-js.js
257extern-pre-js.js := $(dir.api)/extern-pre-js.js
258pre-post-common.flags := \
259  --post-js=$(post-js.js) \
260  --extern-post-js=$(extern-post-js.js) \
261  --extern-pre-js=$(sqlite3-license-version.js)
262pre-post-jses.deps := $(post-js.js) \
263  $(extern-post-js.js) $(extern-pre-js.js) $(sqlite3-license-version.js)
264$(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js) $(MAKEFILE)
265	@echo "Making $@..."; { \
266    cat $(sqlite3-license-version-header.js); \
267    echo '/*'; \
268    echo '** This code was built from sqlite3 version...'; \
269    echo "** "; \
270    awk -e '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' \
271        -e '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \
272    echo '*/'; \
273   } > $@
274
275########################################################################
276# call-make-pre-js creates rules for pre-js-$(1).js. $1 = the base
277# name of the JS file on whose behalf this pre-js is for.
278define call-make-pre-js
279pre-post-$(1).flags ?=
280pre-js-$(1).js: $$(pre-js.js) $$(MAKEFILE)
281	cp $$(pre-js.js) $$@
282	echo "Module[xInstantiateWasm].uri = '$(1).wasm';" >> $$@
283CLEAN_FILES += pre-js-$(1).js
284pre-post-$(1).deps := $$(pre-post-jses.deps) pre-js-$(1).js
285pre-post-$(1).flags += --pre-js=pre-js-$(1).js
286endef
287#$(error $(call call-make-pre-js,sqlite3-wasmfs))
288# /post-js and pre-js
289########################################################################
290
291########################################################################
292# emcc flags for .c/.o/.wasm/.js.
293emcc.flags =
294#emcc.flags += -v # _very_ loud but also informative about what it's doing
295# -g is needed to keep -O2 and higher from creating broken JS via
296# minification.
297
298########################################################################
299# emcc flags for .c/.o.
300emcc.cflags :=
301emcc.cflags += -std=c99 -fPIC
302# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs.
303emcc.cflags += -I. -I$(dir.top)
304
305########################################################################
306# emcc flags specific to building the final .js/.wasm file...
307emcc.jsflags := -fPIC
308emcc.jsflags += --minify 0
309emcc.jsflags += --no-entry
310emcc.jsflags += -sMODULARIZE
311emcc.jsflags += -sSTRICT_JS
312emcc.jsflags += -sDYNAMIC_EXECUTION=0
313emcc.jsflags += -sNO_POLYFILL
314emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api)
315emcc.exportedRuntimeMethods := \
316    -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory
317    # FS ==> stdio/POSIX I/O proxies
318    # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
319emcc.jsflags += $(emcc.exportedRuntimeMethods)
320emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
321emcc.jsflags += -sIMPORTED_MEMORY
322emcc.environment := -sENVIRONMENT=web,worker
323emcc.jsflags += -sALLOW_MEMORY_GROWTH
324# emcc: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code
325#       slowly, see https://github.com/WebAssembly/design/issues/1271
326#       [-Wpthreads-mem-growth]
327emcc.jsflags += -sINITIAL_MEMORY=13107200
328#emcc.jsflags += -sINITIAL_MEMORY=64225280
329# ^^^^ 64MB is not enough for WASMFS/OPFS test runs using batch-runner.js
330emcc.jsflags += $(emcc.environment)
331#emcc.jsflags += -sTOTAL_STACK=4194304
332
333sqlite3.js.init-func := sqlite3InitModule
334# ^^^^ $(sqlite3.js.init-func) symbol name is hard-coded in $(extern-post-js.js)
335
336emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
337emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
338#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
339#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
340#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API
341#emcc.jsflags += -sABORTING_MALLOC
342emcc.jsflags += -sALLOW_TABLE_GROWTH
343emcc.jsflags += -Wno-limited-postlink-optimizations
344# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
345#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why
346# https://lld.llvm.org/WebAssembly.html
347emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0
348emcc.jsflags += -sLLD_REPORT_UNDEFINED
349#emcc.jsflags += --allow-undefined
350#emcc.jsflags += --import-undefined
351#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
352#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
353#emcc.jsflags += --unresolved-symbols=ignore-all
354emcc.jsflags += -sWASM_BIGINT=$(emcc_enable_bigint)
355# ^^^^ MEMORY64=1 fails to load, erroring with:
356#  invalid memory limits flags 0x5
357#    (enable via --experimental-wasm-memory64)
358#
359# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
360#
361#  new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n)
362#
363# because ptr is now a BigInt, so is invalid for passing to arguments
364# which have strict must-be-a-Number requirements.
365########################################################################
366
367
368########################################################################
369# -sSINGLE_FILE:
370# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
371# -sSINGLE_FILE=1 would be really nice but we have to build with -g
372# for -O2 and higher to work (else minification breaks the code) and
373# cannot wasm-strip the binary before it gets encoded into the JS
374# file. The result is that the generated JS file is, because of the -g
375# debugging info, _huge_.
376########################################################################
377
378sqlite3.js := $(dir.dout)/sqlite3.js
379sqlite3.wasm := $(dir.dout)/sqlite3.wasm
380sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
381# sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter
382# (predictably) results in a slightly faster binary, but we're close
383# enough to the target speed requirements that the 500ms makes a
384# difference. Thus we build all binaries against sqlite3-wasm.c
385# instead of building a shared copy of sqlite3-wasm.o.
386$(eval $(call call-make-pre-js,sqlite3))
387$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
388    $(EXPORTED_FUNCTIONS.api) \
389    $(pre-post-sqlite3.deps)
390	@echo "Building $@ ..."
391	$(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \
392    $(emcc.jsflags) $(pre-post-common.flags) $(pre-post-sqlite3.flags) \
393    $(cflags.common) $(SQLITE_OPT) $(sqlite3-wasm.c)
394	chmod -x $(sqlite3.wasm)
395	$(maybe-wasm-strip) $(sqlite3.wasm)
396	@ls -la $@ $(sqlite3.wasm)
397$(sqlite3.wasm): $(sqlite3.js)
398CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm)
399all: $(sqlite3.js)
400wasm: $(sqlite3.js)
401# End main Emscripten-based module build
402########################################################################
403
404########################################################################
405# batch-runner.js...
406dir.sql := sql
407speedtest1 := ../../speedtest1
408speedtest1.c := ../../test/speedtest1.c
409speedtest1.sql := $(dir.sql)/speedtest1.sql
410speedtest1.cliflags := --size 25 --big-transactions
411$(speedtest1):
412	$(MAKE) -C ../.. speedtest1
413$(speedtest1.sql): $(speedtest1) $(MAKEFILE)
414	$(speedtest1) $(speedtest1.cliflags) --script $@
415batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
416	bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
417	ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
418clean-batch:
419	rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
420# ^^^ we don't do this along with 'clean' because we clean/rebuild on
421# a regular basis with different -Ox flags and rebuilding the batch
422# pieces each time is an unnecessary time sink.
423batch: batch-runner.list
424all: batch
425# end batch-runner.js
426########################################################################
427# speedtest1.js...
428# speedtest1-common.eflags = emcc flags used by multiple builds of speedtest1
429# speedtest1.eflags = emcc flags used by main build of speedtest1
430speedtest1-common.eflags := $(emcc_opt_full)
431speedtest1.eflags :=
432speedtest1.eflags += -sENVIRONMENT=web
433speedtest1-common.eflags += -sINVOKE_RUN=0
434speedtest1-common.eflags += --no-entry
435#speedtest1-common.eflags += -flto
436speedtest1-common.eflags += -sABORTING_MALLOC
437speedtest1-common.eflags += -sINITIAL_MEMORY=128450560
438speedtest1-common.eflags += -sSTRICT_JS
439speedtest1-common.eflags += -sMODULARIZE
440speedtest1-common.eflags += -Wno-limited-postlink-optimizations
441EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
442speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
443speedtest1-common.eflags += $(emcc.exportedRuntimeMethods)
444speedtest1-common.eflags += -sALLOW_TABLE_GROWTH
445speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0
446speedtest1-common.eflags += --minify 0
447speedtest1-common.eflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
448speedtest1-common.eflags += -sWASM_BIGINT=$(emcc_enable_bigint)
449speedtest1-common.eflags += $(pre-post-common.flags)
450speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
451speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1
452# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
453# this error from emscripten:
454#
455# > native function `free` called after runtime exit (use
456# NO_EXIT_RUNTIME to keep it alive after main() exits))
457#
458# If it's 0 and it crashes, we get:
459#
460# > stdio streams had content in them that was not flushed. you should
461# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
462# when you printf etc.
463#
464# and pending output is not flushed because it didn't end with a
465# newline (by design). The lesser of the two evils seems to be
466# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
467# which runs speedtest1 multiple times.
468
469$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api)
470	@echo "Making $@ ..."
471	@{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@
472speedtest1.js := $(dir.dout)/speedtest1.js
473speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
474speedtest1.cflags := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM
475speedtest1.cses := $(speedtest1.c) $(sqlite3-wasm.c)
476$(eval $(call call-make-pre-js,speedtest1))
477$(speedtest1.js): $(MAKEFILE) $(speedtest1.cses) \
478    $(pre-post-speedtest1.deps) \
479    $(EXPORTED_FUNCTIONS.speedtest1)
480	@echo "Building $@ ..."
481	$(emcc.bin) \
482        $(speedtest1.eflags) $(speedtest1-common.eflags) $(speedtest1.cflags) \
483        $(pre-post-speedtest1.flags) \
484        $(SQLITE_OPT) \
485        $(speedtest1.exit-runtime0) \
486        -o $@ $(speedtest1.cses) -lm
487	$(maybe-wasm-strip) $(speedtest1.wasm)
488	ls -la $@ $(speedtest1.wasm)
489
490speedtest1: $(speedtest1.js)
491all: speedtest1
492CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
493# end speedtest1.js
494########################################################################
495
496########################################################################
497# fiddle_remote is the remote destination for the fiddle app. It
498# must be a [user@]HOST:/path for rsync.
499# Note that the target "should probably" contain a symlink of
500# index.html -> fiddle.html.
501fiddle_remote ?=
502ifeq (,$(fiddle_remote))
503ifneq (,$(wildcard /home/stephan))
504  fiddle_remote = wh:www/wh/sqlite3/.
505else ifneq (,$(wildcard /home/drh))
506  #fiddle_remote = if appropriate, add that user@host:/path here
507endif
508endif
509$(fiddle_files): default
510push-fiddle: $(fiddle_files)
511	@if [ x = "x$(fiddle_remote)" ]; then \
512		echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
513		exit 1; \
514	fi
515	rsync -va fiddle/ $(fiddle_remote)
516# end fiddle remote push
517########################################################################
518
519########################################################################
520# Convenience rules to rebuild with various -Ox levels. Much
521# experimentation shows -O2 to be the clear winner in terms of speed.
522# Note that build times with anything higher than -O0 are somewhat
523# painful.
524
525.PHONY: o0 o1 o2 o3 os oz
526o-xtra := -flto
527# ^^^^ -flto can have a considerably performance boost at -O0 but
528# doubles the build time and seems to have negligible effect on
529# higher optimization levels.
530o0: clean
531	$(MAKE) -e "emcc_opt=-O0 $(o-xtra)"
532o1: clean
533	$(MAKE) -e "emcc_opt=-O1 $(o-xtra)"
534o2: clean
535	$(MAKE) -e "emcc_opt=-O2 $(o-xtra)"
536o3: clean
537	$(MAKE) -e "emcc_opt=-O3 $(o-xtra)"
538os: clean
539	@echo "WARNING: -Os can result in a build with mysteriously missing pieces!"
540	$(MAKE) -e "emcc_opt=-Os $(o-xtra)"
541oz: clean
542	$(MAKE) -e "emcc_opt=-Oz $(o-xtra)"
543
544########################################################################
545# Sub-makes...
546
547include fiddle.make
548
549ifneq (,$(filter wasmfs,$(MAKECMDGOALS)))
550# wasmfs build disabled 2022-10-19 per /chat
551# discussion. OPFS-over-wasmfs was initially a stopgap measure and a
552# convenient point of comparison for the OPFS sqlite3_vfs's
553# performance, but it currently doubles our deliverables for very
554# little, if any, benefit.
555#
556########################################################################
557# Some platforms do not support the WASMFS build. Raspberry Pi OS is one
558# of them. As such platforms are discovered, add their (uname -m) name
559# to PLATFORMS_WITH_NO_WASMFS to exclude the wasmfs build parts.
560PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here
561THIS_ARCH := $(shell /usr/bin/uname -m)
562ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS)))
563$(info This platform does not support the WASMFS build.)
564HAVE_WASMFS := 0
565else
566HAVE_WASMFS := 1
567include wasmfs.make
568endif
569endif
570# /wasmfs
571########################################################################
572
573########################################################################
574# Create deliverables:
575ifneq (,$(filter dist,$(MAKECMDGOALS)))
576include dist.make
577endif
578
579########################################################################
580# Push files to public wasm-testing.sqlite.org server
581wasm-testing.include = *.wasm *.js *.html \
582    batch-runner.list sql common fiddle jaccwabyt
583wasm-testing.exclude = sql/speedtest1.sql
584wasm-testing.dir     = /jail/sites/wasm-testing
585wasm-testing.dest   ?= wasm-testing:$(wasm-testing.dir)
586# ---------------------^^^^^^^^^^^^ ssh alias
587push:
588	rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
589    $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
590    $(wasm-testing.include) $(wasm-testing.dest)
591	ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
592    echo "SSH failed: it's likely that stale content will be served via old gzip files."
593