CoolFace
Apppublic

devin15/cursor2api-rust

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
build.sh194 linesDownload Raw Back to scripts
1#!/bin/bash2set -euo pipefail3 4# 颜色输出函数5info() { echo -e "\033[1;34m[INFO]\033[0m $*"; }6warn() { echo -e "\033[1;33m[WARN]\033[0m $*"; }7error() { echo -e "\033[1;31m[ERROR]\033[0m $*" >&2; exit 1; }8 9# 检查必要的工具10check_requirements() {11    local missing_tools=()12 13    # 基础工具检查14    for tool in cargo protoc npm node; do15        if ! command -v "$tool" &>/dev/null; then16            missing_tools+=("$tool")17        fi18    done19 20    if [[ ${#missing_tools[@]} -gt 0 ]]; then21        error "缺少必要工具: ${missing_tools[*]}"22    fi23}24 25# 解析参数26USE_STATIC=false27 28while [[ $# -gt 0 ]]; do29    case $1 in30        --static) USE_STATIC=true ;;31        --help) show_help; exit 0 ;;32        *) error "未知参数: $1" ;;33    esac34    shift35done36 37# 帮助信息38show_help() {39    cat << EOF40用法: $(basename "$0") [选项]41 42选项:43  --static        使用静态链接(默认动态链接)44  --help          显示此帮助信息45 46不带参数时只编译当前平台47EOF48}49 50# 并行构建函数51build_target() {52    local target=$153    local extension=""54    local rustflags="${2:-}"55 56    info "正在构建 $target..."57 58    # 确定文件后缀59    [[ $target == *"windows"* ]] && extension=".exe"60 61    # 构建62    if [[ $target != "$CURRENT_TARGET" ]]; then63        env RUSTFLAGS="$rustflags" cargo build --target "$target" --release64    else65        env RUSTFLAGS="$rustflags" cargo build --release66    fi67 68    # 移动编译产物到 release 目录69    local binary_name="cursor-api"70    [[ $USE_STATIC == true ]] && binary_name+="-static"71 72    local binary_path73    if [[ $target == "$CURRENT_TARGET" ]]; then74        binary_path="target/release/cursor-api$extension"75    else76        binary_path="target/$target/release/cursor-api$extension"77    fi78 79    if [[ -f "$binary_path" ]]; then80        cp "$binary_path" "release/${binary_name}-$target$extension"81        info "完成构建 $target"82    else83        warn "构建产物未找到: $target"84        warn "查找路径: $binary_path"85        warn "当前目录内容:"86        ls -R target/87        return 188    fi89}90 91# 获取 CPU 架构和操作系统92ARCH=$(uname -m | sed 's/^aarch64\|arm64$/aarch64/;s/^x86_64\|x86-64\|x64\|amd64$/x86_64/')93OS=$(uname -s)94 95# 确定当前系统的目标平台96get_target() {97    local arch=$198    local os=$299    case "$os" in100        "Darwin") echo "${arch}-apple-darwin" ;;101        "Linux") 102            if [[ $USE_STATIC == true ]]; then103                echo "${arch}-unknown-linux-musl"104            else105                echo "${arch}-unknown-linux-gnu"106            fi107            ;;108        "MINGW"*|"MSYS"*|"CYGWIN"*|"Windows_NT") echo "${arch}-pc-windows-msvc" ;;109        "FreeBSD") echo "${arch}-unknown-freebsd" ;;110        *) error "不支持的系统: $os" ;;111    esac112}113 114# 设置当前目标平台115CURRENT_TARGET=$(get_target "$ARCH" "$OS")116 117# 检查是否成功获取目标平台118[ -z "$CURRENT_TARGET" ] && error "无法确定当前系统的目标平台"119 120# 获取系统对应的所有目标121get_targets() {122    case "$1" in123        "linux")124            # Linux 只构建当前架构125            echo "$CURRENT_TARGET"126            ;;127        "freebsd")128            # FreeBSD 只构建当前架构129            echo "$CURRENT_TARGET"130            ;;131        "windows")132            # Windows 只构建当前架构133            echo "$CURRENT_TARGET"134            ;;135        "macos")136            # macOS 构建所有 macOS 目标137            echo "x86_64-apple-darwin aarch64-apple-darwin"138            ;;139        *) error "不支持的系统组: $1" ;;140    esac141}142 143# 检查依赖144check_requirements145 146# 确定要构建的目标147case "$OS" in148    Darwin) 149        TARGETS=($(get_targets "macos"))150        ;;151    Linux)152        TARGETS=($(get_targets "linux"))153        ;;154    FreeBSD)155        TARGETS=($(get_targets "freebsd"))156        ;;157    MINGW*|MSYS*|CYGWIN*|Windows_NT)158        TARGETS=($(get_targets "windows"))159        ;;160    *) error "不支持的系统: $OS" ;;161esac162 163# 创建 release 目录164mkdir -p release165 166# 设置静态链接标志167RUSTFLAGS="-C link-arg=-s"168[[ $USE_STATIC == true ]] && RUSTFLAGS="-C target-feature=+crt-static -C link-arg=-s"169 170# 并行构建所有目标171info "开始构建..."172for target in "${TARGETS[@]}"; do173    build_target "$target" "$RUSTFLAGS" &174done175 176# 等待所有构建完成177wait178 179# 为 macOS 平台创建通用二进制180if [[ "$OS" == "Darwin" ]] && [[ ${#TARGETS[@]} -gt 1 ]]; then181    binary_suffix=""182    [[ $USE_STATIC == true ]] && binary_suffix="-static"183 184    if [[ -f "release/cursor-api${binary_suffix}-x86_64-apple-darwin" ]] && \185       [[ -f "release/cursor-api${binary_suffix}-aarch64-apple-darwin" ]]; then186        info "创建 macOS 通用二进制..."187        lipo -create \188            "release/cursor-api${binary_suffix}-x86_64-apple-darwin" \189            "release/cursor-api${binary_suffix}-aarch64-apple-darwin" \190            -output "release/cursor-api${binary_suffix}-universal-apple-darwin"191    fi192fi193 194info "构建完成!"