| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 1 | #!/usr/bin/env bash |
| Replace V with Nim 472eb49 nandithebull 11h ago | 2 | # Nim -> WebAssembly, for the Flutter web build of the example. |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 3 | # |
| 4 | # Nothing is installed system-wide. tool/bin/emcc is a DotSlash file pinning |
| 5 | # emscripten by content hash; DotSlash fetches it on first run (~285 MB) and |
| 6 | # caches it under ~/.cache/dotslash. Only `dotslash` itself has to be on PATH. |
| 7 | # |
| 8 | # Output lands in example/web/ and is loaded by a <script> tag in |
| Replace V with Nim 472eb49 nandithebull 11h ago | 9 | # example/web/index.html. Re-run after editing src/nimflutter.nim. |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 10 | set -euo pipefail |
| 11 | cd "$(dirname "$0")/.." |
| 12 | |
| Replace V with Nim 472eb49 nandithebull 11h ago | 13 | nim_bin="${NIM:-nim}" |
| 14 | command -v "$nim_bin" >/dev/null || { |
| 15 | echo "Nim compiler not on PATH (set NIM=/path/to/nim)"; exit 1; } |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 16 | command -v node >/dev/null || { echo "node not on PATH (emscripten needs it)"; exit 1; } |
| 17 | command -v dotslash >/dev/null || { |
| 18 | echo "dotslash not on PATH — https://dotslash-cli.com/docs/installation/" >&2 |
| 19 | exit 1 |
| 20 | } |
| 21 | |
| Replace V with Nim 472eb49 nandithebull 11h ago | 22 | nim_lib="$(dirname "$(dirname "$(command -v "$nim_bin")")")/lib" |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 23 | out=example/web |
| 24 | mkdir -p build "$out" |
| 25 | |
| 26 | # Fetches on first call, then just prints the cached path. |
| 27 | emcc_path="$(dotslash -- fetch tool/bin/emcc)" |
| 28 | root="$(dirname "$(dirname "$emcc_path")")" |
| 29 | |
| 30 | # emcc wants a config file; the DotSlash cache is content-addressed and must |
| 31 | # stay read-only, so the config and the sysroot cache live under build/. |
| Replace V with Nim 472eb49 nandithebull 11h ago | 32 | cat > build/emscripten.config <<CONFIG |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 33 | LLVM_ROOT = '$root/bin' |
| 34 | BINARYEN_ROOT = '$root' |
| 35 | NODE_JS = '$(command -v node)' |
| 36 | CACHE = '$PWD/build/emcache' |
| Replace V with Nim 472eb49 nandithebull 11h ago | 37 | CONFIG |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 38 | export EM_CONFIG="$PWD/build/emscripten.config" |
| 39 | |
| Replace V with Nim 472eb49 nandithebull 11h ago | 40 | # Nim emits C for the wasm32 target; emcc turns that into wasm. --threads:off |
| 41 | # because the emscripten module is single-threaded — bands still render, just |
| 42 | # sequentially (see supportsIsolateParallelism on the Dart side). |
| 43 | cache=build/nimcache_wasm |
| 44 | rm -rf "$cache" |
| 45 | "$nim_bin" c --compileOnly --nimcache:"$cache" --cpu:wasm32 --os:linux \ |
| 46 | --mm:arc -d:danger --threads:off --noMain src/nimflutter.nim |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 47 | |
| Replace V with Nim 472eb49 nandithebull 11h ago | 48 | # No -sGLOBAL_BASE here. The V build needed static data pushed above 1 MB to |
| 49 | # dodge a null-pointer heuristic in its builtin memcpy; Nim's runtime has no |
| 50 | # such guard, and a string round-trip through the default layout is verified |
| 51 | # by tool/test_wasm.mjs. |
| 52 | dotslash tool/bin/emcc "$cache"/*.c -I"$nim_lib" -O3 -o "$out/nimflutter.js" \ |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 53 | -sMODULARIZE=1 \ |
| Replace V with Nim 472eb49 nandithebull 11h ago | 54 | -sEXPORT_NAME=createNimFlutterModule \ |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 55 | -sENVIRONMENT=web,worker,node \ |
| 56 | -sALLOW_MEMORY_GROWTH=1 \ |
| Replace V with Nim 472eb49 nandithebull 11h ago | 57 | -sEXPORTED_FUNCTIONS='["_nf_init","_nf_add","_nf_greet","_nf_free","_nf_mandelbrot","_malloc","_free"]' \ |
| Add a WebAssembly backend for the example 728acaa nandithebull 12h ago | 58 | -sEXPORTED_RUNTIME_METHODS='["ccall","cwrap","UTF8ToString","stringToUTF8","lengthBytesUTF8","HEAPU8"]' |
| 59 | |
| Replace V with Nim 472eb49 nandithebull 11h ago | 60 | echo "built: $out/nimflutter.js ($(stat -c%s "$out/nimflutter.js") bytes)," \ |
| 61 | "$out/nimflutter.wasm ($(stat -c%s "$out/nimflutter.wasm") bytes)" |