-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-all.sh
More file actions
executable file
·295 lines (264 loc) · 10 KB
/
build-all.sh
File metadata and controls
executable file
·295 lines (264 loc) · 10 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
# Enhanced build script for GPU Pro with multiple flavors
# Supports: Linux, macOS, Windows | amd64, arm64 | debug, release, minimal
set -e
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Build info
VERSION=${VERSION:-"2.0.0"}
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Output directory
OUTPUT_DIR="dist"
mkdir -p "$OUTPUT_DIR"
# Detect current platform
CURRENT_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
echo -e "${BLUE}═══════════════════════════════════════════════════${NC}"
echo -e "${BLUE} GPU Pro - Multi-Platform Build Script${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════${NC}"
echo -e "${BLUE}Version:${NC} $VERSION"
echo -e "${BLUE}Build Time:${NC} $BUILD_TIME"
echo -e "${BLUE}Git Commit:${NC} $GIT_COMMIT"
echo ""
# Function to build for a specific platform
build_binary() {
local OS=$1
local ARCH=$2
local FLAVOR=$3
local BUILD_TYPE=$4 # "web" or "cli"
local BINARY_NAME="gpu-pro"
local SOURCE_PATH="."
# Determine source path and binary name based on build type
if [ "$BUILD_TYPE" = "cli" ]; then
BINARY_NAME="gpu-pro-cli"
SOURCE_PATH="./cmd/gpu-pro-cli"
fi
# Windows binaries need .exe extension
if [ "$OS" = "windows" ]; then
BINARY_NAME="${BINARY_NAME}.exe"
fi
# Output path
local OUTPUT_PATH="$OUTPUT_DIR/${BINARY_NAME%-.*}-${OS}-${ARCH}-${FLAVOR}"
if [ "$OS" = "windows" ]; then
OUTPUT_PATH="${OUTPUT_PATH}.exe"
fi
# Build flags based on flavor
local LDFLAGS=""
local BUILD_TAGS=""
local CGO_ENABLED=1
# macOS builds don't need CGO (no GPU support)
if [ "$OS" = "darwin" ]; then
CGO_ENABLED=0
echo -e "${BLUE} Note: macOS build without GPU support (CGO disabled)${NC}"
elif [ "$CURRENT_OS" = "darwin" ] && [ "$OS" != "darwin" ]; then
# Cross-compiling from macOS to Linux/Windows with CGO will fail, fall back to no-CGO
echo -e "${YELLOW} Note: Cross-compiling $OS from macOS (will try CGO first, fall back to static build)${NC}"
fi
case "$FLAVOR" in
release)
LDFLAGS="-s -w -X main.Version=$VERSION -X main.BuildTime=$BUILD_TIME -X main.GitCommit=$GIT_COMMIT"
BUILD_TAGS="$OS"
;;
debug)
LDFLAGS="-X main.Version=$VERSION-debug -X main.BuildTime=$BUILD_TIME -X main.GitCommit=$GIT_COMMIT"
BUILD_TAGS="$OS"
;;
minimal)
LDFLAGS="-s -w -X main.Version=$VERSION-minimal -X main.BuildTime=$BUILD_TIME -X main.GitCommit=$GIT_COMMIT"
CGO_ENABLED=0 # Static build, no CGO
# Use nogpu tag for minimal builds on non-darwin platforms
# Darwin already has no GPU support, so use darwin tag
if [ "$OS" = "darwin" ]; then
BUILD_TAGS="$OS"
else
BUILD_TAGS="nogpu"
fi
;;
esac
local TYPE_LABEL=""
if [ "$BUILD_TYPE" = "cli" ]; then
TYPE_LABEL=" [CLI]"
fi
echo -e "${YELLOW}Building:${NC} $OS/$ARCH ($FLAVOR)${TYPE_LABEL}"
# Build with platform-specific tags
# Try with CGO first, if it fails and we're cross-compiling, try without CGO
if ! GOOS=$OS GOARCH=$ARCH CGO_ENABLED=$CGO_ENABLED go build \
-tags="$BUILD_TAGS" \
-ldflags="$LDFLAGS" \
-o "$OUTPUT_PATH" \
"$SOURCE_PATH" \
2>&1 | grep -v "^#"; then
# If CGO build failed and we're not building for darwin (which already has CGO=0)
if [ "$CGO_ENABLED" = "1" ] && [ "$OS" != "darwin" ]; then
echo -e "${YELLOW} CGO build failed, retrying without CGO (GPU support disabled)...${NC}"
GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build \
-tags="nogpu" \
-ldflags="$LDFLAGS" \
-o "$OUTPUT_PATH" \
"$SOURCE_PATH" \
2>&1 | grep -v "^#" || true
fi
fi
if [ -f "$OUTPUT_PATH" ]; then
local SIZE=$(du -h "$OUTPUT_PATH" | cut -f1)
echo -e "${GREEN}✓ Built:${NC} $OUTPUT_PATH (${SIZE})"
# Compress release builds if UPX is available
if [ "$FLAVOR" = "release" ] && command -v upx &> /dev/null; then
echo -e "${BLUE} Compressing with UPX...${NC}"
upx --best --lzma "$OUTPUT_PATH" 2>&1 | grep -v "^Packing" || true
SIZE=$(du -h "$OUTPUT_PATH" | cut -f1)
echo -e "${GREEN} Compressed:${NC} ${SIZE}"
fi
else
echo -e "${RED}✗ Failed to build $OS/$ARCH${NC}"
fi
echo ""
}
# Install dependencies
echo -e "${BLUE}Installing dependencies...${NC}"
go mod download
go mod tidy
echo ""
# Parse command line arguments
BUILD_MODE="all" # all, single, quick
PLATFORMS=""
FLAVORS=""
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
BUILD_MODE="$2"
shift 2
;;
--platforms)
PLATFORMS="$2"
shift 2
;;
--flavors)
FLAVORS="$2"
shift 2
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --mode MODE Build mode: all, quick, or single (default: all)"
echo " --platforms LIST Comma-separated list of platforms (e.g., linux,darwin,windows)"
echo " --flavors LIST Comma-separated list of flavors (e.g., release,debug,minimal)"
echo " --help Show this help message"
echo ""
echo "Build Modes:"
echo " all Build for all platforms, architectures, and flavors"
echo " quick Build release binaries for all platforms/architectures"
echo " single Build for current platform only (all flavors)"
echo ""
echo "Flavors:"
echo " release Optimized production build (stripped, compressed)"
echo " debug Debug build with symbols"
echo " minimal Minimal static build without CGO"
echo ""
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
exit 1
;;
esac
done
# Determine build targets based on mode
case "$BUILD_MODE" in
all)
echo -e "${YELLOW}Build Mode:${NC} All platforms, all flavors"
PLATFORMS="linux,darwin,windows"
FLAVORS="release,debug,minimal"
;;
quick)
echo -e "${YELLOW}Build Mode:${NC} Quick release builds"
PLATFORMS="linux,darwin,windows"
FLAVORS="release"
;;
single)
echo -e "${YELLOW}Build Mode:${NC} Current platform only"
CURRENT_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
PLATFORMS="$CURRENT_OS"
FLAVORS="release,debug,minimal"
;;
esac
# Convert comma-separated lists to arrays
IFS=',' read -ra PLATFORM_ARRAY <<< "$PLATFORMS"
IFS=',' read -ra FLAVOR_ARRAY <<< "$FLAVORS"
echo ""
echo -e "${BLUE}Building for:${NC}"
echo -e " Platforms: ${PLATFORM_ARRAY[*]}"
echo -e " Flavors: ${FLAVOR_ARRAY[*]}"
echo -e " Types: Web UI + CLI"
echo ""
# Build matrix
TOTAL_BUILDS=0
SUCCESSFUL_BUILDS=0
SKIPPED_BUILDS=0
FAILED_BUILDS=0
for OS in "${PLATFORM_ARRAY[@]}"; do
for FLAVOR in "${FLAVOR_ARRAY[@]}"; do
for BUILD_TYPE in "web" "cli"; do
# Build for amd64
((TOTAL_BUILDS++))
if build_binary "$OS" "amd64" "$FLAVOR" "$BUILD_TYPE"; then
((SUCCESSFUL_BUILDS++))
else
# Check if it was a skip or a failure
if [ "$CURRENT_OS" = "darwin" ] && [ "$OS" != "darwin" ]; then
((SKIPPED_BUILDS++))
else
((FAILED_BUILDS++))
fi
fi
# Build for arm64 (except Windows minimal builds which may have issues)
if [ "$OS" != "windows" ] || [ "$FLAVOR" != "minimal" ]; then
((TOTAL_BUILDS++))
if build_binary "$OS" "arm64" "$FLAVOR" "$BUILD_TYPE"; then
((SUCCESSFUL_BUILDS++))
else
# Check if it was a skip or a failure
if [ "$CURRENT_OS" = "darwin" ] && [ "$OS" != "darwin" ]; then
((SKIPPED_BUILDS++))
else
((FAILED_BUILDS++))
fi
fi
fi
done
done
done
# Summary
echo -e "${BLUE}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN}Build Summary${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════${NC}"
echo -e "Total builds: $TOTAL_BUILDS"
echo -e "Successful: ${GREEN}$SUCCESSFUL_BUILDS${NC}"
if [ $SKIPPED_BUILDS -gt 0 ]; then
echo -e "Skipped: ${YELLOW}$SKIPPED_BUILDS${NC} (cross-compilation not available)"
fi
if [ $FAILED_BUILDS -gt 0 ]; then
echo -e "Failed: ${RED}$FAILED_BUILDS${NC}"
fi
echo ""
# List all generated binaries
echo -e "${BLUE}Generated binaries in ${OUTPUT_DIR}/:${NC}"
ls -lh "$OUTPUT_DIR"/ | grep gpu-pro | awk '{printf " %s (%s)\n", $9, $5}'
echo ""
echo -e "${GREEN}Done!${NC}"
echo ""
echo -e "${BLUE}Usage examples:${NC}"
echo -e " Web UI:"
echo -e " Linux: ./$OUTPUT_DIR/gpu-pro-linux-amd64-release"
echo -e " macOS: ./$OUTPUT_DIR/gpu-pro-darwin-amd64-release"
echo -e " Windows: ./$OUTPUT_DIR/gpu-pro-windows-amd64-release.exe"
echo ""
echo -e " CLI/TUI:"
echo -e " Linux: ./$OUTPUT_DIR/gpu-pro-cli-linux-amd64-release"
echo -e " macOS: ./$OUTPUT_DIR/gpu-pro-cli-darwin-amd64-release"
echo -e " Windows: ./$OUTPUT_DIR/gpu-pro-cli-windows-amd64-release.exe"