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) 171 172######################################################################## 173# emcc flags specific to building the final .js/.wasm file... 174emcc.jsflags := -fPIC 175emcc.jsflags += --no-entry 176emcc.jsflags += -sENVIRONMENT=web 177emcc.jsflags += -sMODULARIZE 178emcc.jsflags += -sSTRICT_JS 179emcc.jsflags += -sDYNAMIC_EXECUTION=0 180emcc.jsflags += -sNO_POLYFILL 181emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.api 182emcc.jsflags += -sEXPORTED_RUNTIME_METHODS=FS,wasmMemory # wasmMemory==>for -sIMPORTED_MEMORY 183emcc.jsflags += -sUSE_CLOSURE_COMPILER=0 184emcc.jsflags += -sIMPORTED_MEMORY 185#emcc.jsflags += -sINITIAL_MEMORY=13107200 186#emcc.jsflags += -sTOTAL_STACK=4194304 187emcc.jsflags += -sEXPORT_NAME=sqlite3InitModule 188emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr. 189emcc.jsflags +=--post-js=$(post-js.js) 190#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...() 191#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS 192#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. sqlite3 needs the FS API 193#emcc.jsflags += -sABORTING_MALLOC 194emcc.jsflags += -sALLOW_MEMORY_GROWTH 195emcc.jsflags += -sALLOW_TABLE_GROWTH 196emcc.jsflags += -Wno-limited-postlink-optimizations 197# ^^^^^ it likes to warn when we have "limited optimizations" via the -g3 flag. 198#emcc.jsflags += -sMALLOC=emmalloc 199#emcc.jsflags += -sMALLOC=dlmalloc # a good 8k larger than emmalloc 200#emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why 201#emcc.jsflags += --import=foo_bar 202#emcc.jsflags += --no-gc-sections 203# https://lld.llvm.org/WebAssembly.html 204emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=0 205emcc.jsflags += -sLLD_REPORT_UNDEFINED 206#emcc.jsflags += --allow-undefined 207emcc.jsflags += --import-undefined 208#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic 209#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined 210#emcc.jsflags += --unresolved-symbols=ignore-all 211enable_bigint ?= 1 212ifneq (0,$(enable_bigint)) 213emcc.jsflags += -sWASM_BIGINT 214endif 215emcc.jsflags += -sMEMORY64=0 216# ^^^^ MEMORY64=1 fails to load, erroring with: 217# invalid memory limits flags 0x5 218# (enable via --experimental-wasm-memory64) 219# 220# ^^^^ MEMORY64=2 builds and loads but dies when we do things like: 221# 222# new Uint8Array(heapWrappers().HEAP8U.buffer, ptr, n) 223# 224# because ptr is now a BigInt, so is invalid for passing to arguments 225# which have strict must-be-a-number requirements. 226######################################################################## 227 228 229sqlite3.js := $(dir.api)/sqlite3.js 230sqlite3.wasm := $(dir.api)/sqlite3.wasm 231$(dir.api)/sqlite3-wasm.o: emcc.cflags += $(SQLITE_OPT) 232$(dir.api)/sqlite3-wasm.o: $(dir.top)/sqlite3.c 233$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT) 234sqlite3.wasm.c := $(dir.api)/sqlite3-wasm.c \ 235 $(dir.jacc)/jaccwabyt_test.c 236# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test 237# apps. However, we want to test the release builds with those apps, 238# so we cannot simply elide that file in release builds. That 239# component is critical to the VFS bindings so needs to be tested 240# along with the core APIs. 241define WASM_C_COMPILE 242$(1).o := $$(subst .c,.o,$(1)) 243sqlite3.wasm.obj += $$($(1).o) 244$$($(1).o): $$(MAKEFILE) $(1) 245 $$(emcc.bin) $$(emcc_opt) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@ 246CLEAN_FILES += $$($(1).o) 247endef 248$(foreach c,$(sqlite3.wasm.c),$(eval $(call WASM_C_COMPILE,$(c)))) 249$(sqlite3.js): 250$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \ 251 EXPORTED_FUNCTIONS.api \ 252 $(post-js.js) 253 $(emcc.bin) -o $@ $(emcc_opt) $(emcc.flags) $(emcc.jsflags) $(sqlite3.wasm.obj) 254 chmod -x $(sqlite3.wasm) 255ifneq (,$(wasm-strip)) 256 $(wasm-strip) $(sqlite3.wasm) 257endif 258 @ls -la $@ $(sqlite3.wasm) 259 260CLEAN_FILES += $(sqlite3.js) $(sqlite3.wasm) 261all: $(sqlite3.js) 262# End main Emscripten-based module build 263######################################################################## 264 265 266######################################################################## 267# fiddle_remote is the remote destination for the fiddle app. It 268# must be a [user@]HOST:/path for rsync. 269# Note that the target "should probably" contain a symlink of 270# index.html -> fiddle.html. 271fiddle_remote ?= 272ifeq (,$(fiddle_remote)) 273ifneq (,$(wildcard /home/stephan)) 274 fiddle_remote = wh:www/wh/sqlite3/. 275else ifneq (,$(wildcard /home/drh)) 276 #fiddle_remote = if appropriate, add that user@host:/path here 277endif 278endif 279$(fiddle_files): default 280push-fiddle: $(fiddle_files) 281 @if [ x = "x$(fiddle_remote)" ]; then \ 282 echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \ 283 exit 1; \ 284 fi 285 rsync -va fiddle/ $(fiddle_remote) 286# end fiddle remote push 287######################################################################## 288