-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·244 lines (211 loc) · 6.42 KB
/
build.sh
File metadata and controls
executable file
·244 lines (211 loc) · 6.42 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
#!/bin/bash
# Stop the script if any command fails
set -e
print_help() {
echo "Builds the project, runs tests, or cleans build files."
echo ""
echo "Usage: $0 [target] [options] | [command]"
echo ""
echo "Targets (defaults to 'debug' if unspecified but flags are present):"
echo " debug Builds with debug information, no optimizations."
echo " release Builds for production (optimized, no debug info)."
echo " relwithdebinfo Builds an optimized build with debug info."
echo " minsizerel Builds the smallest possible release."
echo ""
echo "Options:"
echo " --asan Enables AddressSanitizer."
echo " --test Runs the unit tests after building."
echo " --clean Cleans the target before building."
echo " --rm Deletes 'build/' dir before building."
echo " --help, -h Print help and exit."
echo ""
echo "Commands:"
echo " clean Cleans ALL targets."
echo " rm Deletes 'build/' dir."
echo ""
echo "Examples:"
echo " $0 --test --asan Build debug with ASan and run tests"
echo " $0 release --clean Rebuild (Clean + Build) release"
echo " $0 rm Wipe all builds"
}
deep_clean_project() {
echo "--- DEEP Cleaning project (Removing 'build/' directory) ---"
if [ -d "build" ]; then
rm -rf build
echo "✅ Deep Cleanup complete. 'build/' directory removed."
else
echo "ℹ️ 'build/' directory not found. Nothing to deep-clean."
fi
}
soft_clean_project() {
echo "--- SOFT Cleaning project (Removing compilation artifacts for ALL targets) ---"
local BUILDS_CLEANED=0
for BUILD_TYPE_LOWER in "release" "debug" "relwithdebinfo" "minsizerel"; do
local BUILD_DIR="build/${BUILD_TYPE_LOWER}"
if [ -d "${BUILD_DIR}" ]; then
echo "-> Cleaning ${BUILD_TYPE_LOWER} configuration in ${BUILD_DIR}"
cmake --build "${BUILD_DIR}" --target clean
BUILDS_CLEANED=1
fi
done
if [ "$BUILDS_CLEANED" -eq 0 ]; then
echo "ℹ️ No build configurations found in 'build/'. Run a build first or use 'rm'."
else
echo "✅ Soft Cleanup complete."
fi
}
clean_one_target() {
local BUILD_TYPE_LOWER="$1"
local BUILD_DIR="build/${BUILD_TYPE_LOWER}"
if [ -d "${BUILD_DIR}" ]; then
echo "--- Cleaning ${BUILD_TYPE_LOWER} target ---"
cmake --build "${BUILD_DIR}" --target clean
echo "✅ Cleaned ${BUILD_TYPE_LOWER}."
else
echo "ℹ️ Build directory '${BUILD_DIR}' does not exist. Skipping clean step."
fi
}
build_one_config() {
local BUILD_TYPE_LOWER="$1"
local BUILD_TYPE_CAMEL="$2"
local BUILD_DIR="build/${BUILD_TYPE_LOWER}"
echo "--- Building for ${BUILD_TYPE_CAMEL} ---"
local SANITIZE_FLAGS=""
local LINKER_FLAGS=""
if [ "$ENABLE_ASAN" = true ]; then
echo "💉 Injecting AddressSanitizer flags..."
SANITIZE_FLAGS="-fsanitize=address -fno-omit-frame-pointer"
LINKER_FLAGS="-fsanitize=address"
else
SANITIZE_FLAGS=""
LINKER_FLAGS=""
fi
local CMAKE_CMD=(
cmake -S . -B "${BUILD_DIR}"
-D CMAKE_BUILD_TYPE="${BUILD_TYPE_CAMEL}"
"-DCMAKE_CXX_FLAGS=${SANITIZE_FLAGS}"
"-DCMAKE_C_FLAGS=${SANITIZE_FLAGS}"
"-DCMAKE_EXE_LINKER_FLAGS=${LINKER_FLAGS}"
"-DCMAKE_MODULE_LINKER_FLAGS=${LINKER_FLAGS}"
"-DCMAKE_SHARED_LINKER_FLAGS=${LINKER_FLAGS}"
)
# Configure
"${CMAKE_CMD[@]}"
# Build
cmake --build "${BUILD_DIR}" -- -j8
echo "✅ ${BUILD_TYPE_CAMEL} build complete."
}
run_tests() {
local BUILD_TYPE_LOWER="$1"
local TEST_DIR="build/${BUILD_TYPE_LOWER}/tests"
echo "--- Running Tests for ${BUILD_TYPE_LOWER} ---"
if [ ! -d "${TEST_DIR}" ]; then
echo "❌ Test directory not found: ${TEST_DIR}"
exit 1
fi
# Find the executable that identifies as a Boost.Test module
local FOUND_BIN=""
for f in "${TEST_DIR}"/*; do
if [ -f "$f" ] && [ -x "$f" ] && [[ "$f" != *.sh ]]; then
if "$f" --help 2>&1 | grep -q "Boost.Test"; then
FOUND_BIN="$f"
break
fi
fi
done
if [ -n "${FOUND_BIN}" ]; then
echo "✅ Found Boost.Test binary: ${FOUND_BIN}"
if [ "$ENABLE_ASAN" = true ]; then
echo "👻 Running with AddressSanitizer enabled..."
fi
"${FOUND_BIN}" --log_level=test_suite --color_output=yes
else
echo "❌ No Boost.Test executable found in: ${TEST_DIR}"
echo " Make sure the build succeeded."
exit 1
fi
}
if [ "$#" -eq 0 ]; then
print_help
exit 0
fi
TARGET="debug"
CAMEL_TARGET="Debug"
ACTION="build"
RUN_TESTS=false
DO_CLEAN=false
ENABLE_ASAN=false
DO_DEEP_CLEAN=false
for arg in "$@"; do
LOWER_ARG=$(echo "$arg" | tr '[:upper:]' '[:lower:]')
case "$LOWER_ARG" in
--help|-h)
print_help
exit 0
;;
--test)
RUN_TESTS=true
;;
--clean)
DO_CLEAN=true
;;
--asan)
ENABLE_ASAN=true
;;
--rm)
DO_DEEP_CLEAN=true
;;
clean)
ACTION="soft_clean_all"
;;
rm)
ACTION="deep_clean"
;;
release)
TARGET="release"
CAMEL_TARGET="Release"
;;
debug)
TARGET="debug"
CAMEL_TARGET="Debug"
;;
relwithdebinfo)
TARGET="relwithdebinfo"
CAMEL_TARGET="RelWithDebInfo"
;;
minsizerel)
TARGET="minsizerel"
CAMEL_TARGET="MinSizeRel"
;;
*)
print_help
echo ""
echo "❌ Error: Invalid argument '$arg'." >&2
exit 1
;;
esac
done
case "$ACTION" in
deep_clean)
deep_clean_project
exit 0
;;
soft_clean_all)
soft_clean_project
exit 0
;;
build)
if [ "$DO_DEEP_CLEAN" = true ]; then
deep_clean_project
fi
if [ "$DO_CLEAN" = true ]; then
clean_one_target "$TARGET"
fi
build_one_config "$TARGET" "$CAMEL_TARGET"
if [ "$RUN_TESTS" = true ]; then
run_tests "$TARGET"
fi
;;
esac
echo ""
echo "Done."