build-purego.sh raw
1 #!/bin/bash
2 # Build the Moxie compiler with PureGo LLVM bindings.
3 # No C/C++ toolchain needed at build time.
4 # Runtime deps: libLLVM-21.so, clang-21, ld.lld-21 (distro packages).
5 set -e
6
7 REAL_GOROOT=$(go env GOROOT)
8 MOXIE_GOROOT="${MOXIE_GOROOT_CACHE:-/tmp/moxie-goroot}"
9
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 export GOROOT="$MOXIE_GOROOT"
45 export GOWORK=off
46 export GOTOOLCHAIN=local
47
48 exec "$MOXIE_GOROOT/bin/go" build -mod=vendor -tags "purego" -o moxie-purego "$@" .
49