forked from 0x676e67/vproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·167 lines (152 loc) · 4.18 KB
/
build.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
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
#!/bin/bash
: ${root=$(pwd)}
: ${tag=latest}
: ${os=linux}
: ${name=vproxy}
# Function to print colored text based on log level
log() {
local level=$1
local message=$2
local NC='\033[0m' # Reset to default color
case "$level" in
"info")
echo -e "\033[0;32m[INFO] $message${NC}" # Green for INFO
;;
"warning")
echo -e "\033[0;33m[WARNING] $message${NC}" # Yellow for WARNING
;;
"error")
echo -e "\033[0;31m[ERROR] $message${NC}" # Red for ERROR
;;
*)
echo "$message" # Default to printing message without color for other levels
;;
esac
}
[ ! -d bin ] && mkdir bin
# Build support paltform target
# 1. Linux (force musl)
linux_target=(
"x86_64-unknown-linux-musl:mimalloc"
"aarch64-unknown-linux-musl:mimalloc"
"armv7-unknown-linux-musleabihf:mimalloc"
"arm-unknown-linux-musleabihf:jemalloc"
"i686-unknown-linux-musl:jemalloc"
"i586-unknown-linux-musl:jemalloc"
)
# 2. MacOS
macos_target=(
"x86_64-apple-darwin"
"aarch64-apple-darwin"
)
# 3. Windows
windows_target=(
"x86_64-pc-windows-gnu"
"i686-pc-windows-gnu"
)
# Check linux rustup target installed
check_linux_rustup_target_installed() {
for target in ${linux_target[@]}; do
target=$(echo $target | cut -d':' -f1)
installed=$(rustup target list | grep "${target} (installed)")
if [ -z "$installed" ]; then
log "info" "Installing ${target}..."
rustup target add ${target}
fi
done
}
# Check macos rustup target installed
check_macos_rustup_target_installed() {
for target in ${macos_target[@]}; do
installed=$(rustup target list | grep "${target} (installed)")
if [ -z "$installed" ]; then
log "info" "Installing ${target}..."
rustup target add ${target}
fi
done
}
# Check windows rustup target installed
check_windows_rustup_target_installed() {
for target in ${windows_target[@]}; do
installed=$(rustup target list | grep "${target} (installed)")
if [ -z "$installed" ]; then
log "info" "Installing ${target}..."
rustup target add ${target}
fi
done
}
# Build linux target
build_linux_target() {
for target in "${linux_target[@]}"; do
build_target=$(echo $target | cut -d':' -f1)
feature=$(echo $target | cut -d':' -f2)
log "info" "Building ${target}..."
if cargo zigbuild --release --target "${build_target}" --features "${feature}"; then
compress_and_move $build_target
log "info" "Build ${target} done"
else
log "error" "Build ${target} failed"
exit 1
fi
done
}
# Build macos target
build_macos_target() {
for target in "${macos_target[@]}"; do
log "info" "Building ${target}..."
if CARGO_PROFILE_RELEASE_STRIP=none cargo zigbuild --release --target "${target}"; then
compress_and_move $target
log "info" "Build ${target} done"
else
log "error" "Build ${target} failed"
exit 1
fi
done
}
# Build windows target
build_windows_target() {
for target in "${windows_target[@]}"; do
log "info" "Building ${target}..."
if cargo build --release --target "${target}"; then
compress_and_move $target
log "info" "Build ${target} done"
else
log "error" "Build ${target} failed"
exit 1
fi
done
}
# upx and move target
compress_and_move() {
build_target=$1
target_dir="target/${build_target}/release"
bin_name=$name
if [[ $build_target == *windows* ]]; then
bin_name="${name}.exe"
fi
upx "${target_dir}/${bin_name}"
sudo chmod +x "${target_dir}/${bin_name}"
cd "${target_dir}"
tar czvf $name-$tag-${build_target}.tar.gz $bin_name
shasum -a 256 $name-$tag-${build_target}.tar.gz >$name-$tag-${build_target}.tar.gz.sha256
mv $name-$tag-${build_target}.tar.gz $root/bin/
mv $name-$tag-${build_target}.tar.gz.sha256 $root/bin/
cd -
}
# Execute
if [ "$os" == "linux" ]; then
log "info" "Building linux target..."
check_linux_rustup_target_installed
build_linux_target
elif [ "$os" == "macos" ]; then
log "info" "Building macos target..."
check_macos_rustup_target_installed
build_macos_target
elif [ "$os" == "windows" ]; then
log "info" "Building windows target..."
check_windows_rustup_target_installed
build_windows_target
else
log "error" "Unsupported os: ${os}"
exit 1
fi