-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·190 lines (169 loc) · 5.55 KB
/
build.sh
File metadata and controls
executable file
·190 lines (169 loc) · 5.55 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
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default settings
BUILD_ALL=false
PLATFORMS=()
OUTPUT_DIR="dist"
BINARY_NAME="ccc"
VERSION=""
BUILD_TIME=""
# Available platforms (using plain variables for bash compatibility)
PLATFORMS_INFO=(
"darwin-amd64|macOS x86_64"
"darwin-arm64|macOS ARM64 (Apple Silicon)"
"linux-amd64|Linux x86_64"
"linux-arm64|Linux ARM64"
)
print_help() {
echo -e "${BLUE}Usage:${NC} $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -a, --all Build for all supported platforms"
echo " -p, --platforms Build for specific platforms (comma-separated)"
echo " Available: darwin-amd64, darwin-arm64, linux-amd64, linux-arm64"
echo " -o, --output Output directory (default: dist)"
echo " -n, --name Binary name (default: ccc)"
echo " -v, --version Version string (default: git commit short hash)"
echo " -t, --build-time Build time in ISO 8601 format (default: current UTC time)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Build for current platform only"
echo " $0 --all # Build for all platforms"
echo " $0 -p darwin-arm64,linux-amd64 # Build for specific platforms"
echo " $0 -a -o ./build # Build all platforms to ./build directory"
echo " $0 --all --version v1.0.0 # Build all platforms with version v1.0.0"
}
# Detect current platform
detect_current_platform() {
local os
local arch
os=$(go env GOOS)
arch=$(go env GOARCH)
echo "${os}-${arch}"
}
# Build for a specific platform
build_platform() {
local os=$1
local arch=$2
local platform_key="${os}-${arch}"
local output_name="${BINARY_NAME}-${platform_key}"
local output_path="${OUTPUT_DIR}/${output_name}"
echo -e "${YELLOW}Building for ${BLUE}${os}/${arch}${NC}..."
# Create output directory
mkdir -p "${OUTPUT_DIR}"
# Build with version ldflags
local ldflags="-s -w"
if [ -n "$VERSION" ]; then
ldflags="$ldflags -X main.Version=${VERSION}"
fi
if [ -n "$BUILD_TIME" ]; then
ldflags="$ldflags -X main.BuildTime=${BUILD_TIME}"
fi
CGO_ENABLED=0 GOOS="${os}" GOARCH="${arch}" go build -ldflags="$ldflags" -o "${output_path}" main.go
chmod +x "${output_path}"
echo -e "${GREEN} ✓${NC} ${output_path}"
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-a|--all)
BUILD_ALL=true
shift
;;
-p|--platforms)
IFS=',' read -ra PLATFORMS <<< "$2"
shift 2
;;
-o|--output)
OUTPUT_DIR="$2"
shift 2
;;
-n|--name)
BINARY_NAME="$2"
shift 2
;;
-v|--version)
VERSION="$2"
shift 2
;;
-t|--build-time)
BUILD_TIME="$2"
shift 2
;;
-h|--help)
print_help
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
print_help
exit 1
;;
esac
done
# Main logic
echo -e "${BLUE}Building ccc command line tool...${NC}"
echo ""
# Set default version if not specified
if [ -z "$VERSION" ]; then
VERSION=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
echo -e "${YELLOW}Version: ${BLUE}${VERSION}${NC} (auto-detected from git)"
else
echo -e "${YELLOW}Version: ${BLUE}${VERSION}${NC}"
fi
# Set default build time if not specified
if [ -z "$BUILD_TIME" ]; then
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo -e "${YELLOW}Build time: ${BLUE}${BUILD_TIME}${NC} (auto-generated)"
else
echo -e "${YELLOW}Build time: ${BLUE}${BUILD_TIME}${NC}"
fi
echo ""
current_platform=$(detect_current_platform)
# If no platforms specified, build current platform only
if [ "$BUILD_ALL" = false ] && [ ${#PLATFORMS[@]} -eq 0 ]; then
echo -e "${YELLOW}Building for current platform: ${BLUE}${current_platform}${NC}"
build_platform "$(echo "$current_platform" | cut -d'-' -f1)" "$(echo "$current_platform" | cut -d'-' -f2)"
else
# Build for specified or all platforms
if [ "$BUILD_ALL" = true ]; then
PLATFORMS=("darwin-amd64" "darwin-arm64" "linux-amd64" "linux-arm64")
fi
echo -e "${YELLOW}Building for ${#PLATFORMS[@]} platform(s):${NC}"
for platform in "${PLATFORMS[@]}"; do
os=$(echo "$platform" | cut -d'-' -f1)
arch=$(echo "$platform" | cut -d'-' -f2)
# Validate platform
valid=false
for info in "${PLATFORMS_INFO[@]}"; do
if [[ "$info" == "${platform}"* ]]; then
valid=true
break
fi
done
if [ "$valid" = false ]; then
echo -e "${RED} ✗ Invalid platform: ${platform}${NC}"
echo " Available platforms: darwin-amd64, darwin-arm64, linux-amd64, linux-arm64"
exit 1
fi
build_platform "$os" "$arch"
done
fi
echo ""
echo -e "${GREEN}Build completed successfully!${NC}"
echo -e "${BLUE}Output directory: ${OUTPUT_DIR}${NC}"
echo ""
echo "Contents:"
ls -lh "${OUTPUT_DIR}"/ccc-* 2>/dev/null || echo " (no binaries found)"
echo ""
echo "To install a specific binary, run:"
echo " sudo cp ${OUTPUT_DIR}/ccc-<platform> /usr/local/bin/ccc"
echo "Example for current platform (${current_platform}):"
echo " sudo cp ${OUTPUT_DIR}/ccc-${current_platform} /usr/local/bin/ccc"