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 # Detect LLVM 19.
10 LLVM_PREFIX="/usr/lib/llvm19"
11 if [ ! -d "$LLVM_PREFIX" ]; then
12 LLVM_PREFIX="/usr/lib/llvm-19"
13 fi
14 if [ ! -d "$LLVM_PREFIX" ]; then
15 echo "error: LLVM 19 not found at /usr/lib/llvm19 or /usr/lib/llvm-19" >&2
16 exit 1
17 fi
18
19 # Create or update patched GOROOT.
20 # Only recreate if the Go version changed or patches were modified.
21 STAMP="$MOXIE_GOROOT/.stamp"
22 GO_VER=$(go version)
23 PATCH_HASH=$(sha256sum build/patch-gotypes.go | cut -d' ' -f1)
24 WANT_STAMP="$GO_VER $PATCH_HASH"
25
26 if [ -f "$STAMP" ] && [ "$(cat "$STAMP")" = "$WANT_STAMP" ]; then
27 echo "patched GOROOT up to date" >&2
28 else
29 echo "creating patched GOROOT at $MOXIE_GOROOT ..." >&2
30 rm -rf "$MOXIE_GOROOT"
31 mkdir -p "$MOXIE_GOROOT"
32
33 # Symlink top-level items.
34 for item in "$REAL_GOROOT"/*; do
35 ln -sf "$item" "$MOXIE_GOROOT/$(basename "$item")"
36 done
37
38 # Replace src → symlink tree with writable go/types.
39 rm -f "$MOXIE_GOROOT/src"
40 mkdir -p "$MOXIE_GOROOT/src"
41 for item in "$REAL_GOROOT/src"/*; do
42 ln -sf "$item" "$MOXIE_GOROOT/src/$(basename "$item")"
43 done
44 rm -f "$MOXIE_GOROOT/src/go"
45 mkdir -p "$MOXIE_GOROOT/src/go"
46 for item in "$REAL_GOROOT/src/go"/*; do
47 ln -sf "$item" "$MOXIE_GOROOT/src/go/$(basename "$item")"
48 done
49 # go/types gets copied (not symlinked) so we can patch it.
50 rm -f "$MOXIE_GOROOT/src/go/types"
51
52 GOROOT="$REAL_GOROOT" go run build/patch-gotypes.go "$MOXIE_GOROOT"
53 echo "$WANT_STAMP" > "$STAMP"
54 fi
55
56 # Build.
57 export GOROOT="$MOXIE_GOROOT"
58 export GOWORK=off
59 export CGO_CPPFLAGS="-I${LLVM_PREFIX}/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS"
60 export CGO_CFLAGS="-I${LLVM_PREFIX}/include"
61 export CGO_LDFLAGS="-L${LLVM_PREFIX}/lib -lLLVM-19 -lclang"
62
63 exec go build -mod=vendor -tags llvm19 -o moxie "$@" .
64