-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·442 lines (403 loc) · 15.2 KB
/
build.sh
File metadata and controls
executable file
·442 lines (403 loc) · 15.2 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
#!/bin/bash
# Parse command line arguments
BUILD_TYPE="Debug" # Default to Debug build
COMPILER_CHOICE="" # Default to system default compiler (no forcing)
CLEAN_BUILD=false # Whether to clean before building
BUILD_TARGET="examples" # Default to building library + examples (without bpf_demo)
# Function to show usage
show_usage() {
echo "Usage: $0 [build_type] [compiler] [target] [options]"
echo ""
echo "Parameters:"
echo " build_type: debug|release|relwithdebinfo (default: debug)"
echo " compiler: gcc|clang"
echo " target: lib|examples|tests|bpf|all (default: examples)"
echo ""
echo "Build Targets:"
echo " lib: Build only the xcplib library"
echo " examples: Build library + examples (excluding bpf_demo) [DEFAULT]"
echo " tests: Build library + test targets (a2l_test, cal_test, daq_test, type_detection tests)"
echo " bpf: Build library + examples including bpf_demo (Linux only)"
echo " all: Build everything (library + examples + tests + bpf_demo)"
echo ""
echo "Options:"
echo " clean: Clean build directory before building"
echo " cleanall: Clean all build directories (build-*, build)"
echo ""
echo "Examples:"
echo " $0 # Default: library + examples (no bpf_demo)"
echo " $0 lib # Build only the library"
echo " $0 tests # Build library + test targets"
echo " $0 bpf # Build library + examples including bpf_demo"
echo " $0 all # Build everything"
echo " $0 clean # Clean build and rebuild library + examples"
echo " $0 release gcc all # Release build with GCC, all targets"
echo " $0 debug clang tests # Debug build with Clang, tests only"
echo " $0 cleanall # Clean all build directories and exit"
echo ""
echo "Build directories used:"
echo " build: System default compiler builds"
echo " build-gcc: GCC compiler builds"
echo " build-clang: Clang compiler builds"
echo ""
echo "Platform-specific targets:"
echo " bpf_demo: Only built on Linux systems (requires BPF support)"
}
# Parse arguments and set correct case for CMake
for arg in "$@"; do
# Convert to lowercase for comparison
arg_lower=$(echo "$arg" | tr '[:upper:]' '[:lower:]')
case "$arg_lower" in
debug)
BUILD_TYPE="Debug"
;;
release)
BUILD_TYPE="Release"
;;
relwithdebinfo)
BUILD_TYPE="RelWithDebInfo"
;;
gcc)
COMPILER_CHOICE="gcc"
;;
clang)
COMPILER_CHOICE="clang"
;;
default)
COMPILER_CHOICE="default"
;;
lib|library)
BUILD_TARGET="lib"
;;
examples)
BUILD_TARGET="examples"
;;
tests)
BUILD_TARGET="tests"
;;
bpf)
BUILD_TARGET="bpf"
;;
all)
BUILD_TARGET="all"
;;
clean)
CLEAN_BUILD=true
;;
cleanall)
echo "Cleaning all build and test artefacts ..."
rm -rf build build-gcc build-clang
echo "✅ All build directories cleaned"
rm -f *.bin
rm -f *.hex
rm -f *.log
rm -f *.mf4
rm -f *.a2l
echo "✅ All hex and a2l files cleaned"
exit 0
;;
-h|--help|help)
show_usage
exit 0
;;
*)
echo "Error: Unknown parameter '$arg'"
echo ""
show_usage
exit 1
;;
esac
done
# Build CMake compiler arguments based on choice
CMAKE_COMPILER_ARGS=""
COMPILER_NAME=""
BUILD_DIR="build" # Default build directory
case "$COMPILER_CHOICE" in
"gcc")
CMAKE_COMPILER_ARGS="-DUSE_GCC=ON"
COMPILER_NAME="GCC"
BUILD_DIR="build-gcc"
;;
"clang")
CMAKE_COMPILER_ARGS="-DUSE_CLANG=ON"
COMPILER_NAME="Clang"
BUILD_DIR="build-clang"
;;
"default"|"")
CMAKE_COMPILER_ARGS=""
COMPILER_NAME="System Default"
BUILD_DIR="build"
;;
esac
echo "Building in $BUILD_TYPE mode with $COMPILER_NAME compiler"
echo "Build directory: $BUILD_DIR"
echo "Build target: $BUILD_TARGET"
# Detect actual system default compiler when using default option
if [ "$COMPILER_CHOICE" = "" ] || [ "$COMPILER_CHOICE" = "default" ]; then
# Try to detect the actual system compiler
CC_VERSION=$(cc --version 2>/dev/null | head -n1)
CXX_VERSION=$(c++ --version 2>/dev/null | head -n1)
if echo "$CC_VERSION" | grep -q "clang"; then
ACTUAL_COMPILER="Clang"
if echo "$CC_VERSION" | grep -q "Apple"; then
ACTUAL_COMPILER="Apple Clang"
fi
elif echo "$CC_VERSION" | grep -q "gcc"; then
ACTUAL_COMPILER="GCC"
else
ACTUAL_COMPILER="Unknown"
fi
echo "System default compiler detected: $ACTUAL_COMPILER"
fi
# Clean build directory if requested
if [ "$CLEAN_BUILD" = true ]; then
echo "Cleaning build directory: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi
echo ""
echo "==================================================================="
echo "Configuring CMake build system..."
echo "==================================================================="
cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -S . -B $BUILD_DIR $CMAKE_COMPILER_ARGS
echo ""
if [ -f "$BUILD_DIR/CMakeCache.txt" ]; then
echo "Active Build Type: $BUILD_TYPE"
case "$BUILD_TYPE" in
"Debug")
echo "C Flags: $(grep "CMAKE_C_FLAGS_DEBUG:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
echo "C++ Flags: $(grep "CMAKE_CXX_FLAGS_DEBUG:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
;;
"Release")
echo "C Flags: $(grep "CMAKE_C_FLAGS_RELEASE:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
echo "C++ Flags: $(grep "CMAKE_CXX_FLAGS_RELEASE:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
;;
"RelWithDebInfo")
echo "C Flags: $(grep "CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
echo "C++ Flags: $(grep "CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=" $BUILD_DIR/CMakeCache.txt | cut -d= -f2-)"
;;
esac
fi
echo ""
echo ""
echo "==================================================================="
echo "Building targets..."
echo "==================================================================="
# Define all targets to build based on BUILD_TARGET
LIBRARY_TARGET="xcplib"
# Define target groups
EXAMPLE_TARGETS=(
"hello_xcp"
"hello_xcp_cpp"
"c_demo"
"cpp_demo"
"struct_demo"
"multi_thread_demo"
"point_cloud_demo"
"no_a2l_demo"
"ptp_demo"
)
TEST_TARGETS=(
"a2l_test"
"cal_test"
"daq_test"
"type_detection_test_c"
"type_detection_test_cpp"
)
BPF_TARGETS=()
# Add bpf_demo only on Linux systems
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
BPF_TARGETS+=("bpf_demo")
fi
# Determine which targets to build based on BUILD_TARGET
LIBRARY_DEPENDENT_TARGETS=()
INDEPENDENT_TARGETS=()
case "$BUILD_TARGET" in
"lib")
echo "Build target: Library only"
# Only build the library, no other targets
;;
"examples")
echo "Build target: Library + Examples (excluding bpf_demo)"
LIBRARY_DEPENDENT_TARGETS=("${EXAMPLE_TARGETS[@]}")
;;
"tests")
echo "Build target: Library + Tests"
LIBRARY_DEPENDENT_TARGETS=("${TEST_TARGETS[@]}")
;;
"bpf")
echo "Build target: Library + Examples (including bpf_demo)"
LIBRARY_DEPENDENT_TARGETS=("${EXAMPLE_TARGETS[@]}")
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
LIBRARY_DEPENDENT_TARGETS+=("${BPF_TARGETS[@]}")
echo "Linux system detected - bpf_demo will be built with BPF support"
else
echo "Non-Linux system detected - bpf_demo will be skipped (BPF only supported on Linux)"
fi
;;
"all")
echo "Build target: Everything (Library + Examples + Tests + BPF)"
LIBRARY_DEPENDENT_TARGETS=("${EXAMPLE_TARGETS[@]}" "${TEST_TARGETS[@]}")
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
LIBRARY_DEPENDENT_TARGETS+=("${BPF_TARGETS[@]}")
echo "Linux system detected - bpf_demo will be built with BPF support"
else
echo "Non-Linux system detected - bpf_demo will be skipped (BPF only supported on Linux)"
fi
;;
*)
echo "Error: Unknown build target '$BUILD_TARGET'"
show_usage
exit 1
;;
esac
# Arrays to track success/failure
SUCCESSFUL_TARGETS=()
FAILED_TARGETS=()
echo ""
echo "-------------------------------------------------------------------"
echo "Building core library: $LIBRARY_TARGET"
echo "-------------------------------------------------------------------"
# Build the core library first
if make --directory ./$BUILD_DIR $LIBRARY_TARGET > /dev/null 2>&1; then
echo "✅ SUCCESS: $LIBRARY_TARGET compiled successfully"
echo ""
SUCCESSFUL_TARGETS+=("$LIBRARY_TARGET")
# If BUILD_TARGET is "lib", we're done after building the library
if [ "$BUILD_TARGET" = "lib" ]; then
echo "Library-only build completed successfully."
else
# Build BPF program if bpf_demo is in the target list
if [[ " ${LIBRARY_DEPENDENT_TARGETS[@]} " =~ " bpf_demo " ]]; then
echo ""
echo "-------------------------------------------------------------------"
echo "Building BPF program for bpf_demo..."
echo "-------------------------------------------------------------------"
# Check if build_bpf.sh exists
if [ -f "examples/bpf_demo/build_bpf.sh" ]; then
# Make the script executable
chmod +x examples/bpf_demo/build_bpf.sh
# Run the BPF build script
if examples/bpf_demo/build_bpf.sh > /dev/null 2>&1; then
echo "✅ SUCCESS: BPF program compiled successfully"
# Copy BPF object file to the correct build directory
BPF_OBJ_SRC="examples/bpf_demo/src/process_monitor.bpf.o"
if [ -f "$BPF_OBJ_SRC" ]; then
cp "$BPF_OBJ_SRC" "$BUILD_DIR/"
echo " BPF object file copied to $BUILD_DIR/"
else
echo "⚠️ WARNING: BPF object file not found at $BPF_OBJ_SRC"
fi
else
echo "❌ FAILED: BPF program build failed"
echo " BPF build error details:"
examples/bpf_demo/build_bpf.sh 2>&1 | sed 's/^/ /'
fi
else
echo "⚠️ WARNING: BPF build script not found at examples/bpf_demo/build_bpf.sh"
fi
echo ""
fi
# Build library-dependent targets only if library succeeded
for target in "${LIBRARY_DEPENDENT_TARGETS[@]}"; do
if make --directory ./$BUILD_DIR $target > /dev/null 2>&1; then
SUCCESSFUL_TARGETS+=("$target")
else
echo "❌ FAILED: $target compilation failed"
echo " Error details:"
make --directory ./$BUILD_DIR $target 2>&1 | sed 's/^/ /'
FAILED_TARGETS+=("$target")
fi
done
fi
else
echo "❌ CRITICAL FAILURE: $LIBRARY_TARGET compilation failed"
echo " Error details:"
make --directory ./$BUILD_DIR $LIBRARY_TARGET 2>&1 | sed 's/^/ /'
echo ""
echo "🛑 Library build failed, but proceeding with independent targets..."
echo " Library-dependent targets will be skipped."
echo ""
FAILED_TARGETS+=("$LIBRARY_TARGET")
# Note: We don't add library-dependent targets to FAILED_TARGETS
# since they weren't actually attempted
if [ ${#LIBRARY_DEPENDENT_TARGETS[@]} -gt 0 ]; then
echo "📋 SKIPPED TARGETS (due to library failure):"
for target in "${LIBRARY_DEPENDENT_TARGETS[@]}"; do
echo " - $target (skipped - requires xcplib)"
done
echo ""
fi
fi
# Build independent targets regardless of library status (only for certain build targets)
if [ ${#INDEPENDENT_TARGETS[@]} -gt 0 ]; then
for target in "${INDEPENDENT_TARGETS[@]}"; do
if make --directory ./$BUILD_DIR $target > /dev/null 2>&1; then
SUCCESSFUL_TARGETS+=("$target")
else
echo "❌ FAILED: $target compilation failed"
echo " Error details:"
make --directory ./$BUILD_DIR $target 2>&1 | sed 's/^/ /'
FAILED_TARGETS+=("$target")
fi
done
fi
echo ""
echo "==================================================================="
echo "BUILD SUMMARY"
echo "==================================================================="
echo "Build Configuration: $BUILD_TYPE mode with $COMPILER_NAME compiler"
if [ "$COMPILER_CHOICE" = "" ] || [ "$COMPILER_CHOICE" = "default" ]; then
echo "System Compiler: $ACTUAL_COMPILER"
fi
echo "Build Directory: $BUILD_DIR"
echo "Build Target: $BUILD_TARGET"
echo ""
if [ ${#FAILED_TARGETS[@]} -eq 0 ]; then
echo "✅ All targets compiled successfully!"
echo ""
else
echo ""
echo "✅ SUCCESSFUL TARGETS (${#SUCCESSFUL_TARGETS[@]}):"
if [ ${#SUCCESSFUL_TARGETS[@]} -eq 0 ]; then
echo " None"
else
for target in "${SUCCESSFUL_TARGETS[@]}"; do
echo " - $target"
done
fi
echo ""
echo "❌ FAILED TARGETS (${#FAILED_TARGETS[@]}):"
for target in "${FAILED_TARGETS[@]}"; do
if [ "$target" = "xcplib" ]; then
echo " - $target (CRITICAL - core library failure)"
else
echo " - $target"
fi
done
echo ""
if [[ " ${FAILED_TARGETS[@]} " =~ " xcplib " ]]; then
echo "💥 LIBRARY FAILURE: Core library (xcplib) failed to compile."
echo " Library-dependent targets were skipped."
fi
fi
echo ""
echo "==================================================================="
# Exit with error code if any target failed
if [ ${#FAILED_TARGETS[@]} -gt 0 ]; then
if [ "$COMPILER_CHOICE" = "" ] || [ "$COMPILER_CHOICE" = "default" ]; then
echo "Total: ${#SUCCESSFUL_TARGETS[@]} successful, ${#FAILED_TARGETS[@]} failed ($BUILD_TYPE build with $COMPILER_NAME - $ACTUAL_COMPILER)"
else
echo "Total: ${#SUCCESSFUL_TARGETS[@]} successful, ${#FAILED_TARGETS[@]} failed ($BUILD_TYPE build with $COMPILER_NAME)"
fi
echo "==================================================================="
exit 1
else
if [ "$COMPILER_CHOICE" = "" ] || [ "$COMPILER_CHOICE" = "default" ]; then
echo "Build completed successfully: $BUILD_TYPE mode with $COMPILER_NAME compiler ($ACTUAL_COMPILER)"
else
echo "Build completed successfully: $BUILD_TYPE mode with $COMPILER_NAME compiler"
fi
echo "==================================================================="
echo ""
exit 0
fi