-
Notifications
You must be signed in to change notification settings - Fork 21
/
go_pack_embed.sh
72 lines (61 loc) · 1.7 KB
/
go_pack_embed.sh
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
#!/bin/bash
# 脚本当前目录绝对路径
project_path=$(
cd "$(dirname "$0")" || exit
pwd
)
# 脚本当前目录名
project_name="${project_path##*/}"
# 获得所有受支持平台的列表
#os_arch_array=($(go tool dist list))
#for i in "${!os_arch_array[@]}"; do
# item=${array[i]}
os_arch_array=$(go tool dist list)
# 保存默认分隔符
OLD_IFS="$IFS"
for item in ${os_arch_array}; do
# array=(${item//\// }) # 替换\为空格(IFS的默认分隔符)
# 指定分隔符
IFS="/"
array=(${item})
os=${array[0]}
arch=${array[1]}
binary_file=${project_name}_${os}_${arch}
# 忽略以下平台的编译
if [[ "$os" == "android" ]] || [[ "$os" == "ios" ]] || [[ "$os" == "darwin" && "$arch" == *arm* ]]; then
continue
fi
# 设置变量
export GOOS="$os" GOARCH="$arch"
echo "环境变量设置成功:$GOOS------$GOARCH"
# 指定编译的二进制文件名
if [ "$os" == "windows" ]; then
binary_file="$binary_file".exe
fi
# 编译文件不同的系统架构使用不同的命令参数
if [ "$os" == "android" ]; then
# 开启 CGO
# export CGO_ENABLED=1
# flags="-w -linkmode=external -extldflags=-pie"
: # 占位
elif [ "$os" == "darwin" ]; then
# 开启 CGO
# export CGO_ENABLED=1
flags="-w"
elif [ "$os" == "windows" ]; then
# 交叉编译不支持 CGO 所以要禁用它
# export CGO_ENABLED=0
# flags="-w -H windowsgui"
: # 占位
else
# 交叉编译不支持 CGO 所以要禁用它
export CGO_ENABLED=0
flags="-w"
fi
# 编译二进制文件并输出到build目录
go build -ldflags=$flags -o "build/$binary_file"
done
# 还原默认分隔符
IFS="$OLD_IFS"
# 编译完成清理缓存
go clean -cache