xref: /sqlite-3.40.0/ext/wasm/GNUmakefile (revision 7d24ff29)
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
99CLEAN_FILES += $(dir.tmp)/* $(dir.dout)/*
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.WASM_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	@if [ sqlite3-wasmfs = $(1) ]; then \
299		echo "delete Module[xNameOfInstantiateWasm] /*for WASMFS build*/;"; \
300	elif [ sqlite3 != $(1) ]; then \
301		echo "Module[xNameOfInstantiateWasm].uri = '$(1).wasm';"; \
302	fi >> $$@
303pre-post-$(1).deps := $$(pre-post-jses.deps) $$(dir.tmp)/pre-js-$(1).js
304pre-post-$(1).flags += --pre-js=$$(dir.tmp)/pre-js-$(1).js
305endef
306#$(error $(call call-make-pre-js,sqlite3-wasmfs))
307# /post-js and pre-js
308########################################################################
309
310########################################################################
311# emcc flags for .c/.o/.wasm/.js.
312emcc.flags :=
313#emcc.flags += -v # _very_ loud but also informative about what it's doing
314# -g3 is needed to keep -O2 and higher from creating broken JS via
315# minification.
316
317########################################################################
318# emcc flags for .c/.o.
319emcc.cflags :=
320emcc.cflags += -std=c99 -fPIC
321# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs.
322emcc.cflags += -I. -I$(dir.top)
323
324########################################################################
325# emcc flags specific to building the final .js/.wasm file...
326emcc.jsflags := -fPIC
327emcc.jsflags += --minify 0
328emcc.jsflags += --no-entry
329emcc.jsflags += -sMODULARIZE
330emcc.jsflags += -sSTRICT_JS
331emcc.jsflags += -sDYNAMIC_EXECUTION=0
332emcc.jsflags += -sNO_POLYFILL
333emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api)
334emcc.exportedRuntimeMethods := \
335    -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory
336    # FS ==> stdio/POSIX I/O proxies
337    # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
338emcc.jsflags += $(emcc.exportedRuntimeMethods)
339emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
340emcc.jsflags += -sIMPORTED_MEMORY
341emcc.environment := -sENVIRONMENT=web,worker
342########################################################################
343# -sINITIAL_MEMORY: How much memory we need to start with is governed
344# at least in part by whether -sALLOW_MEMORY_GROWTH is enabled. If so,
345# we can start with less. If not, we need as much as we'll ever
346# possibly use (which, of course, we can't know for sure).  Note,
347# however, that speedtest1 shows that performance for even moderate
348# workloads MAY suffer considerably if we start small and have to grow
349# at runtime. e.g. OPFS-backed (speedtest1 --size 75) take MAY take X
350# time with 16mb+ memory and 3X time when starting with 8MB. However,
351# such test results are inconsistent due to browser internals which
352# are opaque to us.
353emcc.jsflags += -sALLOW_MEMORY_GROWTH
354emcc.INITIAL_MEMORY.128 := 13107200
355emcc.INITIAL_MEMORY.96  := 100663296
356emcc.INITIAL_MEMORY.64  := 64225280
357emcc.INITIAL_MEMORY.32  := 33554432
358emcc.INITIAL_MEMORY.16  := 16777216
359emcc.INITIAL_MEMORY.8   := 8388608
360emcc.INITIAL_MEMORY ?= 16
361ifeq (,$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY)))
362$(error emcc.INITIAL_MEMORY must be one of: 8, 16, 32, 64, 96, 128 (megabytes))
363endif
364emcc.jsflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
365# /INITIAL_MEMORY
366########################################################################
367
368emcc.jsflags += $(emcc.environment)
369#emcc.jsflags += -sTOTAL_STACK=4194304
370
371sqlite3.js.init-func := sqlite3InitModule
372# ^^^^ $(sqlite3.js.init-func) symbol name is hard-coded in
373# $(extern-post-js.js) as well as in numerous docs. If changed, it
374# needs to be globally modified in *.js and all related documentation.
375
376emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
377emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
378#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
379#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
380#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API
381#emcc.jsflags += -sABORTING_MALLOC
382emcc.jsflags += -sALLOW_TABLE_GROWTH
383# -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs
384emcc.jsflags += -Wno-limited-postlink-optimizations
385# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag.
386#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why
387# https://lld.llvm.org/WebAssembly.html
388emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0
389emcc.jsflags += -sLLD_REPORT_UNDEFINED
390#emcc.jsflags += --allow-undefined
391#emcc.jsflags += --import-undefined
392#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
393#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
394#emcc.jsflags += --unresolved-symbols=ignore-all
395emcc.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
396
397########################################################################
398# -sMEMORY64=1 fails to load, erroring with:
399#  invalid memory limits flags 0x5
400#    (enable via --experimental-wasm-memory64)
401#
402# ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
403#
404#  new Uint8Array(wasm.heap8u().buffer, ptr, n)
405#
406# because ptr is now a BigInt, so is invalid for passing to arguments
407# which have strict must-be-a-Number requirements.
408########################################################################
409
410
411########################################################################
412# -sSINGLE_FILE:
413# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704
414# -sSINGLE_FILE=1 would be really nice but we have to build with -g3
415# for -O2 and higher to work (else minification breaks the code) and
416# cannot wasm-strip the binary before it gets encoded into the JS
417# file. The result is that the generated JS file is, because of the -g3
418# debugging info, _huge_.
419########################################################################
420
421########################################################################
422# AN EXPERIMENT: undocumented Emscripten feature: if the target file
423# extension is "mjs", it defaults to ES6 module builds:
424# https://github.com/emscripten-core/emscripten/issues/14383
425ifeq (,$(filter esm,$(MAKECMDGOALS)))
426sqlite3.js.ext := js
427else
428esm.deps := $(filter-out esm,$(MAKECMDGOALS))
429esm: $(if $(esm.deps),$(esm.deps),all)
430sqlite3.js.ext := mjs
431endif
432# /esm
433########################################################################
434sqlite3.js := $(dir.dout)/sqlite3.$(sqlite3.js.ext)
435sqlite3.wasm := $(dir.dout)/sqlite3.wasm
436sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
437# sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter
438# (predictably) results in a slightly faster binary, but we're close
439# enough to the target speed requirements that the 500ms makes a
440# difference. Thus we build all binaries against sqlite3-wasm.c
441# instead of building a shared copy of sqlite3-wasm.o.
442$(eval $(call call-make-pre-js,sqlite3))
443$(sqlite3.js):
444$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
445    $(EXPORTED_FUNCTIONS.api) \
446    $(pre-post-sqlite3.deps)
447	@echo "Building $@ ..."
448	$(emcc.bin) -o $@ $(emcc_opt_full) $(emcc.flags) \
449    $(emcc.jsflags) $(pre-post-common.flags) $(pre-post-sqlite3.flags) \
450    $(cflags.common) $(SQLITE_OPT) $(sqlite3-wasm.c)
451	chmod -x $(sqlite3.wasm)
452	$(maybe-wasm-strip) $(sqlite3.wasm)
453	@ls -la $@ $(sqlite3.wasm)
454$(sqlite3.wasm): $(sqlite3.js)
455CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm)
456all: $(sqlite3.js)
457wasm: $(sqlite3.js)
458# End main Emscripten-based module build
459########################################################################
460
461########################################################################
462# batch-runner.js...
463dir.sql := sql
464speedtest1 := ../../speedtest1
465speedtest1.c := ../../test/speedtest1.c
466speedtest1.sql := $(dir.sql)/speedtest1.sql
467speedtest1.cliflags := --size 25 --big-transactions
468$(speedtest1):
469	$(MAKE) -C ../.. speedtest1
470$(speedtest1.sql): $(speedtest1) $(MAKEFILE)
471	$(speedtest1) $(speedtest1.cliflags) --script $@
472batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
473	bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
474	ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
475clean-batch:
476	rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
477# ^^^ we don't do this along with 'clean' because we clean/rebuild on
478# a regular basis with different -Ox flags and rebuilding the batch
479# pieces each time is an unnecessary time sink.
480batch: batch-runner.list
481all: batch
482# end batch-runner.js
483########################################################################
484# speedtest1.js...
485# speedtest1-common.eflags = emcc flags used by multiple builds of speedtest1
486# speedtest1.eflags = emcc flags used by main build of speedtest1
487speedtest1-common.eflags := $(emcc_opt_full)
488speedtest1.eflags :=
489speedtest1.eflags += -sENVIRONMENT=web
490speedtest1.eflags += -sALLOW_MEMORY_GROWTH
491speedtest1.eflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
492speedtest1-common.eflags += -sINVOKE_RUN=0
493speedtest1-common.eflags += --no-entry
494#speedtest1-common.eflags += -flto
495speedtest1-common.eflags += -sABORTING_MALLOC
496speedtest1-common.eflags += -sSTRICT_JS
497speedtest1-common.eflags += -sMODULARIZE
498speedtest1-common.eflags += -Wno-limited-postlink-optimizations
499EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
500speedtest1-common.eflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
501speedtest1-common.eflags += $(emcc.exportedRuntimeMethods)
502speedtest1-common.eflags += -sALLOW_TABLE_GROWTH
503speedtest1-common.eflags += -sDYNAMIC_EXECUTION=0
504speedtest1-common.eflags += --minify 0
505speedtest1-common.eflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
506speedtest1-common.eflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
507speedtest1-common.eflags += $(pre-post-common.flags)
508speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
509speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1
510# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
511# this error from emscripten:
512#
513# > native function `free` called after runtime exit (use
514# NO_EXIT_RUNTIME to keep it alive after main() exits))
515#
516# If it's 0 and it crashes, we get:
517#
518# > stdio streams had content in them that was not flushed. you should
519# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
520# when you printf etc.
521#
522# and pending output is not flushed because it didn't end with a
523# newline (by design). The lesser of the two evils seems to be
524# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
525# which runs speedtest1 multiple times.
526
527$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api)
528	@echo "Making $@ ..."
529	@{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@
530speedtest1.js := $(dir.dout)/speedtest1.js
531speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
532speedtest1.cflags := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM
533speedtest1.cses := $(speedtest1.c) $(sqlite3-wasm.c)
534$(eval $(call call-make-pre-js,speedtest1))
535$(speedtest1.js): $(MAKEFILE) $(speedtest1.cses) \
536    $(pre-post-speedtest1.deps) \
537    $(EXPORTED_FUNCTIONS.speedtest1)
538	@echo "Building $@ ..."
539	$(emcc.bin) \
540        $(speedtest1.eflags) $(speedtest1-common.eflags) $(speedtest1.cflags) \
541        $(pre-post-speedtest1.flags) \
542        $(SQLITE_OPT) \
543        $(speedtest1.exit-runtime0) \
544        -o $@ $(speedtest1.cses) -lm
545	$(maybe-wasm-strip) $(speedtest1.wasm)
546	ls -la $@ $(speedtest1.wasm)
547
548speedtest1: $(speedtest1.js)
549all: speedtest1
550CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
551# end speedtest1.js
552########################################################################
553
554########################################################################
555# Convenience rules to rebuild with various -Ox levels. Much
556# experimentation shows -O2 to be the clear winner in terms of speed.
557# Note that build times with anything higher than -O0 are somewhat
558# painful.
559
560.PHONY: o0 o1 o2 o3 os oz
561o-xtra := -flto
562# ^^^^ -flto can have a considerably performance boost at -O0 but
563# doubles the build time and seems to have negligible effect on
564# higher optimization levels.
565o0: clean
566	$(MAKE) -e "emcc_opt=-O0"
567o1: clean
568	$(MAKE) -e "emcc_opt=-O1 $(o-xtra)"
569o2: clean
570	$(MAKE) -e "emcc_opt=-O2 $(o-xtra)"
571o3: clean
572	$(MAKE) -e "emcc_opt=-O3 $(o-xtra)"
573os: clean
574	@echo "WARNING: -Os can result in a build with mysteriously missing pieces!"
575	$(MAKE) -e "emcc_opt=-Os $(o-xtra)"
576oz: clean
577	$(MAKE) -e "emcc_opt=-Oz $(o-xtra)"
578
579########################################################################
580# Sub-makes...
581
582include fiddle.make
583
584# Only add wasmfs if wasmfs.enable=1 or we're running (dist)clean
585wasmfs.enable ?= $(if $(filter %clean,$(MAKECMDGOALS)),1,0)
586ifeq (1,$(wasmfs.enable))
587# wasmfs build disabled 2022-10-19 per /chat discussion.
588# OPFS-over-wasmfs was initially a stopgap measure and a convenient
589# point of comparison for the OPFS sqlite3_vfs's performance, but it
590# currently doubles our deliverables and build maintenance burden for
591# little, if any, benefit.
592#
593########################################################################
594# Some platforms do not support the WASMFS build. Raspberry Pi OS is one
595# of them. As such platforms are discovered, add their (uname -m) name
596# to PLATFORMS_WITH_NO_WASMFS to exclude the wasmfs build parts.
597PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here
598THIS_ARCH := $(shell /usr/bin/uname -m)
599ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS)))
600$(info This platform does not support the WASMFS build.)
601HAVE_WASMFS := 0
602else
603HAVE_WASMFS := 1
604include wasmfs.make
605endif
606endif
607# /wasmfs
608########################################################################
609
610########################################################################
611# Create deliverables:
612ifneq (,$(filter dist,$(MAKECMDGOALS)))
613include dist.make
614endif
615
616########################################################################
617# Push files to public wasm-testing.sqlite.org server
618wasm-testing.include = $(dir.dout) *.js *.html \
619    batch-runner.list $(dir.sql) $(dir.common) $(dir.fiddle) $(dir.jacc)
620wasm-testing.exclude = sql/speedtest1.sql
621wasm-testing.dir     = /jail/sites/wasm-testing
622wasm-testing.dest   ?= wasm-testing:$(wasm-testing.dir)
623# ---------------------^^^^^^^^^^^^ ssh alias
624.PHONY: push-testing
625push-testing:
626	rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
627    $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
628    $(wasm-testing.include) $(wasm-testing.dest)
629	@echo "Updating gzipped copies..."; \
630		ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
631    echo "SSH failed: it's likely that stale content will be served via old gzip files."
632
633########################################################################
634# If we find a copy of the sqlite.org/wasm docs checked out, copy
635# certain files over to it, noting that some need automatable edits...
636WDOCS.home ?= ../../../wdoc
637.PHONY: update-docs
638ifneq (,$(wildcard $(WDOCS.home)/api-index.md))
639WDOCS.jswasm := $(WDOCS.home)/jswasm
640update-docs: $(bin.stripccomments) $(sqlite3.js) $(sqlite3.wasm)
641	@echo "Copying files to the /wasm docs. Be sure to use an -Oz build for this!"
642	cp $(sqlite3.wasm) $(WDOCS.jswasm)/.
643	$(bin.stripccomments) -k -k < $(sqlite3.js) \
644		| sed -e '/^[ \t]*$$/d' > $(WDOCS.jswasm)/sqlite3.js
645	cp demo-123.js demo-123.html demo-123-worker.html $(WDOCS.home)
646	sed -n -e '/EXTRACT_BEGIN/,/EXTRACT_END/p' \
647		module-symbols.html > $(WDOCS.home)/module-symbols.html
648else
649update-docs:
650	@echo "Cannot find wasm docs checkout."; \
651	echo "Pass WDOCS.home=/path/to/wasm/docs/checkout or edit this makefile to suit."; \
652	exit 127
653endif
654# end /wasm docs
655########################################################################
656