| 1 | #!/bin/bash |
| 2 | # Build a static rsync binary from a historical git tag, for cross-version |
| 3 | # behaviour testing. Produces ./rsync_<version> in this directory. |
| 4 | # |
| 5 | # Usage: ./build_static.sh <version> [git-tag] |
| 6 | # Example: ./build_static.sh 3.1.3 # uses tag v3.1.3 |
| 7 | # ./build_static.sh 3.2.7 v3.2.7 |
| 8 | # |
| 9 | # Old rsync releases don't compile cleanly on a modern toolchain (GCC >= 14 |
| 10 | # defaults to C23, where an empty () prototype means (void); glibc dropped the |
| 11 | # 1-arg gettimeofday; lseek64 K&R redeclarations clash). This script applies |
| 12 | # the minimal, best-effort workarounds and links statically so the result is |
| 13 | # self-contained and reproducible regardless of the host's shared libraries. |
| 14 | # |
| 15 | # Each workaround is guarded so it's a no-op on versions that don't need it. |
| 16 | set -euo pipefail |
| 17 | |
| 18 | VERSION="${1:?usage: build_static.sh <version> [git-tag]}" |
| 19 | TAG="${2:-v$VERSION}" |
| 20 | |
| 21 | ARCHIVE_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 22 | REPO="${RSYNC_REPO:-/home/tridge/project/rsync/rsync.4}" # any rsync worktree |
| 23 | WORKTREE="$(mktemp -d /tmp/rsync-build-XXXXXX)" |
| 24 | OUT="$ARCHIVE_DIR/rsync_$VERSION" |
| 25 | |
| 26 | # C standard restores K&R () semantics; permissive flags downgrade the pile of |
| 27 | # modern -Werror promotions (incompatible pointers, implicit decls) to warnings. |
| 28 | # _FORTIFY_SOURCE is forced OFF: modern Ubuntu defaults it to =3, whose stricter |
| 29 | # object-size checks turn latent (historically benign) over-reads in OLD rsync |
| 30 | # into hard "*** buffer overflow detected ***" aborts when the binary acts as a |
| 31 | # server/daemon. Disabling it makes these archival binaries behave the way the |
| 32 | # released versions did, which is the whole point of the archive. |
| 33 | CFLAGS_OLD="-I. -I./zlib -O2 -g -std=gnu11 -fcommon -DHAVE_CONFIG_H -Wno-error \ |
| 34 | -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 \ |
| 35 | -Wno-incompatible-pointer-types -Wno-implicit-function-declaration -Wno-int-conversion" |
| 36 | |
| 37 | cleanup() { |
| 38 | cd "$REPO" |
| 39 | git worktree remove --force "$WORKTREE" 2>/dev/null || true |
| 40 | git worktree prune 2>/dev/null || true |
| 41 | } |
| 42 | trap cleanup EXIT |
| 43 | |
| 44 | echo ">>> checking out $TAG into $WORKTREE" |
| 45 | # prefer an exact tag to avoid ambiguity with similarly-named branches |
| 46 | REF="$TAG" |
| 47 | if git -C "$REPO" rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then |
| 48 | REF="refs/tags/$TAG" |
| 49 | fi |
| 50 | git -C "$REPO" worktree add --detach "$WORKTREE" "$REF" |
| 51 | cd "$WORKTREE" |
| 52 | |
| 53 | # --- workaround 1: K&R lseek64 redeclaration clashes with glibc's prototype --- |
| 54 | if grep -q 'off64_t lseek64();' syscall.c 2>/dev/null; then |
| 55 | echo ">>> patching syscall.c lseek64 redeclaration" |
| 56 | perl -0pi -e 's/#ifdef HAVE_LSEEK64\n#if !SIZEOF_OFF64_T\n\tOFF_T lseek64\(\);\n#else\n\toff64_t lseek64\(\);\n#endif\n\treturn lseek64/#ifdef HAVE_LSEEK64\n\treturn lseek64/' syscall.c |
| 57 | fi |
| 58 | |
| 59 | # --- workaround 0: pre-3.0 tags ship configure.in, not a generated configure. |
| 60 | # Generate it. Modern autoconf emits broken shell for their |
| 61 | # AC_CHECK_FUNCS(fn,,AC_LIBOBJ(lib/...)) fallbacks -- but those branches are |
| 62 | # dead on a modern host (glibc has inet_ntop/inet_pton/getaddrinfo/getnameinfo), |
| 63 | # so neutralize the AC_LIBOBJ replacements before regenerating. |
| 64 | OLD_TREE=0 |
| 65 | if [ ! -f ./configure ] && { [ -f configure.in ] || [ -f configure.ac ]; }; then |
| 66 | OLD_TREE=1 |
| 67 | acsrc=configure.ac; [ -f configure.in ] && acsrc=configure.in |
| 68 | echo ">>> generating configure for an old tag (autoheader/autoconf)" |
| 69 | sed -i 's#AC_LIBOBJ(lib/[a-zA-Z_]*)#:#g' "$acsrc" |
| 70 | autoheader 2>/dev/null || true |
| 71 | autoconf 2>/dev/null || { echo "autoconf failed"; exit 1; } |
| 72 | fi |
| 73 | |
| 74 | CONF_ARGS=(--disable-md2man --with-included-zlib=yes --with-included-popt=yes) |
| 75 | # OpenSSL (3.2+) only adds optional MD4/MD5 that rsync already implements, but |
| 76 | # linking libcrypto.a statically drags in jitterentropy + zlib's uncompress, |
| 77 | # which aren't resolvable here. Drop it when the flag exists. |
| 78 | if ./configure --help 2>/dev/null | grep -q -- '--disable-openssl'; then |
| 79 | echo ">>> disabling openssl for self-contained static link" |
| 80 | CONF_ARGS+=(--disable-openssl) |
| 81 | fi |
| 82 | |
| 83 | echo ">>> configure (bundled zlib + popt, static-friendly)" |
| 84 | ./configure "${CONF_ARGS[@]}" \ |
| 85 | >"$WORKTREE/conf.log" 2>&1 || { tail -20 "$WORKTREE/conf.log"; exit 1; } |
| 86 | |
| 87 | # --- workaround 2: modern glibc only has the 2-arg gettimeofday --------------- |
| 88 | if grep -q '/\* #undef HAVE_GETTIMEOFDAY_TZ \*/' config.h; then |
| 89 | echo ">>> forcing HAVE_GETTIMEOFDAY_TZ (configure misdetects it)" |
| 90 | sed -i 's|/\* #undef HAVE_GETTIMEOFDAY_TZ \*/|#define HAVE_GETTIMEOFDAY_TZ 1|' config.h |
| 91 | fi |
| 92 | |
| 93 | # --- workaround 4 (old trees only): generate proto.h if the tree has no make |
| 94 | # rule for it, and stub a vendored lib/addrinfo.h that the git tag dropped |
| 95 | # (modern glibc supplies struct addrinfo / sockaddr_storage, so empty is right). |
| 96 | if [ "$OLD_TREE" = 1 ]; then |
| 97 | if [ ! -f proto.h ] && [ -f mkproto.awk ]; then |
| 98 | echo ">>> generating proto.h" |
| 99 | cat ./*.c ./lib/compat.c 2>/dev/null | awk -f ./mkproto.awk > proto.h |
| 100 | fi |
| 101 | if grep -q 'include "lib/addrinfo.h"' rsync.h 2>/dev/null && [ ! -f lib/addrinfo.h ]; then |
| 102 | echo ">>> stubbing lib/addrinfo.h" |
| 103 | echo '/* emptied: modern glibc provides struct addrinfo */' > lib/addrinfo.h |
| 104 | fi |
| 105 | fi |
| 106 | |
| 107 | echo ">>> building (static)" |
| 108 | make -j"$(nproc)" CFLAGS="$CFLAGS_OLD" LDFLAGS="-static" \ |
| 109 | >"$WORKTREE/make.log" 2>&1 || { grep -E 'error:|\*\*\*' "$WORKTREE/make.log" | head; exit 1; } |
| 110 | |
| 111 | # verify it's actually static before we keep it |
| 112 | if ldd ./rsync 2>&1 | grep -qv 'not a dynamic executable'; then |
| 113 | echo "ERROR: binary is not statically linked:" >&2 |
| 114 | ldd ./rsync >&2 |
| 115 | exit 1 |
| 116 | fi |
| 117 | |
| 118 | GOT="$(./rsync --version | head -1 | awk '{print $3}')" |
| 119 | if [ "$GOT" != "$VERSION" ]; then |
| 120 | echo "ERROR: built version '$GOT' != requested '$VERSION'" >&2 |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | cp ./rsync "$OUT" |
| 125 | strip "$OUT" |
| 126 | echo ">>> installed $OUT" |
| 127 | "$OUT" --version | head -1 |
| 128 | file "$OUT" |