-
Notifications
You must be signed in to change notification settings - Fork 0
/
kabi-graph
executable file
·322 lines (260 loc) · 6.57 KB
/
kabi-graph
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
#!/bin/bash
#
# kabitools
#
declare cmdline=kabitools
declare -i cpus=$(cat /proc/cpuinfo | grep processor | wc -l)
declare clean=true
declare patch
declare target=""
declare distro="$(cat /etc/os-release | grep -w ID | cut -d'=' -f2)"
declare -i optcnt=0
declare -i err_no_patch=1
declare -i err_no_os=2
declare -i err_not_kernel=3
declare -i err_not_redhat=4
declare -i err_no_boost=5
declare -i err_bad_opt=127
declare -i CTRL_C=130
declare BLD="\033[1m"
declare OFF="\033[0m"
declare UND="\033[4m"
declare graphlist="redhat/kabi/kabi-datafiles.list"
declare graphext="kbg"
# strip outside quotes from the distro string
#
distro=$(sed -e 's/^"//' -e 's/"$//' <<<"$distro")
usagestr=$(
cat <<EOF
$BLD
$(basename $0) -[jnDh] [target]$OFF
Builds a graph from the preprocessor output of each file compiled.
Must be invoked from the top of the kernel tree.
The optional $BLD\0target$OFF argument can be any valid kernel make target
including a subdirectory, e.g. "$BLD\0virt/$OFF" or a module build, e.g.
"$BLD\0M=drivers/char/tpm$OFF"
The kABI graph will be limited to the scope of the build of the
optional target.
Default action without the optarg:
make clean # can be disabled with -n option
make
make modules
$UND\0Options$OFF
$BLD -j$OFF - Optional number of processors to assign to the make task.
There must be a space between -j and the number.
Default is all processors in this system: $BLD$cpus$OFF
$BLD -n$OFF - Do not make clean first
$BLD -R$OFF - Recreate the list of graph files: $graphlist
$BLD -D$OFF - Delete all the graph files and exit.
$BLD -h$OFF - This help screen.
$BLD
NOTE:$OFF The graph file extension will be added to $BLD\0.git/info/exclude$OFF,
but will be selectively removed with "$BLD\0kabi-graph -D$OFF" command,
which removes all the graph files.
\0
EOF
)
no_patch_str=$(
cat <<EOF
$BLD$(basename $0)$OFF could not find the correct patch for the kernel Makefiles.
Please check for the existence of:
$BLD
/usr/share/kabitools-rhel-kernel-make.patch
/usr/share/kabitools-pegas-kernel-make.patch$OFF
\0
EOF
)
no_os_str=$(
cat <<EOF
$BLD$(basename $0)$OFF does not handle distro: $BLD$distro$OFF
\0
EOF
)
not_kernel_str=$(
cat <<EOF
$BLD$(basename $0)$OFF must be invoked from the top of a kernel tree.
You are here: $BLD$PWD$OFF
\0
EOF
)
not_redhat_str=$(
cat <<EOF
$BLD$(basename $0)$OFF must be invoked from the top of a RHEL tree.
Your current distro is: "$BLD$distro$OFF"
Your current branch is: "$BLD$(git rev-parse --abbrev-ref HEAD)$OFF"
\0
EOF
)
no_boost_str=$(
cat <<EOF
You must install $BLD\0boost$OFF before you can run $BLD$(basename $0)$OFF\0.
\0
EOF
)
usage() {
echo -e "$usagestr"
exit 0
}
errexit() {
echo -e "$1"
exit $2
}
# ui_waitonproc
#
# Prints a dot to the screen every half second until the passed PID
# completes.
#
# $1 - PID of process we are waiting for.
# $2 - optional time argument
#
ui_waitonproc() {
__pid__=$1;
__time__=1
[ $# -eq 2 ] && __time__=$2
while kill -0 $__pid__ > /dev/null 2>&1; do
echo -n '.';
sleep $__time__;
done
}
# delete_linen - deletes the line 'n' from the file
#
# $1 - number of the line to delete
# $2 - file from which to delete the line
#
delete_linen() {
local tfil="/tmp/__temp__file__"
local mode=$(stat --format '%a' $2)
touch $tfil
chmod $mode $tfil
awk -v line=$1 '{
if (NR != line) {
print $0;
}
}' $2 > $tfil
mv $tfil $2
}
# find_line - return the 1-based line number of the string in the file
#
# Sends a 0 to $3 if not found
#
# $1 - string
# $2 - file
# $3 - number, receives the line number, 0 if not found
#
find_line() {
local _line_="$1"
local _file_="$2"
local _number_
_line_=$(grep -n "$_line_" "$_file_")
[ $? -eq 0 ] || { eval $3=0; return 1; }
_number_=$(echo "$_line_" | cut -d':' -f1)
eval $3=$_number_
return 0
}
delete_graph() {
local mypid
local linenum
local line
echo -n "Deleting graph files.."
find . -name \*.kbg -exec rm -f '{}' \; &
mypid=$!
ui_waitonproc mypid .1
rm -f redhat/kabi/kabi-datafiles.list
# delete the kabi-graph lines from .git/info/exclude
#
find_line '\*.kbg' .git/info/exclude linenum
[ $linenum -gt 0 ] && delete_linen $linenum .git/info/exclude
find_line 'redhat/kabi/kabi-datafiles.list' .git/info/exclude linenum
[ $linenum -gt 0 ] && delete_linen $linenum .git/info/exclude
echo
echo "Done!"
exit 0
}
recreate_graphlist() {
local mypid
> "$graphlist"
find ./ -name "*.kbg" -exec echo '{}' >> "$graphlist" \; &
mypid=$!
ui_waitonproc $mypid .1
echo "Done."
exit 0
}
# run if user hits control-c
#
control_c() {
echo -en "\nCtrl-c detected\nCleaning up and exiting.\n"
patch -Rs -p1 < "$patch"
errexit "Done." $CTRL_C
}
main() {
local kerneltree
local linenumber
local rhel
local kernel
# Make sure that we have boost-serialization. Can't work without it.
#
rpm -q boost-serialization > /dev/null 2>&1
[ $? -eq 0 ] || errexit "$no_boost_str" $err_no_boost
# Determine if we're in a Linux Kernel directory
#
[ -f README ] || errexit "$not_kernel_str" $err_not_kernel
kerneltree="$(head -1 README)"
kerneltree=$(echo $kerneltree)
[[ ${kerneltree:0:12} == "Linux kernel" ]] || \
errexit "$not_kernel_str" $err_not_kernel
# Make sure that we are in a rhel or fedora tree
#
[ -d redhat ] || errexit "$not_redhat_str" $err_not_redhat
while getopts hnRDj: OPTION; do
case "$OPTION" in
h ) usage
;;
j ) cpus=$OPTARG
optcnt=$((optcnt + 2))
;;
n ) clean=false
optcnt=$((optcnt + 1))
;;
R ) recreate_graphlist
;;
D ) delete_graph
;;
* ) usage
errexit "Unrecognized option." $err_bad_opt
esac
done
shift $optcnt
target="$1"
kernel=$(grep -m1 VERSION Makefile | cut -d' ' -f3)
case $kernel in
# RHEL6 and RHEL7 share the same Makefile patch
#
2 ) patch="/usr/share/kabitools-rhel7-kernel-make.patch"
;;
3 ) patch="/usr/share/kabitools-rhel7-kernel-make.patch"
;;
4 ) patch="/usr/share/kabitools-rhel8-kernel-make.patch"
;;
* ) errexit "$no_patch_str" $err_no_patch
esac
[ -f "$patch" ] || errexit "$no_os_str" $err_no_os
patch -s -p1 < "$patch"
$clean && make clean
> redhat/kabi/kabi-datafiles.list
make -j $cpus K=1 $target
patch -Rs -p1 < "$patch"
# If we haven't already done so, then add the graph file extensions
# to .git/info/exclude
#
find_line "\*.$graphext" .git/info/exclude linenumber
[ $linenumber -eq 0 ] && echo "*.$graphext" >> .git/info/exclude
find_line "$graphlist" .git/info/exclude linenumber
[ $linenumber -eq 0 ] && echo "$graphlist" >> .git/info/exclude
exit 0
}
# trap keyboard interrupt (control-c)
#
trap control_c SIGINT
# Call the main function.
#
main $@