1
0

build-kernel.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. #
  3. # Build the native extraction kernel (codegraph-kernel) and stage the .node
  4. # where the TS loader (src/extraction/kernel/loader.ts) finds it for
  5. # from-source runs and tests:
  6. #
  7. # codegraph-kernel/prebuilds/<platform>-<arch>/codegraph-kernel.node
  8. #
  9. # The kernel is OPTIONAL everywhere: when the .node is absent the extraction
  10. # path falls back to the wasm pipeline. This script needs a Rust toolchain
  11. # (rustup.rs); nothing else in the repo does.
  12. #
  13. # Usage:
  14. # scripts/build-kernel.sh # host platform
  15. # scripts/build-kernel.sh --target <rust-triple> [--platform <plat-arch>]
  16. #
  17. # The cross-compile form is what the release workflow uses (e.g.
  18. # --target x86_64-apple-darwin --platform darwin-x64 on a macos-arm runner).
  19. set -euo pipefail
  20. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  21. CRATE="$ROOT/codegraph-kernel"
  22. TARGET=""
  23. PLATFORM=""
  24. while [ $# -gt 0 ]; do
  25. case "$1" in
  26. --target) TARGET="$2"; shift 2 ;;
  27. --platform) PLATFORM="$2"; shift 2 ;;
  28. *) echo "unknown arg: $1" >&2; exit 1 ;;
  29. esac
  30. done
  31. # Map a rust triple (or the host) to the bundle-target naming used across the
  32. # release pipeline (darwin-arm64, linux-x64, win32-arm64, ...).
  33. if [ -z "$PLATFORM" ]; then
  34. if [ -n "$TARGET" ]; then
  35. case "$TARGET" in
  36. aarch64-apple-darwin) PLATFORM="darwin-arm64" ;;
  37. x86_64-apple-darwin) PLATFORM="darwin-x64" ;;
  38. x86_64-unknown-linux-gnu) PLATFORM="linux-x64" ;;
  39. aarch64-unknown-linux-gnu) PLATFORM="linux-arm64" ;;
  40. x86_64-pc-windows-msvc) PLATFORM="win32-x64" ;;
  41. aarch64-pc-windows-msvc) PLATFORM="win32-arm64" ;;
  42. *) echo "cannot map rust target '$TARGET' to a platform name; pass --platform" >&2; exit 1 ;;
  43. esac
  44. else
  45. case "$(uname -s)-$(uname -m)" in
  46. Darwin-arm64) PLATFORM="darwin-arm64" ;;
  47. Darwin-x86_64) PLATFORM="darwin-x64" ;;
  48. Linux-x86_64) PLATFORM="linux-x64" ;;
  49. Linux-aarch64) PLATFORM="linux-arm64" ;;
  50. MINGW*-x86_64|MSYS*-x86_64) PLATFORM="win32-x64" ;;
  51. MINGW*-aarch64|MSYS*-aarch64) PLATFORM="win32-arm64" ;;
  52. *) echo "unrecognized host $(uname -s)-$(uname -m); pass --platform" >&2; exit 1 ;;
  53. esac
  54. fi
  55. fi
  56. echo "[kernel] building codegraph-kernel for ${PLATFORM}${TARGET:+ (target $TARGET)}"
  57. cd "$CRATE"
  58. if [ -n "$TARGET" ]; then
  59. rustup target add "$TARGET" >/dev/null 2>&1 || true
  60. cargo build --release --target "$TARGET"
  61. OUTDIR="$CRATE/target/$TARGET/release"
  62. else
  63. cargo build --release
  64. OUTDIR="$CRATE/target/release"
  65. fi
  66. # cdylib name differs per OS; the staged name is always codegraph-kernel.node.
  67. case "$PLATFORM" in
  68. darwin-*) LIB="$OUTDIR/libcodegraph_kernel.dylib" ;;
  69. linux-*) LIB="$OUTDIR/libcodegraph_kernel.so" ;;
  70. win32-*) LIB="$OUTDIR/codegraph_kernel.dll" ;;
  71. esac
  72. [ -f "$LIB" ] || { echo "[kernel] error: built library not found at $LIB" >&2; exit 1; }
  73. DEST="$CRATE/prebuilds/$PLATFORM"
  74. mkdir -p "$DEST"
  75. # rm first so the copy lands on a FRESH inode: overwriting a signed dylib in
  76. # place leaves macOS's per-inode signature cache stale, and every process
  77. # that then dlopens the staged .node is SIGKILLed at load (the on-disk
  78. # signature still verifies, which makes it maddening to diagnose).
  79. rm -f "$DEST/codegraph-kernel.node"
  80. cp "$LIB" "$DEST/codegraph-kernel.node"
  81. echo "[kernel] staged $DEST/codegraph-kernel.node ($(du -h "$DEST/codegraph-kernel.node" | cut -f1))"