Files
tdc-rs/scripts/fetch-platform.sh
T
admin 400d883e1a feat(p6): 平台改为预编译 rlib 接入,依赖全部来自包内闭包
- 新增 scripts/fetch-platform.sh:读 .sspf-version 解包 vendor/ + 生成 .cargo/config.toml(rustflags + rustdocflags),只校工具链 channel
- Cargo.toml 的 [dependencies]/[dev-dependencies] 清空:17 个 crate 全部 --extern 自平台闭包
- release profile lto 由 thin 改 false(平台 rlib bitcode-free,开 LTO 找不到 __bitcode)
- tower-http 不在闭包 ⇒ 自研 platform::static_files(ServeDir 等价,含 Range 206)与 platform::cors(permissive 等价)
- reqwest 与 extern tokio 双 runtime 必 panic(no reactor running)⇒ 自研 platform::http(HTTP/1.1 明文)
- tests/auth_flow 的 reqwest 调用改用 lib 客户端;tds_biz 的头白名单补 connection
- 新增尺子 check-platform-package.sh:依赖单一来源 + 真编一次证明平台闭包零重编
- 验收:cargo test 306 passed / 七把尺子 + 新尺子全绿;dev 构建 2.5s 只编本仓
2026-11-05 13:01:27 +08:00

