|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Opt-in installer for Verrou (the Valgrind FP-perturbation tool used by |
| 4 | +# `./mfc.sh fp-stability`). Verrou is NOT a Python/pip package - it is a fork of |
| 5 | +# Valgrind. By default this downloads a prebuilt, hash-verified artifact (seconds); |
| 6 | +# if none is available for this tag/arch it falls back to a source build (~20 min). |
| 7 | +# fp-stability auto-runs this on first use when Verrou is absent (printing what it |
| 8 | +# does); it is also safe to run by hand. A failed install aborts, never a silent skip. |
| 9 | +# |
| 10 | +# bash toolchain/bootstrap/verrou.sh # install into $HOME/.local/verrou |
| 11 | +# VERROU_HOME=/path bash toolchain/bootstrap/verrou.sh |
| 12 | +# bash toolchain/bootstrap/verrou.sh --force # reinstall even if present |
| 13 | +# VERROU_BUILD_FROM_SOURCE=1 bash toolchain/bootstrap/verrou.sh # skip the prebuilt |
| 14 | +# |
| 15 | +# Versions are pinned to match the fp-stability CI workflow. |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +VALGRIND_VERSION="3.26.0" |
| 20 | +VERROU_COMMIT="a58d434" |
| 21 | +# Prebuilt artifacts (built once per arch) live in a small companion repo. The tag |
| 22 | +# pins to the (valgrind, verrou) pair above - bump all three together. |
| 23 | +VERROU_DIST_REPO="${VERROU_DIST_REPO:-sbryngelson/verrou-dist}" |
| 24 | +VERROU_DIST_TAG="${VERROU_DIST_TAG:-v1}" |
| 25 | +PREFIX="${VERROU_HOME:-$HOME/.local/verrou}" |
| 26 | +FORCE="${1:-}" |
| 27 | + |
| 28 | +echo "==> Verrou bootstrap (Valgrind ${VALGRIND_VERSION} + edf-hpc/verrou@${VERROU_COMMIT}) -> ${PREFIX}" |
| 29 | + |
| 30 | +# Idempotent: skip if already installed and working. Source env.sh first if present |
| 31 | +# (a prebuilt tree needs VALGRIND_LIB to run; a source build works either way). |
| 32 | +if [ "$FORCE" != "--force" ] && [ -x "${PREFIX}/bin/valgrind" ] \ |
| 33 | + && ( [ -f "${PREFIX}/env.sh" ] && . "${PREFIX}/env.sh"; "${PREFIX}/bin/valgrind" --tool=verrou --version >/dev/null 2>&1 ); then |
| 34 | + echo "==> Verrou already installed at ${PREFIX} (use --force to rebuild). Nothing to do." |
| 35 | + exit 0 |
| 36 | +fi |
| 37 | + |
| 38 | +# Platform: Valgrind has no working modern-macOS support; Linux only. |
| 39 | +if [ "$(uname -s)" != "Linux" ]; then |
| 40 | + echo "ERROR: Verrou requires Linux (Valgrind does not support modern macOS, incl. Apple Silicon)." >&2 |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | +arch_tag="" |
| 44 | +case "$(uname -m)" in |
| 45 | + x86_64) arch_tag="x86_64" ;; |
| 46 | + aarch64|arm64) |
| 47 | + arch_tag="aarch64" |
| 48 | + echo "WARNING: $(uname -m) detected. Valgrind builds here, but Verrou's FP backends are" >&2 |
| 49 | + echo " best-validated on x86_64 - treat results as experimental on this arch." >&2 |
| 50 | + ;; |
| 51 | + *) |
| 52 | + echo "WARNING: unrecognised arch $(uname -m); the build may fail. Proceeding anyway." >&2 |
| 53 | + ;; |
| 54 | +esac |
| 55 | + |
| 56 | +# Fast path: download a prebuilt, hash-verified artifact and source its relocatable |
| 57 | +# env.sh, instead of building from source. Any failure (no asset for this arch/tag, |
| 58 | +# missing zstd/sha256sum, checksum mismatch, won't run) falls through to the build. |
| 59 | +try_prebuilt() { |
| 60 | + [ -n "$arch_tag" ] || return 1 |
| 61 | + [ "${VERROU_BUILD_FROM_SOURCE:-}" = "1" ] && return 1 |
| 62 | + command -v sha256sum >/dev/null 2>&1 || return 1 |
| 63 | + tar --zstd --help >/dev/null 2>&1 || command -v zstd >/dev/null 2>&1 || return 1 |
| 64 | + command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1 || return 1 |
| 65 | + |
| 66 | + local asset base dl |
| 67 | + asset="verrou-${VERROU_COMMIT}-valgrind-${VALGRIND_VERSION}-linux-${arch_tag}.tar.zst" |
| 68 | + base="https://github.com/${VERROU_DIST_REPO}/releases/download/${VERROU_DIST_TAG}/${asset}" |
| 69 | + dl="$(mktemp -d)" |
| 70 | + |
| 71 | + echo "==> Trying prebuilt ${VERROU_DIST_REPO}@${VERROU_DIST_TAG} (${asset})" |
| 72 | + _fetch() { # url dest |
| 73 | + if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$2" "$1"; else wget -q -O "$2" "$1"; fi |
| 74 | + } |
| 75 | + if ! _fetch "$base" "$dl/$asset" || ! _fetch "$base.sha256" "$dl/$asset.sha256"; then |
| 76 | + echo "==> No prebuilt for this tag/arch - building from source instead." |
| 77 | + rm -rf "$dl"; return 1 |
| 78 | + fi |
| 79 | + if ! ( cd "$dl" && sha256sum -c "$asset.sha256" >/dev/null 2>&1 ); then |
| 80 | + echo "WARNING: prebuilt checksum mismatch - building from source instead." >&2 |
| 81 | + rm -rf "$dl"; return 1 |
| 82 | + fi |
| 83 | + |
| 84 | + # Extract + verify in a staging dir, then swap into $PREFIX atomically. set -e |
| 85 | + # is suppressed inside a function used as an `if` condition, so check each step |
| 86 | + # explicitly - otherwise a failed extract would fall through and the source |
| 87 | + # build would install on top of a half-written tree (or a stale one on --force). |
| 88 | + local stage="$dl/stage" |
| 89 | + mkdir -p "$stage" |
| 90 | + if tar --zstd --help >/dev/null 2>&1; then |
| 91 | + tar -C "$stage" --zstd -xf "$dl/$asset" || { echo "WARNING: prebuilt extract failed - building from source instead." >&2; rm -rf "$dl"; return 1; } |
| 92 | + else |
| 93 | + zstd -dc "$dl/$asset" | tar -C "$stage" -xf - || { echo "WARNING: prebuilt extract failed - building from source instead." >&2; rm -rf "$dl"; return 1; } |
| 94 | + fi |
| 95 | + |
| 96 | + # Valgrind bakes its build prefix into the binary; the artifact's env.sh sets |
| 97 | + # VALGRIND_LIB relative to the tree so the relocated install works. Verify the |
| 98 | + # staged tree runs before committing it. |
| 99 | + if ! ( . "${stage}/env.sh" && "${stage}/bin/valgrind" --tool=verrou --version >/dev/null 2>&1 ); then |
| 100 | + echo "WARNING: prebuilt did not run - building from source instead." >&2 |
| 101 | + rm -rf "$dl"; return 1 |
| 102 | + fi |
| 103 | + |
| 104 | + # Commit only now: replace any existing $PREFIX atomically. |
| 105 | + mkdir -p "$(dirname "$PREFIX")" |
| 106 | + rm -rf "$PREFIX" |
| 107 | + if ! mv "$stage" "$PREFIX"; then |
| 108 | + echo "WARNING: could not install prebuilt to ${PREFIX} - building from source instead." >&2 |
| 109 | + rm -rf "$dl"; return 1 |
| 110 | + fi |
| 111 | + rm -rf "$dl" |
| 112 | + return 0 |
| 113 | +} |
| 114 | + |
| 115 | +if try_prebuilt; then |
| 116 | + echo "==> Verifying" |
| 117 | + ( . "${PREFIX}/env.sh" && "${PREFIX}/bin/valgrind" --tool=verrou --version ) |
| 118 | + echo "==> Done (prebuilt). Verrou installed at ${PREFIX}" |
| 119 | + echo " Run: ./mfc.sh fp-stability (or set VERROU_HOME=${PREFIX} if you used a custom prefix)" |
| 120 | + exit 0 |
| 121 | +fi |
| 122 | + |
| 123 | +# Build dependencies. |
| 124 | +missing="" |
| 125 | +for tool in tar git make patch autoconf automake; do |
| 126 | + command -v "$tool" >/dev/null 2>&1 || missing="$missing $tool" |
| 127 | +done |
| 128 | +command -v cc >/dev/null 2>&1 || command -v gcc >/dev/null 2>&1 || missing="$missing gcc" |
| 129 | +command -v wget >/dev/null 2>&1 || command -v curl >/dev/null 2>&1 || missing="$missing wget/curl" |
| 130 | +if [ -n "$missing" ]; then |
| 131 | + echo "ERROR: missing build dependencies:$missing" >&2 |
| 132 | + echo " Install them (e.g. apt: build-essential automake autoconf libtool; or load HPC modules) and retry." >&2 |
| 133 | + exit 1 |
| 134 | +fi |
| 135 | + |
| 136 | +workdir="$(mktemp -d)" |
| 137 | +trap 'rm -rf "$workdir"' EXIT |
| 138 | +cd "$workdir" |
| 139 | + |
| 140 | +tarball="valgrind-${VALGRIND_VERSION}.tar.bz2" |
| 141 | +url="https://sourceware.org/pub/valgrind/${tarball}" |
| 142 | +echo "==> Downloading ${tarball}" |
| 143 | +if command -v wget >/dev/null 2>&1; then |
| 144 | + wget -q "$url" |
| 145 | +else |
| 146 | + curl -fsSL -o "$tarball" "$url" |
| 147 | +fi |
| 148 | +tar xf "$tarball" |
| 149 | + |
| 150 | +echo "==> Cloning Verrou @ ${VERROU_COMMIT}" |
| 151 | +git clone --quiet https://github.com/edf-hpc/verrou.git |
| 152 | +git -C verrou checkout --quiet "$VERROU_COMMIT" |
| 153 | + |
| 154 | +# Merge Verrou into the Valgrind tree and apply its patch. |
| 155 | +cp -r verrou "valgrind-${VALGRIND_VERSION}/verrou" |
| 156 | +cd "valgrind-${VALGRIND_VERSION}" |
| 157 | +cat verrou/valgrind.*diff | patch -p1 |
| 158 | + |
| 159 | +echo "==> Building (this takes ~20 min)" |
| 160 | +./autogen.sh |
| 161 | +./configure --enable-only64bit --prefix="$PREFIX" |
| 162 | +make -j"$(nproc)" |
| 163 | +make install |
| 164 | + |
| 165 | +echo "==> Verifying" |
| 166 | +"${PREFIX}/bin/valgrind" --tool=verrou --version |
| 167 | +echo "==> Done. Verrou installed at ${PREFIX}" |
| 168 | +echo " Run: ./mfc.sh fp-stability (or set VERROU_HOME=${PREFIX} if you used a custom prefix)" |
0 commit comments