Files
OpenIPC_RTL8812AU_Forword_cpp/packaging/build-release.sh
T
2026-09-13 15:06:07 +08:00

133 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# ============================================================
# 打包为 tar.gz(解压即用:可执行文件 + 关键运行库 + Qt)
# - 打包 Qt6/ICU 与 libpcap/libsodium(其余系统库由发行版提供)
# 用法:
# ./packaging/build-release.sh # 仅命令行版
# ./packaging/build-release.sh --with-gui # 含 GUI(需 QT_PREFIX)
# 产物: dist/fpv-relay-<版本>-linux-x86_64.tar.gz
# ============================================================
set -e
REPO="$(cd "$(dirname "$0")/.." && pwd)"
WITH_GUI=0
[ "$1" = "--with-gui" ] && WITH_GUI=1
VERSION="$(cd "$REPO" && git describe --tags --always 2>/dev/null | tr -d 'v' || true)"
[ -z "$VERSION" ] && VERSION="$(date +%Y%m%d)"
case "$VERSION" in
[0-9]*) ;;
*) VERSION="0.0.0+git$(date +%Y%m%d).${VERSION}" ;;
esac
NAME="fpv-relay-${VERSION}-linux-x86_64"
OUT="${REPO}/dist"
STAGE="${OUT}/${NAME}"
LIBDIR="${STAGE}/lib"
echo "==> 构建(CLI)"
cd "${REPO}"
cmake --preset linux-cli >/dev/null
cmake --build --preset linux-cli -j"$(nproc)" >/dev/null
GUI_DIST=""
if [ "$WITH_GUI" = "1" ]; then
: "${QT_PREFIX:?--with-gui 需要设置 QT_PREFIX 指向 Qt 安装目录}"
echo "==> 构建(GUI)"
cmake --preset linux-gui -DFPC_QT_PREFIX="${QT_PREFIX}" >/dev/null
cmake --build --preset linux-gui -j"$(nproc)" >/dev/null
GUI_DIST="${REPO}/build-gui/dist"
fi
echo "==> 准备目录 ${STAGE}"
rm -rf "${STAGE}"
mkdir -p "${STAGE}/bin" "${LIBDIR}" "${STAGE}/tools"
cp "${REPO}/build/dist/fpv-relay" "${STAGE}/bin/"
cp "${REPO}/config.example.yaml" "${STAGE}/" 2>/dev/null || true
[ -f "${REPO}/ass/gs.key" ] && cp "${REPO}/ass/gs.key" "${STAGE}/gs.key"
cp -r "${REPO}/tools/." "${STAGE}/tools/" 2>/dev/null || true
cp "${REPO}/README.md" "${STAGE}/" 2>/dev/null || true
# 只打包这些库(Qt6/ICU/libpcap/libsodium),其余系统库依赖目标机提供
bundle_libs() {
local lp="${LD_LIBRARY_PATH}"
[ -n "${QT_PREFIX:-}" ] && lp="${QT_PREFIX}/lib:${lp}"
LD_LIBRARY_PATH="$lp" ldd "$1" 2>/dev/null | awk '{print $3}' | grep '^/' | while read -r lib; do
base="$(basename "$lib")"
case "$base" in
libQt6*|libicu*|libpcap*|libsodium*) ;;
*) continue ;;
esac
if [ ! -e "${LIBDIR}/${base}" ]; then
cp -L "$lib" "${LIBDIR}/${base}"
bundle_libs "$lib"
fi
done
}
echo "==> 打包命令行版运行库"
bundle_libs "${STAGE}/bin/fpv-relay"
if [ "$WITH_GUI" = "1" ]; then
echo "==> 打包 GUI 与 Qt 运行环境"
cp "${GUI_DIST}/fpv-relay-gui" "${STAGE}/bin/"
cp -rL "${GUI_DIST}/FpvRelay" "${STAGE}/bin/" 2>/dev/null || true
mkdir -p "${STAGE}/plugins/platforms" "${STAGE}/plugins/xcbglintegrations" \
"${STAGE}/plugins/imageformats" "${STAGE}/plugins/iconengines" "${STAGE}/qml"
for p in libqxcb.so libqminimal.so libqoffscreen.so; do
[ -f "${QT_PREFIX}/plugins/platforms/${p}" ] && cp -L "${QT_PREFIX}/plugins/platforms/${p}" "${STAGE}/plugins/platforms/"
done
cp -L "${QT_PREFIX}/plugins/xcbglintegrations/"*.so "${STAGE}/plugins/xcbglintegrations/" 2>/dev/null || true
cp -L "${QT_PREFIX}/plugins/imageformats/"*.so "${STAGE}/plugins/imageformats/" 2>/dev/null || true
cp -L "${QT_PREFIX}/plugins/iconengines/"*.so "${STAGE}/plugins/iconengines/" 2>/dev/null || true
# 只拷 FluentUI/本项目用到的 QML 模块(避免把整个 Qt qml 目录塞进去)
cp -rL "${QT_PREFIX}/qml/QtQml" "${STAGE}/qml/"
mkdir -p "${STAGE}/qml/Qt/labs" "${STAGE}/qml/Qt5Compat" "${STAGE}/qml/QtQuick"
cp -rL "${QT_PREFIX}/qml/Qt/labs/qmlmodels" "${STAGE}/qml/Qt/labs/" 2>/dev/null || true
cp -rL "${QT_PREFIX}/qml/Qt/labs/folderlistmodel" "${STAGE}/qml/Qt/labs/" 2>/dev/null || true
cp -rL "${QT_PREFIX}/qml/Qt5Compat/GraphicalEffects" "${STAGE}/qml/Qt5Compat/" 2>/dev/null || true
find "${QT_PREFIX}/qml/QtQuick" -maxdepth 1 -type f -exec cp -L {} "${STAGE}/qml/QtQuick/" \;
for sub in Controls Dialogs Layouts Shapes Templates Window; do
[ -d "${QT_PREFIX}/qml/QtQuick/${sub}" ] && cp -rL "${QT_PREFIX}/qml/QtQuick/${sub}" "${STAGE}/qml/QtQuick/"
done
# 按实际依赖收集 Qt6/ICU 库
bundle_libs "${STAGE}/bin/fpv-relay-gui"
find "${STAGE}/qml" "${STAGE}/plugins" -name "*.so" 2>/dev/null | while read -r so; do
bundle_libs "$so"
done
fi
cat > "${STAGE}/run.sh" <<'EOF'
#!/usr/bin/env bash
DIR="$(cd "$(dirname "$0")" && pwd)"
export LD_LIBRARY_PATH="$DIR/lib:${LD_LIBRARY_PATH}"
exec "$DIR/bin/fpv-relay" "$@"
EOF
chmod +x "${STAGE}/run.sh"
if [ "$WITH_GUI" = "1" ]; then
cat > "${STAGE}/run-gui.sh" <<'EOF'
#!/usr/bin/env bash
DIR="$(cd "$(dirname "$0")" && pwd)"
export LD_LIBRARY_PATH="$DIR/lib:${LD_LIBRARY_PATH}"
export QT_PLUGIN_PATH="$DIR/plugins"
export QT_QPA_PLATFORM_PLUGIN_PATH="$DIR/plugins/platforms"
export QML2_IMPORT_PATH="$DIR/qml"
export QML_IMPORT_PATH="$DIR/qml"
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "警告: 未找到 ffmpeg,视频预览不可用(apt install ffmpeg)" >&2
fi
exec "$DIR/bin/fpv-relay-gui" "$@"
EOF
chmod +x "${STAGE}/run-gui.sh"
fi
echo "==> 生成 tar.gz"
tar -C "${OUT}" -czf "${OUT}/${NAME}.tar.gz" "${NAME}"
echo
echo "完成: ${OUT}/${NAME}.tar.gz"
du -sh "${OUT}/${NAME}.tar.gz"