1+ #! /bin/bash
2+
3+ # Colors for output
4+ RED=' \033[0;31m'
5+ GREEN=' \033[0;32m'
6+ YELLOW=' \033[1;33m'
7+ BLUE=' \033[0;34m'
8+ NC=' \033[0m' # No Color
9+
10+ # Parse arguments
11+ BUILD_TYPE=" debug"
12+ VERBOSE=0
13+
14+ while [[ $# -gt 0 ]]; do
15+ case $1 in
16+ release)
17+ BUILD_TYPE=" release"
18+ shift
19+ ;;
20+ -v|--verbose)
21+ VERBOSE=1
22+ shift
23+ ;;
24+ -h|--help)
25+ echo " Usage: $0 [release] [-v|--verbose] [-h|--help]"
26+ echo " release Build in release mode (default: debug)"
27+ echo " -v,--verbose Show build output"
28+ echo " -h,--help Show this help"
29+ exit 0
30+ ;;
31+ * )
32+ echo " Unknown argument: $1 "
33+ echo " Use -h for help"
34+ exit 1
35+ ;;
36+ esac
37+ done
38+
39+ BUILD_FLAG=" "
40+ if [ " $BUILD_TYPE " = " release" ]; then
41+ BUILD_FLAG=" --release"
42+ fi
43+
44+ # Arrays to track build results
45+ declare -a BUILD_TARGETS
46+ declare -a BUILD_DESCRIPTIONS
47+ declare -a BUILD_RESULTS
48+ declare -a BUILD_TOOLS
49+
50+ echo -e " ${BLUE} === Cross-Platform Build Script ===${NC} "
51+ echo -e " ${BLUE} Build type: $BUILD_TYPE ${NC} "
52+ if [ $VERBOSE -eq 1 ]; then
53+ echo -e " ${BLUE} Verbose mode: enabled${NC} "
54+ fi
55+ echo
56+
57+ # Function to check if command exists
58+ check_command () {
59+ if command -v " $1 " > /dev/null 2>&1 ; then
60+ echo -e " ${GREEN} ✓${NC} $1 is installed"
61+ return 0
62+ else
63+ echo -e " ${RED} ✗${NC} $1 is not installed"
64+ return 1
65+ fi
66+ }
67+
68+ # Function to check if Rust target is installed
69+ check_rust_target () {
70+ if rustup target list --installed | grep -q " $1 " ; then
71+ echo -e " ${GREEN} ✓${NC} Rust target $1 is installed"
72+ return 0
73+ else
74+ echo -e " ${RED} ✗${NC} Rust target $1 is not installed"
75+ return 1
76+ fi
77+ }
78+
79+ # Function to check if Docker is running
80+ check_docker () {
81+ if docker info > /dev/null 2>&1 ; then
82+ echo -e " ${GREEN} ✓${NC} Docker is running"
83+ return 0
84+ else
85+ echo -e " ${RED} ✗${NC} Docker is not running"
86+ return 1
87+ fi
88+ }
89+
90+ # Function to attempt build and record result
91+ attempt_build () {
92+ local tool=$1
93+ local target=$2
94+ local description=$3
95+
96+ BUILD_TARGETS+=(" $target " )
97+ BUILD_DESCRIPTIONS+=(" $description " )
98+ BUILD_TOOLS+=(" $tool " )
99+
100+ echo -e " ${YELLOW} Building $description ...${NC} "
101+
102+ # Determine output redirection based on verbose flag
103+ local output_redirect=" "
104+ if [ $VERBOSE -eq 0 ]; then
105+ output_redirect=" >/dev/null 2>&1"
106+ fi
107+
108+ if [ " $tool " = " cargo" ]; then
109+ echo -e " ${BLUE} Running: cargo build --target $target $BUILD_FLAG ${NC} "
110+ if [ $VERBOSE -eq 1 ]; then
111+ if cargo build --target " $target " $BUILD_FLAG ; then
112+ BUILD_RESULTS+=(" SUCCESS" )
113+ echo -e " ${GREEN} ✓${NC} Successfully built $description "
114+ else
115+ BUILD_RESULTS+=(" FAILED" )
116+ echo -e " ${RED} ✗${NC} Failed to build $description "
117+ fi
118+ else
119+ if cargo build --target " $target " $BUILD_FLAG > /dev/null 2>&1 ; then
120+ BUILD_RESULTS+=(" SUCCESS" )
121+ echo -e " ${GREEN} ✓${NC} Successfully built $description "
122+ else
123+ BUILD_RESULTS+=(" FAILED" )
124+ echo -e " ${RED} ✗${NC} Failed to build $description "
125+ fi
126+ fi
127+ elif [ " $tool " = " cross" ]; then
128+ echo -e " ${BLUE} Running: cross build --target $target $BUILD_FLAG ${NC} "
129+ if [ $VERBOSE -eq 1 ]; then
130+ if cross build --target " $target " $BUILD_FLAG ; then
131+ BUILD_RESULTS+=(" SUCCESS" )
132+ echo -e " ${GREEN} ✓${NC} Successfully built $description "
133+ else
134+ BUILD_RESULTS+=(" FAILED" )
135+ echo -e " ${RED} ✗${NC} Failed to build $description "
136+ fi
137+ else
138+ if cross build --target " $target " $BUILD_FLAG > /dev/null 2>&1 ; then
139+ BUILD_RESULTS+=(" SUCCESS" )
140+ echo -e " ${GREEN} ✓${NC} Successfully built $description "
141+ else
142+ BUILD_RESULTS+=(" FAILED" )
143+ echo -e " ${RED} ✗${NC} Failed to build $description "
144+ fi
145+ fi
146+ fi
147+ echo
148+ }
149+
150+ # Check prerequisites
151+ echo -e " ${BLUE} === Checking Prerequisites ===${NC} "
152+
153+ MISSING_DEPS=0
154+
155+ # Check basic tools
156+ check_command " rustup" || MISSING_DEPS=1
157+ check_command " cargo" || MISSING_DEPS=1
158+
159+ # Check optional tools (don't fail if missing, just skip those builds)
160+ CROSS_AVAILABLE=0
161+ DOCKER_AVAILABLE=0
162+ LINUX_ARM_AVAILABLE=0
163+ MINGW_AVAILABLE=0
164+
165+ if check_command " cross" ; then
166+ CROSS_AVAILABLE=1
167+ fi
168+
169+ if check_docker; then
170+ DOCKER_AVAILABLE=1
171+ fi
172+
173+ if check_command " aarch64-linux-gnu-gcc" ; then
174+ LINUX_ARM_AVAILABLE=1
175+ fi
176+
177+ if check_command " x86_64-w64-mingw32-gcc" ; then
178+ MINGW_AVAILABLE=1
179+ fi
180+
181+ echo
182+
183+ # Check Rust targets
184+ echo -e " ${BLUE} === Checking Rust Targets ===${NC} "
185+ TARGET_X86_LINUX=0
186+ TARGET_ARM_LINUX=0
187+ TARGET_WIN_GNU=0
188+ TARGET_MAC_X86=0
189+ TARGET_MAC_ARM=0
190+
191+ check_rust_target " x86_64-unknown-linux-gnu" && TARGET_X86_LINUX=1
192+ check_rust_target " aarch64-unknown-linux-gnu" && TARGET_ARM_LINUX=1
193+ check_rust_target " x86_64-pc-windows-gnu" && TARGET_WIN_GNU=1
194+ check_rust_target " x86_64-apple-darwin" && TARGET_MAC_X86=1
195+ check_rust_target " aarch64-apple-darwin" && TARGET_MAC_ARM=1
196+
197+ echo
198+
199+ if [ $MISSING_DEPS -eq 1 ]; then
200+ echo -e " ${RED} Critical dependencies missing (rustup/cargo). Cannot continue.${NC} "
201+ exit 1
202+ fi
203+
204+ echo -e " ${GREEN} Starting builds with available tools...${NC} "
205+ echo
206+
207+ # Start building
208+ echo -e " ${BLUE} === Cross-Platform Builds ===${NC} "
209+
210+ # Native build (always available)
211+ if [ $TARGET_X86_LINUX -eq 1 ]; then
212+ attempt_build " cargo" " x86_64-unknown-linux-gnu" " Linux x86_64 (native)"
213+ else
214+ BUILD_TARGETS+=(" x86_64-unknown-linux-gnu" )
215+ BUILD_DESCRIPTIONS+=(" Linux x86_64 (native)" )
216+ BUILD_TOOLS+=(" cargo" )
217+ BUILD_RESULTS+=(" SKIPPED - target not installed" )
218+ fi
219+
220+ # Linux ARM64 build
221+ if [ $TARGET_ARM_LINUX -eq 1 ] && [ $LINUX_ARM_AVAILABLE -eq 1 ]; then
222+ attempt_build " cargo" " aarch64-unknown-linux-gnu" " Linux ARM64 (cross-compile)"
223+ elif [ $TARGET_ARM_LINUX -eq 0 ]; then
224+ BUILD_TARGETS+=(" aarch64-unknown-linux-gnu" )
225+ BUILD_DESCRIPTIONS+=(" Linux ARM64 (cross-compile)" )
226+ BUILD_TOOLS+=(" cargo" )
227+ BUILD_RESULTS+=(" SKIPPED - target not installed" )
228+ elif [ $LINUX_ARM_AVAILABLE -eq 0 ]; then
229+ BUILD_TARGETS+=(" aarch64-unknown-linux-gnu" )
230+ BUILD_DESCRIPTIONS+=(" Linux ARM64 (cross-compile)" )
231+ BUILD_TOOLS+=(" cargo" )
232+ BUILD_RESULTS+=(" SKIPPED - aarch64-linux-gnu-gcc not available" )
233+ fi
234+
235+ # Windows GNU build
236+ if [ $TARGET_WIN_GNU -eq 1 ] && [ $MINGW_AVAILABLE -eq 1 ]; then
237+ attempt_build " cargo" " x86_64-pc-windows-gnu" " Windows x86_64 GNU (mingw-w64)"
238+ elif [ $TARGET_WIN_GNU -eq 0 ]; then
239+ BUILD_TARGETS+=(" x86_64-pc-windows-gnu" )
240+ BUILD_DESCRIPTIONS+=(" Windows x86_64 GNU (mingw-w64)" )
241+ BUILD_TOOLS+=(" cargo" )
242+ BUILD_RESULTS+=(" SKIPPED - target not installed" )
243+ elif [ $MINGW_AVAILABLE -eq 0 ]; then
244+ BUILD_TARGETS+=(" x86_64-pc-windows-gnu" )
245+ BUILD_DESCRIPTIONS+=(" Windows x86_64 GNU (mingw-w64)" )
246+ BUILD_TOOLS+=(" cargo" )
247+ BUILD_RESULTS+=(" SKIPPED - mingw-w64-gcc not available" )
248+ fi
249+
250+ # Cross-based builds (need cross + docker)
251+ CROSS_READY=0
252+ if [ $CROSS_AVAILABLE -eq 1 ] && [ $DOCKER_AVAILABLE -eq 1 ]; then
253+ CROSS_READY=1
254+ fi
255+
256+
257+ # macOS builds
258+ if [ $TARGET_MAC_X86 -eq 1 ] && [ $CROSS_READY -eq 1 ]; then
259+ attempt_build " cross" " x86_64-apple-darwin" " macOS Intel"
260+ elif [ $TARGET_MAC_X86 -eq 0 ]; then
261+ BUILD_TARGETS+=(" x86_64-apple-darwin" )
262+ BUILD_DESCRIPTIONS+=(" macOS Intel" )
263+ BUILD_TOOLS+=(" cross" )
264+ BUILD_RESULTS+=(" SKIPPED - target not installed" )
265+ elif [ $CROSS_READY -eq 0 ]; then
266+ BUILD_TARGETS+=(" x86_64-apple-darwin" )
267+ BUILD_DESCRIPTIONS+=(" macOS Intel" )
268+ BUILD_TOOLS+=(" cross" )
269+ BUILD_RESULTS+=(" SKIPPED - cross/docker not available" )
270+ fi
271+
272+ if [ $TARGET_MAC_ARM -eq 1 ] && [ $CROSS_READY -eq 1 ]; then
273+ attempt_build " cross" " aarch64-apple-darwin" " macOS Apple Silicon"
274+ elif [ $TARGET_MAC_ARM -eq 0 ]; then
275+ BUILD_TARGETS+=(" aarch64-apple-darwin" )
276+ BUILD_DESCRIPTIONS+=(" macOS Apple Silicon" )
277+ BUILD_TOOLS+=(" cross" )
278+ BUILD_RESULTS+=(" SKIPPED - target not installed" )
279+ elif [ $CROSS_READY -eq 0 ]; then
280+ BUILD_TARGETS+=(" aarch64-apple-darwin" )
281+ BUILD_DESCRIPTIONS+=(" macOS Apple Silicon" )
282+ BUILD_TOOLS+=(" cross" )
283+ BUILD_RESULTS+=(" SKIPPED - cross/docker not available" )
284+ fi
285+
286+ # Display results table
287+ echo
288+ echo -e " ${BLUE} === Build Results Summary ===${NC} "
289+ echo
290+ printf " %-30s %-25s %-8s %-s\n" " Platform" " Target" " Tool" " Result"
291+ printf " %-30s %-25s %-8s %-s\n" " --------" " ------" " ----" " ------"
292+
293+ SUCCESS_COUNT=0
294+ FAILED_COUNT=0
295+ SKIPPED_COUNT=0
296+
297+ for i in " ${! BUILD_TARGETS[@]} " ; do
298+ target=" ${BUILD_TARGETS[i]} "
299+ description=" ${BUILD_DESCRIPTIONS[i]} "
300+ result=" ${BUILD_RESULTS[i]} "
301+ tool=" ${BUILD_TOOLS[i]} "
302+
303+ if [[ " $result " == " SUCCESS" ]]; then
304+ printf " %-30s %-25s %-8s ${GREEN} %-s${NC} \n" " $description " " $target " " $tool " " $result "
305+ (( SUCCESS_COUNT++ ))
306+ elif [[ " $result " == " FAILED" ]]; then
307+ printf " %-30s %-25s %-8s ${RED} %-s${NC} \n" " $description " " $target " " $tool " " $result "
308+ (( FAILED_COUNT++ ))
309+ else
310+ printf " %-30s %-25s %-8s ${YELLOW} %-s${NC} \n" " $description " " $target " " $tool " " $result "
311+ (( SKIPPED_COUNT++ ))
312+ fi
313+ done
314+
315+ echo
316+ echo -e " ${BLUE} Summary: ${GREEN} $SUCCESS_COUNT successful${NC} , ${RED} $FAILED_COUNT failed${NC} , ${YELLOW} $SKIPPED_COUNT skipped${NC} "
317+
318+ # Show successful binaries
319+ if [ $SUCCESS_COUNT -gt 0 ]; then
320+ echo
321+ echo -e " ${BLUE} === Successfully Built Binaries ===${NC} "
322+ find target -name " ci" -o -name " ci.exe" 2> /dev/null | sort | while read binary; do
323+ if [ -f " $binary " ]; then
324+ size=$( du -h " $binary " 2> /dev/null | cut -f1)
325+ echo -e " ${GREEN} ✓${NC} $binary ($size )"
326+ fi
327+ done
328+ fi
329+
330+ echo
331+ echo -e " ${BLUE} Build script completed!${NC} "
0 commit comments