Add a test for the fixes on this branch.
[sqlite.git] / ext / wasm / GNUmakefile
blob115374e6232c614fbd6c9ee467d07d8de0943935
1 #######################################################################
2 # This GNU makefile drives the build of the sqlite3 WASM
3 # components. It is not part of the canonical build process.
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.
10 # Primary targets:
12 # default, all = build in dev mode
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.
18 # quick, q = do just a minimal build (sqlite3.js/wasm, tester1) for
19 # faster development-mode turnaround.
21 # dist = create end user deliverables. Add dist.build=oX to build
22 # with a specific optimization level, where oX is one of the
23 # above-listed o? or qo? target names.
25 # snapshot = like dist, but uses a zip file name which clearly
26 # marks it as a prerelease/snapshot build.
28 # clean = clean up
30 # Required tools beyond those needed for the canonical builds:
32 # - Emscripten SDK: https://emscripten.org/docs/getting_started/downloads.html
33 # - The bash shell
34 # - GNU make, GNU sed, GNU awk, GNU grep (all in the $PATH)
35 # - wasm-strip for release builds: https://github.com/WebAssembly/wabt
36 # - InfoZip for 'dist' zip file
37 ########################################################################
39 # Significant TODOs for this build include, but are not necessarily
40 # limited to:
42 # 1) Consolidate the code generation for sqlite3*.*js into a script
43 # which generates the makefile code, rather than using $(call) and
44 # $(eval), or at least centralize the setup of the numerous vars
45 # related to each build variant $(JS_BUILD_MODES). (Update: an
46 # external script was attempted but generating properly-escaped
47 # makefile code from within a shell script is even less legible
48 # than the $(eval) indirection going on in this file.)
50 default: all
51 #default: quick
52 SHELL := $(shell which bash 2>/dev/null)
53 MAKEFILE := $(lastword $(MAKEFILE_LIST))
54 CLEAN_FILES :=
55 DISTCLEAN_FILES := ./--dummy--
56 release: oz
58 ########################################################################
59 # JS_BUILD_NAMES exists for documentation purposes only. It enumerates
60 # the core build styles:
62 # - sqlite3 = canonical library build
64 # - sqlite3-wasmfs = WASMFS-capable library build
66 JS_BUILD_NAMES := sqlite3 sqlite3-wasmfs
68 ########################################################################
69 # JS_BUILD_MODES exists for documentation purposes only. It enumerates
70 # the various "flavors" of build, each of which requires slight
71 # customization of the output:
73 # - vanilla = plain-vanilla JS for use in browsers. This is the
74 # canonical build mode.
76 # - esm = ES6 module, a.k.a. ESM, for use in browsers.
78 # - bundler-friendly = esm slightly tweaked for "bundler"
79 # tools. Bundlers are invariably based on node.js, so these builds
80 # are intended to be read at build-time by node.js but with a final
81 # target of browsers.
83 # - node = for use by node.js for node.js, as opposed to by node.js on
84 # behalf o browser-side code (use bundler-friendly for that). Note
85 # that persistent storage (OPFS) is not available in these builds.
87 JS_BUILD_MODES := vanilla esm bunder-friendly node
89 ########################################################################
90 # Emscripten SDK home dir and related binaries...
91 EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/emsdk $(HOME)/src/emsdk))
92 emcc.bin ?= $(word 1,$(wildcard $(EMSDK_HOME)/upstream/emscripten/emcc) $(shell which emcc))
93 ifeq (,$(emcc.bin))
94 $(error Cannot find emcc.)
95 endif
96 emcc.version := $(shell "$(emcc.bin)" --version | sed -n 1p \
97 | sed -e 's/^.* \([3-9][^ ]*\) .*$$/\1/;')
98 ifeq (,$(emcc.version))
99 $(warning Cannot determine emcc version. This might unduly impact build flags.)
100 else
101 $(info using emcc version [$(emcc.version)])
102 endif
104 wasm-strip ?= $(shell which wasm-strip 2>/dev/null)
105 ifeq (,$(filter clean,$(MAKECMDGOALS)))
106 ifeq (,$(wasm-strip))
107 $(info WARNING: *******************************************************************)
108 $(info WARNING: builds using -O2/-O3/-Os/-Oz will minify WASM-exported names,)
109 $(info WARNING: breaking _All The Things_. The workaround for that is to build)
110 $(info WARNING: with -g3 (which explodes the file size) and then strip the debug)
111 $(info WARNING: info after compilation, using wasm-strip, to shrink the wasm file.)
112 $(info WARNING: wasm-strip was not found in the PATH so we cannot strip those.)
113 $(info WARNING: If this build uses any optimization level higher than -O1 then)
114 $(info WARNING: the ***resulting JS code WILL NOT BE USABLE***.)
115 $(info WARNING: wasm-strip is part of the wabt package:)
116 $(info WARNING: https://github.com/WebAssembly/wabt)
117 $(info WARNING: on Ubuntu-like systems it can be installed with:)
118 $(info WARNING: sudo apt install wabt)
119 $(info WARNING: *******************************************************************)
120 endif
121 endif # 'make clean' check
123 ifeq (,$(wasm-strip))
124 maybe-wasm-strip = echo "not wasm-stripping"
125 else
126 maybe-wasm-strip = $(wasm-strip)
127 endif
129 ########################################################################
130 # dir.top = the top dir of the canonical build tree, where
131 # sqlite3.[ch] live.
132 dir.top := ../..
133 # Maintenance reminder: some Emscripten flags require absolute paths
134 # but we want relative paths for most stuff simply to reduce
135 # noise. The $(abspath...) GNU make function can transform relative
136 # paths to absolute.
137 dir.wasm := $(patsubst %/,%,$(dir $(MAKEFILE)))
138 dir.api := api
139 dir.jacc := jaccwabyt
140 dir.common := common
141 dir.fiddle := fiddle
142 dir.tool := $(dir.top)/tool
143 CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ $(dir.fiddle)/*~
145 ########################################################################
146 # dir.dout = output dir for deliverables.
148 # MAINTENANCE REMINDER: the output .js and .wasm files of certain emcc
149 # buildables must be in _this_ dir, rather than a subdir, or else
150 # parts of the generated code get confused and cannot load
151 # property. Specifically, when X.js loads X.wasm, whether or not X.js
152 # uses the correct path for X.wasm depends on how it's loaded: an HTML
153 # script tag will resolve it intuitively, whereas a Worker's call to
154 # importScripts() will not. That's a fundamental incompatibility with
155 # how URL resolution in JS happens between those two contexts. See:
157 # https://zzz.buzz/2017/03/14/relative-uris-in-web-development/
159 # We unfortunately have no way, from Worker-initiated code, to
160 # automatically resolve the path from X.js to X.wasm.
162 # We have an "only slightly unsightly" solution for our main builds
163 # but it does not work for the WASMFS builds, so those builds have to
164 # be built to _this_ directory and can only run when the client app is
165 # loaded from the same directory.
166 dir.dout := $(dir.wasm)/jswasm
167 # dir.tmp = output dir for intermediary build files, as opposed to
168 # end-user deliverables.
169 dir.tmp := $(dir.wasm)/bld
170 CLEAN_FILES += $(dir.tmp)/* $(dir.dout)/*
171 ifeq (,$(wildcard $(dir.dout)))
172 dir._tmp := $(shell mkdir -p $(dir.dout))
173 endif
174 ifeq (,$(wildcard $(dir.tmp)))
175 dir._tmp := $(shell mkdir -p $(dir.tmp))
176 endif
178 ########################################################################
179 # Set up sqlite3.c and sqlite3.h...
181 # To build with SEE (https://sqlite.org/see), either put sqlite3-see.c
182 # in $(dir.top) or pass sqlite3.c=PATH_TO_sqlite3-see.c to the $(MAKE)
183 # invocation. Note that only encryption modules with no 3rd-party
184 # dependencies will currently work here: AES256-OFB, AES128-OFB, and
185 # AES128-CCM. Not coincidentally, those 3 modules are included in the
186 # sqlite3-see.c bundle. Note, however, that distributing an SEE build
187 # of the WASM on a public site is in violation of the SEE license
188 # because it effectively provides a usable copy of the SEE build to
189 # all visitors.
191 # A custom sqlite3.c must not have any spaces in its name.
192 # $(sqlite3.canonical.c) must point to the sqlite3.c in
193 # the sqlite3 canonical source tree, as that source file
194 # is required for certain utility and test code.
195 sqlite3.canonical.c := $(dir.top)/sqlite3.c
196 sqlite3.c ?= $(firstword $(wildcard $(dir.top)/sqlite3-see.c) $(sqlite3.canonical.c))
197 sqlite3.h := $(dir.top)/sqlite3.h
198 ifeq (,$(shell grep sqlite3_activate_see $(sqlite3.c) 2>/dev/null))
199 SQLITE_C_IS_SEE := 0
200 else
201 SQLITE_C_IS_SEE := 1
202 $(info This is an SEE build.)
203 endif
204 # Most SQLITE_OPT flags are set in sqlite3-wasm.c but we need them
205 # made explicit here for building speedtest1.c.
206 SQLITE_OPT = \
207 -DSQLITE_ENABLE_FTS5 \
208 -DSQLITE_ENABLE_RTREE \
209 -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
210 -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \
211 -DSQLITE_ENABLE_STMTVTAB \
212 -DSQLITE_ENABLE_DBPAGE_VTAB \
213 -DSQLITE_ENABLE_DBSTAT_VTAB \
214 -DSQLITE_ENABLE_BYTECODE_VTAB \
215 -DSQLITE_ENABLE_OFFSET_SQL_FUNC \
216 -DSQLITE_OMIT_LOAD_EXTENSION \
217 -DSQLITE_OMIT_DEPRECATED \
218 -DSQLITE_OMIT_UTF16 \
219 -DSQLITE_OMIT_SHARED_CACHE \
220 -DSQLITE_THREADSAFE=0 \
221 -DSQLITE_TEMP_STORE=2 \
222 -DSQLITE_OS_KV_OPTIONAL=1 \
223 '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \
224 -DSQLITE_USE_URI=1 \
225 -DSQLITE_WASM_ENABLE_C_TESTS \
226 -DSQLITE_C=$(sqlite3.c)
227 #SQLITE_OPT += -DSQLITE_DEBUG
228 # Enabling SQLITE_DEBUG will break sqlite3_wasm_vfs_create_file()
229 # (and thus sqlite3_js_vfs_create_file()). Those functions are
230 # deprecated and alternatives are in place, but this crash behavior
231 # can be used to find errant uses of sqlite3_js_vfs_create_file()
232 # in client code.
234 ########################################################################@
235 # It's important that sqlite3.h be built to completion before any
236 # other parts of the build run, thus we use .NOTPARALLEL to disable
237 # parallel build of that file and its dependants.
238 .NOTPARALLEL: $(sqlite3.h)
239 $(sqlite3.h):
240 $(MAKE) -C $(dir.top) sqlite3.c
241 $(sqlite3.c): $(sqlite3.h)
243 .PHONY: clean distclean
244 clean:
245 -rm -f $(CLEAN_FILES)
246 distclean: clean
247 -rm -f $(DISTCLEAN_FILES)
249 ifeq (release,$(filter release,$(MAKECMDGOALS)))
250 ifeq (,$(wasm-strip))
251 $(error Cannot make release-quality binary because wasm-strip is not available. \
252 See notes in the warning above)
253 endif
254 else
255 $(info Development build. Use '$(MAKE) release' for a smaller release build.)
256 endif
258 ########################################################################
259 # Adding custom C code via sqlite3_wasm_extra_init.c:
261 # If the canonical build process finds the file
262 # sqlite3_wasm_extra_init.c in the main wasm build directory, it
263 # arranges to include that file in the build of sqlite3.wasm and
264 # defines SQLITE_EXTRA_INIT=sqlite3_wasm_extra_init.
266 # sqlite3_wasm_extra_init() must be a function with this signature:
268 # int sqlite3_wasm_extra_init(const char *)
270 # and the sqlite3 library will call it with an argument of NULL one
271 # time during sqlite3_initialize(). If it returns non-0,
272 # initialization of the library will fail.
274 # The filename can be overridden with:
276 # make sqlite3_wasm_extra_init.c=my_custom_stuff.c
278 # See example_extra_init.c for an example implementation.
279 ########################################################################
280 sqlite3_wasm_extra_init.c ?= $(wildcard sqlite3_wasm_extra_init.c)
281 cflags.wasm_extra_init :=
282 ifneq (,$(sqlite3_wasm_extra_init.c))
283 $(info Enabling SQLITE_EXTRA_INIT via $(sqlite3_wasm_extra_init.c).)
284 cflags.wasm_extra_init := -DSQLITE_WASM_EXTRA_INIT
285 endif
287 #########################################################################
288 # bin.version-info = binary to output various sqlite3 version info for
289 # embedding in the JS files and in building the distribution zip file.
290 # It must NOT be in $(dir.tmp) because we need it to survive the
291 # cleanup process for the dist build to work properly.
293 # Slight caveat: this uses the version info from the in-tree
294 # sqlite3.c/h, which may diff from a user-provided $(sqlite3.c). The
295 # end result is that the generated JS files may have static version
296 # info from $(bin.version-info) which differ from their runtime-emited
297 # version info (e.g. from sqlite3_libversion()).
298 bin.version-info := $(dir.top)/version-info
299 .NOTPARALLEL: $(bin.version-info)
300 $(bin.version-info): $(dir.tool)/version-info.c $(sqlite3.h) $(dir.top)/Makefile
301 $(MAKE) -C $(dir.top) version-info
303 #########################################################################
304 # bin.stripcomments is used for stripping C/C++-style comments from JS
305 # files. The JS files contain large chunks of documentation which we
306 # don't need for all builds. That app's -k flag is of particular
307 # importance here, as it allows us to retain the opening comment
308 # block(s), which contain the license header and version info.
309 bin.stripccomments := $(dir.tool)/stripccomments
310 $(bin.stripccomments): $(bin.stripccomments).c $(MAKEFILE)
311 $(CC) -o $@ $<
312 DISTCLEAN_FILES += $(bin.stripccomments)
315 ########################################################################
316 # C-PP.FILTER: a $(call)able to transform $(1) to $(2) via:
318 # ./c-pp -f $(1) -o $(2) $(3)
320 # Historical notes:
322 # - We first attempted to use gcc and/or clang to preprocess JS files
323 # in the same way we would normally do C files, but C-specific quirks
324 # of each makes that untennable.
326 # - We implemented c-pp.c (the C-Minus Pre-processor) as a custom
327 # generic/file-format-agnostic preprocessor to enable us to pack
328 # code for different target builds into the same JS files. Most
329 # notably, some ES6 module (a.k.a. ESM) features cannot legally be
330 # referenced at all in non-ESM code, e.g. the "import" and "export"
331 # keywords. This preprocessing step permits us to swap out sections
332 # of code where necessary for ESM and non-ESM (a.k.a. vanilla JS)
333 # require different implementations. The alternative to such
334 # preprocessing, would be to have separate source files for ES6
335 # builds, which would have a higher maintenance burden than c-pp.c
336 # seems likely to.
338 # c-pp.c was written specifically for the sqlite project's JavaScript
339 # builds but is maintained as a standalone project:
340 # https://fossil.wanderinghorse.net/r/c-pp
342 # Note that the SQLITE_... build flags used here have NO EFFECT on the
343 # JS/WASM build. They are solely for use with $(bin.c-pp) itself.
345 # -D... flags which should be included in all invocations should be
346 # appended to $(C-PP.FILTER.global).
347 bin.c-pp := ./c-pp
348 $(bin.c-pp): c-pp.c $(sqlite3.c) $(MAKEFILE)
349 $(CC) -O0 -o $@ c-pp.c $(sqlite3.c) '-DCMPP_DEFAULT_DELIM="//#"' -I$(dir.top) \
350 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_UTF16 \
351 -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_WAL -DSQLITE_THREADSAFE=0 \
352 -DSQLITE_TEMP_STORE=3
353 C-PP.FILTER.global ?=
354 ifeq (1,$(SQLITE_C_IS_SEE))
355 C-PP.FILTER.global += -Denable-see
356 endif
357 define C-PP.FILTER
358 # Create $2 from $1 using $(bin.c-pp)
359 # $1 = Input file: c-pp -f $(1).js
360 # $2 = Output file: c-pp -o $(2).js
361 # $3 = optional c-pp -D... flags
362 $(2): $(1) $$(MAKEFILE) $$(bin.c-pp)
363 $$(bin.c-pp) -f $(1) -o $$@ $(3) $(C-PP.FILTER.global)
364 CLEAN_FILES += $(2)
365 endef
366 # /end C-PP.FILTER
367 ########################################################################
369 # cflags.common = C compiler flags for all builds
370 cflags.common := -I. -I$(dir $(sqlite3.c))
371 # emcc.WASM_BIGINT = 1 for BigInt (C int64) support, else 0. The API
372 # disables certain features if BigInt is not enabled and such builds
373 # _are not tested_ on any regular basis.
374 emcc.WASM_BIGINT ?= 1
376 # emcc_opt = optimization-related flags. These are primarily used by
377 # the various oX targets. build times for -O levels higher than 0 are
378 # painful at dev-time.
379 emcc_opt ?= -O0
381 # When passing emcc_opt from the CLI, += and re-assignment have no
382 # effect, so emcc_opt+=-g3 doesn't work. So...
383 emcc_opt_full := $(emcc_opt) -g3
384 # ^^^ ALWAYS use -g3. See below for why.
386 # ^^^ -flto improves runtime speed at -O0 considerably but doubles
387 # build time.
389 # ^^^^ -O3, -Oz, -Os minify symbol names and there appears to be no
390 # way around that except to use -g3, but -g3 causes the binary file
391 # size to absolutely explode (approx. 5x larger). This minification
392 # utterly breaks the resulting module, making it unsable except as
393 # self-contained/self-referential-only code, as ALL of the exported
394 # symbols get minified names.
396 # However, we have an option for using -Oz or -Os:
398 # Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt
399 # tools package (https://github.com/WebAssembly/wabt), to strip the
400 # debugging symbols. That results in a small build with unmangled
401 # symbol names. -Oz gives ever-so-slightly better compression than
402 # -Os: not quite 1% in some completely unscientific tests. Runtime
403 # speed for the unit tests is all over the place either way so it's
404 # difficult to say whether -Os gives any speed benefit over -Oz.
406 # Much practice has demonstrated that -O2 consistently gives the best
407 # runtime speeds, but not by a large enough factor to rule out use of
408 # -Oz when small deliverable size is a priority.
409 ########################################################################
411 ########################################################################
412 # EXPORTED_FUNCTIONS.* = files for use with Emscripten's
413 # -sEXPORTED_FUNCTION flag.
414 EXPORTED_FUNCTIONS.api.main := $(abspath $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api)
415 EXPORTED_FUNCTIONS.api.in := $(EXPORTED_FUNCTIONS.api.main)
416 ifeq (1,$(SQLITE_C_IS_SEE))
417 EXPORTED_FUNCTIONS.api.in += $(abspath $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-see)
418 endif
419 EXPORTED_FUNCTIONS.api := $(dir.tmp)/EXPORTED_FUNCTIONS.api
420 $(EXPORTED_FUNCTIONS.api): $(EXPORTED_FUNCTIONS.api.in) $(sqlite3.c) $(MAKEFILE)
421 cat $(EXPORTED_FUNCTIONS.api.in) > $@
423 ########################################################################
424 # sqlite3-license-version.js = generated JS file with the license
425 # header and version info.
426 sqlite3-license-version.js := $(dir.tmp)/sqlite3-license-version.js
427 # sqlite3-license-version-header.js = JS file containing only the
428 # license header.
429 sqlite3-license-version-header.js := $(dir.api)/sqlite3-license-version-header.js
430 # sqlite3-api-build-version.js = generated JS file which populates the
431 # sqlite3.version object using $(bin.version-info).
432 sqlite3-api-build-version.js := $(dir.tmp)/sqlite3-api-build-version.js
433 # sqlite3-api.jses = the list of JS files which make up
434 # $(sqlite3-api.js.in), in the order they need to be assembled.
435 sqlite3-api.jses := $(sqlite3-license-version.js)
436 # sqlite3-api-prologue.js: initial boostrapping bits:
437 sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.js
438 # whwhasm.js and jaccwabyt.js: Low-level utils, mostly replacing
439 # Emscripten glue:
440 sqlite3-api.jses += $(dir.common)/whwasmutil.js
441 sqlite3-api.jses += $(dir.jacc)/jaccwabyt.js
442 # sqlite3-api-glue.js Glues the previous part together:
443 sqlite3-api.jses += $(dir.api)/sqlite3-api-glue.js
444 # $(sqlite3-api-build-version.js) = library version info
445 sqlite3-api.jses += $(sqlite3-api-build-version.js)
446 # sqlite3-api-oo1.js = the oo1 API:
447 sqlite3-api.jses += $(dir.api)/sqlite3-api-oo1.js
448 # sqlite3-api-worker.js = the Worker1 API:
449 sqlite3-api.jses += $(dir.api)/sqlite3-api-worker1.js
450 # sqlite3-vfs-helper = helper APIs for VFSes:
451 sqlite3-api.jses += $(dir.api)/sqlite3-vfs-helper.c-pp.js
452 # sqlite3-vtab-helper = helper APIs for VTABLEs:
453 sqlite3-api.jses += $(dir.api)/sqlite3-vtab-helper.c-pp.js
454 # sqlite3-vfs-opfs.c-pp.js = the first OPFS VFS:
455 sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs.c-pp.js
456 # sqlite3-vfs-opfs-sahpool.c-pp.js = the second OPFS VFS:
457 sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs-sahpool.c-pp.js
458 # sqlite3-api-cleanup.js = "finalizes" the build and cleans up
459 # any extraneous global symbols which are needed temporarily
460 # by the previous files.
461 sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js
463 ########################################################################
464 # SOAP.js is an external API file which is part of our distribution
465 # but not part of the sqlite3-api.js amalgamation. It's a component of
466 # the first OPFS VFS and necessarily an external file.
467 SOAP.js := $(dir.api)/sqlite3-opfs-async-proxy.js
468 SOAP.js.bld := $(dir.dout)/$(notdir $(SOAP.js))
470 # $(sqlite3-api.ext.jses) = API-related files which are standalone files,
471 # not part of the amalgamation.
473 sqlite3-api.ext.jses := $(SOAP.js.bld)
474 $(SOAP.js.bld): $(SOAP.js)
475 cp $< $@
477 ########################################################################
478 # $(sqlite3-api*.*js) contain the core library code but not the
479 # Emscripten-related glue which deals with loading sqlite3.wasm. In
480 # theory they can be used by arbitrary build environments and WASM
481 # loaders, but in practice that breaks down because the WASM loader
482 # has to be able to provide all of the necessary "imports" to
483 # sqlite3.wasm, and that list of imports is unknown until sqlite3.wasm
484 # is compiled, at which point Emscripten sets up the imports
485 # appropriately. Abstractly speaking, it's impossible for other build
486 # environments to know exactly which imports are needed and provide
487 # them. Tools like wasm-objdump can be used to find the list of
488 # imports but it's questionable whether a non-Emscripten tool could
489 # realistically use that info to provide proper implementations.
490 # Sidebar: some of the imports are used soley by the Emscripten glue,
491 # which the sqlite3 JS code does not rely on.
493 # We build $(sqlite3-api*.*) "because we can" and because it might be
494 # a useful point of experimentation for some clients, but the
495 # above-described caveat may well make them unusable for real-life
496 # clients.
498 # sqlite3-api.js.in = the generated sqlite3-api.js before it gets
499 # preprocessed. It contains all of $(sqlite3-api.jses) but none of the
500 # Emscripten-specific headers and footers.
501 sqlite3-api.js.in := $(dir.tmp)/sqlite3-api.c-pp.js
502 $(sqlite3-api.js.in): $(sqlite3-api.jses) $(MAKEFILE)
503 @echo "Making $@..."
504 @for i in $(sqlite3-api.jses); do \
505 echo "/* BEGIN FILE: $$i */"; \
506 cat $$i; \
507 echo "/* END FILE: $$i */"; \
508 done > $@
510 ########################################################################
511 # emcc flags for .c/.o/.wasm/.js.
512 emcc.flags :=
513 ifeq (1,$(emcc.verbose))
514 emcc.flags += -v
515 # -v is _very_ loud but also informative about what it's doing
516 endif
518 ########################################################################
519 # emcc flags for .c/.o.
520 emcc.cflags :=
521 emcc.cflags += -std=c99 -fPIC
522 # -------------^^^^^^^^ we need c99 for $(sqlite3-wasm.c), primarily
523 # for variadic macros and snprintf() to implement
524 # sqlite3_wasm_enum_json().
525 emcc.cflags += -I. -I$(dir.top)
526 ########################################################################
527 # emcc flags specific to building .js/.wasm files...
528 emcc.jsflags := -fPIC
529 emcc.jsflags += --minify 0
530 emcc.jsflags += --no-entry
531 emcc.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
532 emcc.jsflags += -sMODULARIZE
533 emcc.jsflags += -sDYNAMIC_EXECUTION=0
534 emcc.jsflags += -sNO_POLYFILL
535 emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api)
536 emcc.exportedRuntimeMethods := \
537 -sEXPORTED_RUNTIME_METHODS=wasmMemory
538 # wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY
539 emcc.jsflags += $(emcc.exportedRuntimeMethods)
540 emcc.jsflags += -sUSE_CLOSURE_COMPILER=0
541 emcc.jsflags += -sIMPORTED_MEMORY
542 emcc.jsflags += -sSTRICT_JS=0
543 # STRICT_JS disabled due to:
544 # https://github.com/emscripten-core/emscripten/issues/18610
545 # TL;DR: does not work with MODULARIZE or EXPORT_ES6 as of version
546 # 3.1.31. The fix for that in newer emcc's is to throw a built-time
547 # error if STRICT_JS is used together with those options.
549 # emcc.jsflags += -sSTRICT=1
550 # -sSTRICT=1 Causes failures about unknown symbols which the build
551 # tools should be installing, e.g. __syscall_geteuid32
553 # -sENVIRONMENT values for the various build modes:
554 emcc.environment.vanilla := web,worker
555 emcc.environment.bundler-friendly := $(emcc.environment.vanilla)
556 emcc.environment.esm := $(emcc.environment.vanilla)
557 emcc.environment.node := node
558 # Note that adding ",node" to the list for the other builds causes
559 # Emscripten to generate code which confuses node: it cannot reliably
560 # determine whether the build is for a browser or for node.
562 ########################################################################
563 # -sINITIAL_MEMORY: How much memory we need to start with is governed
564 # at least in part by whether -sALLOW_MEMORY_GROWTH is enabled. If so,
565 # we can start with less. If not, we need as much as we'll ever
566 # possibly use (which, of course, we can't know for sure). Note,
567 # however, that speedtest1 shows that performance for even moderate
568 # workloads MAY suffer considerably if we start small and have to grow
569 # at runtime. e.g. OPFS-backed (speedtest1 --size 75) take MAY take X
570 # time with 16mb+ memory and 3X time when starting with 8MB. However,
571 # such test results are inconsistent due to browser internals which
572 # are opaque to us.
574 # 2024-03-04: emsdk 3.1.55 replaces INITIAL_MEMORY with INITIAL_HEAP,
575 # but also says (in its changelog): "Note that it is currently not
576 # supported in all configurations (#21071)."
577 # https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md
578 emcc.jsflags += -sALLOW_MEMORY_GROWTH
579 emcc.INITIAL_MEMORY.128 := 134217728
580 emcc.INITIAL_MEMORY.96 := 100663296
581 emcc.INITIAL_MEMORY.64 := 67108864
582 emcc.INITIAL_MEMORY.32 := 33554432
583 emcc.INITIAL_MEMORY.16 := 16777216
584 emcc.INITIAL_MEMORY.8 := 8388608
585 emcc.INITIAL_MEMORY ?= 16
586 ifeq (,$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY)))
587 $(error emcc.INITIAL_MEMORY must be one of: 8, 16, 32, 64, 96, 128 (megabytes))
588 endif
589 emcc.jsflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
590 # /INITIAL_MEMORY
591 ########################################################################
593 emcc.jsflags += $(emcc.environment)
594 emcc.jsflags += -sSTACK_SIZE=512KB
595 # ^^^ ACHTUNG: emsdk 3.1.27 reduced the default stack size from 5MB to
596 # a mere 64KB, which leads to silent memory corruption via the kvvfs
597 # VFS, which requires twice that for its xRead() and xWrite() methods.
598 # 2023-03: those methods have since been adapted to use a malloc()'d
599 # buffer.
600 ########################################################################
601 # $(sqlite3.js.init-func) is the name Emscripten assigns our exported
602 # module init/load function. This symbol name is hard-coded in
603 # $(extern-post-js.js) as well as in numerous docs.
605 # "sqlite3InitModule" is the symbol we document for client use, so
606 # that's the symbol name which must be exported, whether it comes from
607 # Emscripten or our own code in extern-post-js.js.
609 # That said... we can change $(sqlite3.js.init-func) as long as the
610 # name "sqlite3InitModule" is the one which gets exposed via the
611 # resulting JS files. That can be accomplished via
612 # extern-post-js.js. However... using a temporary symbol name here
613 # and then adding sqlite3InitModule() ourselves results in 2 global
614 # symbols: we cannot "delete" the Emscripten-defined
615 # $(sqlite3.js.init-func) from vanilla builds (as opposed to ESM
616 # builds) because it's declared with "var".
617 sqlite3.js.init-func := sqlite3InitModule
618 emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func)
619 emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr.
620 #emcc.jsflags += -sSTRICT # fails due to missing __syscall_...()
621 #emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS
622 #emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. fiddle needs the FS API
623 #emcc.jsflags += -sABORTING_MALLOC # only for experimentation
624 emcc.jsflags += -sALLOW_TABLE_GROWTH
625 # ^^^^ -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs
626 emcc.jsflags += -Wno-limited-postlink-optimizations
627 # ^^^^ emcc likes to warn when we have "limited optimizations" via the
628 # -g3 flag.
629 # emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why.
631 # Re. undefined symbol handling, see: https://lld.llvm.org/WebAssembly.html
632 emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=1
633 emcc.jsflags += -sLLD_REPORT_UNDEFINED
634 #emcc.jsflags += --allow-undefined
635 #emcc.jsflags += --import-undefined
636 #emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic
637 #emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined
638 #emcc.jsflags += --unresolved-symbols=ignore-all
640 ########################################################################
641 # -sMEMORY64=1 fails to load, erroring with:
642 # invalid memory limits flags 0x5
643 # (enable via --experimental-wasm-memory64)
645 # ^^^^ MEMORY64=2 builds and loads but dies when we do things like:
647 # new Uint8Array(wasm.heap8u().buffer, ptr, n)
649 # because ptr is now a BigInt, so is invalid for passing to arguments
650 # which have strict must-be-a-Number requirements. That aspect will
651 # make any eventual port to 64-bit address space extremely painful, as
652 # such constructs are found all over the place in the source code.
653 ########################################################################
655 ########################################################################
656 # -sSINGLE_FILE:
657 # https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
659 # -sSINGLE_FILE=1 would be _really_ nice but we have to build with -g3
660 # for -O2 and higher to work (else minification breaks the code) and
661 # cannot wasm-strip the binary before it gets encoded into the JS
662 # file. The result is that the generated JS file is, because of the
663 # -g3 debugging info, _huge_.
664 ########################################################################
666 ########################################################################
667 # $(sqlite3-api-build-version.js) injects the build version info into
668 # the bundle in JSON form.
669 $(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE)
670 @echo "Making $@..."
671 @{ \
672 echo 'globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \
673 echo -n ' sqlite3.version = '; \
674 $(bin.version-info) --json; \
675 echo ';'; \
676 echo '});'; \
677 } > $@
679 ########################################################################
680 # $(sqlite3-license-version.js) contains the license header and
681 # in-comment build version info.
683 # Maintenance reminder: there are awk binaries out there which do not
684 # support -e SCRIPT.
685 $(sqlite3-license-version.js): $(sqlite3.h) $(sqlite3-license-version-header.js) \
686 $(MAKEFILE)
687 @echo "Making $@..."; { \
688 cat $(sqlite3-license-version-header.js); \
689 echo '/*'; \
690 echo '** This code was built from sqlite3 version...'; \
691 echo "**"; \
692 awk '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' $(sqlite3.h); \
693 awk '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \
694 echo "**"; \
695 echo "** Using the Emscripten SDK version $(emcc.version)."; \
696 echo '*/'; \
697 } > $@
699 ########################################################################
700 # --post-js and --pre-js are emcc flags we use to append/prepend JS to
701 # the generated emscripten module file. These rules set up the core
702 # pre/post files for use by the various builds. --pre-js is used to
703 # inject code which needs to run as part of the pre-WASM-load phase.
704 # --post-js injects code which runs after the WASM module is loaded
705 # and includes the entirety of the library plus some
706 # Emscripten-specific post-bootstrapping code.
707 pre-js.js.in := $(dir.api)/pre-js.c-pp.js
708 post-js.js.in := $(dir.tmp)/post-js.c-pp.js
709 post-jses.js := \
710 $(dir.api)/post-js-header.js \
711 $(sqlite3-api.js.in) \
712 $(dir.api)/post-js-footer.js
713 $(post-js.js.in): $(post-jses.js) $(MAKEFILE)
714 @echo "Making $@..."
715 @for i in $(post-jses.js); do \
716 echo "/* BEGIN FILE: $$i */"; \
717 cat $$i; \
718 echo "/* END FILE: $$i */"; \
719 done > $@
722 ########################################################################
723 # call-make-pre-post is a $(call)able which creates rules for
724 # pre-js.$(1)-$(2).js. $1 = the base name of the JS file on whose
725 # behalf this pre-js is for (one of: $(JS_BUILD_NAMES)). $2 is
726 # the build mode: one of $(JS_BUILD_MODES). This sets up
727 # --[extern-][pre/post]-js flags in $(pre-post-$(1)-$(2).flags) and
728 # dependencies in $(pre-post-$(1)-$(2).deps). The resulting files get
729 # filtered using $(C-PP.FILTER). Any flags necessary for such
730 # filtering need to be set in $(c-pp.D.$(1)-$(2)) before $(call)ing
731 # this.
733 # Maintenance note: a shell script was written to generate these rules
734 # with the hope that it would make them more legible and maintainable,
735 # but embedding makefile code in another language makes it even less
736 # legible than having the level of $(eval) indirection which we have
737 # here.
738 define call-make-pre-post
739 pre-post-$(1)-$(2).flags ?=
740 pre-js.js.$(1)-$(2).intermediary := $$(dir.tmp)/pre-js.$(1)-$(2).intermediary.js
741 pre-js.js.$(1)-$(2) := $$(dir.tmp)/pre-js.$(1)-$(2).js
742 #$$(error $$(pre-js.js.$(1)-$(2).intermediary) $$(pre-js.js.$(1)-$(2)))
743 $$(eval $$(call C-PP.FILTER,$$(pre-js.js.in),$$(pre-js.js.$(1)-$(2).intermediary),$$(c-pp.D.$(1)-$(2))))
744 post-js.js.$(1)-$(2) := $$(dir.tmp)/post-js.$(1)-$(2).js
745 $$(eval $$(call C-PP.FILTER,$$(post-js.js.in),$$(post-js.js.$(1)-$(2)),$$(c-pp.D.$(1)-$(2))))
746 extern-post-js.js.$(1)-$(2) := $$(dir.tmp)/extern-post-js.$(1)-$(2).js
747 $$(eval $$(call C-PP.FILTER,$$(extern-post-js.js.in),$$(extern-post-js.js.$(1)-$(2)),$$(c-pp.D.$(1)-$(2))))
748 pre-post-common.flags.$(1)-$(2) := \
749 $$(pre-post-common.flags) \
750 --post-js=$$(post-js.js.$(1)-$(2)) \
751 --extern-post-js=$$(extern-post-js.js.$(1)-$(2))
752 pre-post-jses.$(1)-$(2).deps := $$(pre-post-jses.deps.common) \
753 $$(post-js.js.$(1)-$(2)) $$(extern-post-js.js.$(1)-$(2))
754 $$(pre-js.js.$(1)-$(2)): $$(pre-js.js.$(1)-$(2).intermediary) $$(MAKEFILE)
755 cp $$(pre-js.js.$(1)-$(2).intermediary) $$@
756 @if [ sqlite3-wasmfs = $(1) ]; then \
757 echo "delete Module[xNameOfInstantiateWasm] /*for WASMFS build*/;"; \
758 elif [ sqlite3 != $(1) ]; then \
759 echo "Module[xNameOfInstantiateWasm].uri = '$(1).wasm';"; \
760 fi >> $$@
761 pre-post-$(1)-$(2).deps := \
762 $$(pre-post-jses.$(1)-$(2).deps) \
763 $$(dir.tmp)/pre-js.$(1)-$(2).js
764 pre-post-$(1)-$(2).flags += \
765 $$(pre-post-common.flags.$(1)-$(2)) \
766 --pre-js=$$(dir.tmp)/pre-js.$(1)-$(2).js
767 endef
768 # /post-js and pre-js
769 ########################################################################
771 # Undocumented Emscripten feature: if the target file extension is
772 # "mjs", it defaults to ES6 module builds:
773 # https://github.com/emscripten-core/emscripten/issues/14383
774 sqlite3.wasm := $(dir.dout)/sqlite3.wasm
775 sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c
776 sqlite3-wasm.cfiles := $(sqlite3-wasm.c) $(sqlite3_wasm_extra_init.c)
777 sqlite3-wasmfs.cfiles := $(sqlite3-wasm.cfiles)
778 # sqlite3-wasm.o vs sqlite3-wasm.c: building against the latter
779 # (predictably) results in a slightly faster binary. We're close
780 # enough to the target speed requirements that the 500ms makes a
781 # difference, so we build all binaries against sqlite3-wasm.c instead
782 # of building a shared copy of sqlite3-wasm.o to link against.
783 ########################################################################
784 # SQLITE3.xJS.EXPORT-DEFAULT is part of SQLITE3-WASMFS.xJS.RECIPE and
785 # SETUP_LIB_BUILD_MODE, factored into a separate piece to avoid code
786 # duplication. $1 is 1 if the build mode needs this workaround (esm,
787 # bundler-friendly, node) and 0 if not (vanilla). $2 must be empty for
788 # all builds except sqlite3-wasmfs.mjs, in which case it must be 1.
790 # Reminder for ESM builds: even if we use -sEXPORT_ES6=0, emcc _still_
791 # adds:
793 # export default $(sqlite3.js.init-func);
795 # when building *.mjs, which is bad because we need to export an
796 # overwritten version of that function and cannot "export default"
797 # twice. Because of this, we have to sed *.mjs to remove the _first_
798 # instance (only) of /^export default/.
800 # Upstream RFE:
801 # https://github.com/emscripten-core/emscripten/issues/18237
803 # Maintenance reminder: Mac sed works differently than GNU sed, so we
804 # use awk instead of sed for this.
805 define SQLITE3.xJS.ESM-EXPORT-DEFAULT
806 if [ x1 = x$(1) ]; then \
807 echo "Fragile workaround for emscripten/issues/18237. See SQLITE3.xJS.RECIPE."; \
809 awk '/^export default/ && !f{f=1; next} 1' $@ > $@.tmp && mv $@.tmp $@; \
810 } || exit $$?; \
811 if [ x != x$(2) ]; then \
812 if ! grep -q '^export default' $@; then \
813 echo "Cannot find export default." 1>&2; \
814 exit 1; \
815 fi; \
816 fi; \
818 endef
820 ########################################################################
821 # extern-post-js* and extern-pre-js* are files for use with
822 # Emscripten's --extern-pre-js and --extern-post-js flags.
823 extern-pre-js.js := $(dir.api)/extern-pre-js.js
824 extern-post-js.js.in := $(dir.api)/extern-post-js.c-pp.js
825 # Emscripten flags for --[extern-][pre|post]-js=... for the
826 # various builds.
827 pre-post-common.flags := \
828 --extern-pre-js=$(sqlite3-license-version.js)
829 # pre-post-jses.deps.* = a list of dependencies for the
830 # --[extern-][pre/post]-js files.
831 pre-post-jses.deps.common := $(extern-pre-js.js) $(sqlite3-license-version.js)
833 ########################################################################
834 # SETUP_LIB_BUILD_MODE is a $(call)'able which sets up numerous pieces
835 # for one of the build modes.
837 # $1 = one of: $(JS_BUILD_NAMES)
838 # $2 = build mode name: one of $(JS_BUILD_MODES)
839 # $3 = 1 for ESM build mode, else 0
840 # $4 = resulting sqlite-api JS/MJS file
841 # $5 = resulting JS/MJS file
842 # $6 = -D... flags for $(bin.c-pp)
843 # $7 = optional extra flags for emcc
845 # Maintenance reminder: be careful not to introduce spaces around args
846 # ($1, $2), otherwise string concatenation will malfunction.
848 # Before calling this, emcc.environment.$(2) must be set to a value
849 # for emcc's -sENVIRONMENT flag.
851 # $(cflags.$(1)) and $(cflags.$(1).$(2)) may be defined to append
852 # CFLAGS to a given build mode.
854 # $(emcc.flags.$(1)) and $(emcc.flags.$(1).$(2)) may be defined to
855 # append emcc-specific flags to a given build mode.
856 define SETUP_LIB_BUILD_MODE
857 $(info Setting up build [$(1)-$(2)]: $(5))
858 c-pp.D.$(1)-$(2) := $(6)
859 $$(eval $$(call call-make-pre-post,$(1),$(2)))
860 emcc.flags.$(1).$(2) ?=
861 emcc.flags.$(1).$(2) += $(7)
862 $$(eval $$(call C-PP.FILTER, $$(sqlite3-api.js.in), $(4), $(6)))
863 $(5): $(4) $$(MAKEFILE) $$(sqlite3-wasm.cfiles) $$(EXPORTED_FUNCTIONS.api) $$(pre-post-$(1)-$(2).deps)
864 @echo "Building $$@ ..."
865 $$(emcc.bin) -o $$@ $$(emcc_opt_full) $$(emcc.flags) \
866 $$(emcc.jsflags) \
867 -sENVIRONMENT=$$(emcc.environment.$(2)) \
868 $$(pre-post-$(1)-$(2).flags) \
869 $$(emcc.flags.$(1)) $$(emcc.flags.$(1).$(2)) \
870 $$(cflags.common) $$(SQLITE_OPT) \
871 $$(cflags.$(1)) $$(cflags.$(1).$(2)) \
872 $$(cflags.wasm_extra_init) $$(sqlite3-wasm.cfiles)
873 @$$(call SQLITE3.xJS.ESM-EXPORT-DEFAULT,$(3))
874 @dotwasm=$$(basename $$@).wasm; \
875 chmod -x $$$$dotwasm; \
876 $(maybe-wasm-strip) $$$$dotwasm; \
877 case $(2) in \
878 bundler-friendly|node) \
879 echo "Patching $$@ for $(1).wasm..."; \
880 rm -f $$$$dotwasm; \
881 dotwasm=; \
882 sed -i -e 's/$(1)-$(2).wasm/$(1).wasm/g' $$@ || exit $$$$?; \
883 ;; \
884 esac; \
885 ls -la $$$$dotwasm $$@
886 all: $(5)
887 #quick: $(5)
888 CLEAN_FILES += $(4) $(5)
889 endef
890 # ^^^ /SETUP_LIB_BUILD_MODE
891 ########################################################################
892 sqlite3-api.js := $(dir.dout)/sqlite3-api.js
893 sqlite3.js := $(dir.dout)/sqlite3.js
894 sqlite3-api.mjs := $(dir.dout)/sqlite3-api.mjs
895 sqlite3.mjs := $(dir.dout)/sqlite3.mjs
896 sqlite3-api-bundler-friendly.mjs := $(dir.dout)/sqlite3-api-bundler-friendly.mjs
897 sqlite3-bundler-friendly.mjs := $(dir.dout)/sqlite3-bundler-friendly.mjs
898 sqlite3-api-node.mjs := $(dir.dout)/sqlite3-api-node.mjs
899 sqlite3-node.mjs := $(dir.dout)/sqlite3-node.mjs
900 #$(info $(call SETUP_LIB_BUILD_MODE,sqlite3,vanilla,0, $(sqlite3-api.js), $(sqlite3.js)))
901 $(eval $(call SETUP_LIB_BUILD_MODE,sqlite3,vanilla,0,\
902 $(sqlite3-api.js), $(sqlite3.js)))
903 $(eval $(call SETUP_LIB_BUILD_MODE,sqlite3,esm,1,\
904 $(sqlite3-api.mjs), $(sqlite3.mjs), -Dtarget=es6-module))
905 $(eval $(call SETUP_LIB_BUILD_MODE,sqlite3,bundler-friendly,1,\
906 $(sqlite3-api-bundler-friendly.mjs),$(sqlite3-bundler-friendly.mjs),\
907 $(c-pp.D.sqlite3-esm) -Dtarget=es6-bundler-friendly))
908 $(eval $(call SETUP_LIB_BUILD_MODE,sqlite3,node,1,\
909 $(sqlite3-api-node.mjs),$(sqlite3-node.mjs),\
910 $(c-pp.D.sqlite3-bundler-friendly) -Dtarget=node))
911 # The various -D... values used by *.c-pp.js include:
913 # -Dtarget=es6-module: for all ESM module builds
915 # -Dtarget=node: for node.js builds
917 # -Dtarget=es6-module -Dtarget=es6-bundler-friendly: intended for
918 # "bundler-friendly" ESM module build. These have some restrictions
919 # on how URL() objects are constructed in some contexts: URLs which
920 # refer to files which are part of this project must be referenced
921 # as string literals so that bundlers' static-analysis tools can
922 # find those files and include them in their bundles.
924 # -Dtarget=es6-module -Dtarget=es6-bundler-friendly -Dtarget=node: is
925 # intended for use by node.js for node.js, as opposed to by
926 # node.js on behalf of a browser. Mixing -sENVIRONMENT=web and
927 # -sENVIRONMENT=node leads to ambiguity and confusion on node's
928 # part, as it's unable to reliably determine whether the target is
929 # a browser or node.
931 ########################################################################
932 ########################################################################
933 # We have to ensure that we do not build $(sqlite3*.*js) in parallel
934 # because they all result in the creation of $(sqlite3.wasm). We have
935 # no way to build just a .[m]js file without also building the .wasm
936 # file because the generated .[m]js file has to include info about the
937 # imports needed by the wasm file, so they have to be built
938 # together. i.e. we're building $(sqlite3.wasm) multiple times, but
939 # that's unavoidable (and harmless, just a waste of build time).
940 $(sqlite3.wasm): $(sqlite3.js)
941 $(sqlite3.mjs): $(sqlite3.js)
942 $(sqlite3-bundler-friendly.mjs): $(sqlite3.mjs)
943 $(sqlite3-node.mjs): $(sqlite3.mjs)
944 CLEAN_FILES += $(sqlite3.wasm)
946 ########################################################################
947 # We need separate copies of certain supplementary JS files for the
948 # bundler-friendly build. Concretely, any supplemental JS files which
949 # themselves use importScripts() or Workers or URL() constructors
950 # which refer to other in-tree (m)JS files quire a bundler-friendly
951 # copy.
952 sqlite3-worker1.js.in := $(dir.api)/sqlite3-worker1.c-pp.js
953 sqlite3-worker1-promiser.js.in := $(dir.api)/sqlite3-worker1-promiser.c-pp.js
954 sqlite3-worker1.js := $(dir.dout)/sqlite3-worker1.js
955 sqlite3-worker1-promiser.js := $(dir.dout)/sqlite3-worker1-promiser.js
956 sqlite3-worker1-promiser.mjs := $(dir.dout)/sqlite3-worker1-promiser.mjs
957 sqlite3-worker1-bundler-friendly.mjs := $(dir.dout)/sqlite3-worker1-bundler-friendly.mjs
958 sqlite3-worker1-promiser-bundler-friendly.js := $(dir.dout)/sqlite3-worker1-promiser-bundler-friendly.js
959 $(eval $(call C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1.js)))
960 $(eval $(call C-PP.FILTER,$(sqlite3-worker1.js.in),$(sqlite3-worker1-bundler-friendly.mjs),\
961 $(c-pp.D.sqlite3-bundler-friendly)))
962 $(eval $(call C-PP.FILTER,$(sqlite3-worker1-promiser.js.in),$(sqlite3-worker1-promiser.js)))
963 $(eval $(call C-PP.FILTER,$(sqlite3-worker1-promiser.js.in),\
964 $(sqlite3-worker1-promiser-bundler-friendly.js),\
965 $(c-pp.D.sqlite3-bundler-friendly)))
966 $(eval $(call C-PP.FILTER,$(sqlite3-worker1-promiser.js.in),$(sqlite3-worker1-promiser.mjs),\
967 -Dtarget=es6-module -Dtarget=es6-bundler-friendly))
968 $(sqlite3-bundler-friendly.mjs): $(sqlite3-worker1-bundler-friendly.mjs) \
969 $(sqlite3-worker1-promiser-bundler-friendly.js)
970 $(eval $(call C-PP.FILTER,demo-worker1-promiser.c-pp.js,demo-worker1-promiser.js))
971 $(eval $(call C-PP.FILTER,demo-worker1-promiser.c-pp.js,demo-worker1-promiser.mjs,\
972 -Dtarget=es6-module))
973 $(eval $(call C-PP.FILTER,demo-worker1-promiser.c-pp.html,demo-worker1-promiser.html))
974 $(eval $(call C-PP.FILTER,demo-worker1-promiser.c-pp.html,demo-worker1-promiser-esm.html,\
975 -Dtarget=es6-module))
976 all: $(sqlite3-worker1.js) \
977 $(sqlite3-worker1-promiser.js) $(sqlite3-worker1-promiser.mjs)
979 demo-worker1-promiser.html: $(sqlite3-worker1-promiser.js) demo-worker1-promiser.js
980 demo-worker1-promiser-esm.html: $(sqlite3-worker1-promiser.mjs) demo-worker1-promiser.mjs
981 all: demo-worker1-promiser.html demo-worker1-promiser-esm.html
983 sqlite3-api.ext.jses += \
984 $(sqlite3-worker1-promiser.mjs) \
985 $(sqlite3-worker1-bundler-friendly.mjs) \
986 $(sqlite3-worker1.js)
987 all quick: $(sqlite3-api.ext.jses)
988 q: quick
990 ########################################################################
991 # batch-runner.js is part of one of the test apps which reads in SQL
992 # dumps generated by $(speedtest1) and executes them.
993 dir.sql := sql
994 speedtest1 := ../../speedtest1
995 speedtest1.c := ../../test/speedtest1.c
996 speedtest1.sql := $(dir.sql)/speedtest1.sql
997 speedtest1.cliflags := --size 10 --big-transactions
998 $(speedtest1):
999 $(MAKE) -C ../.. speedtest1
1000 $(speedtest1.sql): $(speedtest1) $(MAKEFILE)
1001 $(speedtest1) $(speedtest1.cliflags) --script $@
1002 batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql
1003 bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql
1004 ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@
1005 clean-batch:
1006 rm -f batch-runner.list $(dir.sql)/speedtest1*.sql
1007 # ^^^ we don't do this along with 'clean' because we clean/rebuild on
1008 # a regular basis with different -Ox flags and rebuilding the batch
1009 # pieces each time is an unnecessary time sink.
1010 batch: batch-runner.list
1011 #all: batch
1012 # end batch-runner.js
1013 ########################################################################
1014 # Wasmified speedtest1 is our primary benchmarking tool.
1016 # emcc.speedtest1.common = emcc flags used by multiple builds of speedtest1
1017 # emcc.speedtest1 = emcc flags used by main build of speedtest1
1018 emcc.speedtest1.common := $(emcc_opt_full)
1019 emcc.speedtest1 := -I. -I$(dir $(sqlite3.canonical.c))
1020 emcc.speedtest1 += -sENVIRONMENT=web
1021 emcc.speedtest1 += -sALLOW_MEMORY_GROWTH
1022 emcc.speedtest1 += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))
1023 emcc.speedtest1.common += -sINVOKE_RUN=0
1024 emcc.speedtest1.common += --no-entry
1025 emcc.speedtest1.common += -sABORTING_MALLOC
1026 emcc.speedtest1.common += -sSTRICT_JS=0
1027 emcc.speedtest1.common += -sMODULARIZE
1028 emcc.speedtest1.common += -Wno-limited-postlink-optimizations
1029 emcc.speedtest1.common += -Wno-unused-main
1030 # ^^^^ -Wno-unused-main is for emcc 3.1.52+. speedtest1 has a wasm_main() which is
1031 # exported and called by the JS code.
1032 EXPORTED_FUNCTIONS.speedtest1 := $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1)
1033 emcc.speedtest1.common += -sSTACK_SIZE=512KB
1034 emcc.speedtest1.common += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1)
1035 emcc.speedtest1.common += $(emcc.exportedRuntimeMethods)
1036 emcc.speedtest1.common += -sALLOW_TABLE_GROWTH
1037 emcc.speedtest1.common += -sDYNAMIC_EXECUTION=0
1038 emcc.speedtest1.common += --minify 0
1039 emcc.speedtest1.common += -sEXPORT_NAME=$(sqlite3.js.init-func)
1040 emcc.speedtest1.common += -sWASM_BIGINT=$(emcc.WASM_BIGINT)
1041 speedtest1.exit-runtime0 := -sEXIT_RUNTIME=0
1042 speedtest1.exit-runtime1 := -sEXIT_RUNTIME=1
1043 # Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get
1044 # this error from emscripten:
1046 # > native function `free` called after runtime exit (use
1047 # NO_EXIT_RUNTIME to keep it alive after main() exits))
1049 # If it's 0 and it crashes, we get:
1051 # > stdio streams had content in them that was not flushed. you should
1052 # set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline
1053 # when you printf etc.
1055 # and pending output is not flushed because it didn't end with a
1056 # newline (by design). The lesser of the two evils seems to be
1057 # -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app
1058 # which runs speedtest1 multiple times.
1060 $(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api.main)
1061 @echo "Making $@ ..."
1062 @{ echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api.main); } > $@
1063 speedtest1.js := $(dir.dout)/speedtest1.js
1064 speedtest1.wasm := $(dir.dout)/speedtest1.wasm
1065 emcc.flags.speedtest1-vanilla := $(cflags.common) -DSQLITE_SPEEDTEST1_WASM
1067 speedtest1.cfiles := $(speedtest1.c) $(sqlite3-wasm.c)
1068 $(eval $(call call-make-pre-post,speedtest1,vanilla))
1069 $(speedtest1.js): $(MAKEFILE) $(speedtest1.cfiles) \
1070 $(pre-post-speedtest1-vanilla.deps) \
1071 $(EXPORTED_FUNCTIONS.speedtest1)
1072 @echo "Building $@ ..."
1073 $(emcc.bin) \
1074 $(emcc.speedtest1) \
1075 $(emcc.speedtest1.common) \
1076 $(emcc.flags.speedtest1-vanilla) $(pre-post-speedtest1-vanilla.flags) \
1077 $(SQLITE_OPT) \
1078 -USQLITE_C -DSQLITE_C=$(sqlite3.canonical.c) \
1079 $(speedtest1.exit-runtime0) \
1080 -o $@ $(speedtest1.cfiles) -lm
1081 $(maybe-wasm-strip) $(speedtest1.wasm)
1082 chmod -x $(speedtest1.wasm)
1083 ls -la $@ $(speedtest1.wasm)
1085 speedtest1: $(speedtest1.js)
1086 all: speedtest1
1087 CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
1088 # end speedtest1.js
1089 ########################################################################
1091 ########################################################################
1092 # tester1 is the main unit and regression test application and needs
1093 # to be able to run in 4 separate modes to cover the primary
1094 # client-side use cases:
1096 # 1) Load sqlite3 in the main UI thread of a conventional script.
1097 # 2) Load sqlite3 in a conventional Worker thread.
1098 # 3) Load sqlite3 as an ES6 module (ESM) in the main thread.
1099 # 4) Load sqlite3 as an ESM worker. (Not all browsers support this.)
1101 # To that end, we require two separate builds of tester1.js:
1103 # tester1.js: cases 1 and 2
1104 # tester1.mjs: cases 3 and 4
1106 # To create those, we filter tester1.c-pp.js with $(bin.c-pp)...
1107 $(eval $(call C-PP.FILTER,tester1.c-pp.js,tester1.js))
1108 $(eval $(call C-PP.FILTER,tester1.c-pp.js,tester1.mjs,$(c-pp.D.sqlite3-esm)))
1109 $(eval $(call C-PP.FILTER,tester1.c-pp.html,tester1.html))
1110 $(eval $(call C-PP.FILTER,tester1.c-pp.html,tester1-esm.html,$(c-pp.D.sqlite3-esm)))
1111 tester1: tester1.js tester1.mjs tester1.html tester1-esm.html
1112 # Note that we do not include $(sqlite3-bundler-friendly.mjs) in this
1113 # because bundlers are client-specific.
1114 all quick: tester1
1115 quick: $(sqlite3.js)
1117 ########################################################################
1118 # Convenience rules to rebuild with various -Ox levels. Much
1119 # experimentation shows -O2 to be the clear winner in terms of speed.
1120 # Note that build times with anything higher than -O0 are somewhat
1121 # painful.
1123 .PHONY: o0 o1 o2 o3 os oz
1124 o-xtra :=
1125 #o-xtra ?= -flto
1126 # ^^^^ -flto can have a considerably performance boost at -O0 but
1127 # doubles the build time and seems to have negligible, if any, effect
1128 # on higher optimization levels.
1129 o0: clean
1130 $(MAKE) -e "emcc_opt=-O0"
1131 o1: clean
1132 $(MAKE) -e "emcc_opt=-O1 $(o-xtra)"
1133 o2: clean
1134 $(MAKE) -j2 -e "emcc_opt=-O2 $(o-xtra)"
1135 o3: clean
1136 $(MAKE) -e "emcc_opt=-O3 $(o-xtra)"
1137 os: clean
1138 @echo "WARNING: -Os can result in a build with mysteriously missing pieces!"
1139 $(MAKE) -e "emcc_opt=-Os $(o-xtra)"
1140 oz: clean
1141 $(MAKE) -j2 -e "emcc_opt=-Oz $(o-xtra)"
1143 ########################################################################
1144 # Sub-makes...
1146 # sqlite.org/fiddle application...
1147 include fiddle.make
1149 # Only add wasmfs if wasmfs.enable=1 or we're running (dist)clean
1150 ifneq (,$(filter wasmfs,$(MAKECMDGOALS)))
1151 wasmfs.enable ?= 1
1152 else
1153 # Unconditionally enable wasmfs for [dist]clean so that the wasmfs
1154 # sub-make can clean up.
1155 wasmfs.enable ?= $(if $(filter %clean,$(MAKECMDGOALS)),1,0)
1156 endif
1157 ifeq (1,$(wasmfs.enable))
1158 # wasmfs build disabled 2022-10-19 per /chat discussion.
1159 # OPFS-over-wasmfs was initially a stopgap measure and a convenient
1160 # point of comparison for the OPFS sqlite3_vfs's performance, but it
1161 # currently doubles our deliverables and build maintenance burden for
1162 # little benefit.
1164 ########################################################################
1165 # Some platforms do not support the WASMFS build. Raspberry Pi OS is one
1166 # of them. As such platforms are discovered, add their (uname -m) name
1167 # to PLATFORMS_WITH_NO_WASMFS to exclude the wasmfs build parts.
1168 PLATFORMS_WITH_NO_WASMFS := aarch64 # add any others here
1169 THIS_ARCH := $(shell /usr/bin/uname -m)
1170 ifneq (,$(filter $(THIS_ARCH),$(PLATFORMS_WITH_NO_WASMFS)))
1171 $(info This platform does not support the WASMFS build.)
1172 HAVE_WASMFS := 0
1173 else
1174 HAVE_WASMFS := 1
1175 include wasmfs.make
1176 endif
1177 endif
1178 # /wasmfs
1179 ########################################################################
1181 ########################################################################
1182 # Push files to public wasm-testing.sqlite.org server
1183 wasm-testing.include = *.js *.mjs *.html \
1184 ./tests \
1185 $(dir.dout) $(dir.common) $(dir.fiddle) $(dir.jacc)
1186 wasm-testing.exclude = sql/speedtest1.sql
1187 wasm-testing.dir = /jail/sites/wasm-testing
1188 wasm-testing.dest ?= wasm-testing:$(wasm-testing.dir)
1189 # ---------------------^^^^^^^^^^^^ ssh alias
1190 .PHONY: push-testing
1191 push-testing:
1192 rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \
1193 $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \
1194 $(wasm-testing.include) $(wasm-testing.dest)
1195 @echo "Updating gzipped copies..."; \
1196 ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \
1197 echo "SSH failed: it's likely that stale content will be served via old gzip files."
1199 ########################################################################
1200 # If we find a copy of the sqlite.org/wasm docs checked out, copy
1201 # certain files over to it, noting that some need automatable edits...
1202 wasm.docs.home ?= ../../../wasm
1203 wasm.docs.found = $(if $(wildcard $(wasm.docs.home)/api-index.md),\
1204 $(wildcard $(wasm.docs.home)),)
1205 .PHONY: update-docs
1206 ifeq (,$(wasm.docs.found))
1207 update-docs:
1208 @echo "Cannot find wasm docs checkout."; \
1209 echo "Pass wasm.docs.home=/path/to/wasm/docs/checkout or edit this makefile to suit."; \
1210 exit 127
1211 else
1212 wasm.docs.jswasm := $(wasm.docs.home)/jswasm
1213 update-docs: $(bin.stripccomments) $(sqlite3.js) $(sqlite3.wasm)
1214 @echo "Copying files to the /wasm docs. Be sure to use an -Oz build for this!"
1215 cp $(sqlite3.wasm) $(wasm.docs.jswasm)/.
1216 $(bin.stripccomments) -k -k < $(sqlite3.js) \
1217 | sed -e '/^[ \t]*$$/d' > $(wasm.docs.jswasm)/sqlite3.js
1218 cp demo-123.js demo-123.html demo-123-worker.html $(wasm.docs.home)
1219 sed -n -e '/EXTRACT_BEGIN/,/EXTRACT_END/p' \
1220 module-symbols.html > $(wasm.docs.home)/module-symbols.html
1221 endif
1222 # end /wasm docs
1223 ########################################################################
1225 ########################################################################
1226 # Create main client downloadable zip file:
1227 ifneq (,$(filter dist snapshot,$(MAKECMDGOALS)))
1228 include dist.make
1229 endif
1231 # Run local web server for the test/demo pages.
1232 httpd:
1233 althttpd -max-age 1 -enable-sab 1 -page index.html