Files
ssw-platform/scripts/relocate-runtime-cross.sh
admin 39201143c5 feat(armv7): 交叉编译 32 位 ARM 平台包,自建 pglite 引擎
上游 pglite-rs 只有 aarch64/x86_64 四架构引擎,没有 armv7 ⇒ 自建。
上游认 PGLITE_LIB_DIR,用它喂自建的 libpglite.a + pglite-runtime.tar。

构建链(amd64 容器 + arm-linux-gnueabihf 交叉工具链 + zig cc 链接):
- build-pglite-engine.sh / build-libpglite-cross.sh / relocate-runtime-cross.sh
- zig-cc-armv7.sh(改写 -Wl,/abs/path + 注入 armhf sysroot)
- build-armv7.sh(一键:引擎 → Rust 交叉编译 → arm/v7 容器冒烟)

三个坑(详见 docs/armv7-build.md):
- strip="symbols" 让 rustc 重建 native 静态库时破坏全局符号 ⇒ 交叉构建强制 strip=none
- PG configure 在 cross_compiling 时假定不支持 --export-dynamic ⇒ 强制 cache 变量
- 上游把 runtime 可移植化拆成独立脚本(RPATH + libpq + libz)⇒ 新增交叉版

release.sh 支持 --target <triple>:跳过宿主消费方编译自检改验架构,
rlib 走隔离目录,解包目录名带 target 后缀(多平台互不覆盖)。

实测(linux/arm/v7 容器):initdb、单用户模式、pglite 读写探针、
sspf-admin 启动 + 14255 条示例数据、登录接口、CurrentUser 菜单树全部通过;
从发布包 tar 解出的二进制端到端复测同样通过。
2026-11-12 20:17:26 +08:00

86 lines
4.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# relocate-runtime-cross.sh —— 上游 native/relocate-runtime.sh 的**交叉编译**变体
#
# 🔴 为什么必须有这个脚本(2026-11-12):
# 上游把「引擎构建」和「runtime 可移植化」拆成两个脚本:
# native/build-libpglite.sh → 产出 libpglite.a + pglite-runtime.tar
# native/relocate-runtime.sh → **给 runtime 打上 RPATH + 打包 libpq**
# 我们的交叉脚本只做了前者,漏了后者 ⇒ 运行时炸:
# [启动失败] 打开数据库失败: boot failed: initdb failed:
# /tmp/pglite-rs-runtime-*/bin/initdb: error while loading shared libraries:
# libpq.so.5: cannot open shared object file: No such file or directory
#
# 上游 relocate-runtime.sh 在 Linux 下做三件事,这里逐条复刻:
# 1. patchelf --set-rpath '$ORIGIN/../lib:$ORIGIN/..' → initdb/postgres/libpqwalreceiver
# (没有 RPATH,initdb 就找不到同目录 lib/ 下的 libpq.so.5)
# 2. 把 lib/libpq.so* 打进 pglite-runtime.tar
# 3. 把 libpq 显式列进 tar 的文件参数($LIBS)
#
# 与上游的差异:
# - 不在交叉编译机上执行(patchelf 改的是 ARM ELF,宿主 macOS 的 patchelf 能改,
# 但更稳的做法是**在交叉容器内改**,因为容器里装的 patchelf 是 linux 版)
# - 额外检查 libz.so.1:armv7 目标机不一定有 libz(官方 x86_64 包靠宿主系统提供,
# 我们把 zlib 静态链进 libpglite.a 但 postgres 仍动态依赖 libz.so.1)
#
# 用法(在交叉容器内执行):
# PGLITE_OUT=/work/engine-out bash relocate-runtime-cross.sh
set -euo pipefail
OUT="${PGLITE_OUT:-${PWD}/native/out}"
PREFIX="$OUT/install"
[[ -d "$PREFIX" ]] || { echo "FATAL: 找不到 $PREFIX" >&2; exit 1; }
command -v patchelf >/dev/null 2>&1 || { echo "FATAL: 容器内需要 patchelf" >&2; exit 1; }
RPATH='$ORIGIN/../lib:$ORIGIN/..'
# ── 1. 给引擎二进制打 RPATH ─────────────────────────────────
echo "==> 设置 RPATH: ${RPATH}"
for f in "$PREFIX/bin/initdb" "$PREFIX/bin/postgres" "$PREFIX/lib/postgresql/libpqwalreceiver.so"; do
[[ -f "$f" ]] || { echo " 跳过(不存在): $f"; continue; }
patchelf --set-rpath "$RPATH" "$f"
echo " patched: ${f#"$PREFIX"/}"
done
# ── 2. 收集要打包进 runtime 的共享库 ──────────────────────────
# libpq 是 initdb 的硬依赖;libz 是 postgres 的硬依赖。
# ⚠️ libz 在 armv7 目标机上未必存在 ⇒ 优先用系统 cross 的 armhf 版本打进去。
LIBS=""
if compgen -G "$PREFIX/lib/libpq.so*" > /dev/null; then
LIBS="$(cd "$PREFIX" && ls lib/libpq.so* | tr '\n' ' ')"
echo "==> libpq: ${LIBS}"
fi
ZLIBS=""
for cand in \
/usr/arm-linux-gnueabihf/lib/libz.so.1 \
/usr/lib/arm-linux-gnueabihf/libz.so.1 ; do
if [[ -f "$cand" ]]; then
cp -n "$cand" "$PREFIX/lib/" 2>/dev/null || true
ZLIBS="$(cd "$PREFIX" && ls lib/libz.so* 2>/dev/null | tr '\n' ' ')"
echo "==> libz: ${ZLIBS}(来自 ${cand})"
break
fi
done
# ── 3. 重新打 runtime tar ─────────────────────────────────────
echo "==> 重新打包 pglite-runtime.tar"
tar -C "$PREFIX" --exclude lib/postgresql/pgxs -cf "$OUT/pglite-runtime.tar" \
share/postgresql lib/postgresql "bin/initdb" "bin/postgres" ${LIBS} ${ZLIBS}
# ── 4. 校验:解包出来跑 initdb --version ──────────────────────
V="$(mktemp -d)"
tar -C "$V" -xf "$OUT/pglite-runtime.tar"
echo "==> runtime 内容:"
(cd "$V" && find . -maxdepth 2 -name '*.so*' -o -maxdepth 2 -name 'initdb' -o -maxdepth 2 -name 'postgres' | sort)
if command -v qemu-arm-static >/dev/null 2>&1; then
echo "==> 用 qemu 校验 initdb --version"
qemu-arm-static -L /usr/arm-linux-gnueabihf "$V/bin/initdb" --version
else
echo "!! 容器内无 qemu-arm-static,跳过 initdb 自检(宿主侧 arm/v7 容器再验)"
fi
ls -lh "$OUT/pglite-runtime.tar"
echo "relocate-runtime-cross: $OUT/pglite-runtime.tar 已生成"