Fossil SCM

fossil-scm / tools / testbuild.sh
Blame History Raw 190 lines
1
#!/bin/sh
2
#
3
# Make an out-of-tree build of Fossil for testing, or report on one.
4
#
5
# tools/testbuild.sh asan DIR Build with ASan and UBSan in DIR
6
# tools/testbuild.sh cov DIR Build with Clang coverage in DIR
7
# tools/testbuild.sh report DIR Print branch coverage for a "cov" build
8
# tools/testbuild.sh missed DIR FILE...
9
# List untested branches in source files
10
#
11
# DIR is created if it does not exist. The source tree is not changed.
12
# Extra arguments after DIR are passed to "make" (for example: -j8).
13
#
14
# The "asan" build stops at the first error that a sanitizer reports.
15
#
16
# A "cov" build adds to a few profile files in DIR/prof on every run of
17
# DIR/fossil, as long as DIR does not move. The runs share the files, so
18
# their size does not grow with the number of runs; one file for every
19
# run filled a disk. Run whatever tests are of interest, for example
20
# "make test" in DIR, then use "report". The report has one line per source file,
21
# showing the number of branch outcomes, the number never taken, and the
22
# percentage taken. The files that implement the artifact store and the
23
# sync protocol are listed first. Delete DIR/prof to start over.
24
#
25
# The "missed" command prints every line of the named source files (for
26
# example: delta.c) that holds a branch which was never taken, or never
27
# not taken. A line is left out if test/coverage-marks.txt lists it with
28
# the reason why it cannot be tested. The exit status is 1 if any line is
29
# printed, so a check can rely on the command to keep a file at full
30
# coverage.
31
#
32
# The "cov", "report" and "missed" commands need Clang, llvm-profdata and
33
# llvm-cov.
34
#
35
usage(){
36
sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' >&2
37
exit 1
38
}
39
die(){
40
echo "$0: $*" >&2
41
exit 1
42
}
43
44
test $# -ge 2 || usage
45
cmd=$1
46
dir=$2
47
shift 2
48
top=`dirname "$0"`/..
49
top=`cd "$top" && pwd` || exit 1
50
51
# Find an LLVM tool. On macOS the tools are reached through xcrun.
52
llvm(){
53
if command -v "$1" >/dev/null 2>&1; then
54
"$@"
55
elif command -v xcrun >/dev/null 2>&1; then
56
xcrun "$@"
57
else
58
die "cannot find $1"
59
fi
60
}
61
62
case $cmd in
63
asan)
64
mkdir -p "$dir" && cd "$dir" || exit 1
65
"$top/configure" --fossil-debug --with-sanitizer=address,undefined || exit 1
66
make "$@"
67
;;
68
cov)
69
mkdir -p "$dir" && cd "$dir" || exit 1
70
dir=`pwd`
71
# Not --fossil-debug, because that option replaces CFLAGS.
72
prof="-fprofile-instr-generate=$dir/prof/fossil-%4m.profraw"
73
# The build's own helper programs are compiled with the same flags, so
74
# they need the same compiler.
75
CC=${CC:-clang} CC_FOR_BUILD=${CC:-clang} LDFLAGS="$prof" \
76
CFLAGS="-g -O0 -Wall -DFOSSIL_DEBUG $prof -fcoverage-mapping" \
77
"$top/configure" || exit 1
78
make "$@"
79
;;
80
report)
81
cd "$dir" || exit 1
82
ls prof/*.profraw >/dev/null 2>&1 || die "no profiles in $dir/prof"
83
llvm llvm-profdata merge -sparse prof/*.profraw -o prof/merged.profdata \
84
|| exit 1
85
# The LCOV form of the coverage data has a documented layout: a record
86
# for each source file, from its "SF:" line to "end_of_record", in which
87
# "BRF:" gives the number of branch outcomes and "BRH:" how many of them
88
# were taken.
89
llvm llvm-cov export ./fossil -instr-profile=prof/merged.profdata \
90
-format=lcov -ignore-filename-regex='(extsrc|/tools)/' |
91
awk '
92
function show(name){
93
if( name in nBr ){
94
printf "%-20s %8d %8d %7.1f%%\n", name, nBr[name], nMiss[name],
95
nBr[name] ? 100.0*(nBr[name]-nMiss[name])/nBr[name] : 100.0
96
delete nBr[name]
97
}
98
}
99
/^SF:/ {
100
name = substr($0, 4)
101
sub(/^.*\//, "", name)
102
sub(/_\.c$/, ".c", name)
103
found = 0
104
hit = 0
105
}
106
/^BRF:/ { found = substr($0, 5) + 0 }
107
/^BRH:/ { hit = substr($0, 5) + 0 }
108
/^end_of_record/ && name ~ /\.c$/ {
109
nBr[name] = found
110
nMiss[name] = found - hit
111
order[++n] = name
112
totBr += found
113
totMiss += found - hit
114
}
115
END{
116
if( n==0 ){ print "no coverage data" > "/dev/stderr"; exit 1 }
117
printf "%-20s %8s %8s %8s\n", "File", "Branches", "Missed", "Taken"
118
show("delta.c"); show("content.c"); show("manifest.c"); show("xfer.c")
119
print "--"
120
for(i=1; i<=n; i++) show(order[i])
121
print "--"
122
printf "%-20s %8d %8d %7.1f%%\n", "Total", totBr, totMiss,
123
totBr ? 100.0*(totBr-totMiss)/totBr : 100.0
124
}'
125
;;
126
missed)
127
test $# -ge 1 || usage
128
cd "$dir" || exit 1
129
ls prof/*.profraw >/dev/null 2>&1 || die "no profiles in $dir/prof"
130
llvm llvm-profdata merge -sparse prof/*.profraw -o prof/merged.profdata \
131
|| exit 1
132
rc=0
133
for file in "$@"; do
134
src=bld/`basename "$file" .c`_.c
135
test -f "$src" || die "no such source file in the build: $file"
136
# This reads the text that "llvm-cov show" prints, because the LCOV
137
# form does not say which branches the compiler folded away, as it
138
# does for "while( 1 )", and would report those as never taken.
139
llvm llvm-cov show ./fossil -instr-profile=prof/merged.profdata \
140
-show-branches=count "$src" |
141
awk -v file="$file" -v marks="$top/test/coverage-marks.txt" '
142
function trim(x){ sub(/^[ \t]+/, "", x); sub(/[ \t]+$/, "", x); return x }
143
BEGIN{
144
FS = "|"
145
# The fields of a mark are separated by " | ". The source line
146
# in the middle can hold "|" and "||" of its own.
147
while( (getline line < marks)>0 ){
148
if( line ~ /^#/ ) continue
149
i = index(line, " | ")
150
if( i==0 ) continue
151
name = trim(substr(line, 1, i-1))
152
line = substr(line, i+3)
153
j = 0
154
while( (k = index(substr(line, j+1), " | "))>0 ) j += k
155
if( j==0 ) continue
156
if( name==file ) isMarked[trim(substr(line, 1, j-1))] = 1
157
}
158
FS = "|"
159
}
160
/^ *[0-9]+\|/ {
161
ln = $1 + 0
162
txt = $0
163
sub(/^[^|]*\|[^|]*\|/, "", txt)
164
text[ln] = trim(txt)
165
next
166
}
167
/Branch \(/ && ( /True: 0,/ || /False: 0\]/ ) {
168
ln = $0
169
sub(/^.*Branch \(/, "", ln)
170
sub(/:.*$/, "", ln)
171
ln += 0
172
if( !(ln in seen) ){ seen[ln] = 1; order[++nMiss] = ln }
173
}
174
END{
175
for(i=1; i<=nMiss; i++){
176
ln = order[i]
177
if( text[ln] in isMarked ) continue
178
printf "%s:%d: %s\n", file, ln, text[ln]
179
bad = 1
180
}
181
exit bad
182
}' || rc=1
183
done
184
exit $rc
185
;;
186
*)
187
usage
188
;;
189
esac
190

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button