summaryrefslogtreecommitdiffstats
path: root/comp.sh
blob: 6b798c6742f123e54952ac036a4f73bcfe1530af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
#
# comp.sh — work out how to build agent686 for *this* machine.
#
# 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.
set -euo pipefail

here=$(cd "$(dirname "$0")" && pwd)
run=0
plan=native

for arg in "$@"; do
    case "$arg" in
        --run) run=1 ;;
        --musl) plan=musl ;;
        --old-glibc) plan=old-glibc ;;
        -h | --help)
            sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//'
            exit 0
            ;;
        *)
            echo "comp.sh: unknown argument: $arg" >&2
            exit 2
            ;;
    esac
done

say() { printf '%s\n' "$*"; }
section() { printf '\n\033[1m== %s\033[0m\n' "$*"; }
note() { printf '   %s\n' "$*"; }

# ---------------------------------------------------------------- detection

arch=$(uname -m)
kernel=$(uname -r)

distro=unknown
if [ -r /etc/os-release ]; then
    # shellcheck disable=SC1091
    . /etc/os-release
    distro=${PRETTY_NAME:-${ID:-unknown}}
fi

libc=unknown
libc_version=
if getconf GNU_LIBC_VERSION >/dev/null 2>&1; then
    libc=glibc
    libc_version=$(getconf GNU_LIBC_VERSION | awk '{print $2}')
elif command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | head -1 | grep -qi musl; then
    libc=musl
    libc_version=$(ldd --version 2>&1 | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
elif ls /lib/ld-musl-*.so.1 >/dev/null 2>&1; then
    libc=musl
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)

rustc_version=$(rustc -V 2>/dev/null || echo "not installed")
cargo_version=$(cargo -V 2>/dev/null | awk '{print $2}' || echo "not installed")

# ---------------------------------------------------------------- triple map

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
        triple_musl=aarch64-unknown-linux-musl
        ;;
    armv7l | armv7)
        triple_gnu=armv7-unknown-linux-gnueabihf
        triple_musl=armv7-unknown-linux-musleabihf
        ;;
    armv6l)
        triple_gnu=arm-unknown-linux-gnueabihf
        triple_musl=arm-unknown-linux-musleabihf
        ;;
    i686 | i386)
        triple_gnu=i686-unknown-linux-gnu
        triple_musl=i686-unknown-linux-musl
        ;;
    riscv64)
        triple_gnu=riscv64gc-unknown-linux-gnu
        triple_musl=riscv64gc-unknown-linux-musl
        ;;
    ppc64le)
        triple_gnu=powerpc64le-unknown-linux-gnu
        triple_musl=powerpc64le-unknown-linux-musl
        ;;
    s390x)
        triple_gnu=s390x-unknown-linux-gnu
        triple_musl=s390x-unknown-linux-musl
        ;;
    *)
        note "unrecognised architecture '$arch'; edit comp.sh's triple map"
        ;;
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.
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"
fi

# ---------------------------------------------------------------- report

section "machine"
note "distro    $distro"
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}"
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"
        ;;
    musl)
        say "   target    $triple_musl (static)"
        ;;
    old-glibc)
        say "   target    $triple_gnu, built inside an older image"
        ;;
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."
        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'"
        say ""
        say "   # Alternative without Docker, pinning glibc with zig:"
        say "   #   cargo install cargo-zigbuild"
        say "   #   cargo zigbuild --release --target $triple_gnu.2.28"
        ;;
esac

section "cfg the code will see"
if command -v rustc >/dev/null 2>&1; then
    triple_for_cfg=$native_triple
    [ "$plan" = musl ] && triple_for_cfg=$triple_musl
    [ "$plan" = old-glibc ] && triple_for_cfg=$triple_gnu
    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
    if [ "$plan" = old-glibc ]; then
        echo "comp.sh: --old-glibc is a recipe; run the docker command above" >&2
        exit 2
    fi
    triple=$native_triple
    [ "$plan" = musl ] && triple=$triple_musl
    extra_flags=$baseline_flag
    [ "$triple" = i686-unknown-linux-gnu ] && extra_flags="$extra_flags $defsym_flag"

    section "building"
    rustup target add "$triple" >/dev/null 2>&1 || true
    (
        cd "$here"
        RUSTFLAGS="$extra_flags" cargo build --release --target "$triple"
    )
    binary="$here/target/$triple/release/agent686"
    section "result"
    note "$(file -b "$binary")"
    case "$triple" in
        *-gnu) "$here/scripts/check-glibc.sh" "$binary" ;;
        *) note "static: no glibc floor to check" ;;
    esac
fi