xref: /sqlite-3.40.0/ext/wasm/fiddle.make (revision 1fc6ffcc)
14bc2f6b4Sstephan#!/do/not/make
24bc2f6b4Sstephan#^^^ help emacs select edit mode
34bc2f6b4Sstephan#
44bc2f6b4Sstephan# Intended to include'd by ./GNUmakefile.
54bc2f6b4Sstephan#######################################################################
64bc2f6b4SstephanMAKEFILE.fiddle := $(lastword $(MAKEFILE_LIST))
74bc2f6b4Sstephan
84bc2f6b4Sstephan########################################################################
94bc2f6b4Sstephan# shell.c and its build flags...
104bc2f6b4Sstephanmake-np-0 := make -C  $(dir.top) -n -p
114bc2f6b4Sstephanmake-np-1 := sed -e 's/(TOP)/(dir.top)/g'
124bc2f6b4Sstephan$(eval $(shell $(make-np-0) | grep -e '^SHELL_OPT ' | $(make-np-1)))
134bc2f6b4Sstephan$(eval $(shell $(make-np-0) | grep -e '^SHELL_SRC ' | $(make-np-1)))
144bc2f6b4Sstephan# ^^^ can't do that in 1 invocation b/c newlines get stripped
154bc2f6b4Sstephanifeq (,$(SHELL_OPT))
164bc2f6b4Sstephan$(error Could not parse SHELL_OPT from $(dir.top)/Makefile.)
174bc2f6b4Sstephanendif
184bc2f6b4Sstephanifeq (,$(SHELL_SRC))
194bc2f6b4Sstephan$(error Could not parse SHELL_SRC from $(dir.top)/Makefile.)
204bc2f6b4Sstephanendif
214bc2f6b4Sstephan$(dir.top)/shell.c: $(SHELL_SRC) $(dir.top)/tool/mkshellc.tcl
224bc2f6b4Sstephan	$(MAKE) -C $(dir.top) shell.c
234bc2f6b4Sstephan# /shell.c
244bc2f6b4Sstephan########################################################################
254bc2f6b4Sstephan
2671de8e02SstephanEXPORTED_FUNCTIONS.fiddle := $(dir.tmp)/EXPORTED_FUNCTIONS.fiddle
274bc2f6b4Sstephanfiddle.emcc-flags = \
28b0ccf50cSstephan  $(emcc.cflags) $(emcc_opt_full) \
294bc2f6b4Sstephan  --minify 0 \
304bc2f6b4Sstephan  -sALLOW_TABLE_GROWTH \
314bc2f6b4Sstephan  -sABORTING_MALLOC \
324bc2f6b4Sstephan  -sSTRICT_JS \
334bc2f6b4Sstephan  -sENVIRONMENT=web,worker \
344bc2f6b4Sstephan  -sMODULARIZE \
354bc2f6b4Sstephan  -sDYNAMIC_EXECUTION=0 \
36*1fc6ffccSstephan  -sWASM_BIGINT=$(emcc.WASM_BIGINT) \
37eb97743cSstephan  -sEXPORT_NAME=$(sqlite3.js.init-func) \
389dc4d5e6Sstephan  -Wno-limited-postlink-optimizations \
39eb97743cSstephan  $(sqlite3.js.flags.--post-js) \
4071de8e02Sstephan  $(emcc.exportedRuntimeMethods) \
4171de8e02Sstephan  -sEXPORTED_FUNCTIONS=@$(abspath $(EXPORTED_FUNCTIONS.fiddle)) \
424bc2f6b4Sstephan  $(SQLITE_OPT) $(SHELL_OPT) \
434bc2f6b4Sstephan  -DSQLITE_SHELL_FIDDLE
444bc2f6b4Sstephan# -D_POSIX_C_SOURCE is needed for strdup() with emcc
454bc2f6b4Sstephan
464bc2f6b4Sstephanfiddle.EXPORTED_FUNCTIONS.in := \
474bc2f6b4Sstephan    EXPORTED_FUNCTIONS.fiddle.in \
4871de8e02Sstephan    $(EXPORTED_FUNCTIONS.api)
494bc2f6b4Sstephan
5071de8e02Sstephan$(EXPORTED_FUNCTIONS.fiddle): $(fiddle.EXPORTED_FUNCTIONS.in) $(MAKEFILE.fiddle)
51e79a0943Sstephan	sort -u $(fiddle.EXPORTED_FUNCTIONS.in) > $@
524bc2f6b4Sstephan
534bc2f6b4Sstephanfiddle-module.js := $(dir.fiddle)/fiddle-module.js
544bc2f6b4Sstephanfiddle-module.wasm := $(subst .js,.wasm,$(fiddle-module.js))
55e79a0943Sstephanfiddle.cses := $(dir.top)/shell.c $(sqlite3-wasm.c)
564bc2f6b4Sstephan
57cd0df83cSstephanfiddle.SOAP.js := $(dir.fiddle)/$(notdir $(SOAP.js))
58cd0df83cSstephan$(fiddle.SOAP.js): $(SOAP.js)
596110a5d0Sstephan	cp $< $@
606110a5d0Sstephan
61f71c954cSstephan$(eval $(call call-make-pre-js,fiddle-module))
624bc2f6b4Sstephan$(fiddle-module.js): $(MAKEFILE) $(MAKEFILE.fiddle) \
6371de8e02Sstephan    $(EXPORTED_FUNCTIONS.fiddle) \
64cd0df83cSstephan    $(fiddle.cses) $(pre-post-fiddle-module.deps) $(fiddle.SOAP.js)
65f71c954cSstephan	$(emcc.bin) -o $@ $(fiddle.emcc-flags) \
6632781427Sstephan    $(pre-post-common.flags) $(pre-post-fiddle-module.flags) \
67e79a0943Sstephan    $(fiddle.cses)
684bc2f6b4Sstephan	$(maybe-wasm-strip) $(fiddle-module.wasm)
694bc2f6b4Sstephan	gzip < $@ > $@.gz
704bc2f6b4Sstephan	gzip < $(fiddle-module.wasm) > $(fiddle-module.wasm).gz
714bc2f6b4Sstephan
724bc2f6b4Sstephan$(dir.fiddle)/fiddle.js.gz: $(dir.fiddle)/fiddle.js
734bc2f6b4Sstephan	gzip < $< > $@
744bc2f6b4Sstephan
754bc2f6b4Sstephanclean: clean-fiddle
764bc2f6b4Sstephanclean-fiddle:
774bc2f6b4Sstephan	rm -f $(fiddle-module.js) $(fiddle-module.js).gz \
784bc2f6b4Sstephan        $(fiddle-module.wasm) $(fiddle-module.wasm).gz \
796110a5d0Sstephan        $(dir.fiddle)/$(SOAP.js) \
80eb97743cSstephan        $(dir.fiddle)/fiddle-module.worker.js \
814bc2f6b4Sstephan        EXPORTED_FUNCTIONS.fiddle
824bc2f6b4Sstephan.PHONY: fiddle
834bc2f6b4Sstephanfiddle: $(fiddle-module.js) $(dir.fiddle)/fiddle.js.gz
844bc2f6b4Sstephanall: fiddle
854bc2f6b4Sstephan
864bc2f6b4Sstephan########################################################################
87ce2d4ae6Sstephan# fiddle_remote is the remote destination for the fiddle app. It
88ce2d4ae6Sstephan# must be a [user@]HOST:/path for rsync.
89ce2d4ae6Sstephan# Note that the target "should probably" contain a symlink of
90ce2d4ae6Sstephan# index.html -> fiddle.html.
91ce2d4ae6Sstephanfiddle_remote ?=
92ce2d4ae6Sstephanifeq (,$(fiddle_remote))
93ce2d4ae6Sstephanifneq (,$(wildcard /home/stephan))
94ce2d4ae6Sstephan  fiddle_remote = wh:www/wh/sqlite3/.
95ce2d4ae6Sstephanelse ifneq (,$(wildcard /home/drh))
96ce2d4ae6Sstephan  #fiddle_remote = if appropriate, add that user@host:/path here
97ce2d4ae6Sstephanendif
98ce2d4ae6Sstephanendif
99ce2d4ae6Sstephanpush-fiddle: fiddle
100ce2d4ae6Sstephan	@if [ x = "x$(fiddle_remote)" ]; then \
101ce2d4ae6Sstephan		echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \
102ce2d4ae6Sstephan		exit 1; \
103ce2d4ae6Sstephan	fi
104ce2d4ae6Sstephan	rsync -va fiddle/ $(fiddle_remote)
105ce2d4ae6Sstephan# end fiddle remote push
106ce2d4ae6Sstephan########################################################################
107ce2d4ae6Sstephan
108ce2d4ae6Sstephan
109ce2d4ae6Sstephan########################################################################
1104bc2f6b4Sstephan# Explanation of the emcc build flags follows. Full docs for these can
1114bc2f6b4Sstephan# be found at:
1124bc2f6b4Sstephan#
1134bc2f6b4Sstephan#  https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
1144bc2f6b4Sstephan#
1154bc2f6b4Sstephan# -sENVIRONMENT=web: elides bootstrap code related to non-web JS
1164bc2f6b4Sstephan#  environments like node.js. Removing this makes the output a tiny
1174bc2f6b4Sstephan#  tick larger but hypothetically makes it more portable to
1184bc2f6b4Sstephan#  non-browser JS environments.
1194bc2f6b4Sstephan#
1204bc2f6b4Sstephan# -sMODULARIZE: changes how the generated code is structured to avoid
1214bc2f6b4Sstephan#  declaring a global Module object and instead installing a function
1224bc2f6b4Sstephan#  which loads and initializes the module. The function is named...
1234bc2f6b4Sstephan#
1244bc2f6b4Sstephan# -sEXPORT_NAME=jsFunctionName (see -sMODULARIZE)
1254bc2f6b4Sstephan#
1264bc2f6b4Sstephan# -sEXPORTED_RUNTIME_METHODS=@/absolute/path/to/file: a file
1274bc2f6b4Sstephan#  containing a list of emscripten-supplied APIs, one per line, which
1284bc2f6b4Sstephan#  must be exported into the generated JS. Must be an absolute path!
1294bc2f6b4Sstephan#
1304bc2f6b4Sstephan# -sEXPORTED_FUNCTIONS=@/absolute/path/to/file: a file containing a
1314bc2f6b4Sstephan#  list of C functions, one per line, which must be exported via wasm
1324bc2f6b4Sstephan#  so they're visible to JS. C symbols names in that file must all
1334bc2f6b4Sstephan#  start with an underscore for reasons known only to the emcc
1344bc2f6b4Sstephan#  developers. e.g., _sqlite3_open_v2 and _sqlite3_finalize. Must be
1354bc2f6b4Sstephan#  an absolute path!
1364bc2f6b4Sstephan#
1374bc2f6b4Sstephan# -sSTRICT_JS ensures that the emitted JS code includes the 'use
1384bc2f6b4Sstephan#  strict' option. Note that -sSTRICT is more broadly-scoped and
1394bc2f6b4Sstephan#  results in build errors.
1404bc2f6b4Sstephan#
1414bc2f6b4Sstephan# -sALLOW_TABLE_GROWTH is required for (at a minimum) the UDF-binding
1424bc2f6b4Sstephan#  feature. Without it, JS functions cannot be made to proxy C-side
1434bc2f6b4Sstephan#  callbacks.
1444bc2f6b4Sstephan#
1454bc2f6b4Sstephan# -sABORTING_MALLOC causes the JS-bound _malloc() to abort rather than
1464bc2f6b4Sstephan#  return 0 on OOM. If set to 0 then all code which uses _malloc()
1474bc2f6b4Sstephan#  must, just like in C, check the result before using it, else
1484bc2f6b4Sstephan#  they're likely to corrupt the JS/WASM heap by writing to its
1494bc2f6b4Sstephan#  address of 0. It is, as of this writing, enabled in Emscripten by
1504bc2f6b4Sstephan#  default but we enable it explicitly in case that default changes.
1514bc2f6b4Sstephan#
1524bc2f6b4Sstephan# -sDYNAMIC_EXECUTION=0 disables eval() and the Function constructor.
1534bc2f6b4Sstephan#  If the build runs without these, it's preferable to use this flag
1544bc2f6b4Sstephan#  because certain execution environments disallow those constructs.
1554bc2f6b4Sstephan#  This flag is not strictly necessary, however.
1564bc2f6b4Sstephan#
1574bc2f6b4Sstephan# -sWASM_BIGINT is UNTESTED but "should" allow the int64-using C APIs
1584bc2f6b4Sstephan#  to work with JS/wasm, insofar as the JS environment supports the
1594bc2f6b4Sstephan#  BigInt type. That support requires an extremely recent browser:
1604bc2f6b4Sstephan#  Safari didn't get that support until late 2020.
1614bc2f6b4Sstephan#
1624bc2f6b4Sstephan# --no-entry: for compiling library code with no main(). If this is
1634bc2f6b4Sstephan#  not supplied and the code has a main(), it is called as part of the
1644bc2f6b4Sstephan#  module init process. Note that main() is #if'd out of shell.c
1654bc2f6b4Sstephan#  (renamed) when building in wasm mode.
1664bc2f6b4Sstephan#
1674bc2f6b4Sstephan# --pre-js/--post-js=FILE relative or absolute paths to JS files to
1684bc2f6b4Sstephan#  prepend/append to the emcc-generated bootstrapping JS. It's
1694bc2f6b4Sstephan#  easier/faster to develop with separate JS files (reduces rebuilding
1704bc2f6b4Sstephan#  requirements) but certain configurations, namely -sMODULARIZE, may
1714bc2f6b4Sstephan#  require using at least a --pre-js file. They can be used
1724bc2f6b4Sstephan#  individually and need not be paired.
1734bc2f6b4Sstephan#
1744bc2f6b4Sstephan# -O0..-O3 and -Oz: optimization levels affect not only C-style
1754bc2f6b4Sstephan#  optimization but whether or not the resulting generated JS code
1764bc2f6b4Sstephan#  gets minified. -O0 compiles _much_ more quickly than -O3 or -Oz,
1774bc2f6b4Sstephan#  and doesn't minimize any JS code, so is recommended for
1784bc2f6b4Sstephan#  development. -O3 or -Oz are recommended for deployment, but
1794bc2f6b4Sstephan#  primarily because -Oz will shrink the wasm file notably. JS-side
1804bc2f6b4Sstephan#  minification makes little difference in terms of overall
1814bc2f6b4Sstephan#  distributable size.
1824bc2f6b4Sstephan#
1834bc2f6b4Sstephan# --minify 0: disables minification of the generated JS code,
1844bc2f6b4Sstephan#  regardless of optimization level. Minification of the JS has
1854bc2f6b4Sstephan#  minimal overall effect in the larger scheme of things and results
1864bc2f6b4Sstephan#  in JS files which can neither be edited nor viewed as text files in
1874bc2f6b4Sstephan#  Fossil (which flags them as binary because of their extreme line
1884bc2f6b4Sstephan#  lengths). Interestingly, whether or not the comments in the
1894bc2f6b4Sstephan#  generated JS file get stripped is unaffected by this setting and
1904bc2f6b4Sstephan#  depends entirely on the optimization level. Higher optimization
1914bc2f6b4Sstephan#  levels reduce the size of the JS considerably even without
1924bc2f6b4Sstephan#  minification.
1934bc2f6b4Sstephan#
1944bc2f6b4Sstephan########################################################################
195