Add Android client support and unify cross-platform builds
This commit is contained in:
179
scripts/build.sh
179
scripts/build.sh
@@ -1,27 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -euo pipefail
|
||||
|
||||
# 项目根目录
|
||||
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
BUILD_DIR="$ROOT_DIR/build"
|
||||
export GOCACHE="${GOCACHE:-$BUILD_DIR/.gocache}"
|
||||
|
||||
# 版本信息
|
||||
VERSION="${VERSION:-dev}"
|
||||
BUILD_TIME=$(date -u '+%Y-%m-%d %H:%M:%S')
|
||||
GIT_COMMIT=$(git -C "$ROOT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
||||
|
||||
# 默认目标平台
|
||||
DEFAULT_PLATFORMS="linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64"
|
||||
|
||||
# 是否启用 UPX 压缩
|
||||
BUILD_TIME="$(date -u '+%Y-%m-%d %H:%M:%S')"
|
||||
GIT_COMMIT="$(git -C "$ROOT_DIR" rev-parse --short HEAD 2>/dev/null || echo unknown)"
|
||||
USE_UPX="${USE_UPX:-true}"
|
||||
|
||||
# 颜色输出
|
||||
DESKTOP_PLATFORMS="linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64"
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
NC='\033[0m'
|
||||
|
||||
log_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
@@ -35,17 +30,14 @@ log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查 UPX 是否可用
|
||||
check_upx() {
|
||||
if command -v upx &> /dev/null; then
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
command -v upx >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# UPX 压缩二进制
|
||||
compress_binary() {
|
||||
local file=$1
|
||||
local os=$2
|
||||
|
||||
if [ "$USE_UPX" != "true" ]; then
|
||||
return
|
||||
fi
|
||||
@@ -53,27 +45,27 @@ compress_binary() {
|
||||
log_warn "UPX not found, skipping compression"
|
||||
return
|
||||
fi
|
||||
# macOS 二进制不支持 UPX
|
||||
if [[ "$file" == *"darwin"* ]]; then
|
||||
if [ "$os" = "darwin" ]; then
|
||||
log_warn "Skipping UPX for macOS binary: $file"
|
||||
return
|
||||
fi
|
||||
|
||||
log_info "Compressing $file with UPX..."
|
||||
upx -9 -q "$file" 2>/dev/null || log_warn "UPX compression failed for $file"
|
||||
}
|
||||
|
||||
# 构建 Web UI
|
||||
build_web() {
|
||||
log_info "Building web UI..."
|
||||
cd "$ROOT_DIR/web"
|
||||
pushd "$ROOT_DIR/web" >/dev/null
|
||||
|
||||
if [ ! -d "node_modules" ]; then
|
||||
log_info "Installing npm dependencies..."
|
||||
npm install
|
||||
fi
|
||||
npm run build
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
# 复制到 embed 目录
|
||||
popd >/dev/null
|
||||
|
||||
log_info "Copying dist to embed directory..."
|
||||
rm -rf "$ROOT_DIR/internal/server/app/dist"
|
||||
cp -r "$ROOT_DIR/web/dist" "$ROOT_DIR/internal/server/app/dist"
|
||||
@@ -81,85 +73,127 @@ build_web() {
|
||||
log_info "Web UI built successfully"
|
||||
}
|
||||
|
||||
# 构建单个二进制
|
||||
output_name() {
|
||||
local component=$1
|
||||
local os=$2
|
||||
|
||||
if [ "$os" = "windows" ]; then
|
||||
echo "${component}.exe"
|
||||
else
|
||||
echo "${component}"
|
||||
fi
|
||||
}
|
||||
|
||||
build_binary() {
|
||||
local os=$1
|
||||
local arch=$2
|
||||
local component=$3 # server 或 client
|
||||
|
||||
local output_name="${component}"
|
||||
if [ "$os" = "windows" ]; then
|
||||
output_name="${component}.exe"
|
||||
fi
|
||||
local component=$3
|
||||
|
||||
local output_dir="$BUILD_DIR/${os}_${arch}"
|
||||
mkdir -p "$output_dir"
|
||||
local output_file
|
||||
output_file="$(output_name "$component" "$os")"
|
||||
local output_path="$output_dir/$output_file"
|
||||
|
||||
mkdir -p "$output_dir"
|
||||
log_info "Building $component for $os/$arch..."
|
||||
|
||||
GOOS=$os GOARCH=$arch go build \
|
||||
GOOS="$os" GOARCH="$arch" CGO_ENABLED=0 go build \
|
||||
-buildvcs=false \
|
||||
-trimpath \
|
||||
-ldflags "-s -w -X 'main.Version=$VERSION' -X 'main.BuildTime=$BUILD_TIME' -X 'main.GitCommit=$GIT_COMMIT'" \
|
||||
-o "$output_dir/$output_name" \
|
||||
-o "$output_path" \
|
||||
"$ROOT_DIR/cmd/$component"
|
||||
|
||||
# UPX 压缩
|
||||
compress_binary "$output_dir/$output_name"
|
||||
compress_binary "$output_path" "$os"
|
||||
log_info " -> $output_path"
|
||||
}
|
||||
|
||||
# 构建所有平台
|
||||
build_all() {
|
||||
local platforms="${1:-$DEFAULT_PLATFORMS}"
|
||||
local platforms="${1:-$DESKTOP_PLATFORMS}"
|
||||
local platform os arch
|
||||
|
||||
for platform in $platforms; do
|
||||
local os="${platform%/*}"
|
||||
local arch="${platform#*/}"
|
||||
build_binary "$os" "$arch" "server"
|
||||
build_binary "$os" "$arch" "client"
|
||||
os="${platform%/*}"
|
||||
arch="${platform#*/}"
|
||||
build_binary "$os" "$arch" server
|
||||
build_binary "$os" "$arch" client
|
||||
done
|
||||
}
|
||||
|
||||
# 仅构建当前平台
|
||||
build_current() {
|
||||
local os=$(go env GOOS)
|
||||
local arch=$(go env GOARCH)
|
||||
local os
|
||||
local arch
|
||||
|
||||
build_binary "$os" "$arch" "server"
|
||||
build_binary "$os" "$arch" "client"
|
||||
os="$(go env GOOS)"
|
||||
arch="$(go env GOARCH)"
|
||||
|
||||
build_binary "$os" "$arch" server
|
||||
build_binary "$os" "$arch" client
|
||||
|
||||
log_info "Binaries built in $BUILD_DIR/${os}_${arch}/"
|
||||
}
|
||||
|
||||
# 清理构建产物
|
||||
build_android() {
|
||||
local output_dir="$BUILD_DIR/android_arm64"
|
||||
|
||||
mkdir -p "$output_dir"
|
||||
log_info "Building client for android/arm64..."
|
||||
GOOS=android GOARCH=arm64 CGO_ENABLED=0 go build \
|
||||
-buildvcs=false \
|
||||
-trimpath \
|
||||
-ldflags "-s -w -X 'main.Version=$VERSION' -X 'main.BuildTime=$BUILD_TIME' -X 'main.GitCommit=$GIT_COMMIT'" \
|
||||
-o "$output_dir/client" \
|
||||
"$ROOT_DIR/cmd/client"
|
||||
|
||||
if command -v gomobile >/dev/null 2>&1; then
|
||||
log_info "Building gomobile Android binding..."
|
||||
gomobile bind -target=android/arm64 -o "$output_dir/gotunnelmobile.aar" github.com/gotunnel/mobile/gotunnelmobile
|
||||
else
|
||||
log_warn "gomobile not found, skipping Android AAR build"
|
||||
fi
|
||||
|
||||
if [ -d "$ROOT_DIR/android" ]; then
|
||||
if [ -x "$ROOT_DIR/android/gradlew" ]; then
|
||||
log_info "Building Android debug APK..."
|
||||
(cd "$ROOT_DIR/android" && ./gradlew assembleDebug)
|
||||
else
|
||||
log_warn "android/gradlew not found, skipping APK build"
|
||||
fi
|
||||
else
|
||||
log_warn "Android host project not found, skipping APK build"
|
||||
fi
|
||||
}
|
||||
|
||||
clean() {
|
||||
log_info "Cleaning build directory..."
|
||||
rm -rf "$BUILD_DIR"
|
||||
log_info "Clean completed"
|
||||
}
|
||||
|
||||
# 显示帮助
|
||||
show_help() {
|
||||
echo "Usage: $0 [command] [options]"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " all Build for all platforms (default: $DEFAULT_PLATFORMS)"
|
||||
echo " current Build for current platform only"
|
||||
echo " web Build web UI only"
|
||||
echo " server Build server for current platform"
|
||||
echo " client Build client for current platform"
|
||||
echo " clean Clean build directory"
|
||||
echo " help Show this help message"
|
||||
echo ""
|
||||
echo "Environment variables:"
|
||||
echo " VERSION Set version string (default: dev)"
|
||||
echo " USE_UPX Enable UPX compression (default: true)"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " $0 current # Build for current platform"
|
||||
echo " $0 all # Build for all platforms"
|
||||
echo " VERSION=1.0.0 $0 all # Build with version"
|
||||
cat <<'EOF'
|
||||
Usage: build.sh [command] [options]
|
||||
|
||||
Commands:
|
||||
all Build web UI + all desktop platforms (default)
|
||||
current Build web UI + current platform only
|
||||
web Build web UI only
|
||||
server Build server for current platform
|
||||
client Build client for current platform
|
||||
android Build android/arm64 client and optional Android artifacts
|
||||
clean Clean build directory
|
||||
help Show this help message
|
||||
|
||||
Environment variables:
|
||||
VERSION Set version string (default: dev)
|
||||
USE_UPX Enable UPX compression (default: true)
|
||||
|
||||
Examples:
|
||||
./scripts/build.sh current
|
||||
VERSION=1.0.0 ./scripts/build.sh all
|
||||
EOF
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
@@ -176,10 +210,13 @@ main() {
|
||||
build_web
|
||||
;;
|
||||
server)
|
||||
build_binary "$(go env GOOS)" "$(go env GOARCH)" "server"
|
||||
build_binary "$(go env GOOS)" "$(go env GOARCH)" server
|
||||
;;
|
||||
client)
|
||||
build_binary "$(go env GOOS)" "$(go env GOARCH)" "client"
|
||||
build_binary "$(go env GOOS)" "$(go env GOARCH)" client
|
||||
;;
|
||||
android)
|
||||
build_android
|
||||
;;
|
||||
clean)
|
||||
clean
|
||||
|
||||
Reference in New Issue
Block a user