Files
agent/device/scripts/build.sh
T
kicer 3df103cd45 feat(device): 0.1.1——shell_ver 编码兜底、all 目标补 linux/mips 软浮点
- shellInfo 拿到的版本号先过 toUTF8:utf8.Valid 为真(英文版 Windows 的
  纯 ASCII、本来就是 UTF-8 的系统)原样返回,只有不是合法 UTF-8 时才按
  GBK 解,解不出来保留原样。中文 Windows 的 `cmd /c ver` 是 GBK,之前
  hello 上报的 shell_ver 显示成「Microsoft Windows [�汾 10.0.19045.7807]」。
- build.sh 的 all 目标补 linux/mips,并固定 GOMIPS=softfloat(AR9331 这类
  OpenWrt 设备没有 FPU,硬浮点二进制一跑就是非法指令);docker 分支同步。
- version 0.1.0 → 0.1.1,两台设备版本号一致但代码不同的坑到此为止。
2026-11-20 14:50:29 +08:00

49 lines
1.9 KiB
Bash

#!/bin/sh
# 交叉编译设备端二进制到 ./bin(纯本地动作:不联网、不推送、不依赖任何特定服务器)。
#
# sh scripts/build.sh # 编当前平台
# sh scripts/build.sh all # 编 linux-arm64 / linux-amd64 / linux-mips / windows-amd64 / darwin-amd64 / darwin-arm64
#
# 有本机 Go 就用本机;没有但有 docker 就用 golang 镜像;都没有就报错。
set -e
cd "$(dirname "$0")/.."
TARGETS="$(uname -s | tr 'A-Z' 'a-z')/$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
[ "${1:-}" = "all" ] && TARGETS="linux/arm64 linux/amd64 linux/mips windows/amd64 darwin/amd64 darwin/arm64"
build_with_go() {
mkdir -p bin
for t in $TARGETS; do
os=${t%/*}; arch=${t#*/}; ext=""; gomips=""
[ "$os" = windows ] && ext=.exe
# mips 设备(OpenWrt / 老路由)基本都没有 FPU,硬浮点二进制一跑就是非法指令
[ "$arch" = mips ] && gomips=softfloat
echo "build $os/$arch"
GOOS=$os GOARCH=$arch GOMIPS=$gomips CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \
-o "bin/agent-device-$os-$arch$ext" ./cmd/agent
done
}
if command -v go >/dev/null 2>&1; then
build_with_go
elif command -v docker >/dev/null 2>&1; then
echo "本机没有 Go,改用 golang:1.23-alpine 容器编译"
docker run --rm -v "$PWD":/w -w /w \
-e CGO_ENABLED=0 -e TARGETS="$TARGETS" \
golang:1.23-alpine sh -c '
mkdir -p bin
for t in $TARGETS; do
os=${t%/*}; arch=${t#*/}; ext=""; gomips=""
[ "$os" = windows ] && ext=.exe
[ "$arch" = mips ] && gomips=softfloat
echo "build $os/$arch"
GOOS=$os GOARCH=$arch GOMIPS=$gomips go build -trimpath -ldflags "-s -w" \
-o "bin/agent-device-$os-$arch$ext" ./cmd/agent
done'
else
echo "需要 Go 或 docker 才能编译" >&2
exit 1
fi
ls -lh bin