interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / mkwasmbuilds.c

1126 lines · 37110 bytes Raw Blame History
1/*
2** 2024-09-23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This app's single purpose is to emit parts of the Makefile code for
14** sqlite3's canonical WASM build. The main motivation is to generate
15** code which "could" be created via GNU Make's eval command but is
16** highly illegible when constructed that way. Attempts to write this
17** app in Bash and TCL have suffered from the problem that those
18** languages require escaping $ symbols, making the resulting script
19** code as illegible as the eval spaghetti we want to get away
20** from. Maintaining it in C is, somewhat surprisingly, _slightly_
21** less illegible than writing it in bash, tcl, or native Make code.
22**
23** The emitted makefile code is not standalone - it depends on
24** variables and $(call)able functions from the main makefile.
25*/
26
27#undef NDEBUG
28#define DEBUG 1
29#include <assert.h>
30#include <stdio.h>
31#include <string.h>
32
33#define pf printf
34#define ps puts
35
36/* Separator to help eyeballs find the different output sections */
37#define zBanner \
38 "\n########################################################################\n"
39
40/*
41** Flags for use with BuildDef::flags.
42**
43** Maintenance reminder: do not combine F_... flags within this enum,
44** e.g. F_BUNDLER_FRIENDLY=0x02|F_ESM, as that will lead to breakage
45** in some of the flag checks.
46*/
47enum BuildDefFlags {
48 /* Indicates an ESM module build. */
49 F_ESM = 0x01,
50 /* Indicates a "bundler-friendly" build mode. These are untested and
51 ** unsupported, provided solely for the downstream npm subproject
52 ** (who is responsible for any testing of these).
53 **
54 ** The only difference beween bundler-friendly and esm builds is
55 ** that bundlers require static filename strings in a few places due
56 ** to limitations of bundler tooling, whereas vanilla and ESM can
57 ** both work with dynamic strings.
58 */
59 F_BUNDLER_FRIENDLY = 1<<1,
60 /* Indicates that this build is unsupported. Such builds are not
61 ** added to the 'all' target. The unsupported builds exist primarily
62 ** for experimentation's sake. */
63 F_UNSUPPORTED = 1<<2,
64 /* Elide this build from the 'all' target. */
65 F_NOT_IN_ALL = 1<<3,
66 /* If it's a 64-bit build. */
67 F_64BIT = 1<<4,
68 /* Indicates a node.js-for-node.js build. This build is very
69 ** specificially untested and unsupported, but the downstream npm
70 ** project makes use of it. None of our JS code is specific to node,
71 ** but Emscripten's generated sqlite3.js differs between
72 ** for-the-browser and for-node builds. */
73 F_NODEJS = 1<<5,
74 /* Indicates a wasmfs build (untested and unsupported). */
75 F_WASMFS = 1<<6,
76
77 /*
78 ** Which compiled files from $(dir.dout)/buildName/*.{js,mjs,wasm}
79 ** to copy to $(dir.dout) after creating them. This should only be
80 ** applied to builds which result in end-user deliverables. Some
81 ** builds, like the bundler-friendly ones, are a hybrid: we keep
82 ** only their JS file and patch their JS to use the WASM file from a
83 ** canonical build which uses that same WASM file. Reusing X.wasm
84 ** that way can only work for builds which are processed identically
85 ** by Emscripten. For a given set of C flags (as distinct from
86 ** JS-influencing flags), all builds of X.js and Y.js will produce
87 ** identical X.wasm and Y.wasm files. Their JS files may well
88 ** differ, however.
89 */
90 CP_JS = 1 << 30, /* X.js or X.mjs, depending on F_ESM */
91 CP_WASM = 1 << 31, /* X.wasm */
92 CP_ALL = CP_JS | CP_WASM
93};
94
95/*
96** Info needed for building one concrete JS/WASM combination..
97**
98** Notes about Emscripten builds...
99**
100** When emcc processes X.js it also generates X.wasm and hard-codes
101** the name "X.wasm" into the JS file (it has to - there's no reliable
102** way to derive that name at runtime for certain modes of loading the
103** WASM file). Because we only need two sqlite3.wasm files (one each
104** for 32- and 64-bit), the build then copies just those into the
105** final build directory $(dir.dout).
106**
107** To keep parallel builds from stepping on each other, each distinct
108** build goes into its own subdir $(dir.dout)/$(BuildDef::zBaseName).
109** Builds which produce deliverables we'd like to keep/distribute copy
110** their final results into the build dir $(dir.dout). See the notes
111** for the CP_JS enum entry for more details on that.
112**
113** The final result of each build is a pair of JS/WASM files, but
114** getting there requires generation of several files, primarily as
115** inputs for specific Emscripten flags:
116**
117** --pre-js = file gets injected after Emscripten's earliest starting
118** point, enabling limited customization of Emscripten's
119** behavior. This code lives/runs within the generated sqlite3InitModule().
120**
121** --post-js = gets injected after Emscripten's main work, but still
122** within the body of sqlite3InitModule().
123**
124** --extern-pre-js = gets injected before sqlite3InitModule(), in the
125** global scope. We inject the license and version info here.
126**
127** --extern-post-js = gets injected immediately after
128** sqlite3InitModule(), in the global scope. In this step we replace
129** sqlite3InitModule() with a slightly customized one, the main
130** purpose of which is to (A) give us (not Emscripten) control over
131** the arguments it accepts and (B) to run the library bootstrap step.
132**
133** Then there's sqlite3-api.BuildName.js, which is the entire SQLite3
134** JS API (generated from the list defined in $(sqlite3-api.jses)). It
135** gets sandwitched inside --post-js.
136**
137** Each of those inputs has to be generated before passing them on to
138** Emscripten so that any build-specific capabilities can get filtered
139** in or out (using ./c-pp).
140*/
141struct BuildDef {
142 /*
143 ** Base name of output JS and WASM files. The X part of X.js and
144 ** X.wasm.
145 */
146 const char *zBaseName;
147 /*
148 ** A glyph to use in log messages for this build, intended to help
149 ** the eyes distinguish the build lines more easily in parallel
150 ** builds.
151 **
152 ** The convention for 32- vs 64-bit pairs is to give them similar
153 ** emoji, e.g. a cookie for 32-bit and a donut or cake for 64.
154 ** Alternately, the same emoji with a "64" suffix, except that that
155 ** throws off the output alignment in parallel builds ;).
156 */
157 const char *zEmo;
158 /*
159 ** If the build needs its x.wasm renamed in its x.{js,mjs} then this
160 ** must hold the base name to rename it to. Typically "sqlite3" or
161 ** "sqlite3-64bit". This is the case for builds which are named
162 ** something like sqlite3-foo-bar but can use the vanilla
163 ** sqlite3.wasm file. In such cases we don't need the extra
164 ** sqlite3-foo-bar.wasm which Emscripten (necessarily) creates when
165 ** compiling the module, so we patch (at build-time) the JS file to
166 ** use this name instead of sqlite3-foo-bar.
167 */
168 const char *zDotWasm;
169 const char *zCmppD; /* Extra -D... flags for c-pp */
170 const char *zEmcc; /* Full flags for emcc. Normally NULL for default. */
171 const char *zEmccExtra; /* Extra flags for emcc */
172 const char *zDeps; /* Extra make target deps */
173 const char *zEnv; /* emcc -sENVIRONMENT=... value */
174 /*
175 ** Makefile code "ifeq (...)". If set, this build is enclosed in a
176 ** $zIfCond/endif block.
177 */
178 const char *zIfCond;
179 int flags; /* Flags from BuildDefFlags */
180};
181typedef struct BuildDef BuildDef;
182
183/*
184** List of distinct library builds. Each one has to be set up in
185** oBuildDefs. See the next comment block.
186**
187** Many makefile vars use these identifiers for naming stuff, e.g.:
188**
189** out.NAME.js = output JS file for the build named NAME
190** out.NAME.wasm = output WASM file for the build named NAME
191** logtag.NAME = Used for decorating log output
192**
193** etc.
194*/
195#define BuildDefs_map(E) \
196 E(vanilla) E(vanilla64) \
197 E(esm) E(esm64) \
198 E(bundler) E(bundler64) \
199 E(speedtest1) E(speedtest164) \
200 E(node) E(node64) \
201 E(wasmfs)
202
203/*
204** The set of WASM builds for the library (as opposed to the apps
205** (fiddle, speedtest1)). Their order in BuildDefs_map is mostly
206** insignificant, but some makefile vars used by some builds are set
207** up by prior builds. Because of that, the vanilla, esm, and
208** bundler-friendly builds should be defined first (in that order).
209*/
210struct BuildDefs {
211#define E(N) BuildDef N;
212 BuildDefs_map(E)
213#undef E
214};
215typedef struct BuildDefs BuildDefs;
216
217const BuildDefs oBuildDefs = {
218 /*
219 ** The canonical build, against which all others are compared and
220 ** contrasted. This is the one we post downloads for.
221 **
222 ** This one's zBaseName and zEnv MUST be non-NULL so it can be used
223 ** as a default for all others
224 */
225 .vanilla = {
226 .zEmo = "🍦",
227 .zBaseName = "sqlite3",
228 .zDotWasm = 0,
229 .zCmppD = 0,
230 .zEmcc = 0,
231 .zEmccExtra = 0,
232 .zEnv = "web,worker",
233 .zDeps = 0,
234 .zIfCond = 0,
235 .flags = CP_ALL
236 },
237
238 /* The canonical build in 64-bit. */
239 .vanilla64 = {
240 .zEmo = "🍨",
241 .zBaseName = "sqlite3-64bit",
242 .zDotWasm = 0,
243 .zCmppD = 0,
244 .zEmcc = 0,
245 .zEmccExtra = "$(emcc.MEMORY64)",
246 .zEnv = 0,
247 .zDeps = 0,
248 .zIfCond = 0,
249 .flags = CP_ALL | F_64BIT | F_NOT_IN_ALL
250 },
251
252 /* The canonical esm build. */
253 .esm = {
254 .zEmo = "🍬",
255 .zBaseName = "sqlite3",
256 .zDotWasm = 0,
257 .zCmppD = "-Dtarget:es6-module",
258 .zEmcc = 0,
259 .zEmccExtra = 0,
260 .zEnv = 0,
261 .zDeps = 0,
262 .zIfCond = 0,
263 .flags = CP_JS | F_ESM
264 },
265
266 /* The canonical esm build in 64-bit. */
267 .esm64 = {
268 .zEmo = "🍫",
269 .zBaseName = "sqlite3-64bit",
270 .zDotWasm = 0,
271 .zCmppD = "-Dtarget:es6-module",
272 .zEmcc = 0,
273 .zEmccExtra = "$(emcc.MEMORY64)",
274 .zEnv = 0,
275 .zDeps = 0,
276 .zIfCond = 0,
277 .flags = CP_JS | F_ESM | F_64BIT | F_NOT_IN_ALL
278 },
279
280 /* speedtest1, our primary benchmarking tool */
281 .speedtest1 = {
282 .zEmo = "🛼",
283 .zBaseName = "speedtest1",
284 .zDotWasm = 0,
285 .zCmppD = 0,
286 .zEmcc =
287 "$(emcc.speedtest1)"
288 " $(emcc.speedtest1.common)"
289 " $(pre-post.speedtest1.flags)"
290 " $(cflags.common)"
291 " -DSQLITE_SPEEDTEST1_WASM"
292 " $(SQLITE_OPT)"
293 " -USQLITE_WASM_BARE_BONES"
294 " -USQLITE_C -DSQLITE_C=$(sqlite3.c)"
295 " $(speedtest1.exit-runtime0)"
296 " $(speedtest1.c.in)"
297 " -lm",
298 .zEmccExtra = 0,
299 .zEnv = 0,
300 .zDeps =
301 "$(speedtest1.c.in)"
302 " $(EXPORTED_FUNCTIONS.speedtest1)",
303 .zIfCond = 0,
304 .flags = CP_ALL
305 },
306
307 /* speedtest1 64-bit */
308 .speedtest164 = {
309 .zEmo = "🛼64",
310 .zBaseName = "speedtest1-64bit",
311 .zDotWasm = 0,
312 .zCmppD = 0,
313 .zEmcc =
314 "$(emcc.speedtest1)"
315 " $(emcc.speedtest1.common)"
316 " $(emcc.MEMORY64)"
317 " $(pre-post.speedtest164.flags)"
318 " $(cflags.common)"
319 " -DSQLITE_SPEEDTEST1_WASM"
320 " $(SQLITE_OPT)"
321 " -USQLITE_WASM_BARE_BONES"
322 " -USQLITE_C -DSQLITE_C=$(sqlite3.c)"
323 " $(speedtest1.exit-runtime0)"
324 " $(speedtest1.c.in)"
325 " -lm",
326 .zEmccExtra = 0,
327 .zEmccExtra = 0,
328 .zEnv = 0,
329 .zDeps =
330 "$(speedtest1.c.in)"
331 " $(EXPORTED_FUNCTIONS.speedtest1)",
332 .zIfCond = 0,
333 .flags = CP_ALL | F_NOT_IN_ALL
334 },
335
336 /*
337 ** Core bundler-friendly build. Untested and "not really" supported,
338 ** but required by the downstream npm subproject.
339 **
340 ** Testing these requires special-purpose node-based tools and
341 ** custom test apps, none of which we have/use. So we can pass them
342 ** off as-is to the npm subproject and they spot failures pretty
343 ** quickly ;).
344 */
345 .bundler = {
346 .zEmo = "👛",
347 .zBaseName = "sqlite3-bundler-friendly",
348 .zDotWasm = "sqlite3",
349 .zCmppD = "$(c-pp.D.esm) -Dtarget:es6-bundler-friendly",
350 .zEmcc = 0,
351 .zEmccExtra = 0,
352 .zEnv = 0,
353 .zDeps = 0,
354 .zIfCond = 0,
355 .flags = CP_JS | F_BUNDLER_FRIENDLY | F_ESM | F_NOT_IN_ALL
356 },
357
358 /* 64-bit bundler-friendly. */
359 .bundler64 = {
360 .zEmo = "📦",
361 .zBaseName = "sqlite3-bundler-friendly-64bit",
362 .zDotWasm = "sqlite3-64bit",
363 .zCmppD = "$(c-pp.D.bundler)",
364 .zEmcc = 0,
365 .zEmccExtra = "$(emcc.MEMORY64)",
366 .zEnv = 0,
367 .zDeps = 0,
368 .zIfCond = 0,
369 .flags = CP_JS | F_ESM | F_BUNDLER_FRIENDLY | F_64BIT | F_NOT_IN_ALL
370 },
371
372 /*
373 ** We neither build node builds on a regular basis nor test them at
374 ** all. They are fully unsupported. Also, our JS targets only
375 ** browsers.
376 */
377 .node = {
378 .zEmo = "🍟",
379 .zBaseName = "sqlite3-node",
380 .zDotWasm = "sqlite3",
381 .zCmppD = "-Dtarget:node $(c-pp.D.bundler)",
382 .zEmcc = 0,
383 .zEmccExtra = 0,
384 .zEnv = "node"
385 /* Adding ",node" to the zEnv list for the other builds causes
386 ** Emscripten to generate code which confuses node: it cannot
387 ** reliably determine whether the build is for a browser or for
388 ** node. */,
389 .zDeps = 0,
390 .zIfCond = 0,
391 .flags = CP_JS | F_UNSUPPORTED | F_ESM | F_NODEJS
392 },
393
394 /* 64-bit node. */
395 .node64 = {
396 .zEmo = "🍔",
397 .zBaseName = "sqlite3-node-64bit",
398 .zDotWasm = "sqlite3-64bit",
399 .zCmppD = "-Dtarget:node $(c-pp.D.bundler)",
400 .zEmcc = 0,
401 .zEmccExtra = 0,
402 .zEnv = "node",
403 .zDeps = 0,
404 .zIfCond = 0,
405 .flags = CP_JS | F_UNSUPPORTED | F_ESM | F_NODEJS | F_64BIT
406 },
407
408 /* Entirely unsupported. */
409 .wasmfs = {
410 .zEmo = "💿",
411 .zBaseName = "sqlite3-wasmfs",
412 .zDotWasm = 0,
413 .zCmppD = "$(c-pp.D.bundler) -Dwasmfs",
414 .zEmcc = 0,
415 .zEmccExtra =
416 "-sEXPORT_ES6 -sUSE_ES6_IMPORT_META"
417 " -sUSE_CLOSURE_COMPILER=0"
418 " -pthread -sWASMFS -sPTHREAD_POOL_SIZE=1"
419 " -sERROR_ON_UNDEFINED_SYMBOLS=0 -sLLD_REPORT_UNDEFINED"
420 " -DSQLITE_ENABLE_WASMFS"
421 ,
422 .zEnv = 0,
423 .zDeps = 0,
424 .zIfCond = "ifeq (1,$(wasmfs.enable))",
425 .flags = CP_ALL | F_UNSUPPORTED | F_WASMFS | F_ESM
426 }
427};
428
429/*
430** Emits common vars needed by the rest of the emitted code (but not
431** needed by makefile code outside of these generated pieces).
432*/
433static void mk_prologue(void){
434 /* A 0-terminated list of makefile vars which we expect to have been
435 ** set up by this point in the build process. */
436 char const * aRequiredVars[] = {
437 "dir.top",
438 "dir.api", "dir.dout", "dir.tmp",
439 "dir.fiddle", "dir.fiddle.debug",
440 /*"just-testing",*/
441 0
442 };
443 char const * zVar;
444 int i;
445 ps(zBanner "# Build setup sanity checks...");
446 for( i = 0; (zVar = aRequiredVars[i]); ++i ){
447 pf("ifeq (,$(%s))\n", zVar);
448 pf(" $(error build process error: expecting make var $$(%s) to "
449 "have been set up by now)\n", zVar);
450 ps("endif");
451 }
452
453 ps("define label.unsupported-build\n"
454 "$(emo.fire)$(emo.fire)$(emo.fire)Unsupported build:"
455 " use at your own risk!\n"
456 "endef");
457
458 ps(zBanner
459 /** $1 = build name */
460 "b.call.wasm-strip = "
461 "echo '[$(emo.b.$(1)) $(out.$(1).wasm)] $(emo.strip) wasm-strip'; "
462 "$(bin.wasm-strip) $(out.$(1).wasm)\n"
463 );
464
465 pf(zBanner
466 "define b.do.emcc\n"
467 /* $1 = build name */
468 "$(bin.emcc) -o $@ $(emcc_opt_full) $(emcc.flags)"
469 " $(emcc.jsflags) -sENVIRONMENT=$(emcc.environment.$(1))"
470 " $(pre-post.$(1).flags)"
471 " $(emcc.flags.$(1))"
472 " $(cflags.common) $(cflags.$(1))"
473 " $(SQLITE_OPT)"
474 " $(cflags.wasm_extra_init) $(sqlite3-wasm.c.in)\n"
475 "endef\n"
476 );
477
478 {
479 /* b.do.wasm-opt
480 **
481 ** $1 = build name
482 **
483 ** Runs $(out.$(1).wasm) through $(bin.wasm-opt)
484 */
485 const char * zOptFlags =
486 /*
487 ** Flags for wasm-opt. It has many, many, MANY "passes" options
488 ** and the ones which appear here were selected solely on the
489 ** basis of trial and error.
490 **
491 ** All wasm file size savings/costs mentioned below are based on
492 ** the vanilla build of sqlite3.wasm with -Oz (our shipping
493 ** configuration). Comments like "saves nothing" may not be
494 ** technically correct: "nothing" means "some neglible amount."
495 **
496 ** Performance gains or losses are _not_ taken into account
497 ** here, only wasm file size.
498 */
499#if 1
500 /* -Oz builds fail to build if we remove this block, for
501 as-yet-unknown reasons. */
502 "--enable-bulk-memory-opt " /* required */
503 "--all-features " /* required */
504 "--disable-compact-imports " /*
505 https://github.com/emscripten-core/emscripten/issues/28016
506 */
507#endif
508 "--post-emscripten " /* Saves roughly 12kb */
509 "--strip-debug " /* We already wasm-strip, but in
510 ** case this environment has no
511 ** wasm-strip... */
512 /*
513 ** The rest are trial-and-error. See wasm-opt --help and search
514 ** for "Optimization passes" to find the full list.
515 **
516 ** With many flags this gets unusuably slow.
517 */
518 /*"--converge " saves nothing for the options we're using */
519 /*"--dce " saves nothing */
520 /*"--directize " saves nothing */
521 /*"--gsi " no: requires --closed-world flag, which does not
522 ** sound like something we want. */
523 /*"--gufa --gufa-cast-all --gufa-optimizing " costs roughly 2kb */
524 /*"--heap-store-optimization " saves nothing */
525 /*"--heap2local " saves nothing */
526 //"--inlining --inlining-optimizing " costs roughly 3kb */
527 "--local-cse " /* saves roughly 1kb */
528 /*"--once-reduction " saves nothing */
529 /*"--remove-memory-init " presumably a performance tweak */
530 /*"--remove-unused-names " saves nothing */
531 /*"--safe-heap "*/
532 /*"--vacuum " saves nothing */
533 ;
534 ps(zBanner
535 "# post-compilation WASM file optimization");
536
537 /* b.do.wasm-opt $1 = build name*/
538 ps("ifeq (,$(bin.wasm-opt))"); {
539 ps("b.do.wasm-opt = echo '$(logtag.$(1)) wasm-opt not available'");
540 }
541 ps("else"); {
542 ps("define b.do.wasm-opt");
543 pf(
544 "echo '[$(emo.b.$(1)) $(out.$(1).wasm)] $(emo.wasm-opt) $(bin.wasm-opt)';\\\n"
545 "\ttmpfile=$(dir.dout.$(1))/wasm-opt-tmp.$(1).wasm; \\\n"
546 "\trm -f $$tmpfile; \\\n"
547 "\tif $(bin.wasm-opt) $(out.$(1).wasm) "
548 "-o $$tmpfile \\\n"
549 "\t\t%s; then \\\n"
550 "\t\tmv $$tmpfile $(out.$(1).wasm); \\\n"
551#if 0
552 "\t\techo -n 'After wasm-opt: '; \\\n"
553 "\t\tls -l $(1); \\\n"
554#endif
555 /* It's very likely that the set of wasm-opt flags varies from
556 ** version to version, so we'll ignore any errors here. */
557 "\telse \\\n"
558 "\t\trm -f $$tmpfile; \\\n"
559 "\t\techo '$(logtag.$(1)) $(emo.fire) ignoring wasm-opt failure'; \\\n"
560 "\tfi\n",
561 zOptFlags
562 );
563 ps("endef");
564 }
565 ps("endif");
566 }
567
568 ps("more: all");
569}
570
571/*
572** WASM_CUSTOM_INSTANTIATE changes how the JS pieces load the .wasm
573** file from the .js file. When set, our JS takes over that step from
574** Emscripten. Both modes are functionally equivalent but
575** customization gives us access to wasm module state which we don't
576** otherwise have. That said, the library also does not _need_ that
577** state, so we don't _need_ to customize that step.
578*/
579#if !defined(WASM_CUSTOM_INSTANTIATE)
580# define WASM_CUSTOM_INSTANTIATE 0
581#elif (WASM_CUSTOM_INSTANTIATE+0)==0
582# undef WASM_CUSTOM_INSTANTIATE
583# define WASM_CUSTOM_INSTANTIATE 0
584#endif
585
586#if WASM_CUSTOM_INSTANTIATE
587/* c-pp -D... flags for the custom instantiateWasm(). */
588#define C_PP_D_CUSTOM_INSTANTIATE " -DModule.instantiateWasm "
589#else
590#define C_PP_D_CUSTOM_INSTANTIATE
591#endif
592
593static char const * BuildDef_jsext(const BuildDef * pB){
594 return (F_ESM & pB->flags) ? ".mjs" : ".js";
595}
596
597static char const * BuildDef_basename(const BuildDef * pB){
598 return pB->zBaseName ? pB->zBaseName : oBuildDefs.vanilla.zBaseName;
599}
600
601/*
602** Emits makefile code for setting up values for the --pre-js=FILE,
603** --post-js=FILE, and --extern-post-js=FILE emcc flags, as well as
604** populating those files. This is necessary for any builds which
605** embed the library's JS parts of this build (as opposed to parts
606** which do not use the library-level code).
607**
608** pB may be NULL.
609*/
610static void mk_pre_post(char const *zBuildName, BuildDef const * pB){
611 char const * const zBaseName = pB
612 ? BuildDef_basename(pB) : 0;
613 char const * const zDotWasm = pB
614 ? (pB->zDotWasm ? pB->zDotWasm : zBaseName)
615 : 0;
616
617 assert( zBuildName
618 && "This function has not supported a null pB in a long time" );
619 pf("%s# Begin --pre/--post flags for %s\n", zBanner, zBuildName);
620
621 ps("# --pre-js=...");
622 pf("pre-js.%s.js = $(dir.tmp)/pre-js.%s.js\n",
623 zBuildName, zBuildName);
624
625 if( 0==WASM_CUSTOM_INSTANTIATE || !pB ){
626 pf("$(eval $(call b.c-pp.target,"
627 "%s,"
628 "$(pre-js.in.js),"
629 "$(pre-js.%s.js),"
630 "$(c-pp.D.%s)"
631 "))",
632 zBuildName, zBuildName, zBuildName);
633 }else{
634 /*
635 ** See BuildDef::zDotWasm for _why_ we do this. _What_ we're doing
636 ** is generate $(pre-js.BUILDNAME.js) as above, but:
637 **
638 ** 1) Add an extra -D... flag to activate the custom
639 ** Module.intantiateWasm() in the JS code.
640 **
641 ** 2) Amend the generated pre-js.js with the name of the WASM
642 ** file which should be loaded. That tells the custom
643 ** Module.instantiateWasm() to use that file instead of
644 ** the default.
645 */
646 pf("$(pre-js.%s.js): $(pre-js.in.js) $(bin.c-pp) $(MAKEFILE_LIST)",
647 zBuildName);
648 if( 0 && zDotWasm ){
649 pf(" $(dir.dout)/%s.wasm" /* This .wasm is from some other
650 build, so this may trigger a full
651 build of the reference copy. */,
652 zDotWasm);
653 }
654 ps("");
655 pf("\t@$(call b.mkdir@); $(call b.c-pp.shcmd,"
656 "%s,"
657 "$(pre-js.in.js),"
658 "$(pre-js.%s.js),"
659 "$(c-pp.D.%s) -Dsqlite3.wasm.base64=$(dir.dout)/%s.wasm"
660 C_PP_D_CUSTOM_INSTANTIATE
661 ")\n",
662 zBuildName, zBuildName, zBuildName,
663 zDotWasm/*null is harmless here*/);
664 }
665
666 ps("\n# --post-js=...");
667 pf("post-js.%s.js = $(dir.tmp)/post-js.%s.js\n",
668 zBuildName, zBuildName);
669 pf("post-js.%s.in ="
670 " $(dir.api)/post-js-header.js"
671 " $(sqlite3-api.%s.js)"
672 " $(dir.api)/post-js-footer.js\n",
673 zBuildName, zBuildName);
674
675 pf("$(eval $(call b.c-pp.target,"
676 "%s,"
677 "$(post-js.%s.in),"
678 "$(post-js.%s.js),"
679 "$(c-pp.D.%s)"
680 "))\n",
681 zBuildName, zBuildName, zBuildName, zBuildName);
682
683 pf("$(post-js.%s.js): $(post-js.%s.in) $(bin.c-pp)\n",
684 zBuildName, zBuildName);
685
686 ps("\n# --extern-post-js=...");
687 pf("extern-post-js.%s.js = $(dir.tmp)/extern-post-js.%s.js\n",
688 zBuildName, zBuildName);
689 if( 0!=WASM_CUSTOM_INSTANTIATE && zBaseName ){
690 pf("$(eval $(call b.c-pp.target,"
691 "%s,"
692 "$(extern-post-js.in.js),"
693 "$(extern-post-js.%s.js),"
694 "$(c-pp.D.%s) --@policy=error -Dsqlite3.wasm=%s.wasm"
695 "))",
696 zBuildName, zBuildName, zBuildName, zDotWasm);
697 }else{
698 pf("$(eval $(call b.c-pp.target,"
699 "%s,"
700 "$(extern-post-js.in.js),"
701 "$(extern-post-js.%s.js),"
702 "$(c-pp.D.%s)"
703 "))",
704 zBuildName, zBuildName, zBuildName);
705 }
706
707 ps("\n# --pre/post misc...");
708 /* Combined flags for use with emcc... */
709 pf("pre-post.%s.flags = "
710 "--extern-pre-js=$(sqlite3-license-version.js) "
711 "--pre-js=$(pre-js.%s.js) "
712 "--post-js=$(post-js.%s.js) "
713 "--extern-post-js=$(extern-post-js.%s.js)\n",
714 zBuildName, zBuildName, zBuildName, zBuildName);
715
716 /* Set up deps... */
717 pf("pre-post.%s.deps = "
718 "$(pre-post-jses.common.deps) "
719 "$(post-js.%s.js) $(extern-post-js.%s.js) "
720 "$(dir.tmp)/pre-js.%s.js\n",
721 zBuildName, zBuildName, zBuildName, zBuildName);
722 pf("# End --pre/--post flags for %s%s", zBuildName, zBanner);
723}
724
725static void emit_compile_start(char const *zBuildName){
726 pf("\t@$(call b.mkdir@);"
727 " $(call b.echo,%s,$(emo.compile) building ...)\n",
728 zBuildName);
729}
730
731static void emit_logtag(char const *zBuildName){
732#if 1
733 pf("logtag.%s ?= [$(emo.b.%s)$(if $@, $@,)]:\n",
734 zBuildName, zBuildName);
735#else
736 pf("logtag.%s ?= [$(emo.b.%s) [%s] $@]:\n",
737 zBuildName, zBuildName, zBuildName);
738#endif
739 pf("$(info $(logtag.%s) Setting up target b-%s)\n",
740 zBuildName, zBuildName );
741}
742
743/**
744 Emit rules for sqlite3-api.${zBuildName}.js.
745*/
746static void emit_api_js(char const *zBuildName){
747 pf("sqlite3-api.%s.js = $(dir.tmp)/sqlite3-api.%s.js\n",
748 zBuildName, zBuildName);
749 pf("$(eval $(call b.c-pp.target,"
750 "%s,"
751 "$(sqlite3-api.jses),"
752 "$(sqlite3-api.%s.js),"
753 "$(c-pp.D.%s)"
754 "))\n",
755 zBuildName, zBuildName, zBuildName);
756 pf("$(out.%s.js): $(sqlite3-api.%s.js)\n",
757 zBuildName, zBuildName);
758 pf("$(sqlite3-api.%s.js):"
759 /* Extra deps needed by the OPFS pieces... */
760 " $(dir.api)/opfs-common-shared.c-pp.js"
761 " $(dir.api)/opfs-common-inline.c-pp.js"
762 "\n",
763 zBuildName);
764}
765
766/*
767** Emits makefile code for one build of the library.
768*/
769static void mk_lib_mode(const char *zBuildName, const BuildDef * pB){
770 const char * zJsExt = BuildDef_jsext(pB);
771 char const * const zBaseName = BuildDef_basename(pB);
772 char const * const zDotWasm = pB
773 ? (pB->zDotWasm ? pB->zDotWasm : zBaseName)
774 : 0;
775
776 assert( oBuildDefs.vanilla.zEnv );
777 assert( zBaseName );
778 assert( zDotWasm );
779
780 pf("%s# Begin build [%s%s]. flags=0x%02x\n", zBanner,
781 pB->zEmo, zBuildName, pB->flags);
782 pf("# zCmppD=%s\n# zBaseName=%s\n# zDotWasm=%s\n",
783 pB->zCmppD ? pB->zCmppD : "",
784 zBaseName, zDotWasm);
785 pf("b.names += %s\n"
786 "emo.b.%s = %s\n",
787 zBuildName, zBuildName, pB->zEmo);
788 emit_logtag(zBuildName);
789
790 if( pB->zIfCond ){
791 pf("%s\n", pB->zIfCond );
792 }
793
794 pf("dir.dout.%s ?= $(dir.dout)/%s\n", zBuildName, zBuildName);
795
796 pf("out.%s.base ?= $(dir.dout.%s)/%s\n",
797 zBuildName, zBuildName, zBaseName);
798 pf("out.%s.js ?= $(dir.dout.%s)/%s%s\n",
799 zBuildName, zBuildName, zBaseName, zJsExt);
800 pf("out.%s.wasm ?= $(dir.dout.%s)/%s.wasm\n",
801 //"$(basename $@).wasm"
802 zBuildName, zBuildName, zBaseName);
803
804 pf("dir.dout.%s ?= $(dir.dout)/%s\n", zBuildName, zBuildName);
805
806 pf("c-pp.D.%s ?= %s\n", zBuildName, pB->zCmppD ? pB->zCmppD : "");
807 if( pB->flags & F_64BIT ){
808 pf("c-pp.D.%s += $(c-pp.D.64bit)\n", zBuildName);
809 }
810 if( pB->flags & F_UNSUPPORTED ){
811 pf("c-pp.D.%s += -Dunsupported-build\n", zBuildName);
812 }
813
814 pf("emcc.environment.%s ?= %s\n", zBuildName,
815 pB->zEnv ? pB->zEnv : oBuildDefs.vanilla.zEnv);
816 if( pB->zEmccExtra ){
817 pf("emcc.flags.%s = %s\n", zBuildName, pB->zEmccExtra);
818 }
819
820 if( pB->zDeps ){
821 pf("deps.%s += %s\n", zBuildName, pB->zDeps);
822 }
823
824 emit_api_js(zBuildName);
825 mk_pre_post(zBuildName, pB);
826
827 { /* build it... */
828 pf(zBanner
829 "$(out.%s.js): $(MAKEFILE_LIST) $(sqlite3-wasm.c.in)"
830 " $(EXPORTED_FUNCTIONS.api) $(deps.%s)"
831 " $(bin.mkwb) $(pre-post.%s.deps)"
832 "\n",
833 zBuildName, zBuildName, zBuildName);
834
835 emit_compile_start(zBuildName);
836
837 if( F_UNSUPPORTED & pB->flags ){
838 pf("\t@echo '$(logtag.%s) $(label.unsupported-build)'\n",
839 zBuildName);
840 }
841
842 /* emcc ... */
843 {
844 pf("\t$(b.cmd@)$(bin.emcc) -o $@ ");
845 if( pB->zEmcc ){
846 pf("%s $(emcc.flags.%s)\n",
847 pB->zEmcc, zBuildName);
848 }else{
849 pf("$(emcc_opt_full) $(emcc.flags)"
850 " $(emcc.jsflags)"
851 " -sENVIRONMENT=$(emcc.environment.%s)"
852 " $(pre-post.%s.flags)"
853 " $(emcc.flags.%s)"
854 " $(cflags.common)"
855 " $(cflags.%s)"
856 " $(SQLITE_OPT)"
857 " $(cflags.wasm_extra_init) $(sqlite3-wasm.c.in)\n",
858 zBuildName, zBuildName, zBuildName, zBuildName
859 );
860 }
861 }
862
863 { /* Post-compilation transformations and copying to
864 $(dir.dout)... */
865
866 /* Avoid a 3rd occurrence of the bug fixed by 65798c09a00662a3,
867 ** which was (in two cases) caused by makefile refactoring and
868 ** not recognized until after a release was made with the broken
869 ** sqlite3-bundler-friendly.mjs (which is used by the npm
870 ** subproject but is otherwise untested/unsupported): */
871 pf("\t@if grep -e '^ *importScripts(' $@; "
872 "then echo '$(logtag.%s) $(emo.bug)$(emo.fire): "
873 "bug fixed in 65798c09a00662a3 has re-appeared'; "
874 "exit 1; fi;\n", zBuildName);
875
876 if( (F_ESM & pB->flags) || (F_NODEJS & pB->flags) ){
877 pf("\t@$(call b.call.patch-export-default,1,%d,$(logtag.%s))\n",
878 (F_WASMFS & pB->flags) ? 1 : 0,
879 zBuildName
880 );
881 }
882
883 pf("\t@chmod -x $(out.%s.wasm)\n", zBuildName
884 /* althttpd will automatically try to execute wasm files
885 if they have the +x bit set. Why that bit is set
886 at all is a mystery. */);
887 pf("\t@$(call b.do.wasm-opt,%s)\n", zBuildName);
888 pf("\t@$(call b.call.wasm-strip,%s)\n", zBuildName);
889 pf("\t@$(call b.strip-js-emcc-bindings,$(logtag.%s))\n", zBuildName);
890
891 pf("\t@if [ x1 = 'x$(base64.wasm)' ]; then \\\n"
892 "\techo '$(logtag.%s) $(emo.magic) Embedding $(out.%s.wasm) into $@ ...'; \\\n"
893 "\telse \\\n"
894 "\techo '$(logtag.%s) $(emo.megaphone) Not embedding WASM in JS. Pass base64.wasm=1 to do that.'; \\\n"
895 "\tfi\n",
896 zBuildName, zBuildName, zBuildName);
897 pf("\t@mv $@ $@.in; \\\n"
898 "\t$(call b.c-pp.shcmd,%s,$@.in,$@,"
899 " \"--delimiter=//%%\" -Dbase64.wasm=$(base64.wasm)"
900 " \"-Dsqlite3.wasm.base64=$(out.%s.wasm)\""
901 "); \\\n"
902 "\tchmod u+w $@;\\\n"
903 /* ^^^ the build makes generated files read-only, which then breaks
904 rebuild. */
905 "\trm -f $@.in\n",
906 zBuildName, zBuildName);
907
908 if( CP_JS & pB->flags ){
909 /*
910 ** $(bin.emcc) will write out $@ and will create a like-named
911 ** .wasm file. The resulting .wasm and .js/.mjs files are
912 ** identical across all builds which have the same pB->zEmmc
913 ** and/or pB->zEmccExtra.
914 **
915 ** For the final deliverables we copy one or both of those
916 ** js/wasm files to $(dir.dout) (the top-most build target
917 ** dir). We only copy the wasm file for the "base-most" builds
918 ** and recycle those for the rest of the builds. The catch is:
919 ** that .wasm file name gets hard-coded into $@ so we need,
920 ** for cases in which we "recycle" a .wasm file from another
921 ** build, to patch the name to pB->zDotWasm when copying to
922 ** $(dir.dout).
923 */
924 if( pB->zDotWasm ){
925 pf("\t@echo '$(logtag.%s) $(emo.disk) "
926 "s/\"%s.wasm\"/\"%s.wasm\"/g "
927 "in $(dir.dout)/$(notdir $@)'; \\\n"
928 "sed"
929 " -e 's/\"%s.wasm\"/\"%s.wasm\"/g'"
930 " -e \"s/'%s.wasm'/'%s.wasm'/g\""
931 " $@ > $(dir.dout)/$(notdir $@);\n",
932 zBuildName,
933 zBaseName, pB->zDotWasm,
934 zBaseName, pB->zDotWasm,
935 zBaseName, pB->zDotWasm);
936 }else{
937 pf("\t@$(call b.cp,%s,$@,$(dir.dout))\n",
938 zBuildName);
939 }
940 }
941 if( CP_WASM & pB->flags ){
942 pf("\t@$(call b.cp,%s,$(basename $@).wasm,$(dir.dout))\n",
943 zBuildName
944 //"\techo '[%s $(out.%s.wasm)] $(emo.disk) $(dir.dout)/$(notdir $(out.%s.wasm))'
945 //pB->zEmo, zBuildName
946 );
947 }
948
949 }
950 }
951 pf("\t@$(call b.echo,%s,$(emo.done) done!%s)\n",
952 zBuildName,
953 (F_UNSUPPORTED & pB->flags)
954 ? " $(label.unsupported-build)"
955 : "");
956
957 pf("\n%dbit: $(out.%s.js)\n"
958 "$(out.%s.wasm): $(out.%s.js)\n"
959 "b-%s: $(out.%s.wasm)\n",
960 (F_64BIT & pB->flags) ? 64 : 32, zBuildName,
961 zBuildName, zBuildName, zBuildName, zBuildName);
962
963 if( CP_JS & pB->flags ){
964 pf("$(dir.dout)/%s%s: $(out.%s.js)\n",
965 zBaseName, zJsExt, zBuildName
966 );
967 }
968
969 if( CP_WASM & pB->flags ){
970 pf("$(dir.dout)/%s.wasm: $(out.%s.wasm)\n",
971 zBaseName, zBuildName
972 );
973 }
974
975 pf("%s: $(out.%s.js)\n",
976 0==((F_UNSUPPORTED | F_NOT_IN_ALL) & pB->flags)
977 ? "all" : "more", zBuildName);
978
979 if( pB->zIfCond ){
980 pf("else\n"
981 "$(info $(logtag.%s) $(emo.stop) disabled by condition: %s)\n"
982 "endif\n",
983 zBuildName, pB->zIfCond);
984 }
985 pf("# End build [%s%s]%s", pB->zEmo, zBuildName, zBanner);
986}
987
988
989static void emit_gz(char const *zBuildName,
990 char const *zFileExt){
991 pf("\n$(out.%s.%s).gz: $(out.%s.%s)\n"
992 "\t@$(call b.echo,%s,$(emo.disk))\n"
993 "\t@gzip < $< > $@\n",
994 zBuildName, zFileExt,
995 zBuildName, zFileExt,
996 zBuildName);
997}
998
999/*
1000** Emits rules for the fiddle builds.
1001*/
1002static void mk_fiddle(void){
1003 for(int i = 0; i < 2; ++i ){
1004 /* 0==normal, 1==debug */
1005 int const isDebug = i>0;
1006 const char * const zBuildName = i ? "fiddle.debug" : "fiddle";
1007
1008 pf(zBanner "# Begin build %s\n", zBuildName);
1009 if( isDebug ){
1010 pf("emo.b.%s = $(emo.b.fiddle)$(emo.bug)\n", zBuildName);
1011 }else{
1012 pf("emo.b.fiddle = 🎻\n");
1013 }
1014 emit_logtag(zBuildName);
1015
1016 pf("dir.%s = %s\n"
1017 "out.%s.js = $(dir.%s)/fiddle-module.js\n"
1018 "out.%s.wasm = $(dir.%s)/fiddle-module.wasm\n"
1019 "$(out.%s.wasm): $(out.%s.js)\n",
1020 zBuildName, zBuildName,
1021 zBuildName, zBuildName,
1022 zBuildName, zBuildName,
1023 zBuildName, zBuildName);
1024
1025 emit_api_js(zBuildName);
1026 mk_pre_post(zBuildName, 0);
1027
1028 {/* emcc */
1029 pf("$(out.%s.js): $(MAKEFILE_LIST) "
1030 "$(EXPORTED_FUNCTIONS.fiddle) "
1031 "$(fiddle.c.in) "
1032 "$(pre-post.%s.deps) "
1033 "$(dir.dout)/sqlite3-opfs-async-proxy.js",
1034 zBuildName, zBuildName);
1035 if( isDebug ){
1036 pf(" $(dir.fiddle)/fiddle-worker.js"
1037 " $(dir.fiddle)/fiddle.js"
1038 " $(dir.fiddle)/index.html");
1039 }
1040 ps("");
1041 emit_compile_start(zBuildName);
1042 pf("\t@$(call b.mkdir@)\n"
1043 "\t$(b.cmd@)$(bin.emcc) -o $@"
1044 " $(emcc.flags.%s)" /* set in GNUmakefile */
1045 " $(pre-post.%s.flags)"
1046 " $(fiddle.c.in)"
1047 "\n",
1048 zBuildName, zBuildName);
1049 pf("\t@chmod -x $(out.%s.wasm)\n", zBuildName);
1050 pf("\t@$(call b.call.wasm-strip,%s)\n", zBuildName);
1051 pf("\t@$(call b.strip-js-emcc-bindings,$(logtag.%s))\n",
1052 zBuildName);
1053 pf("\t@$(call b.cp,%s,$(dir.dout)/sqlite3-opfs-async-proxy.js,"
1054 "$(dir $@)"
1055 ")\n", zBuildName);
1056 if( isDebug ){
1057 pf("\t@$(call b.cp,%s,"
1058 "$(dir.fiddle)/index.html "
1059 "$(dir.fiddle)/fiddle.js "
1060 "$(dir.fiddle)/fiddle-worker.js "
1061 "$(dir.fiddle)/sqlite3-opfs-async-proxy.js,"
1062 "$(dir $@)"
1063 ")\n",
1064 zBuildName);
1065 }
1066 pf("\t@$(call b.echo,%s,$(emo.done) done!)\n", zBuildName);
1067 }
1068
1069 pf("\n%s: $(out.%s.wasm)\n", isDebug ? "more" : "all", zBuildName);
1070
1071 /* Compress fiddle files. We handle each file separately, rather
1072 than compressing them in a loop in the previous target, to help
1073 avoid that hand-edited files, like fiddle-worker.js, do not end
1074 up with stale .gz files (which althttpd will then serve instead
1075 of the up-to-date uncompressed one). */
1076 emit_gz(zBuildName, "js");
1077 emit_gz(zBuildName, "wasm");
1078
1079 pf("\n%s: $(out.%s.js).gz $(out.%s.wasm).gz\n"
1080 "b-%s: %s\n",
1081 zBuildName, zBuildName, zBuildName,
1082 zBuildName, zBuildName);
1083 if( isDebug ){
1084 ps("fiddle-debug: fiddle.debug"); /* older name */
1085 }else{
1086 ps("all: b-fiddle");
1087 }
1088 pf("# End %s" zBanner, zBuildName);
1089 }
1090}
1091
1092int main(int argc, char const ** argv){
1093 int rc = 0;
1094 const BuildDef *pB;
1095 pf("# What follows was GENERATED by %s. Edit at your own risk.\n", __FILE__);
1096
1097 if(argc>1){
1098 /*
1099 ** Only emit the rules for the given list of builds, sans prologue
1100 ** (unless the arg "prologue" is given). Intended only for
1101 ** debugging, not actual makefile generation.
1102 */
1103 for( int i = 1; i < argc; ++i ){
1104 char const * const zArg = argv[i];
1105#define E(N) if(0==strcmp(#N, zArg)) {mk_lib_mode(# N, &oBuildDefs.N);} else
1106 BuildDefs_map(E) if( 0==strcmp("prologue",zArg) ){
1107 mk_prologue();
1108 }else{
1109 fprintf(stderr,"Unknown build name: %s\n", zArg);
1110 rc = 1;
1111 break;
1112 }
1113#undef E
1114 }
1115 }else{
1116 /*
1117 ** Emit the whole shebang...
1118 */
1119 mk_prologue();
1120#define E(N) mk_lib_mode(# N, &oBuildDefs.N);
1121 BuildDefs_map(E)
1122#undef E
1123 mk_fiddle();
1124 }
1125 return rc;
1126}