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) 21 22clean: 23 $(MAKE) -C ../../ clean-fiddle 24 -rm -f $(CLEAN_FILES) 25 26MAKEFILE := $(lastword $(MAKEFILE_LIST)) 27dir.top := ../.. 28# Reminder: some Emscripten flags require absolute paths 29dir.wasm := $(patsubst %/,%,$(dir $(abspath $(MAKEFILE)))) 30dir.api := api 31dir.jacc := jaccwabyt 32dir.common := common 33CLEAN_FILES := *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ 34 35SQLITE_OPT = \ 36 -DSQLITE_ENABLE_FTS4 \ 37 -DSQLITE_ENABLE_RTREE \ 38 -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ 39 -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \ 40 -DSQLITE_ENABLE_STMTVTAB \ 41 -DSQLITE_ENABLE_DBPAGE_VTAB \ 42 -DSQLITE_ENABLE_DBSTAT_VTAB \ 43 -DSQLITE_ENABLE_BYTECODE_VTAB \ 44 -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ 45 -DSQLITE_OMIT_LOAD_EXTENSION \ 46 -DSQLITE_OMIT_DEPRECATED \ 47 -DSQLITE_OMIT_UTF16 \ 48 -DSQLITE_THREADSAFE=0 49#SQLITE_OPT += -DSQLITE_ENABLE_MEMSYS5 50$(dir.top)/sqlite3.c: 51 $(MAKE) -C $(dir.top) sqlite3.c 52 53# SQLITE_OMIT_LOAD_EXTENSION: if this is true, sqlite3_vfs::xDlOpen 54# and friends may be NULL. 55 56emcc_opt ?= -O0 57.PHONY: release 58release: 59 $(MAKE) 'emcc_opt=-Os -g3' 60# ^^^^^ target-specific vars, e.g.: 61# release: emcc_opt=... 62# apparently only work for file targets, not PHONY targets? 63# 64# ^^^^ -O3, -Oz, -Os minify symbol names and there appears to be no 65# way around that except to use -g3, but -g3 causes the binary file 66# size to absolutely explode (approx. 5x larger). This minification 67# utterly breaks the resulting module, making it unsable except as 68# self-contained/self-referential-only code, as ALL of the exported 69# symbols get minified names. 70# 71# However, we have an option for using -Oz or -Os: 72# 73# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt 74# tools package (https://github.com/WebAssembly/wabt), to strip the 75# debugging symbols. That results in a small build with unmangled 76# symbol names. -Oz gives ever-so-slightly better compression than 77# -Os: not quite 1% in some completely unscientific tests. Runtime 78# speed for the unit tests is all over the place either way so it's 79# difficult to say whether -Os gives any speed benefit over -Oz. 80######################################################################## 81 82# Emscripten SDK home dir and related binaries... 83EMSDK_HOME ?= $(word 1,$(wildcard $(HOME)/src/emsdk $(HOME)/emsdk)) 84emcc.bin ?= $(word 1,$(wildcard $(shell which emcc) $(EMSDK_HOME)/upstream/emscripten/emcc)) 85ifeq (,$(emcc.bin)) 86 $(error Cannot find emcc.) 87endif 88 89wasm-strip ?= $(shell which wasm-strip 2>/dev/null) 90ifeq (,$(filter clean,$(MAKECMDGOALS))) 91ifeq (,$(wasm-strip)) 92 $(info WARNING: *******************************************************************) 93 $(info WARNING: builds using -O3/-Os/-Oz will minify WASM-exported names,) 94 $(info WARNING: breaking _All The Things_. The workaround for that is to build) 95 $(info WARNING: with -g3 (which explodes the file size) and then strip the debug) 96 $(info WARNING: info after compilation, using wasm-strip, to shrink the wasm file.) 97 $(info WARNING: wasm-strip was not found in the PATH so we cannot strip those.) 98 $(info WARNING: If this build uses any optimization level higher than -O2 then) 99 $(info WARNING: the ***resulting WASM binary WILL NOT BE USABLE***.) 100 $(info WARNING: wasm-strip is part of the wabt package:) 101 $(info WARNING: https://github.com/WebAssembly/wabt) 102 $(info WARNING: on Ubuntu-like systems it can be installed with:) 103 $(info WARNING: sudo apt install wabt) 104 $(info WARNING: *******************************************************************) 105endif 106endif # 'make clean' check 107 108ifeq (release,$(filter release,$(MAKECMDGOALS))) 109 ifeq (,$(wasm-strip)) 110 $(error Cannot make release-quality binary because wasm-strip is not available. \ 111 See notes in the warning above) 112 endif 113else 114 $(info Development build. Use '$(MAKE) release' for a smaller release build.) 115endif 116 117EXPORTED_FUNCTIONS.api.in := $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-api \ 118 $(dir.jacc)/jaccwabyt_test.exports 119 120EXPORTED_FUNCTIONS.api: $(EXPORTED_FUNCTIONS.api.in) $(MAKEFILE) 121 cat $(EXPORTED_FUNCTIONS.api.in) > $@ 122CLEAN_FILES += EXPORTED_FUNCTIONS.api 123 124sqlite3-api.jses := \ 125 $(dir.api)/sqlite3-api-prologue.js \ 126 $(dir.common)/whwasmutil.js \ 127 $(dir.jacc)/jaccwabyt.js \ 128 $(dir.api)/sqlite3-api-glue.js \ 129 $(dir.api)/sqlite3-api-oo1.js \ 130 $(dir.api)/sqlite3-api-worker.js \ 131 $(dir.api)/sqlite3-api-opfs.js \ 132 $(dir.api)/sqlite3-api-cleanup.js 133 134sqlite3-api.js := $(dir.api)/sqlite3-api.js 135CLEAN_FILES += $(sqlite3-api.js) 136$(sqlite3-api.js): $(sqlite3-api.jses) $(MAKEFILE) 137 @echo "Making $@..." 138 @for i in $(sqlite3-api.jses); do \ 139 echo "/* BEGIN FILE: $$i */"; \ 140 cat $$i; \ 141 echo "/* END FILE: $$i */"; \ 142 done > $@ 143 144post-js.js := $(dir.api)/post-js.js 145CLEAN_FILES += $(post-js.js) 146post-jses := \ 147 $(dir.api)/post-js-header.js \ 148 $(sqlite3-api.js) \ 149 $(dir.api)/post-js-footer.js 150 151$(post-js.js): $(post-jses) $(MAKEFILE) 152 @echo "Making $@..." 153 @for i in $(post-jses); do \ 154 echo "/* BEGIN FILE: $$i */"; \ 155 cat $$i; \ 156 echo "/* END FILE: $$i */"; \ 157 done > $@ 158 159 160######################################################################## 161# emcc flags for .c/.o/.wasm. 162emcc.flags = 163#emcc.flags += -v # _very_ loud but also informative about what it's doing 164 165######################################################################## 166# emcc flags for .c/.o. 167emcc.cflags := 168emcc.cflags += -std=c99 -fPIC 169# -------------^^^^^^^^ we currently need c99 for WASM-specific sqlite3 APIs. 170emcc.cflags += -I. -I$(dir.top) # $(SQLITE_OPT) 171emcc.cflags += -pthread 172 173######################################################################## 174# emcc flags specific to building the final .js/.wasm file... 175emcc.jsflags := -fPIC 176emcc.jsflags += --no-entry 177emcc.jsflags += -sENVIRONMENT=web,worker 178emcc.jsflags += -sMODULARIZE 179emcc.jsflags += -sSTRICT_JS 180emcc.jsflags += -sDYNAMIC_EXECUTION=0 181emcc.jsflags += -sNO_POLYFILL 182emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api 183emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory # wasmMemory==>for -sIMPORTED_MEMORY 184emcc.jsflags += -sUSE_CLOSURE_COMPILER=0 185emcc.jsflags += -sIMPORTED_MEMORY 186emcc.jsflags += -pthread -sWASMFS 187#emcc.jsflags += -sINITIAL_MEMORY=13107200 188#emcc.jsflags += -sTOTAL_STACK=4194304 189emcc.jsflags += -sEXPORT_NAME=sqlite3InitModule 190emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr. 191emcc.jsflags +=--post-js=$(post-js.js) 192#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...() 193#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS 194#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API 195#emcc.jsflags += -sABORTING_MALLOC 196emcc.jsflags += -sALLOW_MEMORY_GROWTH 197emcc.jsflags += -sALLOW_TABLE_GROWTH 198emcc.jsflags += -Wno-limited-postlink-optimizations 199# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag. 200#emcc.jsflags += -sMALLOC=emmalloc 201#emcc.jsflags += -sMALLOC=dlmalloc # a good 8k larger than emmalloc 202#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why 203#emcc.jsflags += --import=foo_bar 204#emcc.jsflags += --no-gc-sections 205# https://lld.llvm.org/WebAssembly.html 206emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0 207emcc.jsflags += -sLLD_REPORT_UNDEFINED 208#emcc.jsflags += --allow-undefined 209emcc.jsflags += --import-undefined 210#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic 211#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined 212#emcc.jsflags += --unresolved-symbols=ignore-all 213enable_bigint ?= 1 214ifneq (0,$(enable_bigint)) 215emcc.jsflags += -sWASM_BIGINT 216endif 217emcc.jsflags += -sMEMORY64=0 218# ^^^^ MEMORY64=1 fails to load, erroring with: 219# invalid memory limits flags 0x5 220# (enable via --experimental-wasm-memory64) 221# 222# ^^^^ MEMORY64=2 builds and loads but dies when we do things like: 223# 224# new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n) 225# 226# because ptr is now a BigInt, so is invalid for passing to arguments 227# which have strict must-be-a-number requirements. 228######################################################################## 229 230 231sqlite3.js := $(dir.api)/sqlite3.js 232sqlite3.wasm := $(dir.api)/sqlite3.wasm 233$(dir.api)/sqlite3-wasm.o: emcc.cflags += $(SQLITE_OPT) 234$(dir.api)/sqlite3-wasm.o: $(dir.top)/sqlite3.c 235$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT) 236sqlite3.wasm.c := $(dir.api)/sqlite3-wasm.c \ 237 $(dir.jacc)/jaccwabyt_test.c 238# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test 239# apps. However, we want to test the release builds with those apps, 240# so we cannot simply elide that file in release builds. That 241# component is critical to the VFS bindings so needs to be tested 242# along with the core APIs. 243define WASM_C_COMPILE 244$(1).o := $$(subst .c,.o,$(1)) 245sqlite3.wasm.obj += $$($(1).o) 246$$($(1).o): $$(MAKEFILE) $(1) 247 $$(emcc.bin) $$(emcc_opt) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@ 248CLEAN_FILES += $$($(1).o) 249endef 250$(foreach c,$(sqlite3.wasm.c),$(eval $(call WASM_C_COMPILE,$(c)))) 251$(sqlite3.js): 252$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \ 253 EXPORTED_FUNCTIONS.api \ 254 $(post-js.js) 255 $(emcc.bin) -o $@ $(emcc_opt) $(emcc.flags) $(emcc.jsflags) $(sqlite3.wasm.obj) 256 chmod -x $(sqlite3.wasm) 257ifneq (,$(wasm-strip)) 258 $(wasm-strip) $(sqlite3.wasm) 259endif 260 @ls -la $@ $(sqlite3.wasm) 261 262CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm) 263all: $(sqlite3.js) 264# End main Emscripten-based module build 265######################################################################## 266 267 268######################################################################## 269# fiddle_remote is the remote destination for the fiddle app. It 270# must be a [user@]HOST:/path for rsync. 271# Note that the target "should probably" contain a symlink of 272# index.html -> fiddle.html. 273fiddle_remote ?= 274ifeq (,$(fiddle_remote)) 275ifneq (,$(wildcard /home/stephan)) 276 fiddle_remote = wh:www/wh/sqlite3/. 277else ifneq (,$(wildcard /home/drh)) 278 #fiddle_remote = if appropriate, add that user@host:/path here 279endif 280endif 281$(fiddle_files): default 282push-fiddle: $(fiddle_files) 283 @if [ x = "x$(fiddle_remote)" ]; then \ 284 echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \ 285 exit 1; \ 286 fi 287 rsync -va fiddle/ $(fiddle_remote) 288# end fiddle remote push 289######################################################################## 290