build.sh raw

   1  #!/bin/bash
   2  set -e
   3  
   4  # The DRIVER speaks one LLVM major: whichever `llvm-config` reports. The tools
   5  # are asked for by their unversioned names and every one of them must report that
   6  # same major - a "-22" suffix on a binary that is really 23 is exactly the lie
   7  # that let this stack drift for months, and mixing majors inside one pipeline
   8  # produces bitcode the next tool cannot read (LLVM reads older bitcode, never
   9  # newer: a newer opt rewrites llvm.returnaddress into a name an older clang
  10  # cannot lower).
  11  #
  12  # The LEGACY bootstrap compiler is a separate client: its vendored go-llvm
  13  # purego layer is LLVM 22 shaped (it reports a `br` with the wrong opcode under
  14  # 23, measured), so it loads libLLVM-22. It writes 22 bitcode that this 23
  15  # driver reads - the supported direction - and no 22 tool ever reads 23 output.
  16  LLVM_MAJOR="${LLVM_MAJOR:-}"
  17  if [ -z "$LLVM_MAJOR" ]; then
  18      if command -v llvm-config >/dev/null 2>&1; then
  19          LLVM_MAJOR="$(llvm-config --version 2>/dev/null | cut -d. -f1)"
  20      fi
  21  fi
  22  LEGACY_LLVM_MAJOR="${LEGACY_LLVM_MAJOR:-22}"
  23  
  24  need_major() {
  25      local name="$1" bin="$1" want="$2"
  26      command -v "$bin" >/dev/null 2>&1 || { FAIL="$FAIL $name"; return; }
  27      local ver
  28      ver="$("$bin" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+' | head -1 | cut -d. -f1)"
  29      if [ "$ver" != "$want" ]; then FAIL="$FAIL $name(=${ver:-unknown})"; fi
  30  }
  31  
  32  FAIL=""
  33  if [ -z "$LLVM_MAJOR" ]; then
  34      echo "error: no llvm-config found; set LLVM_MAJOR to the LLVM major to use" >&2
  35      exit 1
  36  fi
  37  for tool in clang llc ld.lld opt llvm-link llvm-extract; do need_major "$tool" "$LLVM_MAJOR"; done
  38  if [ -n "$FAIL" ]; then
  39      echo "error: LLVM $LLVM_MAJOR required (the host's one major). bad/missing:$FAIL" >&2
  40      echo "  arch:   pacman -S llvm clang lld" >&2
  41      exit 1
  42  fi
  43  
  44  # The legacy bootstrap compiler needs its own major's shared library; without it
  45  # the reference half of the bootstrap (phase 2, phase 6 :legacy) cannot run.
  46  LEGACY_LIB=""
  47  for p in "/usr/lib/libLLVM-${LEGACY_LLVM_MAJOR}.so" \
  48           "/usr/lib64/libLLVM-${LEGACY_LLVM_MAJOR}.so" \
  49           "/usr/lib/llvm-${LEGACY_LLVM_MAJOR}/lib/libLLVM-${LEGACY_LLVM_MAJOR}.so"; do
  50      if [ -f "$p" ]; then LEGACY_LIB="$p"; break; fi
  51  done
  52  if [ -z "$LEGACY_LIB" ]; then
  53      echo "warning: libLLVM-${LEGACY_LLVM_MAJOR}.so not found: legacy/moxie cannot run," >&2
  54      echo "         so the bootstrap chain (phase 2, phase 6 :legacy) is unavailable." >&2
  55  fi
  56  
  57  MOXIEROOT="$(pwd)"
  58  export MOXIEROOT
  59  export LLVM_GLUE_PATH="$MOXIEROOT/llvm-glue/libmoxie-llvm-glue.so"
  60  export PATH="/usr/local/bin:$PATH"
  61  
  62  # Build the LLVM glue when missing or when a source is newer than the .so.
  63  # The purego bindings resolve glue symbols with mustSym, so a stale .so is a
  64  # hard failure (missing symbol), not a silent fallback.
  65  GLUE_SRC_DIR="$MOXIEROOT/llvm-glue"
  66  GLUE_STALE=0
  67  for src in "$GLUE_SRC_DIR/shims.c" \
  68  	"$MOXIEROOT/legacy/vendor/tinygo.org/x/go-llvm/IRBindings.cpp" \
  69  	"$MOXIEROOT/legacy/vendor/tinygo.org/x/go-llvm/backports.cpp"; do
  70  	if [ "$src" -nt "$LLVM_GLUE_PATH" ]; then GLUE_STALE=1; fi
  71  done
  72  if [ ! -f "$LLVM_GLUE_PATH" ] || [ "$GLUE_STALE" = 1 ]; then
  73      echo "Building llvm-glue (LLVM ${LEGACY_LLVM_MAJOR}, the legacy client) ..."
  74      LLVM_VER="$LEGACY_LLVM_MAJOR" bash "$GLUE_SRC_DIR/build.sh"
  75  fi
  76  
  77  # find a moxie binary to bootstrap with
  78  MOXIE="${MOXIE:-}"
  79  if [ -z "$MOXIE" ]; then
  80      for m in "$MOXIEROOT/moxie-new" "$MOXIEROOT/moxie" "$MOXIEROOT/legacy/moxie" "$(command -v moxie 2>/dev/null || true)"; do
  81          if [ -x "$m" ]; then
  82              MOXIE="$m"
  83              break
  84          fi
  85      done
  86  fi
  87  if [ -z "$MOXIE" ]; then
  88      echo "error: no moxie binary found" >&2
  89      echo "  place a bootstrap binary at ./moxie-new, ./moxie, or ./legacy/moxie" >&2
  90      exit 1
  91  fi
  92  
  93  MOXIE="$(cd "$(dirname "$MOXIE")" && pwd)/$(basename "$MOXIE")"
  94  echo "=== Building moxie (self-hosting) ==="
  95  echo "Bootstrap: $MOXIE"
  96  
  97  # compiler source directory
  98  COMPILER_SRC="$MOXIEROOT/_mxc_stage4"
  99  if [ ! -d "$COMPILER_SRC" ] || [ ! -f "$COMPILER_SRC/main.mx" ]; then
 100      echo "error: compiler source not found in _mxc_stage4/" >&2
 101      exit 1
 102  fi
 103  echo "Source: $COMPILER_SRC"
 104  
 105  $MOXIE build -o "$MOXIEROOT/moxie-new" "$COMPILER_SRC/"
 106  
 107  # verify the new binary works
 108  "$MOXIEROOT/moxie-new" version
 109  
 110  mv "$MOXIEROOT/moxie-new" "$MOXIEROOT/moxie"
 111  
 112  # rebuild all cached packages with the new compiler and remove old version caches
 113  "$MOXIEROOT/moxie" update --all
 114  
 115  # run regression validation
 116  #
 117  # --force, because the unit results are persistent state: without it a unit that
 118  # passed once is skipped, so on a machine that has run the suite before this
 119  # "check" examines nothing, prints no verdicts, and returns success. Measured on
 120  # a fresh build host: s1 was installed, zero units ran, and the bootstrap
 121  # reported the regression check as passed. A gate that skips is not a gate.
 122  echo "=== Regression Check ==="
 123  if ! MOXIE="$MOXIEROOT/moxie" bash "$MOXIEROOT/tests/unitrun.sh" --phases 0a --fail-fast --force; then
 124      echo "REGRESSION DETECTED: bootstrap build suspect" >&2
 125      exit 1
 126  fi
 127  
 128  echo "=== Done: ./moxie ==="
 129