-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·165 lines (141 loc) · 5.32 KB
/
build.sh
File metadata and controls
executable file
·165 lines (141 loc) · 5.32 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
#!/bin/bash
# build.sh
# This script builds the Citadel CLI for common server architectures
# and packages them for a formal release.
#
# Usage:
# ./build.sh # Build for current platform only
# ./build.sh --all # Build for all platforms (linux/darwin/windows, amd64/arm64)
set -e
# --- Parse Arguments ---
BUILD_ALL=false
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Build the Citadel CLI binary."
echo ""
echo "Options:"
echo " --all Build for all platforms (linux/darwin/windows, amd64/arm64)"
echo " --help, -h Show this help message"
echo ""
echo "By default, builds only for the current platform."
exit 0
fi
if [[ "$1" == "--all" ]]; then
BUILD_ALL=true
fi
echo "--- Building and Packaging Citadel CLI..."
# --- Configuration ---
VERSION=$(git describe --tags --always --dirty || echo "dev")
BUILD_DIR="build"
RELEASE_DIR="release"
MODULE_PATH=$(go list -m)
VERSION_VAR_PATH="${MODULE_PATH}/cmd.version"
# --- Man Page Generation ---
MAN_DIR="docs/man"
# --- Clean Up ---
rm -rf "$BUILD_DIR" "$RELEASE_DIR" "$MAN_DIR"
mkdir -p "$BUILD_DIR" "$RELEASE_DIR"
echo "--- Cleaned old build and release directories ---"
# Generate man pages (for release builds or when --all is specified)
if [[ "$BUILD_ALL" == true ]]; then
echo "--- Generating man pages ---"
go run docs/gen-manpage.go
fi
# --- Detect Current Platform ---
CURRENT_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
CURRENT_ARCH=$(uname -m)
# Normalize OS name
case "$CURRENT_OS" in
linux) CURRENT_OS="linux" ;;
darwin) CURRENT_OS="darwin" ;;
mingw*|msys*|cygwin*) CURRENT_OS="windows" ;;
*) echo "⚠️ Unknown OS: $CURRENT_OS, defaulting to linux"; CURRENT_OS="linux" ;;
esac
# Normalize architecture name
case "$CURRENT_ARCH" in
x86_64|amd64) CURRENT_ARCH="amd64" ;;
aarch64|arm64) CURRENT_ARCH="arm64" ;;
*) echo "⚠️ Unknown architecture: $CURRENT_ARCH, defaulting to amd64"; CURRENT_ARCH="amd64" ;;
esac
# --- Determine Build Targets ---
if [[ "$BUILD_ALL" == true ]]; then
echo "--- Building for all platforms (--all flag detected) ---"
PLATFORMS=("linux" "darwin" "windows")
ARCHS=("amd64" "arm64")
else
echo "--- Building for current platform only: $CURRENT_OS/$CURRENT_ARCH ---"
echo " (Use --all flag to build for all platforms)"
PLATFORMS=("$CURRENT_OS")
ARCHS=("$CURRENT_ARCH")
fi
# --- Build and Package Loop ---
for OS in "${PLATFORMS[@]}"; do
for ARCH in "${ARCHS[@]}"; do
echo ""
echo "--- Processing $OS/$ARCH ---"
# Define paths and names
PLATFORM_DIR="$BUILD_DIR/${OS}-${ARCH}"
# Define binary name with .exe for Windows
BINARY_NAME="citadel"
if [[ "$OS" == "windows" ]]; then
BINARY_NAME="citadel.exe"
fi
BINARY_PATH="$PLATFORM_DIR/$BINARY_NAME"
mkdir -p "$PLATFORM_DIR"
# 1. Build the binary
echo "Building binary..."
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -ldflags="-X '${VERSION_VAR_PATH}=${VERSION}'" -o "$BINARY_PATH" ./cmd/citadel
# 2. Copy man page if available (not for Windows)
if [[ "$OS" != "windows" ]] && [[ -f "$MAN_DIR/citadel.1" ]]; then
cp "$MAN_DIR/citadel.1" "$PLATFORM_DIR/"
fi
# 3. Package (Windows uses .zip, others use .tar.gz)
if [[ "$OS" == "windows" ]]; then
RELEASE_NAME="citadel_${VERSION}_${OS}_${ARCH}.zip"
RELEASE_PATH="$RELEASE_DIR/$RELEASE_NAME"
echo "Packaging to $RELEASE_NAME..."
# Use absolute path for zip output
ABSOLUTE_RELEASE_PATH="$(cd "$(dirname "$RELEASE_PATH")" && pwd)/$(basename "$RELEASE_PATH")"
(cd "$PLATFORM_DIR" && zip -q "$ABSOLUTE_RELEASE_PATH" "$BINARY_NAME")
else
RELEASE_NAME="citadel_${VERSION}_${OS}_${ARCH}.tar.gz"
RELEASE_PATH="$RELEASE_DIR/$RELEASE_NAME"
echo "Packaging to $RELEASE_NAME..."
# Include man page if available
if [[ -f "$PLATFORM_DIR/citadel.1" ]]; then
tar -C "$PLATFORM_DIR" -czf "$RELEASE_PATH" citadel citadel.1
else
tar -C "$PLATFORM_DIR" -czf "$RELEASE_PATH" citadel
fi
fi
done
done
# --- Create Symlink for Current Platform ---
CURRENT_BINARY="$BUILD_DIR/${CURRENT_OS}-${CURRENT_ARCH}/citadel"
if [[ -f "$CURRENT_BINARY" ]]; then
ln -sf "$CURRENT_BINARY" citadel
echo ""
echo "--- Created symlink: citadel -> $CURRENT_BINARY ---"
fi
# --- Generate Checksums ---
echo ""
echo "--- Generating Checksums ---"
# Use shasum on macOS, sha256sum on Linux
if command -v sha256sum &> /dev/null; then
(cd "$RELEASE_DIR" && sha256sum *.tar.gz *.zip 2>/dev/null > checksums.txt || sha256sum *.tar.gz > checksums.txt)
else
(cd "$RELEASE_DIR" && shasum -a 256 *.tar.gz *.zip 2>/dev/null > checksums.txt || shasum -a 256 *.tar.gz > checksums.txt)
fi
echo "✅ Build and packaging complete."
echo ""
echo "Binaries for local use are in: './$BUILD_DIR'"
tree "$BUILD_DIR"
echo ""
echo "Release artifacts are in: './$RELEASE_DIR'"
tree "$RELEASE_DIR"
echo ""
echo "📋 SHA256 Checksums (copy this into your release notes):"
echo "----------------------------------------------------"
cat "$RELEASE_DIR/checksums.txt"
echo "----------------------------------------------------"