diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 18:54:00 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 18:55:21 +0100 |
| commit | 53c85c99647ded9fab199c97b9383744e2524364 (patch) | |
| tree | 0c4bad8cad75bd4d00e2eff5374c10a42a31210b /comp.sh | |
| parent | 0.7.1: drop argument-less tool calls, print each command once (diff) | |
| download | agent686-53c85c99647ded9fab199c97b9383744e2524364.tar.gz agent686-53c85c99647ded9fab199c97b9383744e2524364.zip | |
0.8.0: comp.sh reports how to build for the machine it runs on
Detects distro, arch, libc/glibc, CPU baseline and toolchain, prints the target
triple, build command, cfg values, and the portable paths (static musl, older
glibc image, cargo-zigbuild). --run builds and checks; --musl and --old-glibc
select the other plans.
Verified: native gnu build needs GLIBC_2.34; static musl build runs on Debian 10
and reaches the live API.
Diffstat (limited to '')
| -rwxr-xr-x | comp.sh | 248 |
1 files changed, 248 insertions, 0 deletions
| @@ -0,0 +1,248 @@ | |||
| 1 | #!/usr/bin/env bash | ||
| 2 | # | ||
| 3 | # comp.sh — work out how to build agent686 for *this* machine. | ||
| 4 | # | ||
| 5 | # Default is dry: it prints the plan. `--run` executes the native build and its | ||
| 6 | # checks. `--musl` selects the static plan; `--old-glibc` prints the container | ||
| 7 | # recipe for the oldest glibc we support. | ||
| 8 | # | ||
| 9 | # The point is to make the target explicit, because "it built" on a modern host | ||
| 10 | # says nothing about whether it runs on baseline Debian. | ||
| 11 | set -euo pipefail | ||
| 12 | |||
| 13 | here=$(cd "$(dirname "$0")" && pwd) | ||
| 14 | run=0 | ||
| 15 | plan=native | ||
| 16 | |||
| 17 | for arg in "$@"; do | ||
| 18 | case "$arg" in | ||
| 19 | --run) run=1 ;; | ||
| 20 | --musl) plan=musl ;; | ||
| 21 | --old-glibc) plan=old-glibc ;; | ||
| 22 | -h | --help) | ||
| 23 | sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' | ||
| 24 | exit 0 | ||
| 25 | ;; | ||
| 26 | *) | ||
| 27 | echo "comp.sh: unknown argument: $arg" >&2 | ||
| 28 | exit 2 | ||
| 29 | ;; | ||
| 30 | esac | ||
| 31 | done | ||
| 32 | |||
| 33 | say() { printf '%s\n' "$*"; } | ||
| 34 | section() { printf '\n\033[1m== %s\033[0m\n' "$*"; } | ||
| 35 | note() { printf ' %s\n' "$*"; } | ||
| 36 | |||
| 37 | # ---------------------------------------------------------------- detection | ||
| 38 | |||
| 39 | arch=$(uname -m) | ||
| 40 | kernel=$(uname -r) | ||
| 41 | |||
| 42 | distro=unknown | ||
| 43 | if [ -r /etc/os-release ]; then | ||
| 44 | # shellcheck disable=SC1091 | ||
| 45 | . /etc/os-release | ||
| 46 | distro=${PRETTY_NAME:-${ID:-unknown}} | ||
| 47 | fi | ||
| 48 | |||
| 49 | libc=unknown | ||
| 50 | libc_version= | ||
| 51 | if getconf GNU_LIBC_VERSION >/dev/null 2>&1; then | ||
| 52 | libc=glibc | ||
| 53 | libc_version=$(getconf GNU_LIBC_VERSION | awk '{print $2}') | ||
| 54 | elif command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | head -1 | grep -qi musl; then | ||
| 55 | libc=musl | ||
| 56 | libc_version=$(ldd --version 2>&1 | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) | ||
| 57 | elif ls /lib/ld-musl-*.so.1 >/dev/null 2>&1; then | ||
| 58 | libc=musl | ||
| 59 | fi | ||
| 60 | |||
| 61 | cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true) | ||
| 62 | cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true) | ||
| 63 | |||
| 64 | rustc_version=$(rustc -V 2>/dev/null || echo "not installed") | ||
| 65 | cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") | ||
| 66 | |||
| 67 | # ---------------------------------------------------------------- triple map | ||
| 68 | |||
| 69 | triple_gnu=unknown-unknown-linux-gnu | ||
| 70 | triple_musl=unknown-unknown-linux-musl | ||
| 71 | baseline_flag= | ||
| 72 | |||
| 73 | case "$arch" in | ||
| 74 | x86_64 | amd64) | ||
| 75 | triple_gnu=x86_64-unknown-linux-gnu | ||
| 76 | triple_musl=x86_64-unknown-linux-musl | ||
| 77 | # x86-64 v1: SSE2 only. Anything higher excludes pre-Nehalem CPUs. | ||
| 78 | baseline_flag="-C target-cpu=x86-64" | ||
| 79 | ;; | ||
| 80 | aarch64 | arm64) | ||
| 81 | triple_gnu=aarch64-unknown-linux-gnu | ||
| 82 | triple_musl=aarch64-unknown-linux-musl | ||
| 83 | ;; | ||
| 84 | armv7l | armv7) | ||
| 85 | triple_gnu=armv7-unknown-linux-gnueabihf | ||
| 86 | triple_musl=armv7-unknown-linux-musleabihf | ||
| 87 | ;; | ||
| 88 | armv6l) | ||
| 89 | triple_gnu=arm-unknown-linux-gnueabihf | ||
| 90 | triple_musl=arm-unknown-linux-musleabihf | ||
| 91 | ;; | ||
| 92 | i686 | i386) | ||
| 93 | triple_gnu=i686-unknown-linux-gnu | ||
| 94 | triple_musl=i686-unknown-linux-musl | ||
| 95 | ;; | ||
| 96 | riscv64) | ||
| 97 | triple_gnu=riscv64gc-unknown-linux-gnu | ||
| 98 | triple_musl=riscv64gc-unknown-linux-musl | ||
| 99 | ;; | ||
| 100 | ppc64le) | ||
| 101 | triple_gnu=powerpc64le-unknown-linux-gnu | ||
| 102 | triple_musl=powerpc64le-unknown-linux-musl | ||
| 103 | ;; | ||
| 104 | s390x) | ||
| 105 | triple_gnu=s390x-unknown-linux-gnu | ||
| 106 | triple_musl=s390x-unknown-linux-musl | ||
| 107 | ;; | ||
| 108 | *) | ||
| 109 | note "unrecognised architecture '$arch'; edit comp.sh's triple map" | ||
| 110 | ;; | ||
| 111 | esac | ||
| 112 | |||
| 113 | native_triple=$triple_gnu | ||
| 114 | [ "$libc" = musl ] && native_triple=$triple_musl | ||
| 115 | |||
| 116 | # The i686 gnu target uses linker flags from .cargo/config.toml; setting | ||
| 117 | # RUSTFLAGS at all would shadow them, so repeat them when needed. | ||
| 118 | defsym_flag= | ||
| 119 | if [ "$triple_gnu" = i686-unknown-linux-gnu ]; then | ||
| 120 | defsym_flag="-C link-arg=-Wl,--defsym,pidfd_spawnp=0 -C link-arg=-Wl,--defsym,pidfd_getpid=0" | ||
| 121 | fi | ||
| 122 | |||
| 123 | # ---------------------------------------------------------------- report | ||
| 124 | |||
| 125 | section "machine" | ||
| 126 | note "distro $distro" | ||
| 127 | note "arch $arch ($(getconf LONG_BIT 2>/dev/null || echo '?')-bit)" | ||
| 128 | note "kernel $kernel" | ||
| 129 | note "libc ${libc}${libc_version:+ $libc_version}" | ||
| 130 | note "cpu ${cpu_model:-unknown}" | ||
| 131 | note "rust $rustc_version" | ||
| 132 | note "cargo $cargo_version" | ||
| 133 | if [ -n "$cpu_flags" ]; then | ||
| 134 | for feature in sse2 sse4_2 avx avx2; do | ||
| 135 | case " $cpu_flags " in | ||
| 136 | *" $feature "*) note "has $feature" ;; | ||
| 137 | esac | ||
| 138 | done | ||
| 139 | fi | ||
| 140 | |||
| 141 | section "target" | ||
| 142 | case "$plan" in | ||
| 143 | native) | ||
| 144 | say " target $native_triple" | ||
| 145 | ;; | ||
| 146 | musl) | ||
| 147 | say " target $triple_musl (static)" | ||
| 148 | ;; | ||
| 149 | old-glibc) | ||
| 150 | say " target $triple_gnu, built inside an older image" | ||
| 151 | ;; | ||
| 152 | esac | ||
| 153 | |||
| 154 | if [ "$baseline_flag" = "-C target-cpu=x86-64" ]; then | ||
| 155 | note "baseline x86-64 (SSE2): runs on any 64-bit x86 CPU" | ||
| 156 | fi | ||
| 157 | |||
| 158 | case "$plan" in | ||
| 159 | native) | ||
| 160 | if [ "$libc" = glibc ]; then | ||
| 161 | say "" | ||
| 162 | say " Building here links against this machine's glibc ($libc_version)." | ||
| 163 | say " The binary will refuse to start on any machine with an older one." | ||
| 164 | say " For a portable build, use --musl or --old-glibc." | ||
| 165 | fi | ||
| 166 | ;; | ||
| 167 | esac | ||
| 168 | |||
| 169 | section "commands" | ||
| 170 | |||
| 171 | case "$plan" in | ||
| 172 | native | musl) | ||
| 173 | triple=$native_triple | ||
| 174 | [ "$plan" = musl ] && triple=$triple_musl | ||
| 175 | extra_flags=$baseline_flag | ||
| 176 | [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" | ||
| 177 | |||
| 178 | say " rustup target add $triple # if not already installed" | ||
| 179 | if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then | ||
| 180 | say " # needs a C toolchain for ring: apt install musl-tools" | ||
| 181 | fi | ||
| 182 | if [ -n "$extra_flags" ]; then | ||
| 183 | say " RUSTFLAGS=\"$extra_flags\" \\" | ||
| 184 | fi | ||
| 185 | say " cargo build --release --target $triple" | ||
| 186 | case "$triple" in | ||
| 187 | *-gnu) | ||
| 188 | say " ./scripts/check-glibc.sh target/$triple/release/agent686" | ||
| 189 | ;; | ||
| 190 | *) | ||
| 191 | say " file target/$triple/release/agent686 # expect: statically linked" | ||
| 192 | ;; | ||
| 193 | esac | ||
| 194 | ;; | ||
| 195 | old-glibc) | ||
| 196 | say " # The floor is the glibc in the image you build in:" | ||
| 197 | say " # debian:12 bookworm -> glibc 2.36" | ||
| 198 | say " # debian:11 bullseye -> glibc 2.31" | ||
| 199 | say " # debian:10 buster -> glibc 2.28" | ||
| 200 | say "" | ||
| 201 | say " docker run --rm -it -v \"\$PWD:/src\" -w /src rust:1-bullseye bash -lc '" | ||
| 202 | say " apt-get update &&" | ||
| 203 | say " apt-get install -y --no-install-recommends build-essential pkg-config &&" | ||
| 204 | say " RUSTFLAGS=\"$baseline_flag\" cargo build --release &&" | ||
| 205 | say " ./scripts/check-glibc.sh target/release/agent686'" | ||
| 206 | say "" | ||
| 207 | say " # Alternative without Docker, pinning glibc with zig:" | ||
| 208 | say " # cargo install cargo-zigbuild" | ||
| 209 | say " # cargo zigbuild --release --target $triple_gnu.2.28" | ||
| 210 | ;; | ||
| 211 | esac | ||
| 212 | |||
| 213 | section "cfg the code will see" | ||
| 214 | if command -v rustc >/dev/null 2>&1; then | ||
| 215 | triple_for_cfg=$native_triple | ||
| 216 | [ "$plan" = musl ] && triple_for_cfg=$triple_musl | ||
| 217 | [ "$plan" = old-glibc ] && triple_for_cfg=$triple_gnu | ||
| 218 | rustc --print cfg --target "$triple_for_cfg" 2>/dev/null \ | ||
| 219 | | grep -E '^(target_(arch|env|family|os|pointer_width|vendor)|target_feature=)' \ | ||
| 220 | | sed 's/^/ /' | sort | ||
| 221 | else | ||
| 222 | note "rustc not found; no cfg to show" | ||
| 223 | fi | ||
| 224 | |||
| 225 | if [ "$run" -eq 1 ]; then | ||
| 226 | if [ "$plan" = old-glibc ]; then | ||
| 227 | echo "comp.sh: --old-glibc is a recipe; run the docker command above" >&2 | ||
| 228 | exit 2 | ||
| 229 | fi | ||
| 230 | triple=$native_triple | ||
| 231 | [ "$plan" = musl ] && triple=$triple_musl | ||
| 232 | extra_flags=$baseline_flag | ||
| 233 | [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" | ||
| 234 | |||
| 235 | section "building" | ||
| 236 | rustup target add "$triple" >/dev/null 2>&1 || true | ||
| 237 | ( | ||
| 238 | cd "$here" | ||
| 239 | RUSTFLAGS="$extra_flags" cargo build --release --target "$triple" | ||
| 240 | ) | ||
| 241 | binary="$here/target/$triple/release/agent686" | ||
| 242 | section "result" | ||
| 243 | note "$(file -b "$binary")" | ||
| 244 | case "$triple" in | ||
| 245 | *-gnu) "$here/scripts/check-glibc.sh" "$binary" ;; | ||
| 246 | *) note "static: no glibc floor to check" ;; | ||
| 247 | esac | ||
| 248 | fi | ||
