-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
591 lines (510 loc) · 15.8 KB
/
build.sh
File metadata and controls
591 lines (510 loc) · 15.8 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
#!/bin/bash
# CFD Project Build Script
# Provides various build, clean, and test operations
set -e # Exit on error
PROJECT_NAME="CFD Framework"
BUILD_DIR="build"
CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-Debug}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "\n${BLUE}================================${NC}"
echo -e "${BLUE} $PROJECT_NAME - Build Script${NC}"
echo -e "${BLUE}================================${NC}\n"
}
# Check if we're in the right directory
check_directory() {
if [[ ! -f "CMakeLists.txt" ]]; then
print_error "CMakeLists.txt not found. Please run this script from the project root."
exit 1
fi
}
# Clean build directory
clean() {
print_status "Cleaning build directory..."
if [[ -d "$BUILD_DIR" ]]; then
rm -rf "$BUILD_DIR"
print_success "Build directory cleaned"
else
print_warning "Build directory doesn't exist"
fi
}
# Clean all build and output directories
clean_all() {
print_status "Cleaning all build and output directories..."
local cleaned_count=0
local dirs_to_clean=("build" "build-release" "install" "packages" "output")
for dir in "${dirs_to_clean[@]}"; do
if [[ -d "$dir" ]]; then
print_status "Removing $dir/..."
if rm -rf "$dir" 2>/dev/null; then
cleaned_count=$((cleaned_count + 1))
else
print_warning "Could not remove $dir (may be in use)"
fi
fi
done
# Clean artifacts/output but keep artifacts directory structure
if [[ -d "artifacts/output" ]]; then
# Use ls to check if directory has contents (more portable than find)
if ls artifacts/output/* >/dev/null 2>&1; then
print_status "Cleaning artifacts/output/..."
if rm -rf artifacts/output/* 2>/dev/null; then
cleaned_count=$((cleaned_count + 1))
else
print_warning "Could not clean artifacts/output (may be in use)"
fi
fi
fi
if [[ $cleaned_count -gt 0 ]]; then
print_success "Cleaned $cleaned_count directories"
else
print_warning "No directories to clean"
fi
}
# Clean only output/artifact directories (keep build)
clean_output() {
print_status "Cleaning output directories..."
local cleaned_count=0
if [[ -d "output" ]]; then
print_status "Removing output/..."
if rm -rf "output" 2>/dev/null; then
cleaned_count=$((cleaned_count + 1))
else
print_warning "Could not remove output (may be in use)"
fi
fi
if [[ -d "artifacts/output" ]]; then
# Use ls to check if directory has contents (more portable than find)
if ls artifacts/output/* >/dev/null 2>&1; then
print_status "Cleaning artifacts/output/..."
if rm -rf artifacts/output/* 2>/dev/null; then
cleaned_count=$((cleaned_count + 1))
else
print_warning "Could not clean artifacts/output (may be in use)"
fi
fi
fi
if [[ $cleaned_count -gt 0 ]]; then
print_success "Cleaned $cleaned_count output directories"
else
print_warning "No output directories to clean"
fi
}
# Create build directory
create_build_dir() {
if [[ ! -d "$BUILD_DIR" ]]; then
print_status "Creating build directory..."
mkdir -p "$BUILD_DIR"
fi
}
# Configure CMake
configure() {
print_status "Configuring CMake (${CMAKE_BUILD_TYPE})..."
create_build_dir
cd "$BUILD_DIR"
# Default options
local cmake_options=(
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DBUILD_EXAMPLES=ON"
"-DBUILD_TESTS=OFF"
)
# Add any additional arguments passed to the function
cmake_options+=("$@")
cmake "${cmake_options[@]}" ..
cd ..
print_success "CMake configuration complete"
}
# Build the project
build() {
print_status "Building project..."
create_build_dir
cd "$BUILD_DIR"
cmake --build . --config "$CMAKE_BUILD_TYPE" -j$(nproc 2>/dev/null || echo 4)
cd ..
print_success "Build complete"
}
# Build with tests
build_with_tests() {
print_status "Configuring with tests enabled..."
configure -DBUILD_TESTS=ON
build
}
# Run tests
test() {
print_status "Running tests..."
if [[ ! -d "$BUILD_DIR" ]]; then
print_error "Build directory not found. Run '$0 build-tests' first."
exit 1
fi
cd "$BUILD_DIR"
if [[ -f "CTestTestfile.cmake" ]]; then
ctest -C "$CMAKE_BUILD_TYPE" --output-on-failure
cd ..
print_success "Tests completed"
else
cd ..
print_error "Tests not configured. Run '$0 build-tests' first."
exit 1
fi
}
# Install the project
install() {
print_status "Installing project..."
if [[ ! -d "$BUILD_DIR" ]]; then
print_error "Build directory not found. Run '$0 build' first."
exit 1
fi
cd "$BUILD_DIR"
cmake --install . --config "$CMAKE_BUILD_TYPE"
cd ..
print_success "Installation complete"
}
# Find all C/C++ source files in the project
# Populates the SOURCE_FILES array
find_source_files() {
SOURCE_FILES=()
while IFS= read -r -d '' file; do
SOURCE_FILES+=("$file")
done < <(find lib examples tests \( -name "*.c" -o -name "*.h" \) -print0 2>/dev/null)
}
# Format source code with clang-format
format() {
print_status "Formatting source code..."
if ! command -v clang-format &> /dev/null; then
print_error "clang-format not found. Please install it:"
echo " - Windows: choco install llvm"
echo " - macOS: brew install clang-format"
echo " - Linux: apt install clang-format"
exit 1
fi
find_source_files
if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then
print_warning "No source files found to format"
return
fi
local format_count=0
for file in "${SOURCE_FILES[@]}"; do
clang-format -i "$file"
format_count=$((format_count + 1))
done
print_success "Formatted $format_count file(s)"
}
# Check formatting without modifying files
format_check() {
print_status "Checking code formatting..."
if ! command -v clang-format &> /dev/null; then
print_error "clang-format not found"
exit 1
fi
find_source_files
if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then
print_warning "No source files found to check"
return
fi
local bad_format=0
for file in "${SOURCE_FILES[@]}"; do
if ! clang-format --dry-run --Werror "$file" 2>/dev/null; then
print_warning "Needs formatting: $file"
bad_format=$((bad_format + 1))
fi
done
if [[ $bad_format -gt 0 ]]; then
print_error "$bad_format file(s) need formatting. Run '$0 format' to fix."
exit 1
else
print_success "All files are properly formatted"
fi
}
# Run clang-tidy static analysis
lint() {
print_status "Running static analysis with clang-tidy..."
if ! command -v clang-tidy &> /dev/null; then
print_error "clang-tidy not found. Please install it:"
echo " - Windows: choco install llvm"
echo " - macOS: brew install llvm"
echo " - Linux: apt install clang-tidy"
exit 1
fi
# Need compile_commands.json for clang-tidy
if [[ ! -f "$BUILD_DIR/compile_commands.json" ]]; then
print_status "Generating compile_commands.json..."
configure -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
fi
find_source_files
if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then
print_warning "No source files found to analyze"
return
fi
local lint_count=0
local error_count=0
for file in "${SOURCE_FILES[@]}"; do
# Only lint .c files, not headers
if [[ "$file" == *.c ]]; then
print_status "Checking $file..."
if ! clang-tidy -p "$BUILD_DIR" "$file"; then
error_count=$((error_count + 1))
fi
lint_count=$((lint_count + 1))
fi
done
if [[ $error_count -gt 0 ]]; then
print_warning "Found issues in $error_count of $lint_count file(s)"
else
print_success "All $lint_count file(s) passed static analysis"
fi
}
# Generate API documentation with Doxygen
docs() {
print_status "Generating API documentation..."
if ! command -v doxygen &> /dev/null; then
print_error "doxygen not found. Please install it:"
echo " - Windows: choco install doxygen.install"
echo " - macOS: brew install doxygen"
echo " - Linux: apt install doxygen"
exit 1
fi
if [[ ! -f "Doxyfile" ]]; then
print_error "Doxyfile not found in project root"
exit 1
fi
doxygen Doxyfile
if [[ -d "docs/html" ]]; then
print_success "Documentation generated in docs/html/"
echo "Open docs/html/index.html in a browser to view"
else
print_error "Documentation generation failed"
exit 1
fi
}
# Run examples
run_examples() {
print_status "Running examples..."
local build_path
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
build_path="$BUILD_DIR/$CMAKE_BUILD_TYPE"
else
build_path="$BUILD_DIR"
fi
if [[ ! -d "$build_path" ]]; then
print_error "Build directory not found. Run '$0 build' first."
exit 1
fi
# Auto-discover examples from the examples/ directory
local example_files=()
if [[ -d "examples" ]]; then
while IFS= read -r -d '' file; do
local basename=$(basename "$file" .c)
example_files+=("$basename")
done < <(find examples -maxdepth 1 -name "*.c" -print0 2>/dev/null | sort -z)
fi
if [[ ${#example_files[@]} -eq 0 ]]; then
print_warning "No example files found in examples/ directory"
return
fi
local ran_count=0
local skipped_count=0
for example in "${example_files[@]}"; do
local exe_name="$example"
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
exe_name="${example}.exe"
fi
if [[ -x "$build_path/$exe_name" ]]; then
print_status "Running $example..."
cd "$build_path"
./"$exe_name"
cd - > /dev/null
echo ""
ran_count=$((ran_count + 1))
else
print_warning "Skipping $example (not built or not executable)"
skipped_count=$((skipped_count + 1))
fi
done
if [[ $ran_count -gt 0 ]]; then
print_success "Ran $ran_count example(s)"
fi
if [[ $skipped_count -gt 0 ]]; then
print_warning "Skipped $skipped_count example(s)"
fi
}
# Package the project
package() {
print_status "Creating package..."
if [[ ! -d "$BUILD_DIR" ]]; then
print_error "Build directory not found. Run '$0 build' first."
exit 1
fi
cd "$BUILD_DIR"
cpack
cd ..
print_success "Package created"
}
# Show project status
status() {
print_header
echo "Project Status:"
echo "---------------"
# Check build directory
if [[ -d "$BUILD_DIR" ]]; then
local size=$(du -sh "$BUILD_DIR" 2>/dev/null | cut -f1 || echo "Unknown")
echo "Build directory: ✓ Exists (${size})"
# Check for executables
local exe_count=0
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
exe_count=$(find "$BUILD_DIR" -name "*.exe" 2>/dev/null | wc -l || echo 0)
else
exe_count=$(find "$BUILD_DIR" -type f -perm +111 2>/dev/null | wc -l || echo 0)
fi
echo "Executables: ${exe_count} built"
# Check for libraries
local lib_count=0
lib_count=$(find "$BUILD_DIR" -name "*.lib" -o -name "*.a" 2>/dev/null | wc -l || echo 0)
echo "Libraries: ${lib_count} built"
else
echo "Build directory: ✗ Not found"
fi
# Check output directories
if [[ -d "artifacts/output" ]]; then
local vtk_count=$(find artifacts/output -name "*.vtk" 2>/dev/null | wc -l || echo 0)
echo "VTK files: ${vtk_count} in artifacts/output/"
else
echo "Output directory: ✗ Not found"
fi
if [[ -d "visualization/visualization_output" ]]; then
local viz_count=$(find visualization/visualization_output -type f 2>/dev/null | wc -l || echo 0)
echo "Visualization files: ${viz_count} generated"
else
echo "Visualization output: ✗ Not found"
fi
echo ""
}
# Show help
help() {
print_header
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " clean Clean build directory"
echo " clean-all Clean all build and output directories"
echo " clean-output Clean only output/artifact directories"
echo " configure Configure CMake"
echo " build Build project (library + examples)"
echo " build-tests Build project with tests enabled"
echo " test Run tests"
echo " install Install project"
echo " run Run example programs"
echo " package Create distribution package"
echo " rebuild Clean and build"
echo " full Full clean, configure, build, and test"
echo " format Format source code with clang-format"
echo " format-check Check formatting without modifying files"
echo " lint Run clang-tidy static analysis"
echo " docs Generate API documentation with Doxygen"
echo " status Show project status"
echo " help Show this help message"
echo ""
echo "Environment Variables:"
echo " CMAKE_BUILD_TYPE Build type (Debug|Release) [default: Debug]"
echo ""
echo "Examples:"
echo " $0 build # Build in Debug mode"
echo " CMAKE_BUILD_TYPE=Release $0 build # Build in Release mode"
echo " $0 clean-all # Clean everything (build + output)"
echo " $0 full # Complete clean build with tests"
echo " $0 run # Run all example programs"
echo ""
}
# Main script logic
main() {
check_directory
case "${1:-help}" in
clean)
clean
;;
clean-all)
clean_all
;;
clean-output)
clean_output
;;
configure)
shift
configure "$@"
;;
build)
configure
build
;;
build-tests)
build_with_tests
;;
test)
test
;;
install)
install
;;
run)
run_examples
;;
package)
package
;;
rebuild)
clean
configure
build
;;
full)
print_header
clean
build_with_tests
test
print_success "Full build cycle complete!"
;;
format)
format
;;
format-check)
format_check
;;
lint)
lint
;;
docs)
docs
;;
status)
status
;;
help|--help|-h)
help
;;
*)
print_error "Unknown command: $1"
echo ""
help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"