修复一些bug

This commit is contained in:
wisdgod
2024-12-25 07:02:50 +08:00
parent 3ab975a5b3
commit ea1acb555f
8 changed files with 384 additions and 223 deletions

View File

@@ -22,8 +22,8 @@ check_requirements() {
fi
done
# Linux 特定检查
if [[ $USE_CROSS == true ]] && ! command -v cross &>/dev/null; then
# cross 工具检查(仅在 Linux 上需要)
if [[ "$OS" == "Linux" ]] && ! command -v cross &>/dev/null; then
missing_tools+=("cross")
fi
@@ -46,6 +46,22 @@ show_help() {
EOF
}
# 判断是否使用 cross
should_use_cross() {
local target=$1
# 如果不是 Linux 环境,直接返回 false
if [[ "$OS" != "Linux" ]]; then
return 1
fi
# 在 Linux 环境下,以下目标不使用 cross
# 1. Linux 上的 x86_64-unknown-linux-gnu
if [[ "$target" == "x86_64-unknown-linux-gnu" ]]; then
return 1
fi
return 0
}
# 并行构建函数
build_target() {
local target=$1
@@ -57,42 +73,41 @@ build_target() {
# 确定文件后缀
[[ $target == *"windows"* ]] && extension=".exe"
# 设置目标特定的环境变量
local build_env=()
if [[ $target == "aarch64-unknown-linux-gnu" ]]; then
build_env+=(
"CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc"
"CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++"
"CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc"
"PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig"
"PKG_CONFIG_ALLOW_CROSS=1"
"OPENSSL_DIR=/usr"
"OPENSSL_INCLUDE_DIR=/usr/include"
"OPENSSL_LIB_DIR=/usr/lib/aarch64-linux-gnu"
)
fi
# 判断是否使用 cross仅在 Linux 上)
if [[ $target != "$CURRENT_TARGET" ]]; then
env ${build_env[@]+"${build_env[@]}"} RUSTFLAGS="$rustflags" cargo build --target "$target" --release
# 判断是否使用 cross
if should_use_cross "$target"; then
env RUSTFLAGS="$rustflags" cross build --target "$target" --release
else
env ${build_env[@]+"${build_env[@]}"} RUSTFLAGS="$rustflags" cargo build --release
if [[ $target != "$CURRENT_TARGET" ]]; then
env RUSTFLAGS="$rustflags" cargo build --target "$target" --release
else
env RUSTFLAGS="$rustflags" cargo build --release
fi
fi
# 移动编译产物到 release 目录
local binary_name="cursor-api"
[[ $USE_STATIC == true ]] && binary_name+="-static"
if [[ -f "target/$target/release/cursor-api$extension" ]]; then
cp "target/$target/release/cursor-api$extension" "release/${binary_name}-$target$extension"
local binary_path
if [[ $target == "$CURRENT_TARGET" ]]; then
binary_path="target/release/cursor-api$extension"
else
binary_path="target/$target/release/cursor-api$extension"
fi
if [[ -f "$binary_path" ]]; then
cp "$binary_path" "release/${binary_name}-$target$extension"
info "完成构建 $target"
else
warn "构建产物未找到: $target"
warn "查找路径: $binary_path"
warn "当前目录内容:"
ls -R target/
return 1
fi
}
# 获取 CPU 架构
# 获取 CPU 架构和操作系统
ARCH=$(uname -m | sed 's/^aarch64\|arm64$/aarch64/;s/^x86_64\|x86-64\|x64\|amd64$/x86_64/')
OS=$(uname -s)
@@ -104,7 +119,7 @@ get_target() {
"Darwin") echo "${arch}-apple-darwin" ;;
"Linux") echo "${arch}-unknown-linux-gnu" ;;
"MINGW"*|"MSYS"*|"CYGWIN"*|"Windows_NT") echo "${arch}-pc-windows-msvc" ;;
"FreeBSD") echo "x86_64-unknown-freebsd" ;;
"FreeBSD") echo "${arch}-unknown-freebsd" ;;
*) error "不支持的系统: $os" ;;
esac
}
@@ -118,21 +133,31 @@ CURRENT_TARGET=$(get_target "$ARCH" "$OS")
# 获取系统对应的所有目标
get_targets() {
case "$1" in
"linux") echo "x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu" ;;
"windows") echo "x86_64-pc-windows-msvc aarch64-pc-windows-msvc" ;;
"macos") echo "x86_64-apple-darwin aarch64-apple-darwin" ;;
"freebsd") echo "x86_64-unknown-freebsd" ;;
"linux")
# Linux 构建所有 Linux 目标和 FreeBSD 目标
echo "x86_64-unknown-linux-gnu x86_64-unknown-freebsd"
;;
"freebsd")
# FreeBSD 只构建当前架构的 FreeBSD 目标
echo "${ARCH}-unknown-freebsd"
;;
"windows")
# Windows 构建所有 Windows 目标
echo "x86_64-pc-windows-msvc"
;;
"macos")
# macOS 构建所有 macOS 目标
echo "x86_64-apple-darwin aarch64-apple-darwin"
;;
*) error "不支持的系统组: $1" ;;
esac
}
# 解析参数
USE_CROSS=false
USE_STATIC=false
while [[ $# -gt 0 ]]; do
case $1 in
--cross) USE_CROSS=true ;;
--static) USE_STATIC=true ;;
--help) show_help; exit 0 ;;
*) error "未知参数: $1" ;;
@@ -144,19 +169,21 @@ done
check_requirements
# 确定要构建的目标
if [[ $USE_CROSS == true ]] && is_linux; then
# 只在 Linux 上使用 cross 进行多架构构建
TARGETS=($(get_targets "linux"))
else
# 其他系统或不使用 cross 时只构建当前系统的所有架构
case "$OS" in
"Darwin") TARGETS=($(get_targets "macos")) ;;
"Linux") TARGETS=("$CURRENT_TARGET") ;;
"MINGW"*|"MSYS"*|"CYGWIN"*|"Windows_NT") TARGETS=($(get_targets "windows")) ;;
"FreeBSD") TARGETS=("$CURRENT_TARGET") ;;
*) error "不支持的系统: $OS" ;;
esac
fi
case "$OS" in
"Darwin")
TARGETS=($(get_targets "macos"))
;;
"Linux")
TARGETS=($(get_targets "linux"))
;;
"FreeBSD")
TARGETS=($(get_targets "freebsd"))
;;
"MINGW"*|"MSYS"*|"CYGWIN"*|"Windows_NT")
TARGETS=($(get_targets "windows"))
;;
*) error "不支持的系统: $OS" ;;
esac
# 创建 release 目录
mkdir -p release