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