build more platforms, better build.sh.

This commit is contained in:
chengsiyuan 2024-11-19 13:58:05 +08:00
parent 2dd32e738e
commit 535312f6b9

View File

@ -1,5 +1,7 @@
#!/bin/bash
set -e # 遇到错误时退出脚本
# 检查是否在 Git 仓库中
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: Not inside a Git repository."
@ -21,7 +23,7 @@ fi
# 决定版本信息
if [ -n "$TAG" ]; then
VERSION="$TAG$DIRTY"
VERSION="$TAG-$COMMIT_SHA$DIRTY"
else
VERSION="$COMMIT_SHA$DIRTY"
fi
@ -29,14 +31,69 @@ fi
# 输出版本信息
echo "Injecting version: $VERSION"
# 将版本信息注入到 Go 构建中
GO_LDFLAGS="-X 'sylixos-uploader/common.Version=$VERSION'"
echo "Building with flags: $GO_LDFLAGS"
OS=linux ARCH=amd64
GOOS=${OS} GOARCH=${ARCH} CGO_ENABLED=0 go build -o "sylixos-uploader-${OS}-${ARCH}-${VERSION}" -ldflags "-s -w $GO_LDFLAGS"
# 处理选项
BUILD_ALL=false
INSTALL_DIR=""
OS=linux ARCH=arm64
GOOS=${OS} GOARCH=${ARCH} CGO_ENABLED=0 go build -o "sylixos-uploader-${OS}-${ARCH}-${VERSION}" -ldflags "-s -w $GO_LDFLAGS"
while [[ $# -gt 0 ]]; do
case $1 in
--all)
BUILD_ALL=true
shift
;;
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
exit 0
# 编译函数
build_binary() {
local OS=$1
local ARCH=$2
local OUTPUT="sylixos-uploader-${OS}-${ARCH}-${VERSION}"
if [ "$OS" = "windows" ]; then
OUTPUT+=".exe"
fi
GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build -o "$OUTPUT" -ldflags "-s -w -X 'sylixos-uploader/common.Version=$VERSION'"
echo "Built: $OUTPUT"
}
# 默认只编译本地平台
if [ "$BUILD_ALL" = true ]; then
echo "Building for all platforms..."
PLATFORMS=(
"linux amd64"
"linux arm64"
"windows amd64"
"windows arm64"
"darwin amd64"
"darwin arm64"
"sylixos arm64"
)
for PLATFORM in "${PLATFORMS[@]}"; do
build_binary $PLATFORM
done
else
echo "Building for local platform..."
HOSTOS=$(go env GOHOSTOS)
HOSTARCH=$(go env GOHOSTARCH)
build_binary $HOSTOS $HOSTARCH
fi
# 安装到指定目录
if [ -n "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR"
BIN_NAME="sylixos-uploader-$(go env GOHOSTOS)-$(go env GOHOSTARCH)-${VERSION}"
cp "$BIN_NAME" "$INSTALL_DIR/sylixos-uploader"
echo "Installed to $INSTALL_DIR"
fi
exit 0