Repository navigation
Expand file tree
/
Copy pathbuild_make.sh
More file actions
executable file
·293 lines (263 loc) · 8.98 KB
/
Copy pathbuild_make.sh
File metadata and controls
executable file
·293 lines (263 loc) · 8.98 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
#!/usr/bin/env bash
#
# Generic Poco component build / test runner.
#
# Usage:
# ./build_make.sh [release|debug|all] [asan|ubsan|tsan] [test] [rebuild] [<component-path>]
#
# Defaults:
# - mode = all (release + debug, shared)
# - sanitizer = none
# - test = off (don't build or run testsuite)
# - rebuild = off (incremental)
# - component = none -> build EVERY discovered component, in dep order
#
# When a component path is given, only that component plus its transitive deps
# (derived from each Makefile's target_libs) are built. When 'test' is passed,
# the corresponding testsuite directory is also walked (which pulls CppUnit
# into the graph through the testsuite's own target_libs) and its testrunner
# is executed at the end.
#
# Examples:
# ./build_make.sh # build every component
# ./build_make.sh test # build every component + run every testsuite
# ./build_make.sh release Data/SQLite test # release-only Data/SQLite + deps + run its tests
# ./build_make.sh Foundation # just Foundation
# ./build_make.sh debug asan Util test rebuild
#
# Configures the make build for $POCO_CONFIG (default Linux-clang-c++20) on
# the first run. Sources poco_env.bash for POCO_BASE / OSNAME / OSARCH /
# LD_LIBRARY_PATH. Pass 'rebuild' to make clean before building (use after
# switching sanitizers).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# poco_env.bash references LD_LIBRARY_PATH/DYLD_LIBRARY_PATH, which may be unset;
# source it before enabling 'set -u' so the unset reference doesn't abort.
# shellcheck disable=SC1091
. "$SCRIPT_DIR/poco_env.bash" >/dev/null
set -euo pipefail
POCO_CONFIG="${POCO_CONFIG:-Linux-clang-c++20}"
OMIT_DEFAULT=Data/ODBC,Data/MySQL,Data/PostgreSQL,MongoDB,Redis,PDF,DNSSD,DNSSD/Avahi,DNSSD/Bonjour,CppParser
MODE=all
SANITIZER=
RUN_TESTS=0
REBUILD=0
COMPONENT=
usage()
{
sed -n '2,/^$/{s/^# \{0,1\}//;p}' "$0"
}
for arg in "$@"; do
case "$arg" in
release|debug|all) MODE=$arg ;;
asan|ubsan|tsan) SANITIZER=$arg ;;
test) RUN_TESTS=1 ;;
rebuild) REBUILD=1 ;;
-h|--help) usage; exit 0 ;;
*)
if [[ -d "$POCO_BASE/$arg" && -f "$POCO_BASE/$arg/Makefile" ]]; then
COMPONENT=$arg
else
echo "error: '$arg' is neither a known flag nor a component dir" >&2
echo "" >&2
usage >&2
exit 1
fi
;;
esac
done
SAN_CFLAGS=
SAN_LDFLAGS=
case "$SANITIZER" in
asan) SAN_CFLAGS="-fsanitize=address -fno-omit-frame-pointer"; SAN_LDFLAGS="-fsanitize=address" ;;
ubsan) SAN_CFLAGS="-fsanitize=undefined -fno-omit-frame-pointer"; SAN_LDFLAGS="-fsanitize=undefined" ;;
tsan) SAN_CFLAGS="-fsanitize=thread -fno-omit-frame-pointer"; SAN_LDFLAGS="-fsanitize=thread" ;;
esac
case "$MODE" in
all) MAKE_TARGETS=() ;;
release) MAKE_TARGETS=(shared_release) ;;
debug) MAKE_TARGETS=(shared_debug) ;;
esac
cd "$POCO_BASE"
if [[ ! -f config.make ]] || ! grep -qE "^POCO_CONFIG[[:space:]]*=[[:space:]]*${POCO_CONFIG}[[:space:]]*$" config.make; then
echo "==> configure --config=$POCO_CONFIG"
./configure --config="$POCO_CONFIG" --omit="$OMIT_DEFAULT"
fi
JOBS=$(nproc 2>/dev/null || echo 4)
MAKE_OPTS=(-s -j"$JOBS")
MAKE_ENV=(POCO_BASE="$POCO_BASE" POCO_BUILD="$POCO_BASE" POCO_CONFIG="$POCO_CONFIG")
if [[ -n "$SAN_CFLAGS" ]]; then
MAKE_ENV+=(POCO_FLAGS="$SAN_CFLAGS" POCO_LDFLAGS="$SAN_LDFLAGS")
fi
# Cheap textual extractor for Makefile assignments. Spawning a fresh `make`
# per component to evaluate $(target) / $(target_libs) is correct but adds
# seconds to startup; POCO's Makefiles are simple enough that grep+sed gets
# the same answer in milliseconds. Handles `var = ...` and `var += ...`,
# both top-level and inside conditional blocks (conditionals are treated as
# always-active, which matches the ifndef-default for POCO's flags and at
# worst over-includes a dep - never miscalculates the build order).
parse_target()
{
# `|| true` so set -e + pipefail don't abort on a Makefile that has no
# `target =` line (grep returns 1 on no match).
{ grep -E "^[[:space:]]*target[[:space:]]*=" "$1" 2>/dev/null || true; } \
| head -1 \
| sed 's/^[^=]*=[[:space:]]*//' \
| awk '{print $1}'
}
parse_target_libs()
{
{ grep -E "^[[:space:]]*target_libs[[:space:]]*[+]?=" "$1" 2>/dev/null || true; } \
| sed 's/^[^=]*=[[:space:]]*//; s/\\$//' \
| tr '\n' ' '
}
# 1. Component discovery: find every component Makefile and build the
# library-name -> directory index, caching target_libs at the same time.
declare -A LIB_TO_DIR
declare -A DEPS_OF
declare -a ALL_DIRS=()
while IFS= read -r mk; do
rel="${mk#"$POCO_BASE"/}"
dir="${rel%/Makefile}"
case "$dir" in
*/testsuite|*/testsuite/*|*/samples|*/samples/*|build|build/*|dependencies|dependencies/*) continue ;;
esac
target=$(parse_target "$mk")
[[ -z "$target" ]] && continue
LIB_TO_DIR[$target]=$dir
DEPS_OF[$dir]=$(parse_target_libs "$mk")
ALL_DIRS+=("$dir")
done < <(find "$POCO_BASE" -maxdepth 3 -name Makefile -type f 2>/dev/null)
# 2. Honor OMIT from config.make (entries are top-level dirs).
declare -A OMITTED
if [[ -f config.make ]]; then
omit_line=$({ grep -E "^OMIT[[:space:]]*=" config.make 2>/dev/null || true; } \
| head -1 | sed 's/^OMIT[[:space:]]*=[[:space:]]*//')
for e in $omit_line; do OMITTED[$e]=1; done
fi
# 3. Topological walk via DFS.
declare -A VISITED
declare -a ORDER=()
declare -a TEST_DIRS=() # component dirs whose testsuite was added
walk()
{
local dir=$1
[[ -n "${VISITED[$dir]:-}" ]] && return
[[ -n "${OMITTED[$dir]:-}" ]] && return
[[ ! -f "$POCO_BASE/$dir/Makefile" ]] && return
VISITED[$dir]=1
# Cache miss happens for testsuite dirs (skipped by discovery scan).
if [[ -z "${DEPS_OF[$dir]+x}" ]]; then
DEPS_OF[$dir]=$(parse_target_libs "$POCO_BASE/$dir/Makefile")
fi
for lib in ${DEPS_OF[$dir]}; do
local depdir="${LIB_TO_DIR[$lib]:-}"
[[ -n "$depdir" ]] && walk "$depdir"
done
ORDER+=("$dir")
}
if [[ -n "$COMPONENT" ]]; then
walk "$COMPONENT"
else
for d in "${ALL_DIRS[@]}"; do walk "$d"; done
fi
# 4. Testsuites. Every testsuite needs CppUnit, so pull it in unconditionally
# before walking any testsuite - some testsuite Makefiles (e.g.
# Foundation/testsuite) delegate to sub-makefiles and don't list CppUnit in
# their own target_libs, so the walker would otherwise place them in ORDER
# ahead of CppUnit and the link step would fail.
#
# Test scope follows the build scope: when a specific component was requested,
# only that component's testsuite is built and run; deps are built as
# libraries only. When no component was specified (build-everything mode),
# every component's testsuite is built and run.
if (( RUN_TESTS )); then
cppunit_dir="${LIB_TO_DIR[CppUnit]:-}"
if [[ -z "$cppunit_dir" ]]; then
echo "error: 'test' was requested but CppUnit component was not discovered" >&2
exit 1
fi
walk "$cppunit_dir"
if [[ -n "$COMPONENT" ]]; then
ts="$COMPONENT/testsuite"
if [[ -f "$POCO_BASE/$ts/Makefile" ]]; then
walk "$ts"
TEST_DIRS+=("$COMPONENT")
else
echo "==> note: $COMPONENT has no testsuite, nothing to test"
fi
else
component_order=("${ORDER[@]}")
for d in "${component_order[@]}"; do
[[ "$d" == "$cppunit_dir" ]] && continue
ts="$d/testsuite"
if [[ -f "$POCO_BASE/$ts/Makefile" ]]; then
walk "$ts"
TEST_DIRS+=("$d")
fi
done
fi
fi
if (( ${#ORDER[@]} == 0 )); then
echo "error: nothing to build (no components resolved)" >&2
exit 1
fi
# 5. Optional clean.
if (( REBUILD )); then
echo "==> cleaning"
for d in "${ORDER[@]}"; do
make "${MAKE_OPTS[@]}" -C "$POCO_BASE/$d" "${MAKE_ENV[@]}" clean
done
fi
# 6. Build.
for d in "${ORDER[@]}"; do
label="$d ($MODE${SANITIZER:+ + $SANITIZER})"
echo "==> $label"
make "${MAKE_OPTS[@]}" -C "$POCO_BASE/$d" "${MAKE_ENV[@]}" "${MAKE_TARGETS[@]}"
done
# 7. Run testsuites.
if (( RUN_TESTS )); then
if [[ "$MODE" == release ]]; then
RUNNER_NAME=testrunner
else
RUNNER_NAME=testrunnerd
fi
case "$SANITIZER" in
asan)
export ASAN_OPTIONS="${ASAN_OPTIONS:-detect_leaks=1:print_stacktrace=1:abort_on_error=0:halt_on_error=0}"
echo "==> ASAN_OPTIONS=$ASAN_OPTIONS"
;;
ubsan)
export UBSAN_OPTIONS="${UBSAN_OPTIONS:-print_stacktrace=1:halt_on_error=1}"
echo "==> UBSAN_OPTIONS=$UBSAN_OPTIONS"
;;
tsan)
export TSAN_OPTIONS="${TSAN_OPTIONS:-halt_on_error=1:second_deadlock_stack=1}"
echo "==> TSAN_OPTIONS=$TSAN_OPTIONS"
;;
esac
failed=()
skipped=()
for d in "${TEST_DIRS[@]}"; do
runner="$POCO_BASE/$d/testsuite/bin/$OSNAME/$OSARCH/$RUNNER_NAME"
if [[ ! -x "$runner" ]]; then
runner=$(find "$POCO_BASE/$d/testsuite/bin" -type f -name "$RUNNER_NAME" 2>/dev/null | head -1)
fi
if [[ -z "$runner" || ! -x "$runner" ]]; then
echo "==> skipping $d (no $RUNNER_NAME under $d/testsuite/bin)"
skipped+=("$d")
continue
fi
echo "==> testing $d"
if ! "$runner" -all; then
failed+=("$d")
fi
done
if (( ${#failed[@]} > 0 )); then
echo "==> FAILED testsuites: ${failed[*]}" >&2
exit 1
fi
if (( ${#skipped[@]} > 0 )); then
echo "==> skipped testsuites (no runner found): ${skipped[*]}"
fi
fi
echo "==> done"