-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·693 lines (577 loc) · 19.6 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·693 lines (577 loc) · 19.6 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/usr/bin/env bash
# ============================================================================
# NovaLLM Build Script for macOS and Linux
# ============================================================================
# This script provides a unified interface for building the NovaLLM project
# with support for different build types, configurations, and targets.
#
# Usage: ./build.sh [options]
# Run with --help for detailed usage information
# ============================================================================
set -e # Exit on error
set -o pipefail # Catch errors in pipes
# ============================================================================
# Configuration and Defaults
# ============================================================================
# Build configuration
BUILD_TYPE="Release"
BUILD_DIR="build"
INSTALL_DIR="install"
ENABLE_LOGGING="ON"
CLEAN_BUILD=false
# Build targets
BUILD_MAIN=true
BUILD_TESTS=false
BUILD_STANDALONE=false
CREATE_PACKAGE=false
# Conan options
CONAN_BUILD_MISSING=true
# Script directory (for relative path resolution)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ============================================================================
# Color Output Functions
# ============================================================================
# Check if terminal supports colors
if [[ -t 1 ]] && command -v tput &> /dev/null && tput colors &> /dev/null; then
COLOR_SUPPORT=true
else
COLOR_SUPPORT=false
fi
print_header() {
if [[ "$COLOR_SUPPORT" = true ]]; then
echo -e "\n\033[1;36m==>\033[0m \033[1m$1\033[0m"
else
echo -e "\n==> $1"
fi
}
print_success() {
if [[ "$COLOR_SUPPORT" = true ]]; then
echo -e "\033[1;32m✓\033[0m $1"
else
echo "✓ $1"
fi
}
print_info() {
if [[ "$COLOR_SUPPORT" = true ]]; then
echo -e "\033[1;34m→\033[0m $1"
else
echo "→ $1"
fi
}
print_warning() {
if [[ "$COLOR_SUPPORT" = true ]]; then
echo -e "\033[1;33m⚠\033[0m $1"
else
echo "⚠ $1"
fi
}
print_error() {
if [[ "$COLOR_SUPPORT" = true ]]; then
echo -e "\033[1;31m✗ Error:\033[0m $1" >&2
else
echo "✗ Error: $1" >&2
fi
}
# ============================================================================
# Help and Usage
# ============================================================================
show_help() {
cat << EOF
NovaLLM Build Script
Usage: $0 [options]
Build Configuration:
-r, --release Build in Release mode (default)
-d, --debug Build in Debug mode
-l, --logging Enable logging (default: ON)
--no-logging Disable logging
Build Targets:
-m, --main Build main project (default)
-t, --tests Build and run tests
-s, --standalone Build standalone application
-p, --package Create Conan package
-a, --all Build everything (main + tests + standalone + package)
Build Options:
-c, --clean Clean all build-* and install-* directories (including custom
directories specified via --build-dir/--install-dir) before building
--build-dir DIR Set custom build directory (default: build)
--install-dir DIR Set custom install directory (default: install)
Other Options:
-h, --help Show this help message
-v, --verbose Enable verbose output
Examples:
$0 # Build main project in Release mode
$0 -d -t # Build and run tests in Debug mode
$0 -r -a # Build everything in Release mode
$0 -c -r -m # Clean build main project in Release mode
EOF
}
# ============================================================================
# Command Line Argument Parsing
# ============================================================================
VERBOSE=false
parse_args() {
# Reset target flags if any target is explicitly specified
local targets_specified=false
local main_explicitly_specified=false
# First pass: check if any targets are specified
for arg in "$@"; do
case $arg in
-m|--main)
main_explicitly_specified=true
;;
-t|--tests|-s|--standalone|-p|--package|-a|--all)
targets_specified=true
;;
esac
done
# If targets are specified but main is not explicitly requested, disable default main build
# We'll check package freshness later to decide if main needs to be built
if [[ "$targets_specified" = true && "$main_explicitly_specified" = false ]]; then
BUILD_MAIN=false
fi
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-r|--release)
BUILD_TYPE="Release"
shift
;;
-d|--debug)
BUILD_TYPE="Debug"
shift
;;
-l|--logging)
ENABLE_LOGGING="ON"
shift
;;
--no-logging)
ENABLE_LOGGING="OFF"
shift
;;
-m|--main)
BUILD_MAIN=true
shift
;;
-t|--tests)
BUILD_TESTS=true
shift
;;
-s|--standalone)
BUILD_STANDALONE=true
shift
;;
-p|--package)
CREATE_PACKAGE=true
shift
;;
-a|--all)
BUILD_MAIN=true
BUILD_TESTS=true
BUILD_STANDALONE=true
CREATE_PACKAGE=true
shift
;;
-c|--clean)
CLEAN_BUILD=true
shift
;;
--build-dir)
BUILD_DIR="$2"
shift 2
;;
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Adjust build directory for Debug builds
if [[ "$BUILD_TYPE" == "Debug" ]] && [[ "$BUILD_DIR" == "build" ]]; then
BUILD_DIR="build-debug"
INSTALL_DIR="install-debug"
fi
}
# ============================================================================
# Dependency Checks
# ============================================================================
check_dependencies() {
print_header "Checking dependencies"
local missing_deps=()
# Check for required commands
if ! command -v cmake &> /dev/null; then
missing_deps+=("cmake")
else
print_info "CMake: $(cmake --version | head -n1)"
fi
if ! command -v conan &> /dev/null; then
missing_deps+=("conan")
else
print_info "Conan: $(conan --version)"
fi
if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
missing_deps+=("python")
fi
# Report missing dependencies
if [[ ${#missing_deps[@]} -gt 0 ]]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
echo
echo "Please install the missing dependencies:"
for dep in "${missing_deps[@]}"; do
case $dep in
cmake)
echo " - CMake: https://cmake.org/download/"
;;
conan)
echo " - Conan: pip install conan"
;;
python)
echo " - Python: https://www.python.org/downloads/"
;;
esac
done
exit 1
fi
print_success "All dependencies satisfied"
}
# ============================================================================
# Conan Setup
# ============================================================================
setup_conan() {
print_header "Setting up Conan"
# Detect Conan profile if it doesn't exist
if ! conan profile show default &> /dev/null; then
print_info "Detecting Conan profile..."
conan profile detect --force
else
print_info "Using existing Conan profile"
fi
}
# ============================================================================
# Package Detection Functions
# ============================================================================
check_package_exists() {
local package_ref="novallm/0.1.0@local/testing"
if conan list "$package_ref" 2>/dev/null | grep -q "$package_ref"; then
return 0 # Package exists
else
return 1 # Package doesn't exist
fi
}
get_package_timestamp() {
local package_ref="novallm/0.1.0@local/testing"
# Get the package folder path from conan cache
local cache_info
cache_info=$(conan cache path "$package_ref" 2>/dev/null)
if [[ -z "$cache_info" ]]; then
echo "0" # Package doesn't exist
return
fi
# Get the most recent modification time in the package
local package_time
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
package_time=$(stat -f "%m" "$cache_info" 2>/dev/null || echo "0")
else
# Linux
package_time=$(stat -c "%Y" "$cache_info" 2>/dev/null || echo "0")
fi
echo "$package_time"
}
get_source_timestamp() {
# Get the most recent modification time of source files
local newest_time=0
local file
# Check source, include, and CMakeLists.txt files
while IFS= read -r -d '' file; do
local file_time
if [[ "$(uname)" == "Darwin" ]]; then
file_time=$(stat -f "%m" "$file" 2>/dev/null || echo "0")
else
file_time=$(stat -c "%Y" "$file" 2>/dev/null || echo "0")
fi
if [[ $file_time -gt $newest_time ]]; then
newest_time=$file_time
fi
done < <(find source include CMakeLists.txt conanfile.py -type f -print0 2>/dev/null)
echo "$newest_time"
}
is_package_outdated() {
if ! check_package_exists; then
print_info "Package not found in cache"
return 0 # Package doesn't exist, needs rebuild
fi
local package_time
local source_time
package_time=$(get_package_timestamp)
source_time=$(get_source_timestamp)
if [[ $source_time -gt $package_time ]]; then
print_info "Source code is newer than cached package"
return 0 # Source is newer, needs rebuild
else
print_info "Cached package is up-to-date"
return 1 # Package is up-to-date
fi
}
# ============================================================================
# Build Functions
# ============================================================================
clean_build_dirs() {
print_header "Cleaning build directories"
local cleaned=false
local dirs_to_clean=()
# First, add user-specified directories if they exist
if [[ -n "$BUILD_DIR" && -d "$BUILD_DIR" ]]; then
dirs_to_clean+=("$BUILD_DIR")
fi
if [[ -n "$INSTALL_DIR" && -d "$INSTALL_DIR" ]]; then
dirs_to_clean+=("$INSTALL_DIR")
fi
# Then, find all build and install directories
shopt -s nullglob # Don't include pattern if no match
for dir in build build-* build_* install install-* install_*; do
if [[ -d "$dir" ]]; then
# Avoid duplicates
local already_added=false
for added_dir in "${dirs_to_clean[@]}"; do
if [[ "$dir" == "$added_dir" ]]; then
already_added=true
break
fi
done
if [[ "$already_added" == false ]]; then
dirs_to_clean+=("$dir")
fi
fi
done
shopt -u nullglob
# Remove all collected directories
for dir in "${dirs_to_clean[@]}"; do
print_info "Removing $dir"
rm -rf "$dir"
cleaned=true
done
if [[ "$cleaned" == false ]]; then
print_info "No build or install directories to clean"
fi
print_success "Clean complete"
}
install_dependencies() {
local target_dir="$1"
local build_tests="$2"
print_info "Installing Conan dependencies..."
local conan_opts=(
--output-folder=.
-s build_type="$BUILD_TYPE"
)
if [[ "$build_tests" == "True" ]]; then
conan_opts+=(-o build_tests=True)
fi
if [[ "$CONAN_BUILD_MISSING" == true ]]; then
conan_opts+=("--build=missing")
fi
if [[ "$VERBOSE" == true ]]; then
conan install .. "${conan_opts[@]}"
else
conan install .. "${conan_opts[@]}" > /dev/null
fi
}
find_toolchain_file() {
local toolchain_file
toolchain_file=$(find "$(pwd)" -name "conan_toolchain.cmake" -type f | head -1)
if [[ -z "$toolchain_file" ]]; then
print_error "Could not find conan_toolchain.cmake"
exit 1
fi
echo "$toolchain_file"
}
build_main_project() {
print_header "Building main project ($BUILD_TYPE)"
# Create build directory
mkdir -p "$BUILD_DIR" "$INSTALL_DIR"
cd "$BUILD_DIR"
# Install dependencies
local build_tests="False"
if [[ "$BUILD_TESTS" == true ]]; then
build_tests="True"
fi
install_dependencies "." "$build_tests"
# Find toolchain file
local toolchain_file
toolchain_file=$(find_toolchain_file)
print_info "Using toolchain: $toolchain_file"
# Configure
print_info "Configuring CMake..."
cmake -S .. -B . \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
-DNOVA_LLM_ENABLE_LOGGING="$ENABLE_LOGGING" \
-DCMAKE_INSTALL_PREFIX="../$INSTALL_DIR" \
-DCMAKE_TOOLCHAIN_FILE="$toolchain_file"
# Build
print_info "Building..."
cmake --build . --config "$BUILD_TYPE" ${VERBOSE:+--verbose}
# Install
print_info "Installing..."
cmake --install . --config "$BUILD_TYPE"
cd ..
print_success "Main project build complete"
}
build_tests() {
print_header "Building and running tests (Debug)"
local test_build_dir="build-test"
# Create build directory
mkdir -p "$test_build_dir"
cd "$test_build_dir"
# Install dependencies
print_info "Installing test dependencies..."
mkdir -p conan
if [[ "$VERBOSE" == true ]]; then
conan install ../test --output-folder=conan --build=missing -s build_type=Debug
else
conan install ../test --output-folder=conan --build=missing -s build_type=Debug > /dev/null
fi
# Find toolchain file
local toolchain_file
toolchain_file=$(find_toolchain_file)
print_info "Using toolchain: $toolchain_file"
# Configure
print_info "Configuring test CMake..."
cmake -S ../test -B . \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_TOOLCHAIN_FILE="$toolchain_file"
# Build
print_info "Building tests..."
cmake --build . --config Debug ${VERBOSE:+--verbose}
# Run tests if available
if [[ -f ./CTestTestfile.cmake ]]; then
print_info "Running tests..."
ctest --output-on-failure || print_warning "Some tests failed"
else
print_warning "No CTest configuration found, skipping test execution"
fi
cd ..
print_success "Test build complete"
}
build_standalone() {
print_header "Building standalone application ($BUILD_TYPE)"
local standalone_build_dir="build-standalone"
# Create build directory
mkdir -p "$standalone_build_dir"
cd "$standalone_build_dir"
# Install dependencies
print_info "Installing standalone dependencies..."
mkdir -p conan
if [[ "$VERBOSE" == true ]]; then
conan install ../standalone --output-folder=conan --build=missing -s build_type="$BUILD_TYPE"
else
conan install ../standalone --output-folder=conan --build=missing -s build_type="$BUILD_TYPE" > /dev/null
fi
# Find toolchain file
local toolchain_file
toolchain_file=$(find_toolchain_file)
print_info "Using toolchain: $toolchain_file"
# Configure
print_info "Configuring standalone CMake..."
cmake -S ../standalone -B . \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
-DCMAKE_TOOLCHAIN_FILE="$toolchain_file"
# Build
print_info "Building standalone..."
cmake --build . --config "$BUILD_TYPE" ${VERBOSE:+--verbose}
cd ..
print_success "Standalone build complete"
}
create_conan_package() {
print_header "Creating Conan package ($BUILD_TYPE)"
print_info "Running conan create..."
if [[ "$VERBOSE" == true ]]; then
conan create . --user=local --channel=testing --build=missing -s build_type="$BUILD_TYPE"
else
conan create . --user=local --channel=testing --build=missing -s build_type="$BUILD_TYPE" > /dev/null
fi
print_success "Conan package created"
}
# ============================================================================
# Build Summary
# ============================================================================
print_build_summary() {
print_header "Build Configuration Summary"
echo " Build Type: $BUILD_TYPE"
echo " Logging: $ENABLE_LOGGING"
echo " Build Directory: $BUILD_DIR"
echo " Install Dir: $INSTALL_DIR"
echo
echo " Build Targets:"
[[ "$BUILD_MAIN" == true ]] && echo " ✓ Main project"
[[ "$BUILD_TESTS" == true ]] && echo " ✓ Tests"
[[ "$BUILD_STANDALONE" == true ]] && echo " ✓ Standalone"
[[ "$CREATE_PACKAGE" == true ]] && echo " ✓ Conan package"
echo
}
# ============================================================================
# Main Execution
# ============================================================================
main() {
# Parse command line arguments
parse_args "$@"
# Print summary
print_build_summary
# Check dependencies
check_dependencies
# Setup Conan
setup_conan
# Clean if requested
if [[ "$CLEAN_BUILD" == true ]]; then
clean_build_dirs
fi
# Execute build targets
# If tests or standalone are requested but main wasn't explicitly requested,
# check if we need to rebuild main based on package freshness
local need_package=false
if [[ "$BUILD_TESTS" == true || "$BUILD_STANDALONE" == true ]]; then
need_package=true
if [[ "$BUILD_MAIN" == false ]]; then
print_header "Checking package freshness"
if is_package_outdated; then
print_info "Enabling main project build due to outdated/missing package"
BUILD_MAIN=true
fi
fi
fi
if [[ "$BUILD_MAIN" == true ]]; then
build_main_project
# Auto-create package if tests or standalone need it
if [[ "$need_package" == true || "$CREATE_PACKAGE" == true ]]; then
print_info "Creating/updating Conan package for downstream consumers..."
CREATE_PACKAGE=true
fi
fi
if [[ "$CREATE_PACKAGE" == true ]]; then
create_conan_package
fi
if [[ "$BUILD_TESTS" == true ]]; then
build_tests
fi
if [[ "$BUILD_STANDALONE" == true ]]; then
build_standalone
fi
# Final success message
echo
print_success "All builds completed successfully! 🎉"
echo
}
# Run main function with all arguments
main "$@"