build-purego.sh raw

   1  #!/bin/bash
   2  # Build the Moxie compiler with PureGo LLVM bindings (no CGo LLVM dependency).
   3  # Still needs CGo for libclang (cgo package).
   4  set -e
   5  
   6  REAL_GOROOT=$(go env GOROOT)
   7  MOXIE_GOROOT="${MOXIE_GOROOT_CACHE:-/tmp/moxie-goroot}"
   8  
   9  # Create or update patched GOROOT (same as build.sh).
  10  STAMP="$MOXIE_GOROOT/.stamp"
  11  GO_VER=$(go version)
  12  PATCH_HASH=$(sha256sum build/patch-gotypes.go | cut -d' ' -f1)
  13  WANT_STAMP="$GO_VER $PATCH_HASH"
  14  
  15  if [ -f "$STAMP" ] && [ "$(cat "$STAMP")" = "$WANT_STAMP" ]; then
  16      echo "patched GOROOT up to date" >&2
  17  else
  18      echo "creating patched GOROOT at $MOXIE_GOROOT ..." >&2
  19      rm -rf "$MOXIE_GOROOT"
  20      mkdir -p "$MOXIE_GOROOT"
  21      for item in "$REAL_GOROOT"/*; do
  22          ln -sf "$item" "$MOXIE_GOROOT/$(basename "$item")"
  23      done
  24      rm -f "$MOXIE_GOROOT/src"
  25      mkdir -p "$MOXIE_GOROOT/src"
  26      for item in "$REAL_GOROOT/src"/*; do
  27          ln -sf "$item" "$MOXIE_GOROOT/src/$(basename "$item")"
  28      done
  29      rm -f "$MOXIE_GOROOT/src/go"
  30      mkdir -p "$MOXIE_GOROOT/src/go"
  31      for item in "$REAL_GOROOT/src/go"/*; do
  32          ln -sf "$item" "$MOXIE_GOROOT/src/go/$(basename "$item")"
  33      done
  34      rm -f "$MOXIE_GOROOT/src/go/types"
  35      GOROOT="$REAL_GOROOT" go run build/patch-gotypes.go "$MOXIE_GOROOT"
  36      echo "$WANT_STAMP" > "$STAMP"
  37  fi
  38  
  39  # Ensure abi_amd64.h exists for purego's fakecgo assembly.
  40  if [ ! -f "$MOXIE_GOROOT/pkg/include/abi_amd64.h" ]; then
  41      cp "$REAL_GOROOT/src/runtime/cgo/abi_amd64.h" "$MOXIE_GOROOT/pkg/include/" 2>/dev/null || true
  42  fi
  43  
  44  # Detect LLVM 19 for libclang (still needed for the cgo package).
  45  LLVM_PREFIX="/usr/lib/llvm19"
  46  if [ ! -d "$LLVM_PREFIX" ]; then
  47      LLVM_PREFIX="/usr/lib/llvm-19"
  48  fi
  49  if [ ! -d "$LLVM_PREFIX" ]; then
  50      echo "error: LLVM 19 not found (needed for libclang)" >&2
  51      exit 1
  52  fi
  53  
  54  export GOROOT="$MOXIE_GOROOT"
  55  export GOWORK=off
  56  export CGO_CFLAGS="-I${LLVM_PREFIX}/include"
  57  export CGO_LDFLAGS="-L${LLVM_PREFIX}/lib -lclang"
  58  
  59  exec go build -mod=vendor -tags "purego" -o moxie-purego "$@" .
  60