188 lines
7.0 KiB
Bash
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.
#!/usr/bin/env bash
# 取 sspf 标准平台包(预编译 rlib 闭包),并生成本仓的 `.cargo/config.toml`。
#
# 用法:
# bash scripts/fetch-platform.sh # 用 .sspf-version 里的版本,从 ../ssw-platform/dist 取
# bash scripts/fetch-platform.sh --pkg <tar.gz> # 显式指定包文件
# bash scripts/fetch-platform.sh --url <url> # 从 http(s) 取
#
# 产出(**都不进版本库**,见 .gitignore):
# vendor/sspf-vX.Y.Z/ 解包后的平台包(lib/ 是预编译闭包,422 个文件)
# .cargo/config.toml rustflags:--extern sspf/axum/tokio/... + -L dependency=vendor/.../lib
#
# 🔴 为什么要有这个脚本:方案 C 的 rustflags 里写的是**本机绝对路径 + 每版本会变的
# rlib hash**,手抄必错、且换机就废。脚本每次重新解析 `.d` 文件里的源码路径来确定
# 「同名 crate 的哪一份才是对的」(例如 rand 有 0.8 与 0.10 两份、md5 有 0.10 与 0.11 两份)。
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VERSION_FILE="$ROOT/.sspf-version"
VERSION="$(cat "$VERSION_FILE" 2>/dev/null | tr -d '[:space:]')"
PKG=""
URL=""
while [ $# -gt 0 ]; do
case "$1" in
--pkg) PKG="${2:?--pkg 需要一个路径}"; shift 2 ;;
--url) URL="${2:?--url 需要一个 URL}"; shift 2 ;;
--version) VERSION="${2:?--version 需要一个版本号}"; shift 2 ;;
*) echo "未知参数: $1" >&2; exit 2 ;;
esac
done
[ -n "$VERSION" ] || { echo "缺少版本:写 .sspf-version 或用 --version" >&2; exit 2; }
VENDOR="$ROOT/vendor/sspf-v$VERSION"
# ---------------- 1. 解包 ----------------
if [ ! -d "$VENDOR/lib" ]; then
mkdir -p "$ROOT/vendor"
if [ -z "$PKG" ] && [ -z "$URL" ]; then
PKG="$ROOT/../ssw-platform/dist/sspf-v$VERSION.tar.gz"
fi
if [ -n "$URL" ]; then
TMP="$(mktemp -t sspf-pkg).tar.gz"
echo "→ 下载 $URL"
curl -fsSL "$URL" -o "$TMP"
PKG="$TMP"
fi
[ -f "$PKG" ] || { echo "找不到发布包: ${PKG}(用 --pkg 或 --url 指定)" >&2; exit 1; }
echo "→ 解包 $PKG"
tar -xzf "$PKG" -C "$ROOT/vendor"
fi
[ -d "$VENDOR/lib" ] || { echo "包结构不对,缺 $VENDOR/lib" >&2; exit 1; }
LIB="$VENDOR/lib"
# ---------------- 2. 挑 rlib:同名多版本时按 .d 里的源码版本定 ----------------
# $1 = rustc crate 名(= 文件名里的名字),$2 = 期望的**包名+版本**(可空)
#
# `.d` 文件里记着源码绝对路径(`.../md-5-0.10.6/src/lib.rs`),是唯一能
# 把 rlib hash 与「哪个版本」对上号的线索。
pick_rlib() {
local crate="$1" want="${2:-}"
local -a cands=()
local f
for f in "$LIB/lib${crate}-"*.rlib; do
[ -e "$f" ] || continue
cands+=("$f")
done
if [ "${#cands[@]}" -eq 0 ]; then
echo " ✗ $crate 不在闭包里" >&2
return 1
fi
if [ "${#cands[@]}" -eq 1 ]; then
printf '%s\n' "${cands[0]}"
return 0
fi
# 多份 ⇒ 按 .d 里的源码目录名过滤(`want` 形如 `md-5-0.10`,前缀匹配)
[ -n "$want" ] || {
echo " ✗ $crate 有 ${#cands[@]} 份却没说版本(want=)" >&2
return 1
}
for f in "${cands[@]}"; do
# 🔴 rlib 叫 `libfoo-<hash>.rlib`,对应的 `.d` 却叫 `foo-<hash>.d`(没 `lib` 前缀)
local base d
base="$(basename "$f" .rlib)"
d="$LIB/${base#lib}.d"
[ -f "$d" ] || continue
if grep -qE "/${want//./\\.}\.[0-9]+/" "$d"; then
printf '%s\n' "$f"
return 0
fi
done
echo " ✗ $crate 找不到版本 $want 对应的 rlib" >&2
return 1
}
# 业务仓代码**直接具名**的 crate(跨 sspf 边界的类型都在这张表里)。
# - sspf/axum/tokio/serde/serde_json/http/bytes:跨边界 ⇒ **必须**是闭包里那一份
# - rand/md5/aes/ecb/...:只在业务仓内部用,但仍取闭包版本 ⇒ 全进程只有一份
EXTERNS=(
"sspf:"
"axum:"
"tokio:"
"serde:"
"serde_json:"
"http:"
"bytes:"
"tower:"
"http_body_util:"
"serde_urlencoded:"
"chrono:"
"hex:"
"rand:rand-0.8"
"md5:md-5-0.10"
"aes:"
"ecb:"
"pglite:pglite-0.2"
)
echo "→ 解析 rlib(${LIB})"
declare -a FLAGS=()
for spec in "${EXTERNS[@]}"; do
name="${spec%%:*}"
want="${spec#*:}"
path="$(pick_rlib "$name" "$want")"
FLAGS+=("--extern" "$name=$path")
printf ' %-18s %s\n' "$name" "$(basename "$path")"
done
FLAGS+=("-L" "dependency=$LIB")
# ---------------- 3. 写 .cargo/config.toml ----------------
mkdir -p "$ROOT/.cargo"
{
echo "# 本文件**由 scripts/fetch-platform.sh 生成**,含本机绝对路径 ⇒ 不进版本库。"
echo "# 换机 / 升级平台包版本:重跑一次脚本即可。"
echo "[build]"
# 🔴 rustflags 只管 rustc,**doc-test / rustdoc 走另一套**(`rustdocflags`)
# ⇒ 两边都要给,否则 `cargo test` 到 Doc-tests 阶段会因为找不到 axum 而红。
echo "rustflags = ["
i=0
while [ "$i" -lt "${#FLAGS[@]}" ]; do
echo " \"${FLAGS[$i]}\", \"${FLAGS[$((i + 1))]}\","
i=$((i + 2))
done
echo "]"
echo "rustdocflags = ["
i=0
while [ "$i" -lt "${#FLAGS[@]}" ]; do
echo " \"${FLAGS[$i]}\", \"${FLAGS[$((i + 1))]}\","
i=$((i + 2))
done
echo "]"
} >"$ROOT/.cargo/config.toml"
echo "→ 已写 .cargo/config.toml(rustflags + rustdocflags)"
# ---------------- 4. 工具链锁 ----------------
# rlib 的 ABI 取决于产出它的 rustc ⇒ 版本必须一字不差。
# ⚠️ **只校 channel,不整份覆盖**:本仓的 `rust-toolchain.toml` 有自己的说明注释,
# 整份拷平台包那份会把注释换成讲 cdylib 插件的话(与本仓无关)。
PKG_CHANNEL="$(grep -oE 'channel = "[^"]+"' "$VENDOR/rust-toolchain.toml" | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')"
TOOL="$ROOT/rust-toolchain.toml"
if [ ! -f "$TOOL" ]; then
cat >"$TOOL" <<EOF
# 🔴 必须与平台包 sspf(ssw-platform)锁同一个 rustc。
# 预编译 rlib 的 ABI 取决于产出它的 rustc:版本不一致 ⇒ 链接失败 / 类型解析异常,
# 报错点常在无关代码里,极难定位。
# ⚠️ 本文件由 scripts/fetch-platform.sh 按包内 rust-toolchain.toml 的 channel 校验。
[toolchain]
channel = "${PKG_CHANNEL}"
components = ["rustfmt", "clippy"]
profile = "minimal"
EOF
echo "→ 已创建 rust-toolchain.toml(channel = ${PKG_CHANNEL})"
else
LOCAL_CHANNEL="$(grep -oE 'channel = "[^"]+"' "$TOOL" | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')"
if [ "$LOCAL_CHANNEL" = "$PKG_CHANNEL" ]; then
echo "→ rust-toolchain.toml 已是 ${PKG_CHANNEL}(与包内一致)"
else
# 只改 channel 那一行,保留本仓注释
sed -i '' -E "s/^channel = \"[^\"]+\"/channel = \"${PKG_CHANNEL}\"/" "$TOOL"
echo "→ rust-toolchain.toml:${LOCAL_CHANNEL} → ${PKG_CHANNEL}(已对齐包内)"
fi
fi
echo "✓ 平台包就绪:v$VERSION"