xref: /sqlite-3.40.0/ext/wasm/fiddle.make (revision ac79fa1c)
1#!/do/not/make
2#^^^ help emacs select edit mode
3#
4# Intended to include'd by ./GNUmakefile.
5#######################################################################
6MAKEFILE.fiddle := $(lastword $(MAKEFILE_LIST))
7
8########################################################################
9# shell.c and its build flags...
10make-np-0 := make -C  $(dir.top) -n -p
11make-np-1 := sed -e 's/(TOP)/(dir.top)/g'
12$(eval $(shell $(make-np-0) | grep -e '^SHELL_OPT ' | $(make-np-1)))
13$(eval $(shell $(make-np-0) | grep -e '^SHELL_SRC ' | $(make-np-1)))
14# ^^^ can't do that in 1 invocation b/c newlines get stripped
15ifeq (,$(SHELL_OPT))
16$(error Could not parse SHELL_OPT from $(dir.top)/Makefile.)
17endif
18ifeq (,$(SHELL_SRC))
19$(error Could not parse SHELL_SRC from $(dir.top)/Makefile.)
20endif
21$(dir.top)/shell.c: $(SHELL_SRC) $(dir.top)/tool/mkshellc.tcl
22	$(MAKE) -C $(dir.top) shell.c
23# /shell.c
24########################################################################
25
26fiddle.emcc-flags = \
27  $(emcc.cflags) $(emcc_opt_full) \
28  --minify 0 \
29  -sALLOW_TABLE_GROWTH \
30  -sABORTING_MALLOC \
31  -sSTRICT_JS \
32  -sENVIRONMENT=web,worker \
33  -sMODULARIZE \
34  -sDYNAMIC_EXECUTION=0 \
35  -sWASM_BIGINT=$(emcc_enable_bigint) \
36  -sEXPORT_NAME=$(sqlite3.js.init-func) \
37  $(sqlite3.js.flags.--post-js) \
38  -sEXPORTED_RUNTIME_METHODS=@$(dir.wasm)/EXPORTED_RUNTIME_METHODS.fiddle \
39  -sEXPORTED_FUNCTIONS=@$(dir.wasm)/EXPORTED_FUNCTIONS.fiddle \
40  $(SQLITE_OPT) $(SHELL_OPT) \
41  -DSQLITE_SHELL_FIDDLE
42# -D_POSIX_C_SOURCE is needed for strdup() with emcc
43
44fiddle.EXPORTED_FUNCTIONS.in := \
45    EXPORTED_FUNCTIONS.fiddle.in \
46    EXPORTED_FUNCTIONS.api
47
48EXPORTED_FUNCTIONS.fiddle: $(fiddle.EXPORTED_FUNCTIONS.in) $(MAKEFILE.fiddle)
49	sort -u $(fiddle.EXPORTED_FUNCTIONS.in) > $@
50
51fiddle-module.js := $(dir.fiddle)/fiddle-module.js
52fiddle-module.wasm := $(subst .js,.wasm,$(fiddle-module.js))
53fiddle.cses := $(dir.top)/shell.c $(sqlite3-wasm.c)
54
55SOAP.js := sqlite3-opfs-async-proxy.js
56$(dir.fiddle)/$(SOAP.js): $(SOAP.js)
57	cp $< $@
58
59$(eval $(call call-make-pre-js,fiddle-module))
60$(fiddle-module.js): $(MAKEFILE) $(MAKEFILE.fiddle) \
61    EXPORTED_FUNCTIONS.fiddle EXPORTED_RUNTIME_METHODS.fiddle \
62    $(fiddle.cses) $(pre-post-fiddle-module.deps) $(dir.fiddle)/$(SOAP.js)
63	$(emcc.bin) -o $@ $(fiddle.emcc-flags) \
64    $(pre-post-common.flags) $(pre-post-fiddle-module.flags) \
65    $(fiddle.cses)
66	$(maybe-wasm-strip) $(fiddle-module.wasm)
67	gzip < $@ > $@.gz
68	gzip < $(fiddle-module.wasm) > $(fiddle-module.wasm).gz
69
70$(dir.fiddle)/fiddle.js.gz: $(dir.fiddle)/fiddle.js
71	gzip < $< > $@
72
73clean: clean-fiddle
74clean-fiddle:
75	rm -f $(fiddle-module.js) $(fiddle-module.js).gz \
76        $(fiddle-module.wasm) $(fiddle-module.wasm).gz \
77        $(dir.fiddle)/$(SOAP.js) \
78        $(dir.fiddle)/fiddle-module.worker.js \
79        EXPORTED_FUNCTIONS.fiddle
80.PHONY: fiddle
81fiddle: $(fiddle-module.js) $(dir.fiddle)/fiddle.js.gz
82all: fiddle
83
84########################################################################
85# Explanation of the emcc build flags follows. Full docs for these can
86# be found at:
87#
88#  https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
89#
90# -sENVIRONMENT=web: elides bootstrap code related to non-web JS
91#  environments like node.js. Removing this makes the output a tiny
92#  tick larger but hypothetically makes it more portable to
93#  non-browser JS environments.
94#
95# -sMODULARIZE: changes how the generated code is structured to avoid
96#  declaring a global Module object and instead installing a function
97#  which loads and initializes the module. The function is named...
98#
99# -sEXPORT_NAME=jsFunctionName (see -sMODULARIZE)
100#
101# -sEXPORTED_RUNTIME_METHODS=@/absolute/path/to/file: a file
102#  containing a list of emscripten-supplied APIs, one per line, which
103#  must be exported into the generated JS. Must be an absolute path!
104#
105# -sEXPORTED_FUNCTIONS=@/absolute/path/to/file: a file containing a
106#  list of C functions, one per line, which must be exported via wasm
107#  so they're visible to JS. C symbols names in that file must all
108#  start with an underscore for reasons known only to the emcc
109#  developers. e.g., _sqlite3_open_v2 and _sqlite3_finalize. Must be
110#  an absolute path!
111#
112# -sSTRICT_JS ensures that the emitted JS code includes the 'use
113#  strict' option. Note that -sSTRICT is more broadly-scoped and
114#  results in build errors.
115#
116# -sALLOW_TABLE_GROWTH is required for (at a minimum) the UDF-binding
117#  feature. Without it, JS functions cannot be made to proxy C-side
118#  callbacks.
119#
120# -sABORTING_MALLOC causes the JS-bound _malloc() to abort rather than
121#  return 0 on OOM. If set to 0 then all code which uses _malloc()
122#  must, just like in C, check the result before using it, else
123#  they're likely to corrupt the JS/WASM heap by writing to its
124#  address of 0. It is, as of this writing, enabled in Emscripten by
125#  default but we enable it explicitly in case that default changes.
126#
127# -sDYNAMIC_EXECUTION=0 disables eval() and the Function constructor.
128#  If the build runs without these, it's preferable to use this flag
129#  because certain execution environments disallow those constructs.
130#  This flag is not strictly necessary, however.
131#
132# -sWASM_BIGINT is UNTESTED but "should" allow the int64-using C APIs
133#  to work with JS/wasm, insofar as the JS environment supports the
134#  BigInt type. That support requires an extremely recent browser:
135#  Safari didn't get that support until late 2020.
136#
137# --no-entry: for compiling library code with no main(). If this is
138#  not supplied and the code has a main(), it is called as part of the
139#  module init process. Note that main() is #if'd out of shell.c
140#  (renamed) when building in wasm mode.
141#
142# --pre-js/--post-js=FILE relative or absolute paths to JS files to
143#  prepend/append to the emcc-generated bootstrapping JS. It's
144#  easier/faster to develop with separate JS files (reduces rebuilding
145#  requirements) but certain configurations, namely -sMODULARIZE, may
146#  require using at least a --pre-js file. They can be used
147#  individually and need not be paired.
148#
149# -O0..-O3 and -Oz: optimization levels affect not only C-style
150#  optimization but whether or not the resulting generated JS code
151#  gets minified. -O0 compiles _much_ more quickly than -O3 or -Oz,
152#  and doesn't minimize any JS code, so is recommended for
153#  development. -O3 or -Oz are recommended for deployment, but
154#  primarily because -Oz will shrink the wasm file notably. JS-side
155#  minification makes little difference in terms of overall
156#  distributable size.
157#
158# --minify 0: disables minification of the generated JS code,
159#  regardless of optimization level. Minification of the JS has
160#  minimal overall effect in the larger scheme of things and results
161#  in JS files which can neither be edited nor viewed as text files in
162#  Fossil (which flags them as binary because of their extreme line
163#  lengths). Interestingly, whether or not the comments in the
164#  generated JS file get stripped is unaffected by this setting and
165#  depends entirely on the optimization level. Higher optimization
166#  levels reduce the size of the JS considerably even without
167#  minification.
168#
169########################################################################
170