1
0

build-bundle.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env bash
  2. #
  3. # Build a self-contained CodeGraph bundle: an official Node runtime + the
  4. # compiled app + its production deps, so CodeGraph runs with NO system Node and
  5. # NO native build — node:sqlite is built into the bundled Node. One archive per
  6. # platform.
  7. #
  8. # Because dropping better-sqlite3 left zero native addons, the recipe is pure
  9. # file-packaging (download the target's Node, copy the app, archive) — so any
  10. # platform's bundle can be built on any OS. No cross-compile, no native runners.
  11. #
  12. # Usage:
  13. # scripts/build-bundle.sh <target> [node-version]
  14. # target: darwin-arm64 | darwin-x64 | linux-x64 | linux-arm64
  15. # | win32-x64 | win32-arm64
  16. # node-version: e.g. v24.16.0 (default below; pin for reproducible builds)
  17. #
  18. # Output:
  19. # unix: release/codegraph-<target>.tar.gz (launcher: bin/codegraph)
  20. # windows: release/codegraph-<target>.zip (launcher: bin/codegraph.cmd)
  21. set -euo pipefail
  22. TARGET="${1:?usage: build-bundle.sh <target> [node-version]}"
  23. NODE_VERSION="${2:-v24.16.0}"
  24. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  25. OUT="$ROOT/release"
  26. WORK="$(mktemp -d)"
  27. trap 'rm -rf "$WORK"' EXIT
  28. ARCH="${TARGET##*-}" # x64 | arm64
  29. OSFAM="${TARGET%-*}" # darwin | linux | win32
  30. echo "[bundle] target=${TARGET} node=${NODE_VERSION}"
  31. # 1. Download + extract the official Node runtime for the target platform.
  32. if [ "$OSFAM" = "win32" ]; then
  33. NODE_DIST="node-${NODE_VERSION}-win-${ARCH}"
  34. NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_DIST}.zip"
  35. echo "[bundle] downloading ${NODE_URL}"
  36. curl -fsSL "$NODE_URL" -o "$WORK/node.zip"
  37. if command -v unzip >/dev/null 2>&1; then
  38. unzip -q "$WORK/node.zip" -d "$WORK"
  39. else
  40. tar -xf "$WORK/node.zip" -C "$WORK" # bsdtar can read zip
  41. fi
  42. NODE_BIN="$WORK/${NODE_DIST}/node.exe"
  43. else
  44. NODE_DIST="node-${NODE_VERSION}-${TARGET}"
  45. NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_DIST}.tar.gz"
  46. echo "[bundle] downloading ${NODE_URL}"
  47. curl -fsSL "$NODE_URL" -o "$WORK/node.tar.gz"
  48. tar -xzf "$WORK/node.tar.gz" -C "$WORK"
  49. NODE_BIN="$WORK/${NODE_DIST}/bin/node"
  50. fi
  51. [ -f "$NODE_BIN" ] || { echo "[bundle] error: node binary not found ($NODE_BIN)" >&2; exit 1; }
  52. # 2. Build the app (compiled JS + copied wasm/schema assets).
  53. echo "[bundle] building app"
  54. ( cd "$ROOT" && npm run build >/dev/null )
  55. # 3. Stage: app + production-only deps (pure JS/wasm → portable across platforms).
  56. STAGE="$WORK/codegraph-${TARGET}"
  57. mkdir -p "$STAGE/lib" "$STAGE/bin"
  58. cp -R "$ROOT/dist" "$STAGE/lib/dist"
  59. cp "$ROOT/package.json" "$ROOT/package-lock.json" "$STAGE/lib/"
  60. echo "[bundle] installing production dependencies"
  61. ( cd "$STAGE/lib" && npm ci --omit=dev --ignore-scripts >/dev/null 2>&1 )
  62. rm -f "$STAGE/lib/package-lock.json"
  63. # 4. Vendored Node + launcher (the launcher uses the bundled Node by relative
  64. # path, so no system Node is ever needed).
  65. #
  66. # `--liftoff-only`: keep tree-sitter's large WASM grammars on V8's Liftoff
  67. # baseline compiler so they never reach the turboshaft optimizing tier, whose
  68. # per-compilation Zone arena OOMs the whole process (`Fatal process out of
  69. # memory: Zone`) on Node >= 22 — even with tens of GB free. The flag is read at
  70. # V8 engine init so it must be on node's command line; the parse worker inherits
  71. # it. See issues #293/#298 and src/extraction/wasm-runtime-flags.ts. (The CLI
  72. # also self-relaunches with this flag when launched without it, so non-bundled
  73. # runs are covered too; passing it here avoids that extra spawn.)
  74. if [ "$OSFAM" = "win32" ]; then
  75. cp "$NODE_BIN" "$STAGE/node.exe"
  76. printf '@"%%~dp0..\\node.exe" --liftoff-only "%%~dp0..\\lib\\dist\\bin\\codegraph.js" %%*\r\n' \
  77. > "$STAGE/bin/codegraph.cmd"
  78. else
  79. cp "$NODE_BIN" "$STAGE/node"
  80. cat > "$STAGE/bin/codegraph" <<'LAUNCH'
  81. #!/bin/sh
  82. # Resolve symlinks (e.g. the ~/.local/bin/codegraph link install.sh creates) so
  83. # we find the real bundle dir, not the symlink's location.
  84. SELF="$0"
  85. while [ -L "$SELF" ]; do
  86. target="$(readlink "$SELF")"
  87. case "$target" in
  88. /*) SELF="$target" ;;
  89. *) SELF="$(dirname "$SELF")/$target" ;;
  90. esac
  91. done
  92. DIR="$(cd "$(dirname "$SELF")/.." && pwd)"
  93. # --liftoff-only: avoid the V8 turboshaft WASM Zone OOM (issues #293/#298).
  94. exec "$DIR/node" --liftoff-only "$DIR/lib/dist/bin/codegraph.js" "$@"
  95. LAUNCH
  96. chmod +x "$STAGE/bin/codegraph"
  97. fi
  98. # 5. Archive (.zip for Windows, .tar.gz otherwise).
  99. mkdir -p "$OUT"
  100. if [ "$OSFAM" = "win32" ]; then
  101. ARCHIVE="$OUT/codegraph-${TARGET}.zip"
  102. rm -f "$ARCHIVE"
  103. ( cd "$WORK" && zip -rqX "$ARCHIVE" "codegraph-${TARGET}" )
  104. else
  105. ARCHIVE="$OUT/codegraph-${TARGET}.tar.gz"
  106. # --no-xattrs: don't embed macOS xattrs that make GNU tar warn on Linux.
  107. tar --no-xattrs -czf "$ARCHIVE" -C "$WORK" "codegraph-${TARGET}"
  108. fi
  109. echo "[bundle] wrote ${ARCHIVE} ($(du -h "$ARCHIVE" | cut -f1))"