-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·248 lines (216 loc) · 8.46 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·248 lines (216 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
echo "Starting cross-compilation for ctfile-cli..."
echo
# 设置Go编译参数以生成最小尺寸的二进制文件
export CGO_ENABLED=0
LDFLAGS="-s -w"
TRIMPATH="-trimpath"
# 创建输出目录
mkdir -p dist
mkdir -p temp
# 构建函数
build_target() {
local goos=$1
local goarch=$2
local goarm=$3
local output=$4
local desc=$5
echo "Building for $desc..."
# 构建二进制文件到临时目录
local temp_binary="temp/$output"
if [ -n "$goarm" ]; then
GOOS=$goos GOARCH=$goarch GOARM=$goarm go build $TRIMPATH -ldflags "$LDFLAGS" -o "$temp_binary" ctfile-cli.go
else
GOOS=$goos GOARCH=$goarch go build $TRIMPATH -ldflags "$LDFLAGS" -o "$temp_binary" ctfile-cli.go
fi
if [ $? -eq 0 ]; then
echo "[OK] $desc build complete"
# 压缩文件
compress_binary "$goos" "$goarch" "$temp_binary" "$output" "$desc"
else
echo "Failed to build for $desc"
fi
echo
}
# Strip二进制文件函数
strip_binary() {
local goos=$1
local goarch=$2
local binary_path=$3
# 对于 macOS 目标,直接跳过 strip 处理
if [ "$goos" = "darwin" ]; then
echo " [INFO] Skipping strip for macOS binary"
return 0
fi
# 获取当前系统架构
local host_os=$(go env GOOS)
local host_arch=$(go env GOARCH)
# 根据目标平台选择合适的strip工具
local strip_cmd="strip"
local use_cross_strip=false
# 检查是否为交叉编译
if [ "$goos" != "$host_os" ] || [ "$goarch" != "$host_arch" ]; then
use_cross_strip=true
case "$goos-$goarch" in
"linux-arm")
if command -v arm-linux-gnueabihf-strip >/dev/null 2>&1; then
strip_cmd="arm-linux-gnueabihf-strip"
use_cross_strip=false
elif command -v arm-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="arm-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-arm64")
if command -v aarch64-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="aarch64-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-mips")
if command -v mips-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="mips-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-mipsle")
if command -v mipsel-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="mipsel-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-mips64")
if command -v mips64-linux-gnuabi64-strip >/dev/null 2>&1; then
strip_cmd="mips64-linux-gnuabi64-strip"
use_cross_strip=false
elif command -v mips64-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="mips64-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-mips64le")
if command -v mips64el-linux-gnuabi64-strip >/dev/null 2>&1; then
strip_cmd="mips64el-linux-gnuabi64-strip"
use_cross_strip=false
elif command -v mips64el-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="mips64el-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-386")
# i686 Linux (32位)
if [ "$host_arch" = "amd64" ]; then
# 在64位系统上,通常可以直接用strip处理32位二进制
strip_cmd="strip"
use_cross_strip=false
elif command -v i686-linux-gnu-strip >/dev/null 2>&1; then
strip_cmd="i686-linux-gnu-strip"
use_cross_strip=false
fi
;;
"linux-amd64")
# x86_64 Linux
if [ "$host_arch" = "amd64" ] || [ "$host_arch" = "386" ]; then
strip_cmd="strip"
use_cross_strip=false
fi
;;
"windows-amd64")
if command -v x86_64-w64-mingw32-strip >/dev/null 2>&1; then
strip_cmd="x86_64-w64-mingw32-strip"
use_cross_strip=false
fi
;;
"windows-386")
if command -v i686-w64-mingw32-strip >/dev/null 2>&1; then
strip_cmd="i686-w64-mingw32-strip"
use_cross_strip=false
fi
;;
esac
else
use_cross_strip=false
fi
# 如果是交叉编译但没有找到对应的strip工具,跳过strip
if [ "$use_cross_strip" = true ]; then
echo " [INFO] Cross-compilation detected ($host_os/$host_arch -> $goos/$goarch)"
echo " [INFO] No cross-strip tool found for $goos/$goarch, skipping strip"
return 0
fi
# 执行strip命令
if command -v "$strip_cmd" >/dev/null 2>&1; then
echo " Stripping with $strip_cmd..."
local strip_output
strip_output=$("$strip_cmd" "$binary_path" 2>&1)
local strip_result=$?
if [ $strip_result -eq 0 ]; then
echo " [OK] Binary stripped successfully"
else
echo " [WARN] Strip failed: $strip_output"
echo " [INFO] Continuing with unstripped binary"
fi
else
echo " [INFO] Strip command '$strip_cmd' not available"
fi
}
# 压缩函数
compress_binary() {
local goos=$1
local goarch=$2
local temp_binary=$3
local original_name=$4
local desc=$5
# Strip二进制文件
strip_binary "$goos" "$goarch" "$temp_binary"
# 获取当前工作目录的绝对路径
local work_dir=$(pwd)
# 创建临时目录用于压缩
local compress_dir="temp/compress_$(basename "$original_name")"
mkdir -p "$compress_dir"
if [ "$goos" = "windows" ]; then
# Windows系统:压缩成zip,内部文件名为ctfile-cli.exe
local inner_name="ctfile-cli.exe"
cp "$temp_binary" "$compress_dir/$inner_name"
# 生成压缩包名称(去掉原始.exe后缀)
local archive_name="${original_name%.exe}.zip"
local archive_path="$work_dir/dist/$archive_name"
echo "Compressing $desc to $archive_name..."
(cd "$compress_dir" && zip -q "$archive_path" "$inner_name")
if [ $? -eq 0 ]; then
echo "[OK] $desc compressed to $archive_name"
else
echo "Failed to compress $desc"
fi
else
# Linux/macOS系统:压缩成tar.gz,内部文件名为ctfile-cli
local inner_name="ctfile-cli"
cp "$temp_binary" "$compress_dir/$inner_name"
local archive_name="${original_name}.tar.gz"
local archive_path="$work_dir/dist/$archive_name"
echo "Compressing $desc to $archive_name..."
(cd "$compress_dir" && tar -czf "$archive_path" "$inner_name")
if [ $? -eq 0 ]; then
echo "[OK] $desc compressed to $archive_name"
else
echo "Failed to compress $desc"
fi
fi
# 清理临时压缩目录
rm -rf "$compress_dir"
}
# 构建所有目标平台
build_target "linux" "arm" "7" "ctfile-cli_linux_arm" "ARM Linux"
build_target "linux" "arm64" "" "ctfile-cli_linux_arm64" "ARM64 Linux"
build_target "linux" "mips" "" "ctfile-cli_linux_mips" "MIPS Linux"
build_target "linux" "mipsle" "" "ctfile-cli_linux_mipsle" "MIPSEL Linux"
build_target "linux" "mips64" "" "ctfile-cli_linux_mips64" "MIPS64 Linux"
build_target "linux" "mips64le" "" "ctfile-cli_linux_mips64le" "MIPS64EL Linux"
build_target "linux" "amd64" "" "ctfile-cli_linux_amd64" "x86_64 Linux"
build_target "linux" "386" "" "ctfile-cli_linux_386" "i686 Linux"
build_target "darwin" "amd64" "" "ctfile-cli_darwin_amd64" "x86_64 macOS"
build_target "darwin" "arm64" "" "ctfile-cli_darwin_arm64" "ARM64 macOS"
build_target "windows" "amd64" "" "ctfile-cli_windows_amd64.exe" "x86_64 Windows"
build_target "windows" "386" "" "ctfile-cli_windows_386.exe" "i686 Windows"
# 清理临时目录
rm -rf temp
echo "All builds completed and compressed. Check the 'dist' directory for archive files."