summaryrefslogtreecommitdiffstats
path: root/comp.sh
diff options
context:
space:
mode:
Diffstat (limited to 'comp.sh')
-rwxr-xr-xcomp.sh248
1 files changed, 248 insertions, 0 deletions
diff --git a/comp.sh b/comp.sh
new file mode 100755
index 0000000..6b798c6
--- /dev/null
+++ b/comp.sh
@@ -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.
11set -euo pipefail
12
13here=$(cd "$(dirname "$0")" && pwd)
14run=0
15plan=native
16
17for 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
31done
32
33say() { printf '%s\n' "$*"; }
34section() { printf '\n\033[1m== %s\033[0m\n' "$*"; }
35note() { printf ' %s\n' "$*"; }
36
37# ---------------------------------------------------------------- detection
38
39arch=$(uname -m)
40kernel=$(uname -r)
41
42distro=unknown
43if [ -r /etc/os-release ]; then
44 # shellcheck disable=SC1091
45 . /etc/os-release
46 distro=${PRETTY_NAME:-${ID:-unknown}}
47fi
48
49libc=unknown
50libc_version=
51if getconf GNU_LIBC_VERSION >/dev/null 2>&1; then
52 libc=glibc
53 libc_version=$(getconf GNU_LIBC_VERSION | awk '{print $2}')
54elif 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)
57elif ls /lib/ld-musl-*.so.1 >/dev/null 2>&1; then
58 libc=musl
59fi
60
61cpu_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)
63
64rustc_version=$(rustc -V 2>/dev/null || echo "not installed")
65cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed")
66
67# ---------------------------------------------------------------- triple map
68
69triple_gnu=unknown-unknown-linux-gnu
70triple_musl=unknown-unknown-linux-musl
71baseline_flag=
72
73case "$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 ;;
111esac
112
113native_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.
118defsym_flag=
119if [ "$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"
121fi
122
123# ---------------------------------------------------------------- report
124
125section "machine"
126note "distro $distro"
127note "arch $arch ($(getconf LONG_BIT 2>/dev/null || echo '?')-bit)"
128note "kernel $kernel"
129note "libc ${libc}${libc_version:+ $libc_version}"
130note "cpu ${cpu_model:-unknown}"
131note "rust $rustc_version"
132note "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
141section "target"
142case "$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 ;;
152esac
153
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
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 ;;
167esac
168
169section "commands"
170
171case "$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 ;;
211esac
212
213section "cfg the code will see"
214if 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
221else
222 note "rustc not found; no cfg to show"
223fi
224
225if [ "$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
248fi