Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·105 lines (89 loc) · 3.15 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·105 lines (89 loc) · 3.15 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
#!/bin/bash
# Get version information
VERSION="${VERSION:-dev}"
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Build flags for version injection
LDFLAGS="-s -w \
-X 'main.VERSION=$VERSION' \
-X 'main.buildDate=$BUILD_DATE' \
-X 'main.gitCommit=$GIT_COMMIT'"
echo "Building NetWatcher..."
echo "Version: $VERSION"
echo "Build Date: $BUILD_DATE"
echo "Git Commit: $GIT_COMMIT"
# Create bin directory if it doesn't exist
mkdir -p bin
# Build for different platforms
# Format: "os/arch:cgo_enabled"
# darwin requires CGO_ENABLED=1 for pcap/libpcap support
platforms=(
"linux/amd64:0"
"linux/arm64:0"
"linux/arm:0"
"linux/mips:0"
"linux/mipsle:0"
"linux/mips64:0"
"linux/mips64le:0"
"darwin/amd64:1"
"darwin/arm64:1"
"windows/amd64:0"
"windows/arm64:0"
)
for platform in "${platforms[@]}"; do
# Split platform into os/arch and cgo setting
IFS=':' read -r osarch cgo <<< "$platform"
platform_split=(${osarch//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name="netwatcher-${GOOS}-${GOARCH}"
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
echo "Building for $GOOS/$GOARCH (CGO_ENABLED=$cgo)..."
# darwin builds with CGO require running on macOS or having cross-compile toolchain
if [ "$cgo" = "1" ] && [ "$(uname -s)" != "Darwin" ] && [ "$GOOS" = "darwin" ]; then
echo " Skipping darwin build - requires macOS or cross-compile toolchain"
echo " Build darwin binaries on a Mac or macOS CI runner"
continue
fi
env CGO_ENABLED=$cgo GOOS=$GOOS GOARCH=$GOARCH go build \
-ldflags "$LDFLAGS" \
-o "bin/${output_name}" \
.
if [ $? -ne 0 ]; then
echo "Failed to build for $GOOS/$GOARCH"
# Don't exit on darwin failure when cross-compiling - just warn
if [ "$GOOS" = "darwin" ]; then
echo " darwin build failed - this is expected when cross-compiling from Linux"
continue
fi
exit 1
fi
# Sign darwin binaries (required for macOS to run them)
if [ "$GOOS" = "darwin" ]; then
echo "Code signing darwin binary..."
codesign -s - -f "bin/${output_name}" 2>/dev/null || echo " Warning: codesign not available (run on macOS to sign)"
fi
# Create zip file for this platform (except Linux which uses tar.gz)
echo "Creating archive for $GOOS/$GOARCH..."
cd bin
if [ "$GOOS" = "linux" ]; then
archive_name="netwatcher-${VERSION}-${GOOS}-${GOARCH}.tar.gz"
tar -czf "$archive_name" "$output_name"
else
archive_name="netwatcher-${VERSION}-${GOOS}-${GOARCH}.zip"
zip "$archive_name" "$output_name"
fi
rm "$output_name" # Remove the binary after archiving
cd ..
done
echo "Build complete! All binaries are in the bin/ directory"
# Create a checksums file
cd bin
echo "Generating checksums..."
if ls *.tar.gz *.zip 1> /dev/null 2>&1; then
sha256sum *.tar.gz *.zip > "netwatcher-${VERSION}-checksums.txt" 2>/dev/null || shasum -a 256 *.tar.gz *.zip > "netwatcher-${VERSION}-checksums.txt"
fi
cd ..
echo "Done!"