provision-build-host.sh raw

   1  #!/bin/bash
   2  # Provision a build host for the Moxie toolchain, from nothing.
   3  #
   4  # Every line here exists because a fresh machine was missing it and a build
   5  # failed: this script IS the bootstrap documentation, versioned in the repo, so
   6  # "it builds on my machine" stops being a statement about accumulated state.
   7  # Run it as root on a clean Ubuntu 24.04 host, then run the bootstrap below.
   8  #
   9  # Versions are PINNED, not latest:
  10  #
  11  #   * Go 1.25.3 - legacy/build/patch-gotypes.go patches go/types by exact
  12  #     source text, so a newer Go is a different source tree and the patch
  13  #     refuses to apply (measured on 1.27.3). Bumping Go means re-checking every
  14  #     anchor in that file in the same commit.
  15  #   * LLVM 22 - the compiler's bitcode and its enum values; 23 reads 22 but the
  16  #     purego layer misreports opcodes (see legacy/build.sh's header). The exact
  17  #     tested build is recorded below and printed by --check.
  18  #
  19  # What the repo does NOT carry, and this script supplies:
  20  #
  21  #   * lib/compiler-rt-builtins - the repository tracked an ABSOLUTE symlink to
  22  #     the author's llvm checkout, so a fresh clone has a dangling path. Fixed
  23  #     here by cloning llvm-project at release/22.x and linking compiler-rt's
  24  #     builtins into place.
  25  #   * llvm-glue/libmoxie-llvm-glue.so - a build product of llvm-glue/build.sh
  26  #     (clang++-22). Without it the legacy compiler dies at start-up with
  27  #     "missing symbol MoxieCreateCompileUnit".
  28  #   * unversioned LLVM tools - the compiler invokes `clang`, `opt`,
  29  #     `llvm-link`, `llc` and `ld.lld` by bare name. On Debian/Ubuntu the
  30  #     versioned packages install only `opt-22` etc., and the unversioned names
  31  #     either do not exist or point at a different major (the author's host had
  32  #     `opt` = 23). Symlinked in /usr/local/bin, which precedes /usr/bin.
  33  #
  34  # Usage:
  35  #   provision-build-host.sh            # provision (idempotent)
  36  #   provision-build-host.sh --check    # report, change nothing
  37  #   provision-build-host.sh --bootstrap  # provision, then build and fixpoint
  38  #
  39  set -euo pipefail
  40  
  41  GO_VERSION="1.25.3"
  42  GO_SHA256="0335f314b6e7bfe08c3d0cfaa7c19db961b7b99fb20be62b0a826c992ad14e0f"
  43  LLVM_MAJOR="22"
  44  LLVM_TESTED="22.1.8"          # the build this tree was verified against
  45  LLVM_BRANCH="release/22.x"    # compiler-rt sources for the target builtins
  46  BUILD_ROOT="${BUILD_ROOT:-/srv/build}"
  47  SRC_ROOT="${SRC_ROOT:-/srv/src}"
  48  GO_ROOT="/opt/go${GO_VERSION}"
  49  TOOLCHAIN="$BUILD_ROOT/toolchain"
  50  
  51  CHECK_ONLY=0
  52  RUN_BOOTSTRAP=0
  53  for a in "$@"; do
  54  	case "$a" in
  55  	--check) CHECK_ONLY=1 ;;
  56  	--bootstrap) RUN_BOOTSTRAP=1 ;;
  57  	*) echo "usage: $0 [--check] [--bootstrap]" >&2; exit 2 ;;
  58  	esac
  59  done
  60  
  61  say() { printf '\n== %s\n' "$*"; }
  62  have() { command -v "$1" >/dev/null 2>&1; }
  63  ok() { printf '   ok   %s\n' "$*"; }
  64  miss() { printf '   MISS %s\n' "$*"; }
  65  
  66  problems=0
  67  want() { # want <description> <test command...>
  68  	local desc="$1"; shift
  69  	if "$@" >/dev/null 2>&1; then ok "$desc"; else miss "$desc"; problems=$((problems + 1)); fi
  70  }
  71  
  72  # ── check ───────────────────────────────────────────────────────────────────
  73  say "checking the host for what a Moxie build needs"
  74  for t in git curl tar xz sha256sum; do want "$t" have "$t"; done
  75  want "go ${GO_VERSION}" test -x "$GO_ROOT/bin/go"
  76  want "clang++-${LLVM_MAJOR}" have "clang++-${LLVM_MAJOR}"
  77  for t in clang opt llvm-link llc ld.lld llvm-config; do
  78  	want "unversioned $t is LLVM ${LLVM_MAJOR}" sh -c \
  79  		"have() { command -v \"\$1\" >/dev/null 2>&1; }; have $t && $t --version 2>/dev/null | grep -q 'version ${LLVM_MAJOR}\.'"
  80  done
  81  want "libLLVM-${LLVM_MAJOR}.so" test -e "/usr/lib/x86_64-linux-gnu/libLLVM-${LLVM_MAJOR}.so" 
  82  want "glue libmoxie-llvm-glue.so" test -e "$TOOLCHAIN/llvm-glue/libmoxie-llvm-glue.so"
  83  want "compiler-rt builtins sources" test -e "$TOOLCHAIN/lib/compiler-rt-builtins/absvdi2.c"
  84  if [ -x "$TOOLCHAIN/legacy/moxie" ]; then
  85  	ok "legacy compiler built"
  86  else
  87  	miss "legacy compiler built (run bootstrap)"
  88  fi
  89  
  90  if [ "$CHECK_ONLY" = 1 ]; then
  91  	printf '\n%d missing\n' "$problems"
  92  	[ "$problems" = 0 ] || exit 1
  93  	exit 0
  94  fi
  95  
  96  # ── provision ───────────────────────────────────────────────────────────────
  97  say "packages"
  98  export DEBIAN_FRONTEND=noninteractive
  99  apt-get update -qq
 100  apt-get install -y -qq \
 101  	build-essential ca-certificates git curl tar xz-utils rsync tmux python3 \
 102  	python3-pip pkg-config zlib1g-dev libssl-dev gdb >/dev/null
 103  ok "base packages"
 104  
 105  # Playwright + chromium: the wasm half of phases 1c and 5 runs every module in a
 106  # real browser, and without a browser it reports 116 units SKIPPED rather than
 107  # failed - which is exactly how a bug that trapped every wasm program in package
 108  # init survived a green suite. Skipping is not verification.
 109  say "playwright chromium (phase 1c/5 browser runs)"
 110  python3 -m pip install --break-system-packages -q playwright
 111  python3 -m playwright install --with-deps chromium >/dev/null
 112  python3 -c 'from playwright.sync_api import sync_playwright
 113  with sync_playwright() as p:
 114      b = p.chromium.launch(); b.close()' || { echo "playwright chromium does not launch" >&2; exit 1; }
 115  ok "chromium launches under playwright"
 116  
 117  say "LLVM ${LLVM_MAJOR} (apt.llvm.org, tested ${LLVM_TESTED})"
 118  if [ ! -e /etc/apt/sources.list.d/llvm-${LLVM_MAJOR}.list ]; then
 119  	curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key \
 120  		| gpg --dearmor -o /etc/apt/keyrings/llvm.gpg
 121  	echo "deb [signed-by=/etc/apt/keyrings/llvm.gpg] http://apt.llvm.org/$(. /etc/os-release && echo "$VERSION_CODENAME")/ llvm-toolchain-$(. /etc/os-release && echo "$VERSION_CODENAME")-${LLVM_MAJOR} main" \
 122  		> "/etc/apt/sources.list.d/llvm-${LLVM_MAJOR}.list"
 123  	apt-get update -qq
 124  fi
 125  apt-get install -y -qq \
 126  	"clang-${LLVM_MAJOR}" "clang++-${LLVM_MAJOR}" "lld-${LLVM_MAJOR}" \
 127  	"llvm-${LLVM_MAJOR}" "llvm-${LLVM_MAJOR}-dev" "libclang-${LLVM_MAJOR}-dev" \
 128  	"libpolly-${LLVM_MAJOR}-dev" >/dev/null
 129  ok "$(clang-${LLVM_MAJOR} --version | head -1)"
 130  
 131  say "unversioned LLVM tools -> ${LLVM_MAJOR}"
 132  for t in clang clang++ opt llvm-link llc ld.lld llvm-extract llvm-as llvm-dis llvm-config; do
 133  	if [ -x "/usr/lib/llvm-${LLVM_MAJOR}/bin/$t" ]; then
 134  		ln -sf "/usr/lib/llvm-${LLVM_MAJOR}/bin/$t" "/usr/local/bin/$t"
 135  	fi
 136  done
 137  hash -r
 138  ok "clang now: $(clang --version | head -1)"
 139  
 140  say "Go ${GO_VERSION} at ${GO_ROOT}"
 141  if [ ! -x "$GO_ROOT/bin/go" ]; then
 142  	cd /tmp
 143  	curl -fsSL -o "go${GO_VERSION}.tar.gz" "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
 144  	echo "${GO_SHA256}  go${GO_VERSION}.tar.gz" | sha256sum -c -
 145  	rm -rf "/tmp/go-extract"
 146  	mkdir -p "/tmp/go-extract"
 147  	tar -C "/tmp/go-extract" -xzf "go${GO_VERSION}.tar.gz"
 148  	rm -rf "$GO_ROOT"
 149  	mv "/tmp/go-extract/go" "$GO_ROOT"
 150  	rm -rf "/tmp/go-extract" "go${GO_VERSION}.tar.gz"
 151  fi
 152  ok "$("$GO_ROOT/bin/go" version)"
 153  
 154  say "toolchain checkout at ${TOOLCHAIN}"
 155  mkdir -p "$BUILD_ROOT" "$SRC_ROOT"
 156  if [ ! -d "$TOOLCHAIN/.git" ]; then
 157  	git clone -q -b dev https://github.com/moxie-language/toolchain.git "$TOOLCHAIN"
 158  fi
 159  ok "$(git -C "$TOOLCHAIN" log --oneline -1)"
 160  
 161  say "compiler-rt builtins (llvm-project ${LLVM_BRANCH})"
 162  if [ ! -e "$TOOLCHAIN/lib/compiler-rt-builtins/absvdi2.c" ]; then
 163  	rm -f "$TOOLCHAIN/lib/compiler-rt-builtins"   # the tracked absolute symlink
 164  	if [ ! -d "$SRC_ROOT/llvm-${LLVM_MAJOR}/.git" ]; then
 165  		git clone -q --depth 1 --filter=blob:none --sparse \
 166  			-b "$LLVM_BRANCH" https://github.com/llvm/llvm-project.git \
 167  			"$SRC_ROOT/llvm-${LLVM_MAJOR}"
 168  		git -C "$SRC_ROOT/llvm-${LLVM_MAJOR}" sparse-checkout set compiler-rt/lib/builtins
 169  	fi
 170  	ln -sfn "$SRC_ROOT/llvm-${LLVM_MAJOR}/compiler-rt/lib/builtins" \
 171  		"$TOOLCHAIN/lib/compiler-rt-builtins"
 172  fi
 173  ok "$(ls "$TOOLCHAIN/lib/compiler-rt-builtins" | wc -l) builtins sources"
 174  
 175  say "LLVM glue (llvm-glue/build.sh)"
 176  if [ ! -e "$TOOLCHAIN/llvm-glue/libmoxie-llvm-glue.so" ]; then
 177  	( cd "$TOOLCHAIN/llvm-glue" && CXX="clang++-${LLVM_MAJOR}" sh build.sh >/dev/null )
 178  fi
 179  ok "$(nm -D "$TOOLCHAIN/llvm-glue/libmoxie-llvm-glue.so" | grep -c ' T Moxie') Moxie shims"
 180  
 181  say "legacy compiler"
 182  if [ ! -x "$TOOLCHAIN/legacy/moxie" ]; then
 183  	( cd "$TOOLCHAIN/legacy" && PATH="$GO_ROOT/bin:$PATH" GOROOT="$GO_ROOT" \
 184  		bash build.sh >/tmp/provision-legacy.log 2>&1 ) \
 185  		|| { echo "legacy build failed; log: /tmp/provision-legacy.log" >&2; exit 1; }
 186  fi
 187  ok "$("$TOOLCHAIN/legacy/moxie" version 2>&1 | head -1)"
 188  
 189  if [ "$RUN_BOOTSTRAP" = 1 ]; then
 190  	# The bootstrap protocol, encoded here rather than documented and hoped for:
 191  	# build.sh produces s1 only, so the fixpoint is this script's job. Every
 192  	# step uses `-a` (build.sh does) because a compiler built against a different
 193  	# runtime must not reuse cached .bc.
 194  	#
 195  	# s1 MUST come from legacy/moxie, so MOXIE pins it. Left to itself build.sh
 196  	# prefers ./moxie-new, then ./moxie, then legacy/moxie - a developer's order,
 197  	# which is right for iterating on a tree whose compiler works and wrong for
 198  	# a bootstrap, where ./moxie may be the very binary being replaced. Measured:
 199  	# a stale ./moxie built s1, s1 inherited its short interface-dispatch table,
 200  	# and s1 then panicked compiling compress_roundtrip with "no impl for Read
 201  	# (16 impl(s), none matches)" - a bootstrap that reproduces the bug it is
 202  	# supposed to replace.
 203  	#
 204  	# The bake is LAST and uses the installed compiler, because the bake is
 205  	# compiler-specific: sysroot/runtime_go.bc is read by application links, and
 206  	# a blob written by a different compiler generation is exactly the drift that
 207  	# makes apps fail to link with undefined symbols.
 208  	say "bootstrap: s1, s2, s3, fixpoint, bake"
 209  	blog=/tmp/provision-bootstrap.log
 210  	: >"$blog"
 211  	cd "$TOOLCHAIN"
 212  	export PATH="$GO_ROOT/bin:$PATH" GOROOT="$GO_ROOT" MOXIEROOT="$TOOLCHAIN"
 213  	step() { # step <description> <command...>
 214  		local desc="$1"; shift
 215  		printf '   ...  %s\n' "$desc"
 216  		if ! "$@" >>"$blog" 2>&1; then
 217  			printf '   FAIL %s\n' "$desc" >&2
 218  			tail -25 "$blog" >&2
 219  			printf '\nbootstrap failed at: %s (log: %s)\n' "$desc" "$blog" >&2
 220  			exit 1
 221  		fi
 222  	}
 223  	step "s1 (legacy builds _mxc_stage4)" env MOXIE="$TOOLCHAIN/legacy/moxie" bash ./build.sh
 224  	step "s2 (s1 rebuilds _mxc_stage4)" ./moxie build -a -o mxc-stage4-2 ./_mxc_stage4
 225  	step "s3 (s2 rebuilds _mxc_stage4)" ./mxc-stage4-2 build -a -o mxc-stage4-3 ./_mxc_stage4
 226  	s2="$(sha256sum mxc-stage4-2 | cut -d' ' -f1)"
 227  	s3="$(sha256sum mxc-stage4-3 | cut -d' ' -f1)"
 228  	# Convergence may take one generation longer than the minimum: the legacy
 229  	# pipeline differs (interp + LowerInterfaces + globaldce) and the first
 230  	# bootstrap inherits that, so s1 != s2 is expected and s2 == s3 is not the
 231  	# only acceptable arrival - s3 == s4 is the other (claude.md, Convergence).
 232  	# What is never acceptable is no generation reproducing itself at all.
 233  	# Measured on the box: this tree took the extra generation, s3 == s4.
 234  	if [ "$s2" = "$s3" ]; then
 235  		ok "fixpoint: s2 == s3 (${s2:0:16})"
 236  		final=mxc-stage4-2
 237  	else
 238  		step "s4 (s3 rebuilds _mxc_stage4; s2 != s3, so one extra generation)" \
 239  			./mxc-stage4-3 build -a -o mxc-stage4-4 ./_mxc_stage4
 240  		s4="$(sha256sum mxc-stage4-4 | cut -d' ' -f1)"
 241  		if [ "$s3" != "$s4" ]; then
 242  			printf '   FAIL fixpoint: no generation reproduces itself\n' >&2
 243  			printf '         s2=%s\n         s3=%s\n         s4=%s (log: %s)\n' \
 244  				"$s2" "$s3" "$s4" "$blog" >&2
 245  			exit 1
 246  		fi
 247  		ok "fixpoint: s3 == s4 (${s3:0:16})"
 248  		final=mxc-stage4-3
 249  	fi
 250  	cp "$final" ./moxie
 251  	ok "installed $final as ./moxie"
 252  	step "runtime bake (with the installed compiler)" ./moxie build-runtime
 253  	ok "baked $(sha256sum sysroot/runtime_go.bc | cut -d' ' -f1 | cut -c1-16)"
 254  fi
 255  
 256  printf '\nprovisioned. Re-run with --check to verify, then:\n'
 257  printf '  %s --bootstrap   # s1 -> s2/s3 (or s3/s4) -> bake, the whole protocol\n' "$0"
 258  printf '  cd %s && bash run_tests.sh      # the full gate\n' "$TOOLCHAIN"
 259