aboutsummaryrefslogtreecommitdiffstats
path: root/comp.sh
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 20:49:36 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 20:49:36 +0100
commit836cf9a5a7fbbd81638e51c3ea4eb976f55506d7 (patch)
treef47e47c4c2da7efed10e89df2b9145b5fb7285aa /comp.sh
parent0.8.0: comp.sh reports how to build for the machine it runs on (diff)
downloadagent686-836cf9a5a7fbbd81638e51c3ea4eb976f55506d7.tar.gz
agent686-836cf9a5a7fbbd81638e51c3ea4eb976f55506d7.zip
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
Diffstat (limited to 'comp.sh')
-rwxr-xr-xcomp.sh205
1 files changed, 132 insertions, 73 deletions
diff --git a/comp.sh b/comp.sh
index 6b798c6..8539c2d 100755
--- a/comp.sh
+++ b/comp.sh
@@ -1,26 +1,30 @@
1#!/usr/bin/env bash 1#!/usr/bin/env bash
2# 2#
3# comp.sh — work out how to build agent686 for *this* machine. 3# comp.sh — build agent686 for the machine you are on.
4# 4#
5# Default is dry: it prints the plan. `--run` executes the native build and its 5# Default: tune for this CPU (-C target-cpu=native). Fine when the binary runs
6# checks. `--musl` selects the static plan; `--old-glibc` prints the container 6# here; it will not start on an older CPU.
7# recipe for the oldest glibc we support. 7# --portable build the oldest baseline instead (x86-64 / SSE2), for copying
8# 8# to another machine
9# The point is to make the target explicit, because "it built" on a modern host 9# --musl static build (no libc dependency), same CPU choice
10# says nothing about whether it runs on baseline Debian. 10# --old-glibc print the container recipe for an older glibc
11# --run execute the selected build, then check it
11set -euo pipefail 12set -euo pipefail
12 13
13here=$(cd "$(dirname "$0")" && pwd) 14here=$(cd "$(dirname "$0")" && pwd)
14run=0 15run=0
15plan=native 16plan=native
17cpu=native
16 18
17for arg in "$@"; do 19for arg in "$@"; do
18 case "$arg" in 20 case "$arg" in
19 --run) run=1 ;; 21 --run) run=1 ;;
22 --portable | --baseline) cpu=portable ;;
23 --native) cpu=native ;;
20 --musl) plan=musl ;; 24 --musl) plan=musl ;;
21 --old-glibc) plan=old-glibc ;; 25 --old-glibc) plan=old-glibc ;;
22 -h | --help) 26 -h | --help)
23 sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' 27 sed -n '3,12p' "$0" | sed 's/^# \{0,1\}//'
24 exit 0 28 exit 0
25 ;; 29 ;;
26 *) 30 *)
@@ -61,6 +65,31 @@ fi
61cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true) 65cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true)
62cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true) 66cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true)
63 67
68has_flag() {
69 case " $cpu_flags " in
70 *" $1 "*) return 0 ;;
71 *) return 1 ;;
72 esac
73}
74
75# x86-64 microarchitecture level, per the psABI. /proc/cpuinfo spells LZCNT as
76# "abm" and never lists "osxsave", so neither is required here.
77cpu_level=
78if [ "$arch" = x86_64 ] || [ "$arch" = amd64 ]; then
79 if has_flag avx512f && has_flag avx512bw && has_flag avx512cd &&
80 has_flag avx512dq && has_flag avx512vl; then
81 cpu_level=v4
82 elif has_flag avx2 && has_flag bmi1 && has_flag bmi2 && has_flag f16c &&
83 has_flag fma && has_flag movbe && has_flag xsave &&
84 { has_flag lzcnt || has_flag abm; }; then
85 cpu_level=v3
86 elif has_flag sse4_2 && has_flag popcnt && has_flag cx16 && has_flag lahf_lm; then
87 cpu_level=v2
88 else
89 cpu_level=v1
90 fi
91fi
92
64rustc_version=$(rustc -V 2>/dev/null || echo "not installed") 93rustc_version=$(rustc -V 2>/dev/null || echo "not installed")
65cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") 94cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed")
66 95
@@ -68,14 +97,11 @@ cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed")
68 97
69triple_gnu=unknown-unknown-linux-gnu 98triple_gnu=unknown-unknown-linux-gnu
70triple_musl=unknown-unknown-linux-musl 99triple_musl=unknown-unknown-linux-musl
71baseline_flag=
72 100
73case "$arch" in 101case "$arch" in
74 x86_64 | amd64) 102 x86_64 | amd64)
75 triple_gnu=x86_64-unknown-linux-gnu 103 triple_gnu=x86_64-unknown-linux-gnu
76 triple_musl=x86_64-unknown-linux-musl 104 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 ;; 105 ;;
80 aarch64 | arm64) 106 aarch64 | arm64)
81 triple_gnu=aarch64-unknown-linux-gnu 107 triple_gnu=aarch64-unknown-linux-gnu
@@ -113,8 +139,26 @@ esac
113native_triple=$triple_gnu 139native_triple=$triple_gnu
114[ "$libc" = musl ] && native_triple=$triple_musl 140[ "$libc" = musl ] && native_triple=$triple_musl
115 141
116# The i686 gnu target uses linker flags from .cargo/config.toml; setting 142# Flags for the selected CPU mode. `native` is the default: build what this
117# RUSTFLAGS at all would shadow them, so repeat them when needed. 143# chip can do. `portable` targets the architecture floor.
144build_flags() {
145 if [ "$cpu" = native ]; then
146 printf '%s' "-C target-cpu=native"
147 return
148 fi
149 case "$arch" in
150 x86_64 | amd64)
151 # x86-64 v1: SSE2 only, runs on any 64-bit x86 CPU.
152 printf '%s' "-C target-cpu=x86-64"
153 ;;
154 *)
155 printf '%s' ""
156 ;;
157 esac
158}
159
160# i686-gnu needs linker flags from .cargo/config.toml. RUSTFLAGS on the command
161# line would shadow them, so repeat them.
118defsym_flag= 162defsym_flag=
119if [ "$triple_gnu" = i686-unknown-linux-gnu ]; then 163if [ "$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" 164 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)"
128note "kernel $kernel" 172note "kernel $kernel"
129note "libc ${libc}${libc_version:+ $libc_version}" 173note "libc ${libc}${libc_version:+ $libc_version}"
130note "cpu ${cpu_model:-unknown}" 174note "cpu ${cpu_model:-unknown}"
175[ -n "$cpu_level" ] && note "cpu level x86-64-$cpu_level"
131note "rust $rustc_version" 176note "rust $rustc_version"
132note "cargo $cargo_version" 177note "cargo $cargo_version"
133if [ -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
139fi
140 178
141section "target" 179section "target"
142case "$plan" in 180case "$plan" in
143 native) 181 native)
144 say " target $native_triple" 182 if [ "$libc" = musl ]; then
183 say " target $native_triple"
184 else
185 say " target $native_triple"
186 fi
187 if [ "$cpu" = native ]; then
188 note "optimised for this CPU${cpu_level:+ (x86-64-$cpu_level)}"
189 note "will not start on an older CPU; --portable for a build that travels"
190 else
191 note "portable: runs on any $arch CPU"
192 fi
145 ;; 193 ;;
146 musl) 194 musl)
147 say " target $triple_musl (static)" 195 say " target $triple_musl (static)"
@@ -151,64 +199,77 @@ case "$plan" in
151 ;; 199 ;;
152esac 200esac
153 201
154if [ "$baseline_flag" = "-C target-cpu=x86-64" ]; then
155 note "baseline x86-64 (SSE2): runs on any 64-bit x86 CPU"
156fi
157
158case "$plan" in 202case "$plan" in
159 native) 203 native)
160 if [ "$libc" = glibc ]; then 204 if [ "$libc" = glibc ]; then
161 say "" 205 say ""
162 say " Building here links against this machine's glibc ($libc_version)." 206 say " This links against this machine's glibc ($libc_version), so the"
163 say " The binary will refuse to start on any machine with an older one." 207 say " binary needs that version or newer. --musl removes the dependency;"
164 say " For a portable build, use --musl or --old-glibc." 208 say " --old-glibc builds against an older glibc."
165 fi 209 fi
166 ;; 210 ;;
167esac 211esac
168 212
169section "commands" 213section "commands"
170 214
171case "$plan" in 215if [ "$plan" = native ] || [ "$plan" = musl ]; then
172 native | musl) 216 triple=$native_triple
173 triple=$native_triple 217 [ "$plan" = musl ] && triple=$triple_musl
174 [ "$plan" = musl ] && triple=$triple_musl 218
175 extra_flags=$baseline_flag 219 flags=$(build_flags)
176 [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" 220 [ "$triple" = i686-unknown-linux-gnu ] && flags="$flags $defsym_flag"
177 221
178 say " rustup target add $triple # if not already installed" 222 if [ "$cpu" = native ]; then
179 if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then 223 say " # for this CPU${cpu_level:+ (x86-64-$cpu_level)}"
180 say " # needs a C toolchain for ring: apt install musl-tools" 224 else
181 fi 225 say " # portable: runs on any $arch CPU"
182 if [ -n "$extra_flags" ]; then 226 fi
183 say " RUSTFLAGS=\"$extra_flags\" \\" 227 say " rustup target add $triple # if not already installed"
184 fi 228 if [ "$plan" = musl ] && [ "$triple" != i686-unknown-linux-musl ]; then
185 say " cargo build --release --target $triple" 229 say " # static linking needs a C toolchain for ring: apt install musl-tools"
186 case "$triple" in 230 fi
187 *-gnu) 231 if [ -n "$flags" ]; then
188 say " ./scripts/check-glibc.sh target/$triple/release/agent686" 232 say " RUSTFLAGS=\"$flags\" \\"
189 ;; 233 fi
190 *) 234 say " cargo build --release --target $triple"
191 say " file target/$triple/release/agent686 # expect: statically linked" 235 case "$triple" in
192 ;; 236 *-gnu) say " ./scripts/check-glibc.sh target/$triple/release/agent686" ;;
193 esac 237 *) say " file target/$triple/release/agent686 # expect: statically linked" ;;
194 ;; 238 esac
195 old-glibc) 239
196 say " # The floor is the glibc in the image you build in:" 240 if [ "$cpu" = native ]; then
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 "" 241 say ""
207 say " # Alternative without Docker, pinning glibc with zig:" 242 say " # if this binary will run on a different machine, build for its floor"
208 say " # cargo install cargo-zigbuild" 243 say " # (or copy the repo there and run comp.sh on it):"
209 say " # cargo zigbuild --release --target $triple_gnu.2.28" 244 if [ "$arch" = x86_64 ] || [ "$arch" = amd64 ]; then
210 ;; 245 say " RUSTFLAGS=\"-C target-cpu=x86-64\" cargo build --release --target $triple"
211esac 246 else
247 say " ./comp.sh --portable"
248 fi
249 fi
250fi
251
252if [ "$plan" = old-glibc ]; then
253 flags=$(build_flags)
254 say " # The floor is the glibc in the image you build in:"
255 say " # debian:12 bookworm -> glibc 2.36"
256 say " # debian:11 bullseye -> glibc 2.31"
257 say " # debian:10 buster -> glibc 2.28"
258 say ""
259 say " docker run --rm -it -v \"\$PWD:/src\" -w /src rust:1-bullseye bash -lc '"
260 say " apt-get update &&"
261 say " apt-get install -y --no-install-recommends build-essential pkg-config &&"
262 if [ -n "$flags" ]; then
263 say " RUSTFLAGS=\"$flags\" cargo build --release &&"
264 else
265 say " cargo build --release &&"
266 fi
267 say " ./scripts/check-glibc.sh target/release/agent686'"
268 say ""
269 say " # Without Docker, pinning glibc with zig:"
270 say " # cargo install cargo-zigbuild"
271 say " # cargo zigbuild --release --target $triple_gnu.2.28"
272fi
212 273
213section "cfg the code will see" 274section "cfg the code will see"
214if command -v rustc >/dev/null 2>&1; then 275if command -v rustc >/dev/null 2>&1; then
@@ -218,8 +279,6 @@ if command -v rustc >/dev/null 2>&1; then
218 rustc --print cfg --target "$triple_for_cfg" 2>/dev/null \ 279 rustc --print cfg --target "$triple_for_cfg" 2>/dev/null \
219 | grep -E '^(target_(arch|env|family|os|pointer_width|vendor)|target_feature=)' \ 280 | grep -E '^(target_(arch|env|family|os|pointer_width|vendor)|target_feature=)' \
220 | sed 's/^/ /' | sort 281 | sed 's/^/ /' | sort
221else
222 note "rustc not found; no cfg to show"
223fi 282fi
224 283
225if [ "$run" -eq 1 ]; then 284if [ "$run" -eq 1 ]; then
@@ -229,14 +288,14 @@ if [ "$run" -eq 1 ]; then
229 fi 288 fi
230 triple=$native_triple 289 triple=$native_triple
231 [ "$plan" = musl ] && triple=$triple_musl 290 [ "$plan" = musl ] && triple=$triple_musl
232 extra_flags=$baseline_flag 291 flags=$(build_flags)
233 [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag" 292 [ "$triple" = i686-unknown-linux-gnu ] && flags="$flags $defsym_flag"
234 293
235 section "building" 294 section "building"
236 rustup target add "$triple" >/dev/null 2>&1 || true 295 rustup target add "$triple" >/dev/null 2>&1 || true
237 ( 296 (
238 cd "$here" 297 cd "$here"
239 RUSTFLAGS="$extra_flags" cargo build --release --target "$triple" 298 RUSTFLAGS="$flags" cargo build --release --target "$triple"
240 ) 299 )
241 binary="$here/target/$triple/release/agent686" 300 binary="$here/target/$triple/release/agent686"
242 section "result" 301 section "result"