From 836cf9a5a7fbbd81638e51c3ea4eb976f55506d7 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 20:49:36 +0100 Subject: 0.8.2: comp.sh defaults to this CPU, --portable for travel - default build uses -C target-cpu=native, with the portable command printed alongside and the SIGILL consequence stated - fix x86-64 level detection: /proc/cpuinfo has 'abm' not 'lzcnt', and never 'osxsave', so a v3 CPU was reported as v2 - --old-glibc recipe carries the same CPU choice --- comp.sh | 205 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 132 insertions(+), 73 deletions(-) (limited to 'comp.sh') diff --git a/comp.sh b/comp.sh index 6b798c6..8539c2d 100755 --- a/comp.sh +++ b/comp.sh @@ -1,26 +1,30 @@ #!/usr/bin/env bash # -# comp.sh — work out how to build agent686 for *this* machine. +# comp.sh — build agent686 for the machine you are on. # -# Default is dry: it prints the plan. `--run` executes the native build and its -# checks. `--musl` selects the static plan; `--old-glibc` prints the container -# recipe for the oldest glibc we support. -# -# The point is to make the target explicit, because "it built" on a modern host -# says nothing about whether it runs on baseline Debian. +# Default: tune for this CPU (-C target-cpu=native). Fine when the binary runs +# here; it will not start on an older CPU. +# --portable build the oldest baseline instead (x86-64 / SSE2), for copying +# to another machine +# --musl static build (no libc dependency), same CPU choice +# --old-glibc print the container recipe for an older glibc +# --run execute the selected build, then check it set -euo pipefail here=$(cd "$(dirname "$0")" && pwd) run=0 plan=native +cpu=native for arg in "$@"; do case "$arg" in --run) run=1 ;; + --portable | --baseline) cpu=portable ;; + --native) cpu=native ;; --musl) plan=musl ;; --old-glibc) plan=old-glibc ;; -h | --help) - sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' + sed -n '3,12p' "$0" | sed 's/^# \{0,1\}//' exit 0 ;; *) @@ -61,6 +65,31 @@ fi cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true) cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true) +has_flag() { + case " $cpu_flags " in + *" $1 "*) return 0 ;; + *) return 1 ;; + esac +} + +# x86-64 microarchitecture level, per the psABI. /proc/cpuinfo spells LZCNT as +# "abm" and never lists "osxsave", so neither is required here. +cpu_level= +if [ "$arch" = x86_64 ] || [ "$arch" = amd64 ]; then + if has_flag avx512f && has_flag avx512bw && has_flag avx512cd && + has_flag avx512dq && has_flag avx512vl; then + cpu_level=v4 + elif has_flag avx2 && has_flag bmi1 && has_flag bmi2 && has_flag f16c && + has_flag fma && has_flag movbe && has_flag xsave && + { has_flag lzcnt || has_flag abm; }; then + cpu_level=v3 + elif has_flag sse4_2 && has_flag popcnt && has_flag cx16 && has_flag lahf_lm; then + cpu_level=v2 + else + cpu_level=v1 + fi +fi + rustc_version=$(rustc -V 2>/dev/null || echo "not installed") cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") @@ -68,14 +97,11 @@ cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") triple_gnu=unknown-unknown-linux-gnu triple_musl=unknown-unknown-linux-musl -baseline_flag= case "$arch" in x86_64 | amd64) triple_gnu=x86_64-unknown-linux-gnu triple_musl=x86_64-unknown-linux-musl - # x86-64 v1: SSE2 only. Anything higher excludes pre-Nehalem CPUs. - baseline_flag="-C target-cpu=x86-64" ;; aarch64 | arm64) triple_gnu=aarch64-unknown-linux-gnu @@ -113,8 +139,26 @@ esac native_triple=$triple_gnu [ "$libc" = musl ] && native_triple=$triple_musl -# The i686 gnu target uses linker flags from .cargo/config.toml; setting -# RUSTFLAGS at all would shadow them, so repeat them when needed. +# Flags for the selected CPU mode. `native` is the default: build what this +# chip can do. `portable` targets the architecture floor. +build_flags() { + if [ "$cpu" = native ]; then + printf '%s' "-C target-cpu=native" + return + fi + case "$arch" in + x86_64 | amd64) + # x86-64 v1: SSE2 only, runs on any 64-bit x86 CPU. + printf '%s' "-C target-cpu=x86-64" + ;; + *) + printf '%s' "" + ;; + esac +} + +# i686-gnu needs linker flags from .cargo/config.toml. RUSTFLAGS on the command +# line would shadow them, so repeat them. defsym_flag= if [ "$triple_gnu" = i686-unknown-linux-gnu ]; then defsym_flag="-C link-arg=-Wl,--defsym,pidfd_spawnp=0 -C link-arg=-Wl,--defsym,pidfd_getpid=0" @@ -128,20 +172,24 @@ note "arch $arch ($(getconf LONG_BIT 2>/dev/null || echo '?')-bit)" note "kernel $kernel" note "libc ${libc}${libc_version:+ $libc_version}" note "cpu ${cpu_model:-unknown}" +[ -n "$cpu_level" ] && note "cpu level x86-64-$cpu_level" note "rust $rustc_version" note "cargo $cargo_version" -if [ -n "$cpu_flags" ]; then - for feature in sse2 sse4_2 avx avx2; do - case " $cpu_flags " in - *" $feature "*) note "has $feature" ;; - esac - done -fi section "target" case "$plan" in native) - say " target $native_triple" + if [ "$libc" = musl ]; then + say " target $native_triple" + else + say " target $native_triple" + fi + if [ "$cpu" = native ]; then + note "optimised for this CPU${cpu_level:+ (x86-64-$cpu_level)}" + note "will not start on an older CPU; --portable for a build that travels" + else + note "portable: runs on any $arch CPU" + fi ;; musl) say " target $triple_musl (static)" @@ -151,64 +199,77 @@ case "$plan" in ;; esac -if [ "$baseline_flag" = "-C target-cpu=x86-64" ]; then - note "baseline x86-64 (SSE2): runs on any 64-bit x86 CPU" -fi - case "$plan" in native) if [ "$libc" = glibc ]; then say "" - say " Building here links against this machine's glibc ($libc_version)." - say " The binary will refuse to start on any machine with an older one." - say " For a portable build, use --musl or --old-glibc." + say " This links against this machine's glibc ($libc_version), so the" + say " binary needs that version or newer. --musl removes the dependency;" + say " --old-glibc builds against an older glibc." fi ;; esac section "commands" -case "$plan" in - native | musl) - triple=$native_triple - [ "$plan" = musl ] && triple=$triple_musl - extra_flags=$baseline_flag - [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" - - say " rustup target add $triple # if not already installed" - if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then - say " # needs a C toolchain for ring: apt install musl-tools" - fi - if [ -n "$extra_flags" ]; then - say " RUSTFLAGS=\"$extra_flags\" \\" - fi - say " cargo build --release --target $triple" - case "$triple" in - *-gnu) - say " ./scripts/check-glibc.sh target/$triple/release/agent686" - ;; - *) - say " file target/$triple/release/agent686 # expect: statically linked" - ;; - esac - ;; - old-glibc) - say " # The floor is the glibc in the image you build in:" - say " # debian:12 bookworm -> glibc 2.36" - say " # debian:11 bullseye -> glibc 2.31" - say " # debian:10 buster -> glibc 2.28" - say "" - say " docker run --rm -it -v \"\$PWD:/src\" -w /src rust:1-bullseye bash -lc '" - say " apt-get update &&" - say " apt-get install -y --no-install-recommends build-essential pkg-config &&" - say " RUSTFLAGS=\"$baseline_flag\" cargo build --release &&" - say " ./scripts/check-glibc.sh target/release/agent686'" +if [ "$plan" = native ] || [ "$plan" = musl ]; then + triple=$native_triple + [ "$plan" = musl ] && triple=$triple_musl + + flags=$(build_flags) + [ "$triple" = i686-unknown-linux-gnu ] && flags="$flags $defsym_flag" + + if [ "$cpu" = native ]; then + say " # for this CPU${cpu_level:+ (x86-64-$cpu_level)}" + else + say " # portable: runs on any $arch CPU" + fi + say " rustup target add $triple # if not already installed" + if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then + say " # static linking needs a C toolchain for ring: apt install musl-tools" + fi + if [ -n "$flags" ]; then + say " RUSTFLAGS=\"$flags\" \\" + fi + say " cargo build --release --target $triple" + case "$triple" in + *-gnu) say " ./scripts/check-glibc.sh target/$triple/release/agent686" ;; + *) say " file target/$triple/release/agent686 # expect: statically linked" ;; + esac + + if [ "$cpu" = native ]; then say "" - say " # Alternative without Docker, pinning glibc with zig:" - say " # cargo install cargo-zigbuild" - say " # cargo zigbuild --release --target $triple_gnu.2.28" - ;; -esac + say " # if this binary will run on a different machine, build for its floor" + say " # (or copy the repo there and run comp.sh on it):" + if [ "$arch" = x86_64 ] || [ "$arch" = amd64 ]; then + say " RUSTFLAGS=\"-C target-cpu=x86-64\" cargo build --release --target $triple" + else + say " ./comp.sh --portable" + fi + fi +fi + +if [ "$plan" = old-glibc ]; then + flags=$(build_flags) + say " # The floor is the glibc in the image you build in:" + say " # debian:12 bookworm -> glibc 2.36" + say " # debian:11 bullseye -> glibc 2.31" + say " # debian:10 buster -> glibc 2.28" + say "" + say " docker run --rm -it -v \"\$PWD:/src\" -w /src rust:1-bullseye bash -lc '" + say " apt-get update &&" + say " apt-get install -y --no-install-recommends build-essential pkg-config &&" + if [ -n "$flags" ]; then + say " RUSTFLAGS=\"$flags\" cargo build --release &&" + else + say " cargo build --release &&" + fi + say " ./scripts/check-glibc.sh target/release/agent686'" + say "" + say " # Without Docker, pinning glibc with zig:" + say " # cargo install cargo-zigbuild" + say " # cargo zigbuild --release --target $triple_gnu.2.28" +fi section "cfg the code will see" if command -v rustc >/dev/null 2>&1; then @@ -218,8 +279,6 @@ if command -v rustc >/dev/null 2>&1; then rustc --print cfg --target "$triple_for_cfg" 2>/dev/null \ | grep -E '^(target_(arch|env|family|os|pointer_width|vendor)|target_feature=)' \ | sed 's/^/ /' | sort -else - note "rustc not found; no cfg to show" fi if [ "$run" -eq 1 ]; then @@ -229,14 +288,14 @@ if [ "$run" -eq 1 ]; then fi triple=$native_triple [ "$plan" = musl ] && triple=$triple_musl - extra_flags=$baseline_flag - [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" + flags=$(build_flags) + [ "$triple" = i686-unknown-linux-gnu ] && flags="$flags $defsym_flag" section "building" rustup target add "$triple" >/dev/null 2>&1 || true ( cd "$here" - RUSTFLAGS="$extra_flags" cargo build --release --target "$triple" + RUSTFLAGS="$flags" cargo build --release --target "$triple" ) binary="$here/target/$triple/release/agent686" section "result" -- cgit v1.2.3