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