interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / GNUmakefile

1778 lines · 64920 bytes Raw Blame History
1#
2# This GNU makefile creates the canonical sqlite3 WASM builds. Plus some
3# others.
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 its 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
15# indicated by the target name. A clean rebuild is necessary for
16# all components to get 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# snapshot = like dist, but uses a zip file name which clearly
23# marks it as a prerelease/snapshot build.
24#
25# clean = clean up
26#
27# Required tools beyond those needed for the canonical builds:
28#
29# - Emscripten SDK: https://emscripten.org/docs/getting_started/downloads.html
30#
31# - The bash shell
32#
33# - GNU make, GNU sed, GNU awk, GNU grep (all in the $PATH and without
34# a "g" prefix like they have on some non-GNU systems)
35#
36# - wasm-strip for release builds: https://github.com/WebAssembly/wabt.
37# It will build without this but the .wasm files will be huge.
38#
39# - InfoZip for 'dist' zip file
40#
41default: all
42MAKEFILE = $(lastword $(MAKEFILE_LIST))
43CLEAN_FILES =
44DISTCLEAN_FILES = config.make
45MAKING_CLEAN = $(if $(filter %clean,$(MAKECMDGOALS)),1,0)
46
47#
48# dir.X = various directory names.
49#
50# dir.top = the top dir of the canonical build tree, where
51# sqlite3.[ch] live.
52#
53dir.top = ../..
54dir.wasm = $(patsubst %/,%,$(dir $(MAKEFILE)))
55dir.api = api
56dir.jacc = jaccwabyt
57dir.common = common
58dir.fiddle = fiddle
59dir.fiddle.debug = fiddle-debug
60dir.tool = $(dir.top)/tool
61# dir.dout = output dir for deliverables
62dir.dout = $(dir.wasm)/jswasm
63# dir.tmp = output dir for intermediary build files, as opposed to
64# end-user deliverables.
65dir.tmp = $(dir.wasm)/bld
66dir.wasmfs = $(dir.dout)
67
68#
69# Emoji for log messages.
70#
71emo.bug = ๐Ÿž
72emo.compile = โณ
73emo.roadblock = ๐Ÿšง
74emo.disk = ๐Ÿ’พ
75emo.done = โœ…
76emo.fire = ๐Ÿ”ฅ
77emo.folder = ๐Ÿ“
78emo.garbage = ๐Ÿ—‘
79emo.lock = ๐Ÿ”’
80emo.magic = ๐Ÿง™
81emo.megaphone = ๐Ÿ“ฃ
82emo.mute = ๐Ÿ”‡
83emo.edit = โœ๏ธ
84emo.stop = ๐Ÿ›‘
85emo.strip = ๐Ÿ’ˆ
86emo.test = ๐Ÿงช
87emo.tool = ๐Ÿ”จ
88emo.wasm-opt = ๐Ÿงผ
89emo.cleanup = ๐Ÿงผ
90# ๐Ÿ‘ท๐Ÿช„๐Ÿงฎ๐Ÿงซ๐Ÿงฝ๐Ÿฟโ›ฝ๐Ÿšง๐ŸŽฑ๐Ÿชš๐Ÿ†๐Ÿงผ
91
92#
93# Special-case builds for which we require certain pre-conditions
94# which, if not met, may cause warnings or fatal errors in the build.
95# This also affects the default optimization level flags. The fiddle
96# targets are in this list because they are used for generating
97# sqlite.org/fiddle.
98#
99OPTIMIZED_TARGETS = dist snapshot fiddle fiddle.debug
100
101ifeq (1,$(MAKING_CLEAN))
102 bin.wasm-strip = echo "not stripping"
103 bin.wasm-opt = irrelevant
104 bin.emcc = irrelevant
105 bin.bash = irrelevant
106 emcc.version = unknown
107else
108 # Include config.make and perform some bootstrapping...
109 ifeq (,$(wildcard ./config.make))
110 $(error Missing config.make. It gets generated by the configure script if the EMSDK is found)
111 endif
112 include ./config.make
113 ifeq (,$(bin.bash))
114 $(error Configure script did not find the bash shell)
115 endif
116 ifeq (,$(bin.emcc))
117 $(error Configure script did not find emcc)
118 endif
119 emcc.version = $(shell $(bin.emcc) --version | sed -n 1p | sed -e 's/^.* \([3-9][^ ]*\) .*$$/\1/;')
120 $(info using emcc version [$(emcc.version)])
121 ifeq (,$(bin.wasm-strip))
122 #
123 # We need wasm-strip for release builds (see below for why) but
124 # not strictly for non-release builds.
125 #
126 achtung = $(emo.fire)WARNING
127 $(info $(achtung): *******************************************************************)
128 $(info $(achtung): Builds using -Oz will minify WASM-exported names, breaking)
129 $(info $(achtung): _All The Things_. The workaround for that is to build)
130 $(info $(achtung): with -g3 (which explodes the file size) and then strip the debug)
131 $(info $(achtung): info after compilation, using wasm-strip, to shrink the wasm file.)
132 $(info $(achtung): wasm-strip was not found in the PATH so we cannot strip those.)
133 $(info $(achtung): If this build uses any optimization level higher than -O1 then)
134 $(info $(achtung): the ***resulting JS code WILL NOT BE USABLE***.)
135 $(info $(achtung): wasm-strip is part of the wabt package:)
136 $(info $(achtung): https://github.com/WebAssembly/wabt)
137 $(info $(achtung): on Ubuntu-like systems it can be installed with:)
138 $(info $(achtung): sudo apt install wabt)
139 $(info $(achtung): *******************************************************************)
140 ifneq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS)))
141 $(error Cannot make release-quality binary because wasm-strip is not available.)
142 endif
143 bin.wasm-strip = echo "not wasm-stripping"
144 endif
145 ifeq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS)))
146 $(info ==============================================================)
147 $(info == Development build. Make one of (dist, snapshot) for a)
148 $(info == smaller and faster release build.)
149 $(info ==============================================================)
150 endif
151endif
152# ^^^ end of are-we-MAKING_CLEAN
153
154#
155# Common vars and $(call)/$(eval)able utilities.
156#
157# The "b." prefix on some APIs is for "build". It was initially used
158# only for features specific to each distinct js/wasm build. That's no
159# longer the case, but the naming convention has stuck.
160#
161
162loud ?= 0
163ifeq (1,$(loud))
164 $(info $(emo.megaphone) Emitting loud build info. Pass loud=0 to disable it.)
165 b.cmd@ =
166 loud.if = 1
167else
168 $(info $(emo.mute) Eliding loud build info. Pass loud=1 to enable it.)
169 b.cmd@ = @
170 loud.if =
171endif
172
173#
174# logtag.X value for log context labeling. logtag.OTHERX can be
175# assigned to customize it for a given X. This tag is used by
176# b.call.X, b.eval.X, etc. for logging. There motivation for this is
177# adding a build-specific prefix to messages so that the output of
178# parallel builds is easier to sort through. We use emoji for the
179# prefixes because it's far easier for my eyes to sort through than
180# using only each build's name as the prefix.
181#
182# Each distinct build sets up its own logtag.BUILDNAME.
183#
184logtag.@ = [$@]
185logtag.filter = [$(emo.disk) $@]
186logtag.test = [$(emo.test) $@]
187logtag.cp = [$(emo.disk) $@]
188
189#
190# $(call b.echo,LOGTAG,msg)
191#
192b.echo = echo $(logtag.$(1)) $(2)
193
194#
195# $(call b.mkdir@)
196#
197# $1 = optional LOGTAG
198#
199b.mkdir@ = if [ ! -d $(dir $@) ]; then \
200 echo '[$(emo.folder)+] $(if $(1),$(logtag.$(1)),[$(dir $@)])'; \
201 mkdir -p $(dir $@) || exit; fi
202
203#
204# $(call b.cp,@,src,dest)
205#
206# $1 = logtag, $2 = src file(s). $3 = dest dir
207b.cp = $(call b.mkdir@); \
208 echo '$(logtag.$(1)) $(emo.disk) $(2) ==> $3'; \
209 cp -f -p $(2) $(3) || exit
210
211# D.common = flags to use for all $(bin.c-pp) invocations. These must
212# be passed before any input file names because c-pp's args are
213# processed in their given order.
214D.common = -@policy=error
215
216#
217# $(call b.c-pp.shcmd,LOGTAG,src,dest,-Dx=y...)
218#
219# Resolves to shell code to create $(3) from $(2) and $(4) using
220# $(bin.c-pp).
221#
222# $1 = build name/logtag
223# $2 = Input file(s)
224# $3 = Output file
225# $4 = optional $(bin.c-pp) flags
226define b.c-pp.shcmd
227$(call b.mkdir@); \
228$(call b.echo,$(1),$(emo.disk)$(emo.lock)[$(3)] $(4)); \
229rm -f $(3); \
230if [ x1 = x$(loud.if) ]; then set -x; fi; \
231$(bin.c-pp) -o $(3) $(D.common) $(4) $(2) || exit; \
232chmod -w $(3)
233endef
234
235#
236# $(eval $(call b.c-pp.target,LOGTAG,src,dest,-Dx=y...))
237#
238# Creates target $(3) using $(bin.c-pp) $(2) $(4).
239#
240# Args: as for $(b.c-pp.shcmd).
241define b.c-pp.target
242$(3): $$(MAKEFILE_LIST) $$(bin.c-pp) $(2)
243 @$$(call b.mkdir@)
244 @$$(call b.c-pp.shcmd,$(1),$(2),$(3),$(4))
245CLEAN_FILES += $(3)
246endef
247
248
249#
250# The various -D... values used by *.c-pp.js include:
251#
252# -Dtarget:es6-module: for all ESM module builds
253#
254# -Dtarget:node: for node.js builds
255#
256# -Dtarget:es6-module -Dtarget:es6-bundler-friendly: intended for
257# "bundler-friendly" ESM module build. These have some restrictions
258# on how URL() objects are constructed in some contexts: URLs which
259# refer to files which are part of this project must be referenced
260# as string literals so that bundlers' static-analysis tools can
261# find those files and include them in their bundles.
262#
263# -Dtarget:es6-module -Dtarget:node: is intended for use by node.js
264# for node.js, as opposed to by node.js on behalf of a
265# browser. Mixing -sENVIRONMENT=web and -sENVIRONMENT=node leads to
266# ambiguity and confusion on node's part, as it's unable to
267# reliably determine whether the target is a browser or node.
268#
269# To repeat: all node.js builds are 100% untested and unsupported.
270#
271# Most c-pp.D.X are set via $(bin.mkwb) and X is a build name.
272# Those make rules reference c-pp.D.64bit, so it should be defined in
273# advance.
274#
275c-pp.D.64bit = -Dbits64
276
277#
278# $(call b.strip-js-emcc-bindings)
279#
280# $1 = an optional log message prefix
281#
282# Our JS code installs bindings of each sqlite3_...() WASM export. The
283# generated Emscripten JS file does the same using its own framework,
284# but we don't use those results and can speed up lib init, and reduce
285# memory cost a bit, by stripping them out. Emscripten code-generation
286# changes can "break" this, causing this to be a no-op, but (probably)
287# the worst that can happen in that case is that it doesn't actually
288# strip anything, leading to slightly larger JS files.
289#
290# This is intended to be used in makefile targets which generate an
291# Emscripten module and where $@ is the module's .js/.mjs file.
292#
293# This is inherently fragile and has been broken by Emscripten updates
294# before.
295#
296ifeq (1,1)
297define b.strip-js-emcc-bindings
298echo "$(1) $(emo.garbage) Stripping export wrappers."; \
299sed -i -e '/var _sqlite3.*makeInvalidEarly.*;/d' \
300-e '/assert.*sqlite3.*missing.*;/d' \
301-e '/_sqlite.*createExportWrapper.*;/d' $@
302endef
303else
304b.strip-js-emcc-bindings = \
305 echo '$(1) $(emo.bug) (disabled because it breaks emsdk 4.0.16+)'
306endif
307
308#
309# Set up sqlite3.c and sqlite3.h...
310#
311# To build with SEE (https://sqlite.org/see), either put sqlite3-see.c
312# in $(dir.top) or pass sqlite3.c=/path/to/sqlite3-see.c to the
313# $(MAKE) invocation. Only encryption modules with no 3rd-party
314# dependencies will currently work here: AES256-OFB, AES128-OFB, and
315# AES128-CCM. Not coincidentally, those 3 modules are included in the
316# sqlite3-see.c bundle. However, distributing an SEE build of the WASM
317# on a public site is in violation of the SEE license because it
318# effectively provides a usable copy of the SEE build to all visitors.
319# Its use is permitted within the intranet of an SEE licensee, but use
320# on networks accessible to non-licensees is in violation.
321#
322# A custom sqlite3.c must not have any spaces in its name.
323# $(sqlite3.canonical.c) must point to the sqlite3.c in the sqlite3
324# canonical source tree, as that source file is required for certain
325# utility and test code.
326#
327sqlite3.canonical.c = $(dir.top)/sqlite3.c
328sqlite3.c ?= $(firstword $(wildcard $(dir.top)/sqlite3-see.c) $(sqlite3.canonical.c))
329sqlite3.h = $(dir $(sqlite3.c))sqlite3.h
330
331#
332# bin.version-info = binary to output various sqlite3 version info for
333# embedding in the JS files and in building the distribution zip file.
334# It must NOT be in $(dir.tmp) because we need it to survive the
335# cleanup process for the dist build to work properly.
336#
337bin.version-info = ./version-info
338$(bin.version-info): $(dir.tool)/version-info.c $(sqlite3.h) $(dir.top)/Makefile
339 $(CC) -o $@ -I$(dir $(sqlite3.h)) $(dir.tool)/version-info.c
340t-version-info: $(bin.version-info)
341DISTCLEAN_FILES += $(bin.version-info)
342
343#
344# bin.stripcomments is used for stripping C/C++-style comments from JS
345# files. The JS files contain large chunks of documentation which we
346# don't need for all builds. That app's -k flag is of particular
347# importance here, as it allows us to retain the opening comment
348# block(s), which contain the license header and version info.
349#
350bin.stripccomments = $(dir.tool)/stripccomments
351$(bin.stripccomments): $(bin.stripccomments).c $(MAKEFILE)
352 $(CC) -o $@ $<
353t-stripccomments: $(bin.stripccomments)
354DISTCLEAN_FILES += $(bin.stripccomments)
355
356ifeq (1,$(MAKING_CLEAN))
357 SQLITE_C_IS_SEE = 0
358else
359 ifeq (,$(shell grep sqlite3_activate_see $(sqlite3.c)))
360 SQLITE_C_IS_SEE = 0
361 else
362 SQLITE_C_IS_SEE = 1
363 D.common += -Denable-see
364 $(info $(emo.lock)$(emo.lock)$(emo.lock) This is an SEE build $(emo.lock)$(emo.lock)$(emo.lock))
365 endif
366endif
367
368#
369# barebones=1 disables all "extraneous" stuff from sqlite3-wasm.c, the
370# goal being to create a WASM file with only the core APIs.
371#
372ifeq (1,$(barebones))
373 wasm-bare-bones = 1
374 $(info ==============================================================)
375 $(info == This is a bare-bones build. It trades away features for)
376 $(info == a smaller .wasm file.)
377 $(info ==============================================================)
378else
379 wasm-bare-bones = 0
380endif
381# undefine barebones # relatively new gmake feature, not ubiquitous
382
383#
384# It's important that sqlite3.[ch] be built to completion before any
385# other parts of the build run, thus we use .NOTPARALLEL to disable
386# parallel build of that file and its dependants. However, that makes
387# the whole build non-parallelizable because everything has a dep on
388# sqlite3.h/c. The alternative is to force the user to run (make
389# sqlite3.c) from the top of the tree before running this build.
390#
391#.NOTPARALLEL: $(sqlite3.h)
392#
393$(sqlite3.h):
394 @echo "$(sqlite3.h) is out of date. "; \
395 echo "To avoid problems with parallel builds, we're exiting now. Please do:"; \
396 echo " $(MAKE) -C $(dir.top) sqlite3.c"; \
397 echo "and try again."; exit 1
398# $(MAKE) -C $(dir.top) sqlite3.c
399$(sqlite3.c): $(sqlite3.h)
400
401#
402# Common options for building sqlite3-wasm.c and speedtest1.c.
403# Explicit ENABLEs...
404#
405SQLITE_OPT.common = \
406 -DSQLITE_THREADSAFE=0 \
407 -DSQLITE_TEMP_STORE=2 \
408 -DSQLITE_ENABLE_MATH_FUNCTIONS \
409 -DSQLITE_OS_KV_OPTIONAL=1 \
410 '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \
411 -DSQLITE_USE_URI=1 \
412 -DSQLITE_C=$(sqlite3.c) \
413 -DSQLITE_OMIT_DEPRECATED \
414 -DSQLITE_OMIT_UTF16 \
415 -DSQLITE_OMIT_LOAD_EXTENSION \
416 -DSQLITE_OMIT_SHARED_CACHE
417# ^^^ These particular OMITs are hard-coded in sqlite3-wasm.c and
418# removing them from this list will serve only to break the speedtest1
419# builds.
420
421#
422# Currently always needed but TODO is paring tester1.c-pp.js down
423# to be able to run without this:
424#
425SQLITE_OPT.common += -DSQLITE_WASM_ENABLE_C_TESTS
426
427#
428# Extra flags for full-featured builds...
429#
430SQLITE_OPT.full-featured = \
431 -DSQLITE_ENABLE_BYTECODE_VTAB \
432 -DSQLITE_ENABLE_DBPAGE_VTAB \
433 -DSQLITE_ENABLE_DBSTAT_VTAB \
434 -DSQLITE_ENABLE_FTS5 \
435 -DSQLITE_ENABLE_MATH_FUNCTIONS \
436 -DSQLITE_ENABLE_OFFSET_SQL_FUNC \
437 -DSQLITE_ENABLE_PREUPDATE_HOOK \
438 -DSQLITE_ENABLE_RTREE \
439 -DSQLITE_ENABLE_SESSION \
440 -DSQLITE_ENABLE_STMTVTAB \
441 -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \
442 -DSQLITE_ENABLE_COLUMN_METADATA \
443 -DSQLITE_ENABLE_PERCENTILE=1
444
445
446ifeq (0,$(wasm-bare-bones))
447 # The so-called canonical build is full-featured:
448 SQLITE_OPT = \
449 $(SQLITE_OPT.common) \
450 $(SQLITE_OPT.full-featured)
451else
452 # The so-called bare-bones build is exactly that:
453 SQLITE_OPT = \
454 $(SQLITE_OPT.common) \
455 -DSQLITE_WASM_BARE_BONES
456 # SQLITE_WASM_BARE_BONES tells sqlite3-wasm.c to explicitly omit
457 # a bunch of stuff, in the interest of keeping the wasm file size
458 # down. As of this writing it equates to:
459 #
460 # -USQLITE_ENABLE_DBPAGE_VTAB
461 # -USQLITE_ENABLE_DBSTAT_VTAB
462 # -USQLITE_ENABLE_EXPLAIN_COMMENTS
463 # -USQLITE_ENABLE_FTS5
464 # -USQLITE_ENABLE_OFFSET_SQL_FUNC
465 # -USQLITE_ENABLE_PREUPDATE_HOOK
466 # -USQLITE_ENABLE_RTREE
467 # -USQLITE_ENABLE_SESSION
468 # -USQLITE_ENABLE_STMTVTAB
469 # -DSQLITE_OMIT_AUTHORIZATION
470 # -DSQLITE_OMIT_GET_TABLE
471 # -DSQLITE_OMIT_INCRBLOB
472 # -DSQLITE_OMIT_INTROSPECTION_PRAGMAS
473 # -DSQLITE_OMIT_JSON
474 # -DSQLITE_OMIT_PROGRESS_CALLBACK
475 # -DSQLITE_OMIT_WAL
476 #
477 # There are others we want here but which require explicit OMIT when
478 # creating their amalgamation, and that step is TODO:
479 #
480 # -DSQLITE_OMIT_EXPLAIN
481 # -DSQLITE_OMIT_TRIGGER
482 # -DSQLITE_OMIT_VIRTUALTABLE
483 # -DSQLITE_OMIT_WINDOWFUNC
484endif
485
486#
487#SQLITE_OPT += -DSQLITE_DEBUG
488# Enabling SQLITE_DEBUG will break sqlite3_wasm_vfs_create_file()
489# (and thus sqlite3_js_vfs_create_file()). Those functions are
490# deprecated and alternatives are in place, but this crash behavior
491# can be used to find errant uses of sqlite3_js_vfs_create_file()
492# in client code.
493#
494# The following flags are hard-coded into sqlite3-wasm.c and cannot be
495# modified via the build process:
496#
497# SQLITE_ENABLE_API_ARMOR
498# SQLITE_OMIT_LOAD_EXTENSION
499# SQLITE_OMIT_DEPRECATED
500# SQLITE_OMIT_UTF16
501# SQLITE_OMIT_SHARED_CACHE
502#
503
504#
505# Adding custom C code via sqlite3_wasm_extra_init.c:
506#
507# If the canonical build process finds the file
508# sqlite3_wasm_extra_init.c in the main wasm build directory, it
509# arranges to include that file in the build of sqlite3.wasm and
510# defines SQLITE_EXTRA_INIT_MUTEXED=sqlite3_wasm_extra_init.
511#
512# sqlite3_wasm_extra_init() must be a function with this signature:
513#
514# int sqlite3_wasm_extra_init(const char *)
515#
516# and the sqlite3 library will call it with an argument of NULL one
517# time during sqlite3_initialize(). If it returns non-0,
518# initialization of the library will fail.
519#
520# The filename can be overridden with:
521#
522# make sqlite3_wasm_extra_init.c=my_custom_stuff.c
523#
524# See example_extra_init.c for an example implementation.
525#
526sqlite3_wasm_extra_init.c ?= $(wildcard sqlite3_wasm_extra_init.c)
527cflags.wasm_extra_init =
528ifneq (,$(sqlite3_wasm_extra_init.c))
529 $(info Enabling SQLITE_EXTRA_INIT via $(sqlite3_wasm_extra_init.c).)
530 cflags.wasm_extra_init = -DSQLITE_WASM_EXTRA_INIT
531endif
532
533#
534# Actitivates (or not) a custom Module.instantiateWasm() override for
535# Emscripten. That override gives us more control over exactly which
536# WASM file is in use.
537#
538# If $(WASM_CUSTOM_INSTANTIATE) is 1 then mkwasmbuilds will add
539# -Dcustom-Module.instantiateWasm to some of the builds. This problem
540# this solves is that various JS builds usually result in different JS
541# but identical binary WASM files. A custom loader allows us to define
542# the WASM filename used by the JS, enabling us to consolidate
543# identical binaries by redirecting the JS to use a single
544# copy. Without this we would need to distribute all JS/WASM files in
545# pairs, and most of the WASM files in that set would be identical.
546#
547# Changing this may require a clean rebuild. It also might not work.
548#
549# A value of 1 is required for the base64.wasm=1 build, as this custom
550# instantiator is where we embed the base64-encoded WASM files.
551#
552# 2026-07: it's been deployed almost a year now and the only issues
553# with it were in npm-/node-specific builds, where we now
554# automatically disable it.
555WASM_CUSTOM_INSTANTIATE = 1
556
557ifeq (0,$(WASM_CUSTOM_INSTANTIATE))
558ifeq (1,$(base64.wasm))
559 $(error base64.wasm=1 is incompatible with WASM_CUSTOM_INSTANTIATE=0)
560endif
561endif
562
563#
564# $(bin.c-pp): a minimal text file preprocessor. Like C's but much
565# less so.
566#
567# Historical notes about preprocessing files in this project:
568#
569# - We first attempted to use gcc and/or clang to preprocess JS files
570# in the same way we would normally do C files, but C-specific quirks
571# of each makes that untennable.
572#
573# - We implemented c-pp-lite.c (the C-Minus Pre-processor) as a custom
574# generic/file-format-agnostic preprocessor to enable us to pack
575# code for different target builds into the same JS files. Most
576# notably, some ES6 module (a.k.a. ESM) features cannot legally be
577# referenced at all in non-ESM code, e.g. the "import" and "export"
578# keywords. This preprocessing step permits us to swap out sections
579# of code where necessary for ESM and non-ESM (a.k.a. vanilla JS)
580# require different implementations. The alternative to such
581# preprocessing, would be to have separate source files for ES6
582# builds, which would have a higher maintenance burden than
583# c-pp-lite.c seems likely to.
584#
585# c-pp-lite.c was written specifically for the sqlite project's
586# JavaScript builds but is maintained as a standalone project:
587# https://fossil.wanderinghorse.net/r/c-pp. It has since been vastly
588# extended, largely reimplemented, and has dropped the "lite" suffix.
589#
590# The SQLITE_... build flags used here have NO EFFECT on the JS/WASM
591# build. They are solely for use with $(bin.c-pp) itself.
592#
593# -D... flags which should be included in all invocations should be
594# appended to $(D.common).
595#
596bin.c-pp = ./c-pp
597$(bin.c-pp): libcmpp.c $(sqlite3.c) $(MAKEFILE)
598 $(CC) -O0 -o $@ libcmpp.c $(dir.top)/sqlite3.c -I$(dir.top) \
599 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_UTF16 \
600 -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_WAL -DSQLITE_THREADSAFE=0 \
601 -DSQLITE_TEMP_STORE=3 \
602 '-DCMPP_DEFAULT_DELIM="//#"' -DCMPP_MAIN -DCMPP_OMIT_D_MODULE \
603 -DCMPP_OMIT_D_PIPE
604DISTCLEAN_FILES += $(bin.c-pp)
605
606ifeq (0,$(MAKING_CLEAN))
607 # omit-api="feature names..." disables various features by name.
608 # Recognized names are:
609 #
610 # kvvfs oo1 opfs opfs-sahpool opfs-wl vtab worker1
611 #
612 # The name "OPFS" is shorthand for all of the OPFS VFSes. For-nodejs
613 # builds filter out OPFS from within the OPFS-using files so OPFS is
614 # implied for those builds.
615 #
616 # The name "ALL" is shorthand for all of the above names.
617 D.omit =
618 ifneq (,$(omit-api))
619 # Translate omit-api="X Y" into -Domit-X...
620 Xomit-api := $(subst ALL,kvvfs oo1 OPFS worker1,$(omit-api))
621 Xomit-api := $(subst OPFS,opfs opfs-sahpool opfs-wl,$(Xomit-api))
622 Xomit-api := $(sort $(Xomit-api))
623 #$(foreach X,$(Xomit-api),$(eval D.omit += -Domit-$(X)))
624 D.omit = $(patsubst %,-Domit-%,$(Xomit-api))
625 endif
626 ifneq (,$(D.omit))
627 D.common += $(D.omit)
628 achtung = $(emo.megaphone) WARNING $(emo.megaphone)
629 $(info $(achtung): *******************************************************************)
630 $(info $(achtung): Be sure to do 'make clean' before using custom omit-api flags.)
631 $(info $(achtung): Omit flags: $(D.omit))
632 $(info $(achtung): *******************************************************************)
633 endif
634endif
635
636#
637# cflags.common = C compiler flags for all builds
638cflags.common = -I. -I$(dir $(sqlite3.c)) -std=c99 -fPIC
639
640#
641# emcc.WASM_BIGINT = 1 for BigInt (C int64) support, else 0. The API
642# disables certain features if BigInt is not enabled and such builds
643# _are not tested_ on any regular basis. Update 2026-07: Browsers old
644# enough to not have BigInt probably won't otherwise run the WASM
645# build (and we've never had direct access to such a browser to test
646# on). i.e. non-BigInt builds are effectively unnecessary and can be
647# safely removed at some point.
648#
649# Emscripten 6.0.8 (2026-08-20) deprecates WASM_BIGINT, now behaving
650# as if WASM_BIGINT were always 1. WASM_BIGINT=1 has been its default
651# since 4.0.0 (2025-01-14). We have not tested WASM_BIGINT=0 builds
652# since the earliest days of ext/wasm's development, so this does not
653# really change anything for us but does give us justification to
654# strip out the parts of the JS code which try to adapt to no-BigInt
655# environments (but are untested in such).
656#
657emcc.WASM_BIGINT ?= 1
658# emcc 6.0.1 deprecates -sMEMORY64=1 in favor of --target=wasm64
659emcc.MEMORY64 ?= --target=wasm64
660#emcc.MEMORY64 ?= -sMEMORY64=1
661#
662# https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
663#
664# 64-bit build requires wasm-strip 1.0.36 (maybe 1.0.35, but not
665# 1.0.34) or will fail to strip with "tables may not be 64-bit".
666#
667
668#
669# emcc_opt = optimization-related flags. These are primarily used by
670# the various oX targets. build times for -O levels higher than 0 are
671# painful at dev-time.
672#
673# When running any of the $(OPTIMIZED_TARGETS) explicitly, e.g. for a
674# release distribution, use a higher optimization level. Experience
675# has shown -Oz to produce the smallest deliverables with only a
676# roughly 10% performance hit in the resulting WASM file compared to
677# -O2 (which consistently creates the fastest-running deliverables).
678# Build time suffers greatly compared to -O0, which is why -O0 is the
679# default.
680#
681ifeq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS)))
682 emcc_opt ?= -O0
683else
684 emcc_opt ?= -Oz
685endif
686
687#
688# When passing emcc_opt from the CLI, += and re-assignment have no
689# effect, so emcc_opt+=-g3 doesn't work. So...
690#
691emcc_opt_full = $(emcc_opt) -g3
692# ^^^ ALWAYS use -g3. See below for why.
693#
694# ^^^ -flto improves runtime speed at -O0 considerably but doubles
695# build time.
696#
697# ^^^^ (-O3, -Oz, -Os) all minify symbol names and there appears to be
698# no way around that except to use -g3, but -g3 causes the binary file
699# size to absolutely explode (approx. 5x larger). This minification
700# utterly breaks the resulting module, making it unsable except as
701# self-contained/self-referential-only code, as ALL of the exported
702# symbols get minified names.
703#
704# However, we have an option for using -Oz or -Os:
705#
706# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt
707# tools package (https://github.com/WebAssembly/wabt), to strip the
708# debugging symbols. That results in a small build with unmangled
709# symbol names. -Oz gives ever-so-slightly better compression than
710# -Os: not quite 1% in some completely unscientific tests. Runtime
711# speed for the unit tests is all over the place either way so it's
712# difficult to say whether -Os gives any speed benefit over -Oz.
713#
714# Much practice has demonstrated that -O2 consistently gives the best
715# runtime speeds, but not by a large enough factor to rule out use of
716# -Oz when smaller deliverable size is a priority.
717#
718
719#
720# EXPORTED_FUNCTIONS.* = files for use with Emscripten's
721# -sEXPORTED_FUNCTION flag.
722#
723EXPORTED_FUNCTIONS.api.in = $(dir.api)/EXPORTED_FUNCTIONS.c-pp
724EXPORTED_FUNCTIONS.api = $(dir.tmp)/EXPORTED_FUNCTIONS.api
725EXPORTED_FUNCTIONS.c-pp.flags =
726ifeq (1,$(wasm-bare-bones))
727 EXPORTED_FUNCTIONS.c-pp.flags += -Dbare-bones
728endif
729$(eval $(call b.c-pp.target,filter,\
730 $(EXPORTED_FUNCTIONS.api.in),\
731 $(EXPORTED_FUNCTIONS.api),\
732 $(EXPORTED_FUNCTIONS.c-pp.flags)))
733
734#
735# emcc flags for .c/.o/.wasm/.js.
736#
737emcc.flags =
738ifeq (1,$(emcc.verbose))
739emcc.flags += -v
740# -v is _very_ loud but also informative about what it's doing
741endif
742
743#
744# emcc flags for .c/.o.
745#
746emcc.cflags =
747emcc.cflags += -std=c99 -fPIC
748# -------------^^^^^^^^ we need c99 for $(sqlite3-wasm.c), primarily
749# for variadic macros and snprintf() to implement
750# sqlite3__wasm_enum_json().
751emcc.cflags += -I. -I$(dir.top)
752
753#
754# emcc flags specific to building .js/.wasm files...
755#
756emcc.jsflags = -fPIC
757#emcc.jsflags += -Wno-gcc-install-dir-libstdcxx
758#emcc is not passing ^^^ this on to clang
759emcc.jsflags += --no-entry
760# emcc 6.0.12+ emits ugly warnings if we turn on BIGINT,
761# despite the value being the same one it will use by
762# default: emcc.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
763emcc.jsflags += -sMODULARIZE
764emcc.jsflags += -sDYNAMIC_EXECUTION=0
765emcc.jsflags += -sNO_POLYFILL
766emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api)
767emcc.exportedRuntimeMethods = \
768 -sEXPORTED_RUNTIME_METHODS=wasmMemory
769# wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
770# Emscripten 4.0.7 (2025-04-15) stops exporting HEAP* by default.
771emcc.jsflags += $(emcc.exportedRuntimeMethods)
772emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
773emcc.jsflags += -sIMPORTED_MEMORY
774ifeq (,$(filter -O0,$(emcc_opt)))
775emcc.assert ?= 0
776else
777emcc.assert ?= 2
778endif
779emcc.jsflags += -sASSERTIONS=$(emcc.assert)
780emcc.jsflags += -sSTRICT_JS=0
781# STRICT_JS disabled due to:
782# https://github.com/emscripten-core/emscripten/issues/18610
783# TL;DR: does not work with MODULARIZE or EXPORT_ES6 as of version
784# 3.1.31. The fix for that in newer emcc's is to throw a built-time
785# error if STRICT_JS is used together with those options.
786
787# emcc.jsflags += -sSTRICT=1
788# -sSTRICT=1 Causes failures about unknown symbols which the build
789# tools should be installing, e.g. __syscall_geteuid32
790
791#
792# -sINITIAL_MEMORY: How much memory we need to start with is governed
793# at least in part by whether -sALLOW_MEMORY_GROWTH is enabled. If so,
794# we can start with less. If not, we need as much as we'll ever
795# possibly use (which, of course, we can't know for sure). speedtest1
796# shows that performance for even moderate workloads MAY suffer
797# considerably if we start small and have to grow at runtime.
798# e.g. OPFS-backed (speedtest1 --size 75) take MAY take X time with
799# 16mb+ memory and 3X time when starting with 8MB. However, such test
800# results are inconsistent due to browser internals which are opaque
801# to us.
802#
803# 2024-03-04: emsdk 3.1.55 replaces INITIAL_MEMORY with INITIAL_HEAP,
804# but also says (in its changelog): "Note that it is currently not
805# supported in all configurations (#21071)."
806# https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md
807#
808# 2025-09-25: it turns out that _this_ WASM's heap size is not
809# affected by Emscripten's in-memory virtual filesystem, so we don't
810# strictly need a lot of heap. Resizing the heap is slow, though, so
811# we want to start off with some room to grow.
812#
813emcc.jsflags += -sALLOW_MEMORY_GROWTH
814emcc.INITIAL_MEMORY.128 = 134217728
815emcc.INITIAL_MEMORY.96 = 100663296
816emcc.INITIAL_MEMORY.64 = 67108864
817emcc.INITIAL_MEMORY.32 = 33554432
818emcc.INITIAL_MEMORY.16 = 16777216
819emcc.INITIAL_MEMORY.8 = 8388608
820emcc.INITIAL_MEMORY.4 = 4194304
821emcc.INITIAL_MEMORY ?= 8
822ifeq (,$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY)))
823$(error emcc.INITIAL_MEMORY must be one of: 4, 8, 16, 32, 64, 96, 128 (megabytes))
824endif
825emcc.jsflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
826#
827# /INITIAL_MEMORY
828#
829
830emcc.jsflags += $(emcc.environment)
831emcc.jsflags += -sSTACK_SIZE=512KB
832# ^^^ ACHTUNG: emsdk 3.1.27 reduced the default stack size from 5MB to
833# a mere 64KB, which leads to silent memory corruption via the kvvfs
834# VFS, which requires twice that for its xRead() and xWrite() methods.
835# 2023-03: those methods have since been adapted to use a malloc()'d
836# buffer.
837
838#
839# $(sqlite3.js.init-func) is the name Emscripten assigns our exported
840# module init/load function. This symbol name is hard-coded in
841# $(extern-post-js.js) as well as in numerous docs.
842#
843# "sqlite3InitModule" is the symbol we document for client use, so
844# that's the symbol name which must be exported, whether it comes from
845# Emscripten or our own code in extern-post-js.js.
846#
847# That said... we can change $(sqlite3.js.init-func) as long as the
848# name "sqlite3InitModule" is the one which gets exposed via the
849# resulting JS files. That can be accomplished via
850# extern-post-js.js. However... using a temporary symbol name here
851# and then adding sqlite3InitModule() ourselves results in 2 global
852# symbols: we cannot "delete" the Emscripten-defined
853# $(sqlite3.js.init-func) from vanilla builds (as opposed to ESM
854# builds) because it's declared with "var".
855sqlite3.js.init-func = sqlite3InitModule
856emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
857emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
858#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
859#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
860#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. fiddle needs the FS API
861#emcc.jsflags += -sABORTING_MALLOC # only for experimentation
862emcc.jsflags += -sALLOW_TABLE_GROWTH
863# ^^^^ -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs
864emcc.jsflags += -Wno-limited-postlink-optimizations
865# ^^^^ emcc likes to warn when we have "limited optimizations" via the
866# -g3 flag.
867# emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why.
868
869# Re. undefined symbol handling, see: https://lld.llvm.org/WebAssembly.html
870emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=1
871emcc.jsflags += -sLLD_REPORT_UNDEFINED
872#emcc.jsflags += --allow-undefined
873#emcc.jsflags += --import-undefined
874#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
875#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
876#emcc.jsflags += --unresolved-symbols=ignore-all
877
878#
879# -sSINGLE_FILE:
880# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
881#
882# -sSINGLE_FILE=1 would be _really_ nice but we have to build with -g3
883# for -O2 and higher to work (else minification breaks the code) and
884# cannot wasm-strip the binary before it gets encoded into the JS
885# file. The result is that the generated JS file is, because of the
886# -g3 debugging info, _huge_.
887#
888
889#
890# b.call.patch-export-default is used by mkwasmbuilds.c. $1 is 1 if
891# the build mode needs this workaround (modes: esm, bundler-friendly,
892# node) and 0 if not (vanilla). $2 must be 0 for all builds except
893# sqlite3-wasmfs.mjs, in which case it must be 1. $(3) is an optional
894# log prefix, defaulting to $(logtag.@)
895#
896# Reminder for ESM builds: even if we use -sEXPORT_ES6=0, emcc _still_
897# adds:
898#
899# export default $(sqlite3.js.init-func);
900#
901# when building *.mjs, which is bad because we need to export an
902# overwritten version of that function and cannot "export default"
903# twice. Because of this, we have to sed *.mjs to remove the _first_
904# instance (only) of /^export default/.
905#
906# Upstream RFE:
907# https://github.com/emscripten-core/emscripten/issues/18237
908#
909# Maintenance reminder: Mac sed works differently than GNU sed, so we
910# use awk instead of sed for this.
911#
912define b.call.patch-export-default
913if [ x1 = x$(1) ]; then \
914 echo "$(if $(3),$(3),$(logtag.@)) $(emo.bug) Fragile" \
915 "workaround for emscripten/issues/18237." \
916 "See b.call.patch-export-default."; \
917 {\
918 awk '/^export default/ && !f{f=1; next} 1' $@ \
919 > $@.tmp && mv $@.tmp $@; \
920 } || exit; \
921 if [ x1 = x$(2) ]; then \
922 if ! grep -q '^export default' $@; then \
923 echo "Cannot find export default." 1>&2; \
924 exit 1; \
925 fi; \
926 fi; \
927fi
928endef
929
930#
931# $(sqlite3-license-version.js) contains the license header and
932# in-comment build version info.
933#
934# Maintenance reminder: there are awk binaries out there which do not
935# support -e SCRIPT.
936#
937sqlite3-license-version.js = $(dir.tmp)/sqlite3-license-version.js
938$(sqlite3-license-version.js): $(bin.version-info) \
939 $(dir.api)/sqlite3-license-version-header.js $(MAKEFILE)
940 @$(call b.mkdir@); echo '$(logtag.@) $(emo.disk)'; { \
941 cat $(dir.api)/sqlite3-license-version-header.js || exit $$?; \
942 echo '/* @preserve'; \
943 echo '** This code was built from sqlite3 version...'; \
944 echo '**'; \
945 awk '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' $(sqlite3.h); \
946 awk '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \
947 echo '**'; echo '** Emscripten SDK: $(emcc.version)'; \
948 echo '*/'; \
949 } > $@
950
951#
952# $(sqlite3-api-build-version.js) injects the build version info into
953# the bundle in JSON form.
954#
955sqlite3-api-build-version.js = $(dir.tmp)/sqlite3-api-build-version.js
956$(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE)
957 @echo '$(logtag.@) $(emo.disk)'; { \
958 $(call b.mkdir@); \
959 echo 'sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \
960 echo -n ' sqlite3.version = '; \
961 $(bin.version-info) --json; \
962 echo ';'; \
963 echo '});'; \
964 } > $@
965
966#
967# $(sqlite3-api.jses) = the list of JS files which make up
968# sqlite3-api.js, in the order they need to be assembled.
969sqlite3-api.jses = $(sqlite3-license-version.js)
970sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.c-pp.js
971sqlite3-api.jses += $(sqlite3-api-build-version.js)
972sqlite3-api.jses += $(dir.common)/whwasmutil.c-pp.js
973sqlite3-api.jses += $(dir.jacc)/jaccwabyt.c-pp.js
974sqlite3-api.jses += $(dir.api)/sqlite3-api-glue.c-pp.js
975sqlite3-api.jses += $(dir.api)/sqlite3-api-oo1.c-pp.js
976sqlite3-api.jses += $(dir.api)/sqlite3-api-worker1.c-pp.js
977sqlite3-api.jses += $(dir.api)/sqlite3-vfs-helper.c-pp.js
978ifeq (0,$(wasm-bare-bones))
979 sqlite3-api.jses += $(dir.api)/sqlite3-vtab-helper.c-pp.js
980endif
981sqlite3-api.jses += $(dir.api)/sqlite3-vfs-kvvfs.c-pp.js
982sqlite3-api.jses += $(dir.api)/opfs-common-shared.c-pp.js
983sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs.c-pp.js
984sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs-sahpool.c-pp.js
985sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs-wl.c-pp.js
986
987# Parallel builds can fail if $(sqlite3-license-version.js) is not
988# created early enough, so make all files in $(sqlite-api.jses) except
989# for $(sqlite3-license-version.js) depend on
990# $(sqlite3-license-version.js).
991deps.jses = $(filter-out $(sqlite3-license-version.js),$(sqlite3-api.jses))
992$(deps.jses): $(sqlite3-license-version.js)
993
994#
995# extern-post-js* and extern-pre-js* are files for use with
996# Emscripten's --extern-pre-js and --extern-post-js flags.
997#
998extern-pre-js.js = $(dir.api)/extern-pre-js.js
999extern-post-js.in.js = $(dir.api)/extern-post-js.c-pp.js
1000
1001#
1002# Emscripten flags for --[extern-][pre|post]-js=... for the
1003# various builds.
1004# pre-post-jses.*.deps = lists of dependencies for the
1005# --[extern-][pre/post]-js files.
1006#
1007pre-post-jses.common.deps = $(extern-pre-js.js) $(sqlite3-license-version.js)
1008
1009# --post-js and --pre-js are emcc flags we use to append/prepend JS to
1010# the generated emscripten module file. These rules set up the core
1011# pre/post files for use by the various builds. --pre-js is used to
1012# inject code which needs to run as part of the pre-WASM-load phase.
1013# --post-js injects code which runs after the WASM module is loaded
1014# and includes the entirety of the library plus some
1015# Emscripten-specific post-bootstrapping code.
1016pre-js.in.js = $(dir.api)/pre-js.c-pp.js
1017post-js.in.js = $(dir.tmp)/post-js.c-pp.js
1018post-jses.js = \
1019 $(dir.api)/post-js-header.js \
1020 $(sqlite3-api.jses) \
1021 $(dir.api)/post-js-footer.js
1022$(post-js.in.js): $(MKDIR.bld) $(post-jses.js) $(MAKEFILE)
1023 @$(call b.echo,@,$(emo.disk)); \
1024 for i in $(post-jses.js); do \
1025 echo "/* BEGIN FILE: $$i */"; \
1026 cat $$i || exit $$?; \
1027 echo "/* END FILE: $$i */"; \
1028 done > $@
1029
1030#
1031# speedtest1 decls needed before the $(bin.mkwb)-generated makefile
1032# is included.
1033#
1034bin.speedtest1 = ../../speedtest1
1035speedtest1.c = ../../test/speedtest1.c
1036speedtest1.c.in = $(speedtest1.c) $(sqlite3-wasm.c)
1037EXPORTED_FUNCTIONS.speedtest1 = $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
1038
1039#
1040# fiddle build flags
1041#
1042# Flags specifically for debug builds of fiddle. Performance suffers
1043# greatly in debug builds.
1044########################################################################
1045# shell.c and its build flags...
1046#
1047# We should ideally collect these from ../../configure and past
1048# them in ./config.make. The problem with that is that SHELL_OPT is
1049# generated at make-time, not configure-time.
1050ifneq (1,$(MAKING_CLEAN))
1051 make-np-0 = make -C $(dir.top) -n -p
1052 make-np-1 = sed -e 's/(TOP)/(dir.top)/g'
1053 # Extract SHELL_OPT and SHELL_DEP from the top-most makefile and import
1054 # them as vars here...
1055 $(eval $(shell $(make-np-0) | grep -e '^SHELL_OPT ' | $(make-np-1)))
1056 $(eval $(shell $(make-np-0) | grep -e '^SHELL_DEP ' | $(make-np-1)))
1057 # ^^^ can't do that in 1 invocation b/c newlines get stripped
1058 ifeq (,$(SHELL_OPT))
1059 $(error Could not parse SHELL_OPT from $(dir.top)/Makefile.)
1060 endif
1061 ifeq (,$(SHELL_DEP))
1062 $(error Could not parse SHELL_DEP from $(dir.top)/Makefile.)
1063 endif
1064$(dir.top)/shell.c: $(SHELL_DEP) $(dir.tool)/mkshellc.tcl $(sqlite3.c)
1065 $(MAKE) -C $(dir.top) shell.c
1066endif
1067# /shell.c
1068########################################################################
1069
1070#
1071# Fiddle-related decls we need before .wasmbuilds is included
1072#
1073
1074fiddle.c.in = $(dir.top)/shell.c $(sqlite3-wasm.c)
1075
1076EXPORTED_FUNCTIONS.fiddle = $(dir.tmp)/EXPORTED_FUNCTIONS.fiddle
1077$(EXPORTED_FUNCTIONS.fiddle): $(EXPORTED_FUNCTIONS.api.in) \
1078 $(MAKEFILE_LIST) $(bin.c-pp)
1079 @$(call b.mkdir@)
1080 @$(call b.c-pp.shcmd,fiddle,$(EXPORTED_FUNCTIONS.api.in),\
1081 $@,$(EXPORTED_FUNCTIONS.c-pp.flags) -Dfiddle)
1082 @echo $(logtag.@) $(emo.disk)
1083
1084
1085emcc.flags.fiddle = \
1086 $(emcc.cflags) $(emcc_opt_full) \
1087 --minify 0 \
1088 -sALLOW_TABLE_GROWTH \
1089 -sALLOW_MEMORY_GROWTH \
1090 -sASSERTIONS \
1091 -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.8) \
1092 -sABORTING_MALLOC \
1093 -sSTRICT_JS=0 \
1094 -sENVIRONMENT=web,worker \
1095 -sMODULARIZE \
1096 -sDYNAMIC_EXECUTION=0 \
1097 -sEXPORT_NAME=$(sqlite3.js.init-func) \
1098 -Wno-limited-postlink-optimizations \
1099 $(emcc.exportedRuntimeMethods),FS \
1100 -sEXPORTED_FUNCTIONS=@$(abspath $(EXPORTED_FUNCTIONS.fiddle)) \
1101 $(SQLITE_OPT.full-featured) \
1102 $(SQLITE_OPT.common) \
1103 $(SHELL_OPT) \
1104 -UHAVE_READLINE -UHAVE_EDITLINE -UHAVE_LINENOISE \
1105 -USQLITE_HAVE_ZLIB \
1106 -USQLITE_WASM_BARE_BONES \
1107 -DSQLITE_SHELL_FIDDLE
1108
1109emcc.flags.fiddle.debug = $(emcc.flags.fiddle) \
1110 -DSQLITE_DEBUG \
1111 -DSQLITE_ENABLE_SELECTTRACE \
1112 -DSQLITE_ENABLE_WHERETRACE
1113
1114clean: clean-fiddle
1115clean-fiddle:
1116 rm -f $(dir.fiddle)/fiddle-module.js \
1117 $(dir.fiddle)/*.wasm \
1118 $(dir.fiddle)/sqlite3-opfs-*.js \
1119 $(dir.fiddle)/*.gz \
1120 $(dir.fiddle)/index.html \
1121 $(EXPORTED_FUNCTIONS.fiddle)
1122 rm -fr $(dir.fiddle-debug)
1123distclean: distclean-fiddle
1124distclean-fiddle:
1125 rm -fr $(dir.fiddle)/jqterm
1126
1127#
1128# WASMFS build - unsupported and untested. We used WASMFS
1129# to jumpstart development early on, but it has always been
1130# a moving target, in that Emscripten updates have broken
1131# our build often enough that we no longer actively support it.
1132# It's interesting to keep around, though.
1133#
1134# Only add wasmfs if wasmfs.enable=1 or we're running (dist)clean
1135#
1136ifneq (,$(filter wasmfs b-wasmfs for-testing,$(MAKECMDGOALS)))
1137wasmfs.enable ?= 1
1138else
1139# Unconditionally enable wasmfs for [dist]clean so that the wasmfs
1140# sub-make can clean up.
1141wasmfs.enable ?= $(MAKING_CLEAN)
1142endif
1143cflags.wasmfs = -DSQLITE_ENABLE_WASMFS
1144# end wasmfs (the rest is in mkwasmbuilds.c)
1145#
1146
1147#
1148#
1149#
1150sqlite3-wasm.c = $(dir.api)/sqlite3-wasm.c
1151# List of input files for compiling $(sqlite3-wasm.c). That file
1152# #include's sqlite3.c directly, so it's implicitly includes here.
1153sqlite3-wasm.c.in = $(sqlite3-wasm.c) $(sqlite3_wasm_extra_init.c)
1154
1155#
1156# $(bin.mkwb) is used for generating much of the makefile code for the
1157# various wasm builds. It used to be generated in this makefile via a
1158# difficult-to-read/maintain block of $(eval)'d code. Attempts were
1159# made to generate it from tcl and bash (shell) but having to escape
1160# the $ references in those languages made it just as illegible as the
1161# native makefile code. Somewhat surprisingly, moving that code
1162# generation to C makes it slightly less illegible than the previous 3
1163# options.
1164#
1165# Maintenance notes:
1166#
1167# - Ordering of this block within this file is fragile. The generated
1168# makefile sets up many vars which are useful for the other targets.
1169#
1170# - Vars which are used by $(bin.mkwb) in dependency lists and such
1171# need to be defined before this is included. Those used in recipies
1172# may be defined after this step.
1173#
1174bin.mkwb = ./mkwasmbuilds
1175ifneq (1,$(MAKING_CLEAN))
1176$(bin.mkwb): $(bin.mkwb).c $(MAKEFILE)
1177 $(CC) -O0 -g -std=c99 -o $@ $< -DWASM_CUSTOM_INSTANTIATE=$(WASM_CUSTOM_INSTANTIATE)
1178
1179.wasmbuilds.make: $(bin.mkwb)
1180 @rm -f $@
1181 $(bin.mkwb) > $@
1182 @chmod -w $@
1183-include .wasmbuilds.make
1184endif
1185CLEAN_FILES += .wasmbuilds.make $(bin.mkwb)
1186
1187#
1188# $(sqlite3.ext.js) = API-related files which are standalone
1189# files, not part of the amalgamation. This list holds
1190# the name of each such _output_ file.
1191#
1192sqlite3.ext.js =
1193
1194########################################################################
1195# We need separate copies of certain supplementary JS files for the
1196# bundler-friendly build. Concretely, any supplemental JS files which
1197# themselves use importScripts() or Workers or URL() constructors
1198# which refer to other in-tree (m)JS files require a bundler-friendly
1199# copy. Bundler-friendly builds replace certain references to string
1200# vars/expressions with string literals, as bundler tools are static
1201# code analyzers and cannot cope with the former.
1202
1203#
1204# sqlite3-worker1*.*
1205# TODO: 64-bit
1206#
1207define gen-worker1
1208# $1 = X.ext part of sqlite3-worker1X.ext
1209# $2 = $(c-pp.D.NAME)
1210$(call b.c-pp.target,filter,$$(dir.api)/sqlite3-worker1.c-pp.js,\
1211 $$(dir.dout)/sqlite3-worker1$(1),$(2))
1212sqlite3.ext.js += $$(dir.dout)/sqlite3-worker1$(1)
1213all: $$(dir.dout)/sqlite3-worker1$(1)
1214endef
1215
1216$(eval $(call gen-worker1,.js,$(c-pp.D.vanilla)))
1217$(eval $(call gen-worker1,.mjs,$(c-pp.D.esm)))
1218$(eval $(call gen-worker1,-bundler-friendly.mjs,$(c-pp.D.bundler)))
1219
1220#
1221# sqlite3-worker1-promiser*.*
1222#
1223define gen-promiser
1224# $1 = X.ext part of sqlite3-worker1-promiserX.ext
1225# $2 = $(c-pp.D.NAME)
1226$(call b.c-pp.target,filter,$$(dir.api)/sqlite3-worker1-promiser.c-pp.js,\
1227 $$(dir.dout)/sqlite3-worker1-promiser$(1),$(2))
1228sqlite3.ext.js += $$(dir.dout)/sqlite3-worker1-promiser$(1)
1229all: $$(dir.dout)/sqlite3-worker1-promiser$(1)
1230endef
1231
1232$(eval $(call gen-promiser,.js,$(c-pp.D.vanilla)))
1233$(eval $(call gen-promiser,.mjs,$(c-pp.D.esm)))
1234$(eval $(call gen-promiser,-bundler-friendly.mjs,$(c-pp.D.bundler)))
1235
1236#
1237# demo1-worker-*.*:
1238# $1 = .js or .mjs
1239# $2 = .html or -esm.html
1240# $3 = -D... flags for $(bin.c-pp)
1241#
1242define gen-dwp
1243$(call b.c-pp.target,test,demo-worker1-promiser.c-pp.js,demo-worker1-promiser$(1),$(3))
1244$(call b.c-pp.target,test,demo-worker1-promiser.c-pp.html,demo-worker1-promiser$(2),$(3))
1245demos: demo-worker1-promiser$(1) demo-worker1-promiser$(2)
1246endef
1247$(eval $(call gen-dwp,.js,.html,$(c-pp.D.vanilla)))
1248$(eval $(call gen-dwp,.mjs,-esm.html,$(c-pp.D.esm)))
1249all: demos
1250# End worker/promiser generation
1251#######################################################################
1252
1253#
1254# "SOAP" is not part of the amalgamation but gets copied into the
1255# build output folder and into each of the fiddle builds.
1256#
1257sqlite3.ext.js += $(dir.dout)/sqlite3-opfs-async-proxy.js
1258$(eval $(call b.c-pp.target,soap,\
1259 $(dir.api)/sqlite3-opfs-async-proxy.c-pp.js,\
1260 $(dir.dout)/sqlite3-opfs-async-proxy.js))
1261
1262#
1263# Add a dep of $(sqlite3.ext.js) on every individual build's JS file.
1264# The primary purpose of this is to force them to be copied early in
1265# the build process, which is sometimes a time-saver during
1266# development, allowing the developer to reload a test page while
1267# other parts of the build are still running. Another reason is that
1268# we don't otherwise have a great place to attach them such that
1269# they're always copied when we need them.
1270#
1271# The var $(out.$(B).js) comes from $(bin.mkwb) and $(B) is the name
1272# of a build set up by that tool, e.g. b-vanilla or b-esm64.
1273#
1274$(foreach B,$(b.names),$(eval $(out.$(B).js): $(sqlite3.ext.js)))
1275
1276#
1277# b-all: builds all available js/wasm builds.
1278#
1279$(foreach B,$(b.names),$(eval b-all: $(out.$(B).js)))
1280
1281#$(foreach B,$(b.names),$(eval pre: $(pre-js.$(B).js)))
1282$(foreach B,$(b.names),$(eval post: $(post-js.$(B).js)))
1283
1284#
1285# speedtest1 is our primary benchmarking tool.
1286#
1287# emcc.speedtest1.common = emcc flags used by multiple builds of speedtest1
1288# emcc.speedtest1 = emcc flags used by the main build of speedtest1
1289#
1290# These flags get applied via $(bin.mkwb).
1291emcc.speedtest1.common = $(emcc_opt_full)
1292emcc.speedtest1 = -I. -I$(dir $(sqlite3.canonical.c))
1293emcc.speedtest1 += -sENVIRONMENT=web,worker
1294emcc.speedtest1 += -sALLOW_MEMORY_GROWTH
1295emcc.speedtest1 += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.32)
1296emcc.speedtest1.common += -sINVOKE_RUN=0
1297emcc.speedtest1.common += --no-entry
1298emcc.speedtest1.common += -sABORTING_MALLOC
1299emcc.speedtest1.common += -sSTRICT_JS=0
1300emcc.speedtest1.common += -sMODULARIZE
1301emcc.speedtest1.common += -Wno-limited-postlink-optimizations
1302emcc.speedtest1.common += -Wno-unused-main
1303# ^^^^ -Wno-unused-main is for emcc 3.1.52+. speedtest1 has a wasm_main() which is
1304# exported and called by the JS code.
1305emcc.speedtest1.common += -sSTACK_SIZE=512KB
1306emcc.speedtest1.common += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
1307emcc.speedtest1.common += $(emcc.exportedRuntimeMethods)
1308emcc.speedtest1.common += -sALLOW_TABLE_GROWTH
1309emcc.speedtest1.common += -sDYNAMIC_EXECUTION=0
1310emcc.speedtest1.common += --minify 0
1311emcc.speedtest1.common += -sEXPORT_NAME=$(sqlite3.js.init-func)
1312speedtest1.exit-runtime0 = -sEXIT_RUNTIME=0
1313speedtest1.exit-runtime1 = -sEXIT_RUNTIME=1
1314# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
1315# this error from Emscripten:
1316#
1317# > native function `free` called after runtime exit (use
1318# NO_EXIT_RUNTIME to keep it alive after main() exits))
1319#
1320# If it's 0 and it crashes, we get:
1321#
1322# > stdio streams had content in them that was not flushed. you should
1323# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
1324# when you printf etc.
1325#
1326# and pending output is not flushed because it didn't end with a
1327# newline (by design). The lesser of the two evils seems to be
1328# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
1329# which runs speedtest1 multiple times.
1330
1331$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api)
1332 @$(call b.mkdir@); $(call b.echo,@,$(emo.disk)); \
1333 { echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api); } > $@ || exit
1334speedtest1: b-speedtest1
1335st: speedtest1
1336
1337#
1338# Generate 64-bit variants of speedtest1*.{js,html}
1339#
1340# $1 = input file
1341# $2 = output file
1342#
1343# TODO: preprocess these like we do the rest.
1344#
1345define gen-st64
1346$(2): $(1)
1347 @$$(call b.echo,speedtest164,$$(emo.disk)$(emo.lock) Creating from $$<)
1348 rm -f $$@; \
1349 sed -e 's/speedtest1\.js/speedtest1-64bit\.js/' \
1350 -e 's/speedtest1-worker\.js/speedtest1-worker-64bit\.js/' \
1351 < $$< > $$@; \
1352 chmod -w $$@
1353$(2): b-speedtest164
1354speedtest1: $(1) $(2)
1355CLEAN_FILES += $(2)
1356endef
1357speedtest1: b-speedtest164
1358
1359$(eval $(call gen-st64,speedtest1.html,speedtest1-64bit.html))
1360$(eval $(call gen-st64,speedtest1-worker.html,speedtest1-worker-64bit.html))
1361$(eval $(call gen-st64,speedtest1-worker.js,speedtest1-worker-64bit.js))
1362# end speedtest1.js
1363########################################################################
1364
1365#
1366# tester1 is the main unit and regression test application and needs
1367# to be able to run in 4 separate modes to cover the primary
1368# client-side use cases:
1369#
1370# 1) Load sqlite3 in the main UI thread of a conventional script.
1371# 2) Load sqlite3 in a conventional Worker thread.
1372# 3) Load sqlite3 as an ES6 module (ESM) in the main thread.
1373# 4) Load sqlite3 as an ESM worker.
1374#
1375# To that end, we require two separate builds of tester1.js:
1376#
1377# tester1.js: cases 1 and 2
1378# tester1.mjs: cases 3 and 4
1379#
1380# Then we need those again in 64-bit builds, which require a 64-bit
1381# pair of js/wasm files.
1382#
1383# To create those, we filter tester1.c-pp.js/html with $(bin.c-pp)...
1384
1385#
1386# tester1.js variants:
1387#
1388define gen-tester1.js
1389# $1 = build name to have a dep on
1390# $2 = suffix for tester1${2} JS
1391# $3 = $(bin.c-pp) flags
1392$(call b.c-pp.target,test,tester1.c-pp.js,tester1$(2),$(3))
1393tester1$(2): $(sqlite3.ext.js) $(out.$(1).wasm)
1394tester1-$(1): tester1$(2)
1395tester1: tester1$(2)
1396endef
1397
1398$(eval $(call gen-tester1.js,vanilla,.js,\
1399 $(c-pp.D.vanilla) -Dsqlite3.js=$(dir.dout)/sqlite3.js))
1400$(eval $(call gen-tester1.js,vanilla64,-64bit.js,\
1401 $(c-pp.D.vanilla64) -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.js))
1402$(eval $(call gen-tester1.js,esm,.mjs,\
1403 $(c-pp.D.esm) -Dsqlite3.js=$(dir.dout)/sqlite3.mjs))
1404$(eval $(call gen-tester1.js,esm64,-64bit.mjs,\
1405 $(c-pp.D.esm64) -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.mjs))
1406
1407#
1408# tester1.html variants:
1409#
1410define gen-tester1.html
1411# $1 = build name to have a dep on
1412# $2 = filename suffix: empty, -64bit, -esm, esm-64bit
1413# $3 = $(bin.c-pp) flags
1414$(call b.c-pp.target,test,tester1.c-pp.html,tester1$(2).html,$(3))
1415tester1$(2).html: tester1-$(1)
1416tester1: tester1$(2).html
1417endef
1418
1419$(eval $(call gen-tester1.html,vanilla,,\
1420 $(c-pp.D.vanilla) \
1421 -Dbitness=32 \
1422 -Dtitle="UI thread" \
1423 -Dtester1.js=tester1.js \
1424 -Dsqlite3.js=$(dir.dout)/sqlite3.js))
1425$(eval $(call gen-tester1.html,vanilla64,-64bit,\
1426 $(c-pp.D.vanilla64) \
1427 -Dbitness=64 \
1428 -Dtitle="UI thread" \
1429 -Dtester1.js=tester1-64bit.js \
1430 -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.js))
1431$(eval $(call gen-tester1.html,esm,-esm,\
1432 $(c-pp.D.esm) \
1433 -Dbitness=32 \
1434 -Dtitle="ES6 Module in UI thread" \
1435 -Dtester1.js=tester1.mjs \
1436 -Dsqlite3.js=$(dir.dout)/sqlite3.mjs))
1437$(eval $(call gen-tester1.html,esm64,-esm-64bit,\
1438 $(c-pp.D.esm64) \
1439 -Dbitness=64 \
1440 -Dtitle="ES6 Module in UI thread" \
1441 -Dtester1.js=tester1-64bit.mjs \
1442 -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.mjs))
1443
1444#
1445# tester1-worker.html variants: There is no ESM variant of this
1446# file. Instead, that page accepts the ?esm URL flag to switch to ESM
1447# mode.
1448#
1449$(eval $(call b.c-pp.target,test,tester1-worker.c-pp.html,\
1450 tester1-worker.html,-Dbitness=32))
1451$(eval $(call b.c-pp.target,test,tester1-worker.c-pp.html,\
1452 tester1-worker-64bit.html,$(c-pp.D.64bit) -Dbitness=64))
1453tester1: tester1-worker.html tester1-worker-64bit.html
1454tester1-worker.html: tester1.mjs
1455tester1-worker-64bit.html: tester1-64bit.mjs
1456
1457all: tester1
1458#
1459# end tester1
1460#
1461
1462#
1463# jquery.terminal support for fiddle:
1464#
1465# If a clone of https://github.com/jcubic/jquery.terminal is found in
1466# dir $(JQTERM), defaulting to $(HOME)/src/jquery.terminal then add
1467# jquery.terminal support to fiddle.
1468#
1469# To build that package, from its checkout dir:
1470#
1471# npm install
1472# make
1473#
1474c-pp.D.fiddle ?=
1475JQTERM ?= $(HOME)/src/jquery.terminal
1476dir.jqtermExt = $(firstword $(wildcard $(JQTERM)))
1477#$(info dir.jqtermExt=$(dir.jqtermExt))
1478ifeq (0,$(MAKING_CLEAN))
1479ifeq (,$(wildcard $(dir.jqtermExt)/js/jquery.terminal.min.js))
1480$(info $(emo.magic) To add jquery.terminal support to fiddle, set JQTERM=/path/to/its/built/checkout)
1481else
1482$(info $(emo.magic) jquery.terminal found in $(dir.jqtermExt) - adding it to fiddle. Make sure it is built!)
1483
1484dir.jqterm = $(dir.fiddle)/jqterm
1485$(dir.fiddle)/jqterm/jquery.terminal.bundle.min.js:
1486 @$(call b.mkdir@)
1487 cat $(dir.jqtermExt)/js/jquery-1*.min.js \
1488 $(dir.jqtermExt)/js/jquery.terminal.min.js > $@
1489
1490$(dir.fiddle)/jqterm/jquery.terminal.min.css: $(dir.jqtermExt)/css/jquery.terminal.min.css
1491 @$(call b.mkdir@)
1492 @$(call b.cp,fiddle,$<,$(dir $@))
1493
1494$(dir.fiddle)/index.html: $(dir.fiddle)/jqterm/jquery.terminal.bundle.min.js \
1495 $(dir.fiddle)/jqterm/jquery.terminal.min.css
1496c-pp.D.fiddle += -Djqterm
1497endif
1498endif
1499# ^^^ JQTERM/MAKING_CLEAN
1500
1501#
1502# Generate fiddle/index.html. Must come after JQTERM is handled.
1503#
1504$(dir.fiddle)/index.html: $(dir.fiddle)/index.c-pp.html
1505$(eval $(call b.c-pp.target,fiddle,\
1506 $(dir.fiddle)/index.c-pp.html,$(dir.fiddle)/index.html,$(c-pp.D.fiddle)))
1507$(out.fiddle.wasm): $(dir.fiddle)/index.html
1508
1509
1510#
1511# Convenience rules to rebuild with various -Ox levels. Much
1512# experimentation shows -O2 to be the clear winner in terms of speed.
1513# -Oz results are significantly smaller and only slightly slower than
1514# -O2 (very roughly 10% in highly unscientific tests), so -Oz is the
1515# shipping configuration.
1516#
1517# Achtung: build times with anything higher than -O0 are somewhat
1518# painful, which is why -O0 is the default.
1519#
1520.PHONY: o0 o1 o2 o3 os oz
1521emcc-opt-extra =
1522#ifeq (1,$(wasm-bare-bones))
1523#emcc-opt-extra += -flto
1524# ^^^^ -flto can have a considerably performance boost at -O0 but
1525# doubles the build time and seems to have negligible, if any, effect
1526# on higher optimization levels.
1527#
1528# -flto does not shrink the size of bare-bones builds by any measurable
1529# amount.
1530#endif
1531o0: clean
1532 $(MAKE) -e "emcc_opt=-O0"
1533o1: clean
1534 $(MAKE) -e "emcc_opt=-O1 $(emcc-opt-extra)"
1535o2: clean
1536 $(MAKE) -e "emcc_opt=-O2 $(emcc-opt-extra)"
1537o3: clean
1538 $(MAKE) -e "emcc_opt=-O3 $(emcc-opt-extra)"
1539os: clean
1540 @echo "$(emo.fire)WARNING$(emo.fire): -Os can result in a build with mysteriously missing pieces!"
1541 $(MAKE) -e "emcc_opt=-Os $(emcc-opt-extra)"
1542oz: clean
1543 $(MAKE) -e "emcc_opt=-Oz $(emcc-opt-extra)"
1544
1545#
1546# Push files to the public wasm-testing.sqlite.org server.
1547#
1548# Ideally only -Oz builds should be pushed, so the practice has become:
1549#
1550# make clean
1551# make -j4 for-testing
1552# make push-testing
1553#
1554wasm-testing.include = *.js *.mjs *.html \
1555 ./tests \
1556 $(dir.dout) $(dir.common) $(dir.fiddle) $(dir.fiddle.debug) $(dir.jacc)
1557wasm-testing.exclude = sql/speedtest1.sql jswasm/*/*
1558wasm-testing.dir = /jail/sites/wasm-testing
1559wasm-testing.dest ?= wasm-testing:$(wasm-testing.dir)
1560# ---------------------^^^^^^^^^^^^ ssh alias
1561.PHONY: push-testing
1562push-testing:
1563 rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
1564 $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
1565 $(wasm-testing.include) $(wasm-testing.dest)
1566 @echo "Updating gzipped copies..."; \
1567 ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
1568 echo "SSH failed: it's likely that stale content will be served via old gzip files."
1569
1570#
1571# build everything needed by push-testing with -Oz
1572#
1573.PHONY: for-testing
1574for-testing: emcc_opt=-Oz
1575for-testing: loud=1
1576for-testing.deps = \
1577 tester1 demos \
1578 b-vanilla b-vanilla64 \
1579 b-esm b-esm64 \
1580 b-fiddle b-fiddle.debug \
1581 b-speedtest1 b-speedtest164 \
1582 b-wasmfs
1583for-testing: $(for-testing.deps)
1584
1585########################################################################
1586# If we find a copy of https://sqlite.org/wasm checked out, copy
1587# certain files over to it, applying some automated edits...
1588wasm.docs.home ?= ../../../wasm
1589wasm.docs.found = $(if $(wildcard $(wasm.docs.home)/api-index.md),\
1590 $(wildcard $(wasm.docs.home)),)
1591.PHONY: update-docs
1592ifeq (,$(wasm.docs.found))
1593update-docs:
1594 @echo "Cannot find wasm docs checkout."; \
1595 echo "Pass wasm.docs.home=/path/to/wasm/docs/checkout or edit this makefile to suit."; \
1596 exit 127
1597else
1598wasm.docs.jswasm = $(wasm.docs.home)/jswasm
1599update-docs: $(bin.stripccomments) $(out.vanilla.js) $(out.vanilla.wasm)
1600 @echo "Copying files to the /wasm docs. Be sure to use an -Oz build for this!";
1601 cp -p $(out.vanilla.wasm) $(wasm.docs.jswasm)/.
1602 $(bin.stripccomments) -k -k < $(out.vanilla.js) \
1603 | sed -e '/^[ \t]*$$/d' > $(wasm.docs.jswasm)/sqlite3.js
1604 cp -p demo-123.js demo-123.html demo-123-worker.html $(wasm.docs.home)/.
1605 sed -n -e '/EXTRACT_BEGIN/,/EXTRACT_END/p' \
1606 module-symbols.html > $(wasm.docs.home)/module-symbols.html
1607endif
1608# end /wasm docs
1609########################################################################
1610
1611# Run local web server for the test/demo pages.
1612httpd:
1613 althttpd -max-age 1 -enable-sab 1 -page index.html
1614
1615########################################################################
1616# fiddle_remote is the remote destination for the fiddle app. It must
1617# be a [user@]HOST:/path for rsync. The target "should probably"
1618# contain a symlink of index.html -> fiddle.html.
1619fiddle_remote ?=
1620ifeq (,$(fiddle_remote))
1621ifneq (,$(wildcard /home/stephan))
1622 fiddle_remote = wh:www/wasm-testing/fiddle/.
1623else ifneq (,$(wildcard /home/drh))
1624 #fiddle_remote = if appropriate, add that user@host:/path here
1625endif
1626endif
1627push-fiddle: fiddle
1628 @if [ x = "x$(fiddle_remote)" ]; then \
1629 echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
1630 exit 1; \
1631 fi
1632 rsync -va fiddle/ $(fiddle_remote)
1633# end fiddle remote push
1634########################################################################
1635
1636.PHONY: clean distclean
1637clean:
1638 -rm -f $(CLEAN_FILES)
1639 -rm -fr $(dir.dout) $(dir.tmp)
1640
1641distclean: clean
1642 -rm -f $(DISTCLEAN_FILES)
1643
1644CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ $(dir.fiddle)/*~ \
1645
1646########################################################################
1647# Create main client downloadable zip file:
1648ifneq (,$(filter dist snapshot,$(MAKECMDGOALS)))
1649ifeq (1,$(SQLITE_C_IS_SEE))
1650dist-name-extra = -see
1651else
1652dist-name-extra =
1653endif
1654dist-name-prefix = sqlite-wasm$(dist-name-extra)
1655.PHONY: dist
1656dist:
1657 $(bin.bash) ./mkdist.sh $(dist-name-prefix)
1658snapshot:
1659 $(bin.bash) ./mkdist.sh $(dist-name-prefix) --snapshot
1660endif
1661# ^^^ making dist/snapshot
1662CLEAN_FILES += $(wildcard sqlite-wasm-*.zip)
1663
1664########################################################################
1665# The npm target is specifically for preparing files for the downstream
1666# https://github.com/sqlite/sqlite-wasm (npm) distribution.
1667#
1668# Per agreement with that project's maintainers, these filenames need
1669# to remain stable. To avoid breakage in their deployment process, any
1670# changes (like renaming files) which potentially break their deployment
1671# needs to be communicated to that project via opening a new ticket or
1672# direct coordination with its maintainers.
1673#
1674# This target does a full clean/rebuild so that we can ensure that the
1675# optimization level is set consistently across all files.
1676#
1677npm.bundle.zip = npm-bundle.zip
1678CLEAN_FILES += $(npm.bundle.zip)
1679# Distributables which need to be built for npm:
1680npm_files = $(addprefix $(dir.dout)/, \
1681sqlite3-bundler-friendly.mjs \
1682sqlite3-opfs-async-proxy.js \
1683sqlite3-worker1-bundler-friendly.mjs \
1684sqlite3-worker1-promiser.mjs \
1685sqlite3-worker1.mjs \
1686sqlite3.mjs \
1687sqlite3.wasm \
1688sqlite3-node.mjs \
1689)
1690npm: $(sqlite3.canonical.c)
1691 @echo "$(emo.cleanup) Forcing a clean rebuild to ensure consistent optimization flags."
1692 $(MAKE) clean
1693 $(MAKE) -e "emcc_opt=-Oz $(emcc-opt-extra)" "omit-api=$(omit-api)" $(npm_files)
1694 rm -f $(npm.bundle.zip); zip -r $(npm.bundle.zip) $(npm_files)
1695 unzip -l $(npm.bundle.zip)
1696
1697########################################################################
1698# Explanation of, and some commentary on, various emcc build flags
1699# follows. Full docs for these can be found at:
1700#
1701# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
1702#
1703# -sENVIRONMENT=web: elides bootstrap code related to non-web JS
1704# environments like node.js. Removing this makes the output a tiny
1705# tick larger but hypothetically makes it more portable to
1706# non-browser JS environments.
1707#
1708# -sMODULARIZE: changes how the generated code is structured to avoid
1709# declaring a global Module object and instead installing a function
1710# which loads and initializes the module. The function is named...
1711#
1712# -sEXPORT_NAME=jsFunctionName (see -sMODULARIZE)
1713#
1714# -sEXPORTED_RUNTIME_METHODS=@/absolute/path/to/file: a file
1715# containing a list of emscripten-supplied APIs, one per line, which
1716# must be exported into the generated JS. Must be an absolute path!
1717#
1718# -sEXPORTED_FUNCTIONS=@/absolute/path/to/file: a file containing a
1719# list of C functions, one per line, which must be exported via wasm
1720# so they're visible to JS. C symbols names in that file must all
1721# start with an underscore for reasons known only to the emcc
1722# developers. e.g., _sqlite3_open_v2 and _sqlite3_finalize. Must be
1723# an absolute path!
1724#
1725# -sSTRICT_JS ensures that the emitted JS code includes the 'use
1726# strict' option. Note that -sSTRICT is more broadly-scoped and
1727# results in build errors.
1728#
1729# -sALLOW_TABLE_GROWTH is required for (at a minimum) the UDF-binding
1730# feature. Without it, JS functions cannot be made to proxy C-side
1731# callbacks.
1732#
1733# -sABORTING_MALLOC causes the JS-bound _malloc() to abort rather than
1734# return 0 on OOM. If set to 0 then all code which uses _malloc()
1735# must, just like in C, check the result before using it, else
1736# they're likely to corrupt the JS/WASM heap by writing to its
1737# address of 0. It is, as of this writing, enabled in Emscripten by
1738# default but we enable it explicitly in case that default changes.
1739#
1740# -sDYNAMIC_EXECUTION=0 disables eval() and the Function constructor.
1741# If the build runs without these, it's preferable to use this flag
1742# because certain execution environments disallow those constructs.
1743# This flag is not strictly necessary, however.
1744#
1745# --no-entry: for compiling library code with no main(). If this is
1746# not supplied and the code has a main(), it is called as part of the
1747# module init process. Note that main() is #if'd out of shell.c
1748# (renamed) when building in wasm mode.
1749#
1750# --pre-js/--post-js=FILE relative or absolute paths to JS files to
1751# prepend/append to the emcc-generated bootstrapping JS. It's
1752# easier/faster to develop with separate JS files (reduces rebuilding
1753# requirements) but certain configurations, namely -sMODULARIZE, may
1754# require using at least a --pre-js file. They can be used
1755# individually and need not be paired.
1756#
1757# -O0..-O3 and -Oz: optimization levels affect not only C-style
1758# optimization but whether or not the resulting generated JS code
1759# gets minified. -O0 compiles _much_ more quickly than -O3 or -Oz,
1760# and doesn't minimize any JS code, so is recommended for
1761# development. -O3 or -Oz are recommended for deployment, but
1762# primarily because -Oz will shrink the wasm file notably. JS-side
1763# minification makes little difference in terms of overall
1764# distributable size.
1765#
1766# --minify 0: supposedly disables minification of the generated JS
1767# code, regardless of optimization level, but that's not quite true:
1768# search the main makefile for wasm-strip for details. Minification
1769# of the JS has minimal overall effect in the larger scheme of things
1770# and results in JS files which can neither be edited nor viewed as
1771# text files in Fossil (which flags them as binary because of their
1772# extreme line lengths). Interestingly, whether or not the comments
1773# in the generated JS file get stripped is unaffected by this setting
1774# and depends entirely on the optimization level. Higher optimization
1775# levels reduce the size of the JS considerably even without
1776# minification.
1777#
1778########################################################################