build.sh raw
1 #!/bin/bash
2 # Build the Moxie compiler with a patched GOROOT.
3 # The patched go/types enables string=[]byte unification.
4 set -e
5
6 REAL_GOROOT=$(go env GOROOT)
7 MOXIE_GOROOT="${MOXIE_GOROOT_CACHE:-/tmp/moxie-goroot}"
8
9 LLVM_PREFIX=""
10 LLVM_VER=""
11 for v in 21 19; do
12 for p in /usr/lib/llvm-$v /usr/lib/llvm$v /usr/lib/llvm/$v; do
13 if [ -d "$p" ]; then
14 LLVM_PREFIX="$p"
15 LLVM_VER="$v"
16 break 2
17 fi
18 done
19 done
20 if [ -z "$LLVM_PREFIX" ]; then
21 echo "error: LLVM (21 or 19) not found" >&2
22 exit 1
23 fi
24
25 # Create or update patched GOROOT.
26 # Only recreate if the Go version changed or patches were modified.
27 STAMP="$MOXIE_GOROOT/.stamp"
28 GO_VER=$(go version)
29 PATCH_HASH=$(sha256sum build/patch-gotypes.go | cut -d' ' -f1)
30 WANT_STAMP="$GO_VER $PATCH_HASH"
31
32 if [ -f "$STAMP" ] && [ "$(cat "$STAMP")" = "$WANT_STAMP" ]; then
33 echo "patched GOROOT up to date" >&2
34 else
35 echo "creating patched GOROOT at $MOXIE_GOROOT ..." >&2
36 rm -rf "$MOXIE_GOROOT"
37 mkdir -p "$MOXIE_GOROOT"
38
39 # Symlink top-level items.
40 for item in "$REAL_GOROOT"/*; do
41 ln -sf "$item" "$MOXIE_GOROOT/$(basename "$item")"
42 done
43
44 # Replace src → symlink tree with writable go/types.
45 rm -f "$MOXIE_GOROOT/src"
46 mkdir -p "$MOXIE_GOROOT/src"
47 for item in "$REAL_GOROOT/src"/*; do
48 ln -sf "$item" "$MOXIE_GOROOT/src/$(basename "$item")"
49 done
50 rm -f "$MOXIE_GOROOT/src/go"
51 mkdir -p "$MOXIE_GOROOT/src/go"
52 for item in "$REAL_GOROOT/src/go"/*; do
53 ln -sf "$item" "$MOXIE_GOROOT/src/go/$(basename "$item")"
54 done
55 # go/types gets copied (not symlinked) so we can patch it.
56 rm -f "$MOXIE_GOROOT/src/go/types"
57
58 GOROOT="$REAL_GOROOT" go run build/patch-gotypes.go "$MOXIE_GOROOT"
59 echo "$WANT_STAMP" > "$STAMP"
60 fi
61
62 # Build.
63 export GOROOT="$MOXIE_GOROOT"
64 export GOWORK=off
65 export GOTOOLCHAIN=local
66 export CGO_ENABLED=0
67 export LLVM_LIB_PATH="${LLVM_PREFIX}/lib/libLLVM-${LLVM_VER}.so"
68
69 exec "$MOXIE_GOROOT/bin/go" build -mod=vendor -tags purego -gcflags="-N -l" -o moxie "$@" .
70