mkwasmbuilds.c 16 KB

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