libpthread: Add pthread_mutex_timedlock() reference to some manual pages.
[dragonfly.git] / contrib / bmake / mk / meta2deps.sh
blob8fb5ec6670e3d024efc2875717806f9f95b490da
1 #!/bin/sh
3 # NAME:
4 # meta2deps.sh - extract useful info from .meta files
6 # SYNOPSIS:
7 # meta2deps.sh SB="SB" "meta" ...
8 #
9 # DESCRIPTION:
10 # This script looks each "meta" file and extracts the
11 # information needed to deduce build and src dependencies.
13 # To do this, we extract the 'CWD' record as well as all the
14 # syscall traces which describe 'R'ead, 'C'hdir and 'E'xec
15 # syscalls.
17 # The typical meta file looks like::
18 #.nf
20 # # Meta data file "path"
21 # CMD "command-line"
22 # CWD "cwd"
23 # TARGET "target"
24 # -- command output --
25 # -- filemon acquired metadata --
26 # # buildmon version 2
27 # V 2
28 # E "pid" "path"
29 # R "pid" "path"
30 # C "pid" "cwd"
31 # R "pid" "path"
32 # X "pid" "status"
33 #.fi
35 # The fact that all the syscall entry lines start with a single
36 # character make these files quite easy to process using sed(1).
38 # To simplify the logic the 'CWD' line is made to look like a
39 # normal 'C'hdir entry, and "cwd" is remembered so that it can
40 # be prefixed to any "path" which is not absolute.
42 # If the "path" being read ends in '.srcrel' it is the content
43 # of (actually the first line of) that file that we are
44 # interested in.
46 # Any "path" which lies outside of the sandbox "SB" is generally
47 # not of interest and is ignored.
49 # The output, is a set of absolute paths with "SB" like:
50 #.nf
52 # $SB/obj-i386/bsd/gnu/lib/csu
53 # $SB/obj-i386/bsd/gnu/lib/libgcc
54 # $SB/obj-i386/bsd/include
55 # $SB/obj-i386/bsd/lib/csu/i386-elf
56 # $SB/obj-i386/bsd/lib/libc
57 # $SB/src/bsd/include
58 # $SB/src/bsd/sys/i386/include
59 # $SB/src/bsd/sys/sys
60 # $SB/src/pan-release/rtsock
61 # $SB/src/pfe-shared/include/jnx
62 #.fi
64 # Which can then be further processed by 'gendirdeps.mk'
66 # If we are passed 'DPDEPS='"dpdeps", then for each src file
67 # outside of "CURDIR" we read, we output a line like:
68 #.nf
70 # DPDEPS_$path += $RELDIR
71 #.fi
73 # with "$path" geting turned into reldir's, so that we can end
74 # up with a list of all the directories which depend on each src
75 # file in another directory. This can allow for efficient yet
76 # complete testing of changes.
79 # RCSid:
80 # $Id: meta2deps.sh,v 1.10 2016/03/02 18:53:36 sjg Exp $
82 # Copyright (c) 2010-2013, Juniper Networks, Inc.
83 # All rights reserved.
85 # Redistribution and use in source and binary forms, with or without
86 # modification, are permitted provided that the following conditions
87 # are met:
88 # 1. Redistributions of source code must retain the above copyright
89 # notice, this list of conditions and the following disclaimer.
90 # 2. Redistributions in binary form must reproduce the above copyright
91 # notice, this list of conditions and the following disclaimer in the
92 # documentation and/or other materials provided with the distribution.
94 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
95 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
96 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
97 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
98 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
100 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
101 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
103 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
104 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 meta2src() {
107 cat /dev/null "$@" |
108 sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
109 sort -u
112 meta2dirs() {
113 cat /dev/null "$@" |
114 sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
115 sort -u
118 add_list() {
119 sep=' '
120 suffix=
121 while :
123 case "$1" in
124 "|") sep="$1"; shift;;
125 -s) suffix="$2"; shift 2;;
126 *) break;;
127 esac
128 done
129 name=$1
130 shift
131 eval list="\$$name"
132 for top in "$@"
134 case "$sep$list$sep" in
135 *"$sep$top$suffix$sep"*) continue;;
136 esac
137 list="${list:+$list$sep}$top$suffix"
138 done
139 eval "$name=\"$list\""
142 _excludes_f() {
143 egrep -v "$EXCLUDES"
146 meta2deps() {
147 DPDEPS=
148 SRCTOPS=$SRCTOP
149 OBJROOTS=
150 EXCLUDES=
151 while :
153 case "$1" in
154 *=*) eval export "$1"; shift;;
155 -a) MACHINE_ARCH=$2; shift 2;;
156 -m) MACHINE=$2; shift 2;;
157 -C) CURDIR=$2; shift 2;;
158 -H) HOST_TARGET=$2; shift 2;;
159 -S) add_list SRCTOPS $2; shift 2;;
160 -O) add_list OBJROOTS $2; shift 2;;
161 -X) add_list EXCLUDES '|' $2; shift 2;;
162 -R) RELDIR=$2; shift 2;;
163 -T) TARGET_SPEC=$2; shift 2;;
164 *) break;;
165 esac
166 done
168 _th= _o=
169 case "$MACHINE" in
170 host) _ht=$HOST_TARGET;;
171 esac
173 for o in $OBJROOTS
175 case "$MACHINE,/$o/" in
176 host,*$HOST_TARGET*) ;;
177 *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
178 *) add_list _o $o; continue;;
179 esac
180 for x in $_ht $TARGET_SPEC $MACHINE
182 case "$o" in
183 "") continue;;
184 */$x/) add_list _o ${o%$x/}; o=;;
185 */$x) add_list _o ${o%$x}; o=;;
186 *$x/) add_list _o ${o%$x/}; o=;;
187 *$x) add_list _o ${o%$x}; o=;;
188 esac
189 done
190 done
191 OBJROOTS="$_o"
193 case "$OBJTOP" in
195 for o in $OBJROOTS
197 OBJTOP=$o${TARGET_SPEC:-$MACHINE}
198 break
199 done
201 esac
202 src_re=
203 obj_re=
204 add_list '|' -s '/*' src_re $SRCTOPS
205 add_list '|' -s '*' obj_re $OBJROOTS
207 [ -z "$RELDIR" ] && unset DPDEPS
208 tf=/tmp/m2d$$-$USER
209 rm -f $tf.*
210 trap 'rm -f $tf.*; trap 0' 0
212 > $tf.dirdep
213 > $tf.qual
214 > $tf.srcdep
215 > $tf.srcrel
216 > $tf.dpdeps
218 seenit=
219 seensrc=
220 lpid=
221 case "$EXCLUDES" in
222 "") _excludes=cat;;
223 *) _excludes=_excludes_f;;
224 esac
225 # handle @list files
226 case "$@" in
227 *@[!.]*)
228 for f in "$@"
230 case "$f" in
231 *.meta) cat $f;;
232 @*) xargs cat < ${f#@};;
233 *) cat $f;;
234 esac
235 done
237 *) cat /dev/null "$@";;
238 esac 2> /dev/null |
239 sed -e 's,^CWD,C C,;/^[CREFLM] /!d' -e "s,',,g" |
240 $_excludes |
241 while read op pid path junk
243 : op=$op pid=$pid path=$path
244 # we track cwd and ldir (of interest) per pid
245 # CWD is bmake's cwd
246 case "$lpid,$pid" in
247 ,C) CWD=$path cwd=$path ldir=$path
248 if [ -z "$SB" ]; then
249 SB=`echo $CWD | sed 's,/obj.*,,'`
251 SRCTOP=${SRCTOP:-$SB/src}
252 continue
254 $pid,$pid) ;;
256 case "$lpid" in
257 "") ;;
258 *) eval ldir_$lpid=$ldir cwd_$lpid=$cwd;;
259 esac
260 eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
261 lpid=$pid
263 esac
265 case "$op,$path" in
266 W,*srcrel|*.dirdep) continue;;
267 C,*)
268 case "$path" in
269 /*) cwd=$path;;
270 *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
271 esac
272 # watch out for temp dirs that no longer exist
273 test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
274 continue
276 F,*) eval cwd_$path=$cwd ldir_$path=$ldir
277 continue
279 *) dir=${path%/*}
280 case "$path" in
281 $src_re|$obj_re) ;;
282 /*/stage/*) ;;
283 /*) continue;;
284 *) for path in $ldir/$path $cwd/$path
286 test -e $path && break
287 done
288 dir=${path%/*}
290 esac
292 esac
293 # avoid repeating ourselves...
294 case "$DPDEPS,$seensrc," in
296 case ",$seenit," in
297 *,$dir,*) continue;;
298 esac
300 *,$path,*) continue;;
301 esac
302 # canonicalize if needed
303 case "/$dir/" in
304 */../*|*/./*)
305 rdir=$dir
306 dir=`cd $dir 2> /dev/null && /bin/pwd`
307 seen="$rdir,$dir"
309 *) seen=$dir;;
310 esac
311 case "$dir" in
312 ${CURDIR:-.}|"") continue;;
313 $src_re)
314 # avoid repeating ourselves...
315 case "$DPDEPS,$seensrc," in
317 case ",$seenit," in
318 *,$dir,*) continue;;
319 esac
321 esac
324 case ",$seenit," in
325 *,$dir,*) continue;;
326 esac
328 esac
329 if [ -d $path ]; then
330 case "$path" in
331 */..) ldir=${dir%/*};;
332 *) ldir=$path;;
333 esac
334 continue
336 [ -f $path ] || continue
337 case "$dir" in
338 $CWD) continue;; # ignore
339 $src_re)
340 seenit="$seenit,$seen"
341 echo $dir >> $tf.srcdep
342 case "$DPDEPS,$reldir,$seensrc," in
343 ,*) ;;
344 *) seensrc="$seensrc,$path"
345 echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
347 esac
348 continue
350 esac
351 # if there is a .dirdep we cannot skip
352 # just because we've seen the dir before.
353 if [ -s $path.dirdep ]; then
354 # this file contains:
355 # '# ${RELDIR}.<machine>'
356 echo $path.dirdep >> $tf.qual
357 continue
358 elif [ -s $dir.dirdep ]; then
359 echo $dir.dirdep >> $tf.qual
360 seenit="$seenit,$seen"
361 continue
363 seenit="$seenit,$seen"
364 case "$dir" in
365 $obj_re)
366 echo $dir;;
367 esac
368 done > $tf.dirdep
369 _nl=echo
370 for f in $tf.dirdep $tf.qual $tf.srcdep
372 [ -s $f ] || continue
373 case $f in
374 *qual) # a list of .dirdep files
375 # we can prefix everything with $OBJTOP to
376 # tell gendirdeps.mk that these are
377 # DIRDEP entries, since they are already
378 # qualified with .<machine> as needed.
379 # We strip .$MACHINE though
380 xargs cat < $f | sort -u |
381 sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
383 *) sort -u $f;;
384 esac
385 _nl=:
386 done
387 if [ -s $tf.dpdeps ]; then
388 case "$DPDEPS" in
389 */*) ;;
390 *) echo > $DPDEPS;; # the echo is needed!
391 esac
392 sort -u $tf.dpdeps |
393 sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
395 # ensure we produce _something_ else egrep -v gets upset
396 $_nl
399 case /$0 in
400 */meta2dep*) meta2deps "$@";;
401 */meta2dirs*) meta2dirs "$@";;
402 */meta2src*) meta2src "$@";;
403 esac