diff options
Diffstat (limited to 'comp.sh')
| -rwxr-xr-x | comp.sh | 205 |
1 files changed, 132 insertions, 73 deletions
| @@ -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 | ||
| 11 | set -euo pipefail | 12 | set -euo pipefail |
| 12 | 13 | ||
| 13 | here=$(cd "$(dirname "$0")" && pwd) | 14 | here=$(cd "$(dirname "$0")" && pwd) |
| 14 | run=0 | 15 | run=0 |
| 15 | plan=native | 16 | plan=native |
| 17 | cpu=native | ||
| 16 | 18 | ||
| 17 | for arg in "$@"; do | 19 | for 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 | |||
| 61 | cpu_model=$(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2- | sed 's/^ *//' || true) | 65 | 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) | 66 | cpu_flags=$(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2- || true) |
| 63 | 67 | ||
| 68 | has_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. | ||
| 77 | cpu_level= | ||
| 78 | if [ "$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 | ||
| 91 | fi | ||
| 92 | |||
| 64 | rustc_version=$(rustc -V 2>/dev/null || echo "not installed") | 93 | rustc_version=$(rustc -V 2>/dev/null || echo "not installed") |
| 65 | cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed") | 94 | cargo_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 | ||
| 69 | triple_gnu=unknown-unknown-linux-gnu | 98 | triple_gnu=unknown-unknown-linux-gnu |
| 70 | triple_musl=unknown-unknown-linux-musl | 99 | triple_musl=unknown-unknown-linux-musl |
| 71 | baseline_flag= | ||
| 72 | 100 | ||
| 73 | case "$arch" in | 101 | case "$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 | |||
| 113 | native_triple=$triple_gnu | 139 | native_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. |
| 144 | build_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. | ||
| 118 | defsym_flag= | 162 | defsym_flag= |
| 119 | if [ "$triple_gnu" = i686-unknown-linux-gnu ]; then | 163 | 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" | 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)" | |||
| 128 | note "kernel $kernel" | 172 | note "kernel $kernel" |
| 129 | note "libc ${libc}${libc_version:+ $libc_version}" | 173 | note "libc ${libc}${libc_version:+ $libc_version}" |
| 130 | note "cpu ${cpu_model:-unknown}" | 174 | note "cpu ${cpu_model:-unknown}" |
| 175 | [ -n "$cpu_level" ] && note "cpu level x86-64-$cpu_level" | ||
| 131 | note "rust $rustc_version" | 176 | note "rust $rustc_version" |
| 132 | note "cargo $cargo_version" | 177 | 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 | 178 | ||
| 141 | section "target" | 179 | section "target" |
| 142 | case "$plan" in | 180 | case "$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 | ;; |
| 152 | esac | 200 | esac |
| 153 | 201 | ||
| 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 | 202 | case "$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 | ;; |
| 167 | esac | 211 | esac |
| 168 | 212 | ||
| 169 | section "commands" | 213 | section "commands" |
| 170 | 214 | ||
| 171 | case "$plan" in | 215 | if [ "$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" |
| 211 | esac | 246 | else |
| 247 | say " ./comp.sh --portable" | ||
| 248 | fi | ||
| 249 | fi | ||
| 250 | fi | ||
| 251 | |||
| 252 | if [ "$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" | ||
| 272 | fi | ||
| 212 | 273 | ||
| 213 | section "cfg the code will see" | 274 | section "cfg the code will see" |
| 214 | if command -v rustc >/dev/null 2>&1; then | 275 | if 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 |
| 221 | else | ||
| 222 | note "rustc not found; no cfg to show" | ||
| 223 | fi | 282 | fi |
| 224 | 283 | ||
| 225 | if [ "$run" -eq 1 ]; then | 284 | if [ "$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" |
