1# This GNU makefile exists primarily to simplify/speed up development 2# of the sqlite3 WASM components. It is not part of the canonical 3# build process. 4# 5# Maintenance notes: the fiddle build is currently performed in the 6# top-level ../../Makefile.in. It may be moved into this file at some 7# point, as GNU Make has been deemed acceptable for the WASM-related 8# components (whereas POSIX Make is required for the more conventional 9# components). 10SHELL := $(shell which bash 2>/dev/null) 11all: 12 13.PHONY: fiddle 14ifneq (,$(wildcard /home/stephan)) 15 fiddle_opt ?= -O0 16else 17 fiddle_opt = -Os 18endif 19fiddle: 20 $(MAKE) -C ../.. fiddle -e emcc_opt=$(fiddle_opt) 21all: fiddle 22 23clean: 24 $(MAKE) -C ../../ clean-fiddle 25 -rm -f $(CLEAN_FILES) 26 27MAKEFILE := $(lastword $(MAKEFILE_LIST)) 28dir.top := ../.. 29# Reminder: some Emscripten flags require absolute paths 30dir.wasm := $(patsubst %/,%,$(dir $(abspath $(MAKEFILE)))) 31dir.api := api 32dir.jacc := jaccwabyt 33dir.common := common 34CLEAN_FILES := *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ 35 36sqlite3.c := $(dir.top)/sqlite3.c 37$(sqlite3.c): 38 $(MAKE) -C $(dir.top) sqlite3.c 39 40SQLITE_OPT = \ 41 -DSQLITE_ENABLE_FTS4 \ 42 -DSQLITE_ENABLE_RTREE \ 43 -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ 44 -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \ 45 -DSQLITE_ENABLE_STMTVTAB \ 46 -DSQLITE_ENABLE_DBPAGE_VTAB \ 47 -DSQLITE_ENABLE_DBSTAT_VTAB \ 48 -DSQLITE_ENABLE_BYTECODE_VTAB \ 49 -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ 50 -DSQLITE_OMIT_LOAD_EXTENSION \ 51 -DSQLITE_OMIT_DEPRECATED \ 52 -DSQLITE_OMIT_UTF16 \ 53 -DSQLITE_OMIT_SHARED_CACHE \ 54 -DSQLITE_THREADSAFE=0 \ 55 -DSQLITE_TEMP_STORE=3 56#SQLITE_OPT += -DSQLITE_ENABLE_MEMSYS5 57# ^^^ MEMSYS5 is hypothetically useful for non-Emscripten builds but 58# requires adding more infrastructure and fixing one spot in the 59# sqlite3 internals which calls malloc() early on. 60 61# SQLITE_OMIT_LOAD_EXTENSION: if this is true, sqlite3_vfs::xDlOpen 62# and friends may be NULL. 63 64emcc_opt ?= -O0 65.PHONY: release 66release: 67 $(MAKE) 'emcc_opt=-Os -g3 -flto' 68# ^^^^^ target-specific vars, e.g.: 69# release: emcc_opt=... 70# apparently only work for file targets, not PHONY targets? 71# 72# ^^^ -flto improves runtime speed at -O0 considerably but doubles 73# build time. 74# 75# ^^^^ -O3, -Oz, -Os minify symbol names and there appears to be no 76# way around that except to use -g3, but -g3 causes the binary file 77# size to absolutely explode (approx. 5x larger). This minification 78# utterly breaks the resulting module, making it unsable except as 79# self-contained/self-referential-only code, as ALL of the exported 80# symbols get minified names. 81# 82# However, we have an option for using -Oz or -Os: 83# 84# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt 85# tools package (https://github.com/WebAssembly/wabt), to strip the 86# debugging symbols. That results in a small build with unmangled 87# symbol names. -Oz gives ever-so-slightly better compression than 88# -Os: not quite 1% in some completely unscientific tests. Runtime 89# speed for the unit tests is all over the place either way so it's 90# difficult to say whether -Os gives any speed benefit over -Oz. 91######################################################################## 92 93# Emscripten SDK home dir and related binaries... 94EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/src/emsdk $(HOME)/emsdk)) 95emcc.bin ?= $(word 1,$(wildcard $(shell which emcc) $(EMSDK_HOME)/upstream/emscripten/emcc)) 96ifeq (,$(emcc.bin)) 97 $(error Cannot find emcc.) 98endif 99 100wasm-strip ?= $(shell which wasm-strip 2>/dev/null) 101ifeq (,$(filter clean,$(MAKECMDGOALS))) 102ifeq (,$(wasm-strip)) 103 $(info WARNING: *******************************************************************) 104 $(info WARNING: builds using -O3/-Os/-Oz will minify WASM-exported names,) 105 $(info WARNING: breaking _All The Things_. The workaround for that is to build) 106 $(info WARNING: with -g3 (which explodes the file size) and then strip the debug) 107 $(info WARNING: info after compilation, using wasm-strip, to shrink the wasm file.) 108 $(info WARNING: wasm-strip was not found in the PATH so we cannot strip those.) 109 $(info WARNING: If this build uses any optimization level higher than -O2 then) 110 $(info WARNING: the ***resulting WASM binary WILL NOT BE USABLE***.) 111 $(info WARNING: wasm-strip is part of the wabt package:) 112 $(info WARNING: https://github.com/WebAssembly/wabt) 113 $(info WARNING: on Ubuntu-like systems it can be installed with:) 114 $(info WARNING: sudo apt install wabt) 115 $(info WARNING: *******************************************************************) 116endif 117endif # 'make clean' check 118 119ifeq (release,$(filter release,$(MAKECMDGOALS))) 120 ifeq (,$(wasm-strip)) 121 $(error Cannot make release-quality binary because wasm-strip is not available. \ 122 See notes in the warning above) 123 endif 124else 125 $(info Development build. Use '$(MAKE) release' for a smaller release build.) 126endif 127 128EXPORTED_FUNCTIONS.api.in := $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api \ 129 $(dir.jacc)/jaccwabyt_test.exports 130 131EXPORTED_FUNCTIONS.api: $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE) 132 cat $(EXPORTED_FUNCTIONS.api.in) > $@ 133CLEAN_FILES += EXPORTED_FUNCTIONS.api 134 135sqlite3-api.jses := \ 136 $(dir.api)/sqlite3-api-prologue.js \ 137 $(dir.common)/whwasmutil.js \ 138 $(dir.jacc)/jaccwabyt.js \ 139 $(dir.api)/sqlite3-api-glue.js \ 140 $(dir.api)/sqlite3-api-oo1.js \ 141 $(dir.api)/sqlite3-api-worker1.js 142#sqlite3-api.jses += $(dir.api)/sqlite3-api-opfs.js 143sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js 144 145sqlite3-api.js := sqlite3-api.js 146CLEAN_FILES += $(sqlite3-api.js) 147$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE) 148 @echo "Making $@..." 149 @for i in $(sqlite3-api.jses); do \ 150 echo "/* BEGIN FILE: $$i */"; \ 151 cat $$i; \ 152 echo "/* END FILE: $$i */"; \ 153 done > $@ 154 155post-js.js := post-js.js 156CLEAN_FILES += $(post-js.js) 157post-jses := \ 158 $(dir.api)/post-js-header.js \ 159 $(sqlite3-api.js) \ 160 $(dir.api)/post-js-footer.js 161 162$(post-js.js): $(post-jses) $(MAKEFILE) 163 @echo "Making $@..." 164 @for i in $(post-jses); do \ 165 echo "/* BEGIN FILE: $$i */"; \ 166 cat $$i; \ 167 echo "/* END FILE: $$i */"; \ 168 done > $@ 169 170 171######################################################################## 172# emcc flags for .c/.o/.wasm/.js. 173emcc.flags = 174#emcc.flags += -v # _very_ loud but also informative about what it's doing 175# -g is needed to keep -O2 and higher from creating broken JS via 176# minification. 177emcc.flags += -g 178 179######################################################################## 180# emcc flags for .c/.o. 181emcc.cflags := 182emcc.cflags += -std=c99 -fPIC 183# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs. 184emcc.cflags += -I. -I$(dir.top) # $(SQLITE_OPT) 185 186######################################################################## 187# emcc flags specific to building the final .js/.wasm file... 188emcc.jsflags := -fPIC 189emcc.jsflags := --minify 0 190emcc.jsflags += --no-entry 191emcc.jsflags += -sMODULARIZE 192emcc.jsflags += -sSTRICT_JS 193emcc.jsflags += -sDYNAMIC_EXECUTION=0 194emcc.jsflags += -sNO_POLYFILL 195emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api 196emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory # wasmMemory==>for -sIMPORTED_MEMORY 197emcc.jsflags += -sUSE_CLOSURE_COMPILER=0 198emcc.jsflags += -sIMPORTED_MEMORY 199emcc.environment := -sENVIRONMENT=web 200ENABLE_WASMFS ?= 0 201ifneq (0,$(ENABLE_WASMFS)) 202 emcc.cflags += -pthread 203 emcc.jsflags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2 204 emcc.cflags += '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' 205 emcc.environment := $(emcc.environment),worker 206 emcc.jsflags += -sINITIAL_MEMORY=128450560 207else 208 emcc.jsflags += -sALLOW_MEMORY_GROWTH 209 # emcc: warning: USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code 210 # slowly, see https://github.com/WebAssembly/design/issues/1271 211 # [-Wpthreads-mem-growth] 212 emcc.jsflags += -sINITIAL_MEMORY=13107200 213 #emcc.jsflags += -sINITIAL_MEMORY=64225280 214 # ^^^^ 64MB is not enough for WASMFS/OPFS test runs using batch-runner.js 215endif 216emcc.jsflags += $(emcc.environment) 217#emcc.jsflags += -sTOTAL_STACK=4194304 218emcc.jsflags += -sEXPORT_NAME=sqlite3InitModule 219emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr. 220emcc.jsflags += --post-js=$(post-js.js) 221#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...() 222#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS 223#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API 224#emcc.jsflags += -sABORTING_MALLOC 225emcc.jsflags += -sALLOW_TABLE_GROWTH 226emcc.jsflags += -Wno-limited-postlink-optimizations 227# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag. 228#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why 229# https://lld.llvm.org/WebAssembly.html 230emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0 231emcc.jsflags += -sLLD_REPORT_UNDEFINED 232#emcc.jsflags += --allow-undefined 233emcc.jsflags += --import-undefined 234#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic 235#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined 236#emcc.jsflags += --unresolved-symbols=ignore-all 237enable_bigint ?= 1 238ifneq (0,$(enable_bigint)) 239emcc.jsflags += -sWASM_BIGINT 240endif 241emcc.jsflags += -sMEMORY64=0 242# ^^^^ MEMORY64=1 fails to load, erroring with: 243# invalid memory limits flags 0x5 244# (enable via --experimental-wasm-memory64) 245# 246# ^^^^ MEMORY64=2 builds and loads but dies when we do things like: 247# 248# new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n) 249# 250# because ptr is now a BigInt, so is invalid for passing to arguments 251# which have strict must-be-a-Number requirements. 252######################################################################## 253 254 255######################################################################## 256# -sSINGLE_FILE: 257# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1704 258# -sSINGLE_FILE=1 would be really nice but we have to build with -g 259# for -O2 and higher to work (else minification breaks the code) and 260# cannot wasm-strip the binary before it gets encoded into the JS 261# file. The result is that the generated JS file is, because of the -g 262# debugging info, _huge_. 263######################################################################## 264 265######################################################################## 266# Maintenance reminder: the output .js and .wasm files of emcc must be 267# in _this_ dir, rather than a subdir, or else parts of the generated 268# code get confused and cannot load property (namely, the 269# sqlite3.worker.js generated in conjunction with -sWASMFS). 270sqlite3.js := sqlite3.js 271sqlite3.wasm := sqlite3.wasm 272sqlite3-wasm.o := $(dir.api)/sqlite3-wasm.o 273$(sqlite3-wasm.o): emcc.cflags += $(SQLITE_OPT) 274$(sqlite3-wasm.o): $(dir.top)/sqlite3.c 275$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT) 276sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c \ 277 $(dir.jacc)/jaccwabyt_test.c 278# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test apps, 279# so we don't really want to include it in release builds. However, we 280# want to test the release builds with those apps, so we cannot simply 281# elide that file in release builds. That component is critical to the 282# VFS bindings so needs to be tested along with the core APIs. 283ifneq (,$(filter -sWASMFS,$(emcc.jsflags))) 284 $(sqlite3-wasm.o): emcc.cflags+=-DSQLITE_WASM_OPFS 285endif 286define WASM_C_COMPILE 287$(1).o := $$(subst .c,.o,$(1)) 288sqlite3.wasm.obj += $$($(1).o) 289$$($(1).o): $$(MAKEFILE) $(1) 290 $$(emcc.bin) $$(emcc_opt) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@ 291CLEAN_FILES += $$($(1).o) 292endef 293$(foreach c,$(sqlite3-wasm.c),$(eval $(call WASM_C_COMPILE,$(c)))) 294$(sqlite3.js): 295$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \ 296 EXPORTED_FUNCTIONS.api \ 297 $(post-js.js) 298 $(emcc.bin) -o $(sqlite3.js) $(emcc_opt) $(emcc.flags) $(emcc.jsflags) $(sqlite3.wasm.obj) 299 chmod -x $(sqlite3.wasm) 300ifneq (,$(wasm-strip)) 301 $(wasm-strip) $(sqlite3.wasm) 302endif 303 @ls -la $@ $(sqlite3.wasm) 304 305CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm) 306all: $(sqlite3.js) 307wasm: $(sqlite3.js) 308# End main Emscripten-based module build 309######################################################################## 310 311######################################################################## 312# batch-runner.js... 313dir.sql := sql 314speedtest1 := ../../speedtest1 315speedtest1.c := ../../test/speedtest1.c 316speedtest1.sql := $(dir.sql)/speedtest1.sql 317$(speedtest1): 318 $(MAKE) -C ../.. speedtest1 319$(speedtest1.sql): $(speedtest1) 320 $(speedtest1) --script $@ 321batch-runner.list: $(MAKEFILE) $(speedtest1.sql) $(dir.sql)/000-mandelbrot.sql 322 bash split-speedtest1-script.sh $(dir.sql)/speedtest1.sql 323 ls -1 $(dir.sql)/*.sql | grep -v speedtest1.sql | sort > $@ 324clean-batch: 325 rm -f batch-runner.list $(dir.sql)/speedtest1*.sql 326# ^^^ we don't do this along with 'clean' because we clean/rebuild on 327# a regular basis with different -Ox flags and rebuilding the batch 328# pieces each time is an unnecessary time sink. 329batch: batch-runner.list 330all: batch 331# end batch-runner.js 332######################################################################## 333# speedtest1.js... 334emcc.speedtest1-flags := -g $(emcc_opt) 335ifneq (0,$(ENABLE_WASMFS)) 336 emcc.speedtest1-flags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2 337 emcc.speedtest1-flags += -DSQLITE_WASM_OPFS 338endif 339emcc.speedtest1-flags += -sINVOKE_RUN=0 340#emcc.speedtest1-flags += --no-entry 341emcc.speedtest1-flags += -flto 342emcc.speedtest1-flags += -sABORTING_MALLOC 343emcc.speedtest1-flags += -sINITIAL_MEMORY=128450560 344emcc.speedtest1-flags += -sSTRICT_JS 345emcc.speedtest1-flags += $(emcc.environment) 346emcc.speedtest1-flags += -sMODULARIZE 347emcc.speedtest1-flags += -sEXPORT_NAME=sqlite3Speedtest1InitModule 348emcc.speedtest1-flags += -Wno-limited-postlink-optimizations 349emcc.speedtest1-flags += -sEXPORTED_FUNCTIONS=_main,_malloc,_free,_sqlite3_wasm_vfs_unlink,_sqlite3_wasm_init_opfs 350emcc.speedtest1-flags += -sDYNAMIC_EXECUTION=0 351emcc.speedtest1-flags += --minify 0 352 353speedtest1.js := speedtest1.js 354speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js)) 355$(speedtest1.js): emcc.cflags+= 356# speedtest1 notes re. sqlite3-wasm.o vs sqlite3-wasm.c: building against 357# the latter (predictably) results in a slightly faster binary, but we're 358# close enough to the target speed requirements that the 500ms makes a 359# difference. 360$(speedtest1.js): $(speedtest1.c) $(sqlite3-wasm.c) $(MAKEFILE) 361 $(emcc.bin) \ 362 $(emcc.speedtest1-flags) \ 363 -I. -I$(dir.top) \ 364 -DSQLITE_THREADSAFE=0 \ 365 -DSQLITE_TEMP_STORE=3 \ 366 -DSQLITE_OMIT_UTF16 \ 367 -DSQLITE_OMIT_DEPRECATED \ 368 -DSQLITE_OMIT_SHARED_CACHE \ 369 '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \ 370 -DSQLITE_SPEEDTEST1_WASM \ 371 -o $@ $(speedtest1.c) $(sqlite3-wasm.c) -lm 372ifneq (,$(wasm-strip)) 373 $(wasm-strip) $(speedtest1.wasm) 374endif 375 ls -la $@ $(speedtest1.wasm) 376 377speedtest1: $(speedtest1.js) 378all: $(speedtest1.js) 379CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm) 380# end speedtest1.js 381######################################################################## 382# fiddle_remote is the remote destination for the fiddle app. It 383# must be a [user@]HOST:/path for rsync. 384# Note that the target "should probably" contain a symlink of 385# index.html -> fiddle.html. 386fiddle_remote ?= 387ifeq (,$(fiddle_remote)) 388ifneq (,$(wildcard /home/stephan)) 389 fiddle_remote = wh:www/wh/sqlite3/. 390else ifneq (,$(wildcard /home/drh)) 391 #fiddle_remote = if appropriate, add that user@host:/path here 392endif 393endif 394$(fiddle_files): default 395push-fiddle: $(fiddle_files) 396 @if [ x = "x$(fiddle_remote)" ]; then \ 397 echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \ 398 exit 1; \ 399 fi 400 rsync -va fiddle/ $(fiddle_remote) 401# end fiddle remote push 402######################################################################## 403