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