Files
ssw-platform/scripts/build-pglite-engine.sh
T
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

176 lines
7.9 KiB
Bash
Executable File
Raw 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.
#!/usr/bin/env bash
#
# build-pglite-engine.sh —— 交叉编译 PGlite 原生引擎(libpglite.a)
#
# 🔴 背景(2026-11-12):
# `pglite-rs-sys` 的 build.rs 会去 GitHub 下 `pglite-<target>.tar.gz`。
# 上游只发布 x86_64 / aarch64 的 darwin 与 linux-gnu,**没有 armv7** ⇒ 404。
# 官方给的出路是 `PGLITE_LIB_DIR`:把自建的引擎目录指过去,build.rs 就不再下载。
#
# 引擎 = PostgreSQL(electric-sql/postgres-pglite fork)编译成静态库 + 运行时资产。
# 上游构建脚本 `native/build-libpglite.sh` 假定「本机编译」,这里需要一个**交叉**变体。
#
# 为什么在 amd64 容器里交叉编(而不是 arm/v7 容器里原生编):
# arm/v7 容器 = qemu 模拟 ⇒ 实测 apt 装依赖就跑了 9 分钟,PostgreSQL 全量 make
# 要数小时,不可行。amd64 容器原生速度,交叉 gcc 产出 ARM 静态库。
#
# 用法:
# bash scripts/build-pglite-engine.sh # 默认 armv7
# bash scripts/build-pglite-engine.sh --target aarch64-unknown-linux-gnu
# bash scripts/build-pglite-engine.sh --skip-download # 复用已下源码
#
# 环境变量:
# ENGINE_TAG 上游引擎 tag(默认 engine-06c837c6a303-p3)
# PG_COMMIT postgres-pglite commit(默认该 tag 对应的 06c837c6a303…)
#
set -euo pipefail
ENGINE_TAG="${ENGINE_TAG:-engine-06c837c6a303-p3}"
PG_COMMIT="${PG_COMMIT:-06c837c6a30335169df786c610ed224bc51d81c0}"
TARGET="${TARGET:-armv7-unknown-linux-gnueabihf}"
OUT_DIR=""
WORK_DIR=""
SKIP_DOWNLOAD=0
while [[ $# -gt 0 ]]; do
case "$1" in
--engine-tag) ENGINE_TAG="$2"; shift 2 ;;
--pg-commit) PG_COMMIT="$2"; shift 2 ;;
--target) TARGET="$2"; shift 2 ;;
--out) OUT_DIR="$2"; shift 2 ;;
--work) WORK_DIR="$2"; shift 2 ;;
--skip-download) SKIP_DOWNLOAD=1; shift ;;
-h|--help) sed -n '1,30p' "$0"; exit 0 ;;
*) echo "未知参数: $1" >&2; exit 2 ;;
esac
done
# armv7 → debian 交叉三元组;aarch64 同理
case "${TARGET}" in
armv7-unknown-linux-gnueabihf) TRIPLE=arm-linux-gnueabihf; DOCKER_ARCH=linux/arm/v7 ;;
aarch64-unknown-linux-gnu) TRIPLE=aarch64-linux-gnu; DOCKER_ARCH=linux/arm64 ;;
*) echo "暂不支持的目标: ${TARGET}" >&2; exit 2 ;;
esac
OUT_DIR="${OUT_DIR:-$(cd "$(dirname "$0")/.." && pwd)/dist/pglite-engine-${TARGET%%-*}}"
WORK_DIR="${WORK_DIR:-/tmp/pglite-engine-${TARGET%%-*}}"
mkdir -p "${OUT_DIR}" "${WORK_DIR}"
# 🔴 容器镜像里装了 armhf 交叉工具链 + multiarch 的 armhf libc/zlib;
# 镜像在 scripts/build-armv7.sh 的说明里(首次构建见下方 docker build)。
if ! docker image inspect pglite-armhf-toolchain:latest >/dev/null 2>&1; then
echo "!! 缺少交叉工具链镜像 pglite-armhf-toolchain:latest"
echo " 先构建(一次性,约 1.5 分钟):"
echo " docker build -t pglite-armhf-toolchain:latest - <<'EOF'"
echo " FROM debian:bookworm-slim"
echo " ENV DEBIAN_FRONTEND=noninteractive"
echo " RUN dpkg --add-architecture armhf \\"
echo " && sed -i 's|^deb |deb [arch=amd64,armhf] |' /etc/apt/sources.list.d/debian.sources \\"
echo " && apt-get update -qq \\"
echo " && apt-get install -y -qq --no-install-recommends clang llvm make bison flex \\"
echo " perl pkg-config ca-certificates file patchelf \\"
echo " gcc-arm-linux-gnueabihf libc6-dev-armhf-cross zlib1g-dev:armhf \\"
echo " && rm -rf /var/lib/apt/lists/*"
echo " EOF"
exit 1
fi
echo "==> 目标: ${TARGET}(交叉三元组 ${TRIPLE})"
echo "==> 上游引擎 tag: ${ENGINE_TAG}"
echo "==> PostgreSQL 源码 commit: ${PG_COMMIT}"
echo "==> 产物目录: ${OUT_DIR}"
# ── 1. 拉源码 ────────────────────────────────────────────────
SRC_TAR="${WORK_DIR}/pg-src.tar.gz"
if [[ "${SKIP_DOWNLOAD}" == "0" || ! -f "${SRC_TAR}" ]]; then
echo "==> [1/3] 下载 postgres-pglite 源码"
# 🔴 GitHub 直连在本机很慢(~40KB/s)⇒ 用 API tarball 端点,且带重试。
for i in $(seq 1 40); do
if curl -fsSL --max-time 900 \
"https://api.github.com/repos/electric-sql/postgres-pglite/tarball/${PG_COMMIT}" \
-o "${SRC_TAR}.part"; then
mv "${SRC_TAR}.part" "${SRC_TAR}"
echo " 下载完成: $(du -h "${SRC_TAR}" | cut -f1)"
break
fi
echo " retry ${i}($(stat -f '%z' "${SRC_TAR}.part" 2>/dev/null || echo 0) bytes)"
sleep 3
done
[[ -s "${SRC_TAR}" ]] || { echo "!! 源码下载失败" >&2; exit 1; }
tar -tzf "${SRC_TAR}" >/dev/null 2>&1 || { echo "!! 源码包损坏" >&2; exit 1; }
else
echo "==> [1/3] 复用已有源码: ${SRC_TAR}"
fi
echo "==> 解压源码"
SRC_DIR="${WORK_DIR}/postgres-pglite"
if [[ ! -f "${SRC_DIR}/configure" ]]; then
mkdir -p "${SRC_DIR}"
tar -xzf "${SRC_TAR}" -C "${SRC_DIR}" --strip-components=1
fi
[[ -f "${SRC_DIR}/configure" ]] || { echo "!! 源码不完整(缺 configure)" >&2; exit 1; }
# ── 2. 取上游 native 脚本 + 补丁 ─────────────────────────────
echo "==> [2/3] 拉取上游 native 构建脚本与补丁(${ENGINE_TAG})"
NATIVE_DIR="${WORK_DIR}/native"
mkdir -p "${NATIVE_DIR}/patches"
python3 - "${ENGINE_TAG}" "${NATIVE_DIR}" <<'PY'
import json, urllib.request, base64, os, sys
tag, out = sys.argv[1], sys.argv[2]
def get(path, dest):
url = f"https://api.github.com/repos/Midwess/pglite-rs/contents/{path}?ref={tag}"
req = urllib.request.Request(url, headers={"User-Agent": "curl/8"})
with urllib.request.urlopen(req, timeout=60) as r:
d = json.load(r)
if isinstance(d, list):
for x in d:
get(x["path"], os.path.join(dest, x["name"]))
return
os.makedirs(os.path.dirname(dest) or ".", exist_ok=True)
open(dest, "wb").write(base64.b64decode(d["content"]))
print(" ", path)
files = ["build-libpglite.sh", "pglite_native.c", "pglite_native.h",
"pglite_reset.c", "pglite_static_ext.c"]
for f in files:
get(f"native/{f}", os.path.join(out, f))
get("native/patches", os.path.join(out, "patches"))
PY
# ── 3. 容器内交叉编译 ───────────────────────────────────────
echo "==> [3/3] 容器内交叉编译引擎"
# 🔴 交叉版构建脚本随本仓走(scripts/build-libpglite-cross.sh),挂进容器 /work/native/
# —— 它是上游 build-libpglite.sh 的交叉变体:只改 CC/--host/AR,其余逐字照抄。
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
docker run --rm \
-v "${WORK_DIR}:/work" \
-v "${OUT_DIR}:/out" \
-v "${REPO_ROOT}/scripts/build-libpglite-cross.sh:/work/native/build-libpglite-cross.sh:ro" \
-v "${REPO_ROOT}/scripts/relocate-runtime-cross.sh:/work/native/relocate-runtime-cross.sh:ro" \
-e "TRIPLE=${TRIPLE}" \
-e "TARGET_TRIPLE=${TARGET}" \
-e "NPROC=${NPROC:-4}" \
pglite-armhf-toolchain:latest \
bash -c '
set -euo pipefail
export CC=${TRIPLE}-gcc AR=${TRIPLE}-ar RANLIB=${TRIPLE}-ranlib
export HOST=${TRIPLE} TARGET=${TRIPLE}
# 🔴 OUT/PGLITE_SRC 指向挂载卷(/work),宿主才拿得到产物
export PGLITE_SRC=/work/postgres-pglite PGLITE_OUT=/work/engine-out
cd /work
bash /work/native/build-libpglite-cross.sh
# 🔴 runtime 可移植化必须做(上游拆成了独立的 relocate-runtime.sh):
# 打 RPATH + 打包 libpq/libz,否则 initdb 报
# "error while loading shared libraries: libpq.so.5"
bash /work/native/relocate-runtime-cross.sh
' 2>&1 | tail -40
# 引擎产物从容器工作区搬到最终目录
mkdir -p "${OUT_DIR}"
cp -f "${WORK_DIR}/engine-out/libpglite.a" "${OUT_DIR}/" 2>/dev/null || true
cp -f "${WORK_DIR}/engine-out/pglite-runtime.tar" "${OUT_DIR}/" 2>/dev/null || true
cp -f "${NATIVE_DIR}/pglite_native.h" "${OUT_DIR}/" 2>/dev/null || true
echo ""
echo "==> 产物"
ls -lh "${OUT_DIR}/libpglite.a" "${OUT_DIR}/pglite-runtime.tar"
file "${OUT_DIR}/libpglite.a"