mkwasmbuilds.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. ** building sqlite3's WASM build. The main motivation is to generate
  15. ** code which "can" 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 both
  18. ** require escaping $ symbols, making the resulting script code as
  19. ** illegible as the eval spaghetti we want to get away from. Writing
  20. ** it in C is, somewhat surprisingly, _slightly_ less illegible than
  21. ** 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. #define pf printf
  33. #define ps puts
  34. /* Very common printf() args combo. */
  35. #define zNM zName, zMode
  36. /*
  37. ** Valid names for the zName arguments.
  38. */
  39. #define JS_BUILD_NAMES sqlite3 sqlite3-wasmfs
  40. /*
  41. ** Valid names for the zMode arguments of the "sqlite3" build. For the
  42. ** "sqlite3-wasmfs" build, only "esm" (ES6 Module) is legal.
  43. */
  44. #define JS_BUILD_MODES vanilla esm bundler-friendly node
  45. /* Separator to help eyeballs find the different sections */
  46. static const char * zBanner =
  47. "\n########################################################################\n";
  48. /*
  49. ** Emits common vars needed by the rest of the emitted code (but not
  50. ** needed by makefile code outside of these generated pieces).
  51. */
  52. static void mk_prologue(void){
  53. pf("%s", zBanner);
  54. ps("# extern-post-js* and extern-pre-js* are files for use with");
  55. ps("# Emscripten's --extern-pre-js and --extern-post-js flags.");
  56. ps("extern-pre-js.js := $(dir.api)/extern-pre-js.js");
  57. ps("extern-post-js.js.in := $(dir.api)/extern-post-js.c-pp.js");
  58. ps("# Emscripten flags for --[extern-][pre|post]-js=... for the");
  59. ps("# various builds.");
  60. ps("pre-post-common.flags := --extern-pre-js=$(sqlite3-license-version.js)");
  61. ps("# pre-post-jses.deps.* = a list of dependencies for the");
  62. ps("# --[extern-][pre/post]-js files.");
  63. ps("pre-post-jses.deps.common := $(extern-pre-js.js) $(sqlite3-license-version.js)");
  64. {
  65. /* SQLITE.CALL.WASM-OPT = shell code to run $(1) (source wasm file
  66. ** name) through $(bin.wasm-opt) */
  67. const char * zOptFlags =
  68. /*
  69. ** Flags for wasm-opt. It has many, many, MANY "passes" options
  70. ** and the ones which appear here were selected solely on the
  71. ** basis of trial and error.
  72. **
  73. ** All wasm file size savings/costs mentioned below are based on
  74. ** the vanilla build of sqlite3.wasm with -Oz (our shipping
  75. ** configuration). Comments like "saves nothing" may not be
  76. ** technically correct: "nothing" means "some neglible amount."
  77. **
  78. ** Note that performance gains/losses are _not_ taken into
  79. ** account here: only wasm file size.
  80. */
  81. "--enable-bulk-memory-opt " /* required */
  82. "--all-features " /* required */
  83. "--post-emscripten " /* Saves roughly 12kb */
  84. "--strip-debug " /* We already wasm-strip, but in
  85. ** case this environment has no
  86. ** wasm-strip... */
  87. /*
  88. ** The rest are trial-and-error. See wasm-opt --help and search
  89. ** for "Optimization passes" to find the full list.
  90. **
  91. ** With many flags this gets unusuably slow.
  92. */
  93. /*"--converge " saves nothing for the options we're using */
  94. /*"--dce " saves nothing */
  95. /*"--directize " saves nothing */
  96. /*"--gsi " no: requires --closed-world flag, which does not
  97. ** sound like something we want. */
  98. /*"--gufa --gufa-cast-all --gufa-optimizing " costs roughly 2kb */
  99. /*"--heap-store-optimization " saves nothing */
  100. /*"--heap2local " saves nothing */
  101. //"--inlining --inlining-optimizing " costs roughly 3kb */
  102. "--local-cse " /* saves roughly 1kb */
  103. /*"--once-reduction " saves nothing */
  104. /*"--remove-memory-init " presumably a performance tweak */
  105. /*"--remove-unused-names " saves nothing */
  106. /*"--safe-heap "*/
  107. /*"--vacuum " saves nothing */
  108. ;
  109. ps("ifeq (,$(bin.wasm-opt))");
  110. ps("define SQLITE.CALL.WASM-OPT");
  111. ps("echo 'wasm-opt not available for $(1)'");
  112. ps("endef");
  113. ps("else");
  114. ps("define SQLITE.CALL.WASM-OPT");
  115. pf("echo -n 'Before wasm-opt:'; ls -l $(1);\\\n"
  116. "\trm -f wasm-opt-tmp.wasm;\\\n"
  117. /* It's very likely that the set of wasm-opt flags varies from
  118. ** version to version, so we'll ignore any errors here. */
  119. "\tif $(bin.wasm-opt) $(1) -o wasm-opt-tmp.wasm \\\n"
  120. "\t\t%s; then \\\n"
  121. "\t\tmv wasm-opt-tmp.wasm $(1); \\\n"
  122. "\t\techo -n 'After wasm-opt: '; \\\n"
  123. "\t\tls -l $(1); \\\n"
  124. "\telse \\\n"
  125. "\t\techo 'WARNING: ignoring wasm-opt failure'; \\\n"
  126. "\tfi\n",
  127. zOptFlags
  128. );
  129. ps("endef");
  130. ps("endif");
  131. }
  132. }
  133. /*
  134. ** Flags for use with the 3rd argument to mk_pre_post() and
  135. ** mk_lib_mode().
  136. **
  137. ** Maintenance reminder: do not combine flags within this enum,
  138. ** e.g. LIBMODE_BUNDLER_FRIENDLY=0x02|LIBMODE_ESM, as that will lead
  139. ** to breakage in some of the flag checks.
  140. */
  141. enum LibModeFlags {
  142. /* Indicates an ESM module build. */
  143. LIBMODE_ESM = 0x01,
  144. /* Indicates a "bundler-friendly" build mode. */
  145. LIBMODE_BUNDLER_FRIENDLY = 0x02,
  146. /* Indicates to _not_ add this build to the 'all' target. */
  147. LIBMODE_DONT_ADD_TO_ALL = 0x04,
  148. /* Indicates a node.js-for-node.js build (untested and
  149. ** unsupported). */
  150. LIBMODE_NODEJS = 0x08,
  151. /* Indicates a wasmfs build (untested and unsupported). */
  152. LIBMODE_WASMFS = 0x10
  153. };
  154. /*
  155. ** Emits makefile code for setting up values for the --pre-js=FILE,
  156. ** --post-js=FILE, and --extern-post-js=FILE emcc flags, as well as
  157. ** populating those files.
  158. */
  159. static void mk_pre_post(const char *zName /* build name */,
  160. const char *zMode /* build mode */,
  161. int flags /* LIBMODE_... mask */,
  162. const char *zCmppD /* optional -D flags for c-pp for the
  163. ** --pre/--post-js files. */){
  164. pf("%s# Begin --pre/--post flags for %s-%s\n", zBanner, zNM);
  165. pf("c-pp.D.%s-%s := %s\n", zNM, zCmppD ? zCmppD : "");
  166. pf("pre-post-%s-%s.flags ?=\n", zNM);
  167. /* --pre-js=... */
  168. pf("pre-js.js.%s-%s := $(dir.tmp)/pre-js.%s-%s.js\n",
  169. zNM, zNM);
  170. pf("$(pre-js.js.%s-%s): $(MAKEFILE_LIST)\n", zNM);
  171. #if 1
  172. pf("$(eval $(call SQLITE.CALL.C-PP.FILTER,$(pre-js.js.in),$(pre-js.js.%s-%s),"
  173. "$(c-pp.D.%s-%s)))\n", zNM, zNM);
  174. #else
  175. /* This part is needed if/when we re-enable the custom
  176. ** Module.instantiateModule() impl in api/pre-js.c-pp.js. */
  177. pf("pre-js.js.%s-%s.intermediary := $(dir.tmp)/pre-js.%s-%s.intermediary.js\n",
  178. zNM, zNM);
  179. pf("$(eval $(call SQLITE.CALL.C-PP.FILTER,$(pre-js.js.in),$(pre-js.js.%s-%s.intermediary),"
  180. "$(c-pp.D.%s-%s) -Dcustom-Module.instantiateModule))\n", zNM, zNM);
  181. pf("$(pre-js.js.%s-%s): $(pre-js.js.%s-%s.intermediary)\n", zNM, zNM);
  182. pf("\tcp $(pre-js.js.%s-%s.intermediary) $@\n", zNM);
  183. /* Amend $(pre-js.js.zName-zMode) for all targets except the plain
  184. ** "sqlite3" and the "sqlite3-wasmfs" builds... */
  185. if( 0!=strcmp("sqlite3-wasmfs", zName)
  186. && 0!=strcmp("sqlite3", zName) ){
  187. #error "This part ^^^ is needs adapting for use with the LIBMODE_... flags"
  188. pf("\t@echo 'Module[xNameOfInstantiateWasm].uri = "
  189. "\"%s.wasm\";' >> $@\n", zName);
  190. }
  191. #endif
  192. /* --post-js=... */
  193. pf("post-js.js.%s-%s := $(dir.tmp)/post-js.%s-%s.js\n", zNM, zNM);
  194. pf("$(eval $(call SQLITE.CALL.C-PP.FILTER,$(post-js.js.in),"
  195. "$(post-js.js.%s-%s),$(c-pp.D.%s-%s)))\n", zNM, zNM);
  196. /* --extern-post-js=... */
  197. pf("extern-post-js.js.%s-%s := $(dir.tmp)/extern-post-js.%s-%s.js\n", zNM, zNM);
  198. pf("$(eval $(call SQLITE.CALL.C-PP.FILTER,$(extern-post-js.js.in),$(extern-post-js.js.%s-%s),"
  199. "$(c-pp.D.%s-%s)))\n", zNM, zNM);
  200. /* Combined flags for use with emcc... */
  201. pf("pre-post-common.flags.%s-%s := "
  202. "$(pre-post-common.flags) "
  203. "--post-js=$(post-js.js.%s-%s) "
  204. "--extern-post-js=$(extern-post-js.js.%s-%s)\n", zNM, zNM, zNM);
  205. pf("pre-post-%s-%s.flags += $(pre-post-common.flags.%s-%s) "
  206. "--pre-js=$(pre-js.js.%s-%s)\n", zNM, zNM, zNM);
  207. /* Set up deps... */
  208. pf("pre-post-jses.%s-%s.deps := $(pre-post-jses.deps.common) "
  209. "$(post-js.js.%s-%s) $(extern-post-js.js.%s-%s)\n",
  210. zNM, zNM, zNM);
  211. pf("pre-post-%s-%s.deps := $(pre-post-jses.%s-%s.deps) $(dir.tmp)/pre-js.%s-%s.js\n",
  212. zNM, zNM, zNM);
  213. pf("# End --pre/--post flags for %s-%s%s", zNM, zBanner);
  214. }
  215. /*
  216. ** Emits rules for the fiddle builds.
  217. **
  218. */
  219. static void mk_fiddle(){
  220. int i = 0;
  221. mk_pre_post("fiddle-module","vanilla", 0, 0);
  222. for( ; i < 2; ++i ){
  223. const char *zTail = i ? ".debug" : "";
  224. const char *zDir = i ? "$(dir.fiddle-debug)" : "$(dir.fiddle)";
  225. pf("%s# Begin fiddle%s\n", zBanner, zTail);
  226. pf("fiddle-module.js%s := %s/fiddle-module.js\n", zTail, zDir);
  227. pf("fiddle-module.wasm%s := "
  228. "$(subst .js,.wasm,$(fiddle-module.js%s))\n", zTail, zTail);
  229. pf("$(fiddle-module.js%s):%s $(MAKEFILE_LIST) $(MAKEFILE.fiddle) "
  230. "$(EXPORTED_FUNCTIONS.fiddle) "
  231. "$(fiddle.cses) $(pre-post-fiddle-module-vanilla.deps) "
  232. "$(SOAP.js)\n",
  233. zTail, (i ? " $(fiddle-module.js)" : ""));
  234. if( 1==i ){/*fiddle.debug*/
  235. pf("\t@test -d \"$(dir $@)\" || mkdir -p \"$(dir $@)\"\n");
  236. }
  237. pf("\t$(bin.emcc) -o $@ $(fiddle.emcc-flags%s) "
  238. "$(pre-post-fiddle-module-vanilla.flags) $(fiddle.cses)\n",
  239. zTail);
  240. pf("\t$(maybe-wasm-strip) $(fiddle-module.wasm%s)\n", zTail);
  241. pf("\t@cp -p $(SOAP.js) $(dir $@)\n");
  242. if( 1==i ){/*fiddle.debug*/
  243. pf("\tcp -p $(dir.fiddle)/index.html "
  244. "$(dir.fiddle)/fiddle.js "
  245. "$(dir.fiddle)/fiddle-worker.js "
  246. "$(dir $@)\n");
  247. }
  248. pf("\t@for i in %s/*.*js %s/*.html %s/*.wasm; do \\\n"
  249. "\t\ttest -f $${i} || continue; \\\n"
  250. "\t\tgzip < $${i} > $${i}.gz; \\\n"
  251. "\tdone\n", zDir, zDir, zDir);
  252. if( 0==i ){
  253. ps("fiddle: $(fiddle-module.js)");
  254. }else{
  255. ps("fiddle-debug: $(fiddle-module-debug.js)");
  256. }
  257. pf("# End fiddle%s%s", zTail, zBanner);
  258. }
  259. }
  260. /*
  261. ** Emits makefile code for one build of the library, primarily defined
  262. ** by the combination of zName and zMode, each of which must be values
  263. ** from JS_BUILD_NAMES resp. JS_BUILD_MODES.
  264. */
  265. static void mk_lib_mode(const char *zName /* build name */,
  266. const char *zMode /* build mode */,
  267. int flags /* LIBMODE_... mask */,
  268. const char *zApiJsOut /* name of generated sqlite3-api.js/.mjs */,
  269. const char *zJsOut /* name of generated sqlite3.js/.mjs */,
  270. const char *zCmppD /* extra -D flags for c-pp */,
  271. const char *zEmcc /* extra flags for emcc */){
  272. const char * zWasmOut = "$(basename $@).wasm"
  273. /* The various targets named X.js or X.mjs (zJsOut) also generate
  274. ** X.wasm, and we need that part of the name to perform some
  275. ** post-processing after Emscripten generates X.wasm. */;
  276. assert( zName );
  277. assert( zMode );
  278. assert( zApiJsOut );
  279. assert( zJsOut );
  280. if( !zCmppD ) zCmppD = "";
  281. if( !zEmcc ) zEmcc = "";
  282. pf("%s# Begin build [%s-%s]\n", zBanner, zNM);
  283. pf("# zApiJsOut=%s\n# zJsOut=%s\n# zCmppD=%s\n", zApiJsOut, zJsOut, zCmppD);
  284. pf("$(info Setting up build [%s-%s]: %s)\n", zNM, zJsOut);
  285. mk_pre_post(zNM, flags, zCmppD);
  286. pf("\nemcc.flags.%s.%s ?=\n", zNM);
  287. if( zEmcc[0] ){
  288. pf("emcc.flags.%s.%s += %s\n", zNM, zEmcc);
  289. }
  290. pf("$(eval $(call SQLITE.CALL.C-PP.FILTER, $(sqlite3-api.js.in), %s, %s))\n",
  291. zApiJsOut, zCmppD);
  292. /* target zJsOut */
  293. pf("%s: %s $(MAKEFILE_LIST) $(sqlite3-wasm.cfiles) $(EXPORTED_FUNCTIONS.api) "
  294. "$(pre-post-%s-%s.deps) "
  295. "$(sqlite3-api.ext.jses)"
  296. /* ^^^ maintenance reminder: we set these as deps so that they
  297. get copied into place early. That allows the developer to
  298. reload the base-most test pages while the later-stage builds
  299. are still compiling, which is especially helpful when running
  300. builds with long build times (like -Oz). */
  301. "\n",
  302. zJsOut, zApiJsOut, zNM);
  303. pf("\t@echo \"Building $@ ...\"\n");
  304. pf("\t$(bin.emcc) -o $@ $(emcc_opt_full) $(emcc.flags) \\\n");
  305. pf("\t\t$(emcc.jsflags) -sENVIRONMENT=$(emcc.environment.%s) \\\n", zMode);
  306. pf("\t\t$(pre-post-%s-%s.flags) \\\n", zNM);
  307. pf("\t\t$(emcc.flags.%s) $(emcc.flags.%s.%s) \\\n", zName, zNM);
  308. pf("\t\t$(cflags.common) $(SQLITE_OPT) \\\n"
  309. "\t\t$(cflags.%s) $(cflags.%s.%s) \\\n"
  310. "\t\t$(cflags.wasm_extra_init) $(sqlite3-wasm.cfiles)\n", zName, zNM);
  311. if( LIBMODE_ESM & flags ){
  312. /* TODO? Replace this $(call) with the corresponding makefile
  313. ** code. OTOH, we also use this $(call) in the speedtest1-wasmfs
  314. ** build, which is not part of the rules emitted by this
  315. ** program. */
  316. pf("\t@$(call SQLITE.CALL.xJS.ESM-EXPORT-DEFAULT,1,%d)\n",
  317. (LIBMODE_WASMFS & flags) ? 1 : 0);
  318. }
  319. pf("\t@chmod -x %s; \\\n"
  320. "\t\t$(maybe-wasm-strip) %s;\n",
  321. zWasmOut, zWasmOut);
  322. pf("\t@$(call SQLITE.CALL.WASM-OPT,%s)\n", zWasmOut);
  323. pf("\t@sed -i -e '/^var _sqlite3.*createExportWrapper/d' %s || exit; \\\n"
  324. /* ^^^^^^ reminder: Mac/BSD sed has no -i flag */
  325. "\t\techo 'Stripped out createExportWrapper() parts.'\n",
  326. zJsOut) /* Our JS code installs bindings of each WASM export. The
  327. generated Emscripten JS file does the same using its
  328. own framework, but we don't use those results and can
  329. speed up lib init, and reduce memory cost
  330. considerably, by stripping them out. */;
  331. /*
  332. ** The above $(bin.emcc) call will write zJsOut and will create a
  333. ** like-named .wasm file (zWasmOut). That .wasm file name gets
  334. ** hard-coded into zJsOut so we need to, for some cases, patch
  335. ** zJsOut to use the name sqlite3.wasm instead. Note that the
  336. ** resulting .wasm file is identical for all builds for which zEmcc
  337. ** is empty.
  338. */
  339. if( (LIBMODE_BUNDLER_FRIENDLY & flags)
  340. || (LIBMODE_NODEJS & flags) ){
  341. pf("\t@echo 'Patching $@ for %s.wasm...'; \\\n", zName);
  342. pf("\t\trm -f %s; \\\n", zWasmOut);
  343. pf("\t\tsed -i -e 's/%s-%s.wasm/%s.wasm/g' $@ || exit;\n",
  344. /* ^^^^^^ reminder: Mac/BSD sed has no -i flag */
  345. zNM, zName);
  346. pf("\t@ls -la $@\n");
  347. if( LIBMODE_BUNDLER_FRIENDLY & flags ){
  348. /* Avoid a 3rd occurrence of the bug fixed by 65798c09a00662a3,
  349. ** which was (in two cases) caused by makefile refactoring and
  350. ** not recognized until after a release was made with the broken
  351. ** sqlite3-bundler-friendly.mjs: */
  352. pf("\t@if grep -e '^ *importScripts(' $@; "
  353. "then echo 'ERROR: bug fixed in 65798c09a00662a3 has re-appeared'; "
  354. "exit 1; fi;\n");
  355. }
  356. }else{
  357. pf("\t@ls -la %s $@\n", zWasmOut);
  358. }
  359. if( 0==(LIBMODE_DONT_ADD_TO_ALL & flags) ){
  360. pf("all: %s\n", zJsOut);
  361. }
  362. pf("# End build [%s-%s]%s", zNM, zBanner);
  363. }
  364. int main(void){
  365. int rc = 0;
  366. pf("# What follows was GENERATED by %s. Edit at your own risk.\n", __FILE__);
  367. mk_prologue();
  368. mk_lib_mode("sqlite3", "vanilla", 0,
  369. "$(sqlite3-api.js)", "$(sqlite3.js)", 0, 0);
  370. mk_lib_mode("sqlite3", "esm", LIBMODE_ESM,
  371. "$(sqlite3-api.mjs)", "$(sqlite3.mjs)",
  372. "-Dtarget=es6-module", 0);
  373. mk_lib_mode("sqlite3", "bundler-friendly",
  374. LIBMODE_BUNDLER_FRIENDLY | LIBMODE_ESM,
  375. "$(sqlite3-api-bundler-friendly.mjs)",
  376. "$(sqlite3-bundler-friendly.mjs)",
  377. "$(c-pp.D.sqlite3-esm) -Dtarget=es6-bundler-friendly", 0);
  378. mk_lib_mode("sqlite3" , "node",
  379. LIBMODE_NODEJS | LIBMODE_DONT_ADD_TO_ALL,
  380. "$(sqlite3-api-node.mjs)", "$(sqlite3-node.mjs)",
  381. "$(c-pp.D.sqlite3-bundler-friendly) -Dtarget=node", 0);
  382. mk_lib_mode("sqlite3-wasmfs", "esm" ,
  383. LIBMODE_WASMFS | LIBMODE_ESM | LIBMODE_DONT_ADD_TO_ALL,
  384. /* The sqlite3-wasmfs build is optional and needs to be invoked
  385. ** conditionally using info we don't have here. */
  386. "$(sqlite3-api-wasmfs.mjs)", "$(sqlite3-wasmfs.mjs)",
  387. "$(c-pp.D.sqlite3-bundler-friendly) -Dwasmfs",
  388. "-sEXPORT_ES6 -sUSE_ES6_IMPORT_META");
  389. mk_fiddle();
  390. mk_pre_post("speedtest1","vanilla", 0, 0);
  391. mk_pre_post("speedtest1-wasmfs","esm", 0,
  392. "$(c-pp.D.sqlite3-bundler-friendly) -Dwasmfs");
  393. return rc;
  394. }