4 # meta2deps.sh - extract useful info from .meta files
7 # meta2deps.sh SB="SB" "meta" ...
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
17 # The typical meta file looks like::
20 # # Meta data file "path"
24 # -- command output --
25 # -- filemon acquired metadata --
26 # # buildmon version 2
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
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:
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
58 # $SB/src/bsd/sys/i386/include
60 # $SB/src/pan-release/rtsock
61 # $SB/src/pfe-shared/include/jnx
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:
70 # DPDEPS_$path += $RELDIR
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.
80 # $Id: meta2deps.sh,v 1.19 2022/09/09 18:37:37 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
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.
108 sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
114 sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
124 "|") sep="$1"; shift;;
125 -s) suffix="$2"; shift 2;;
134 case "$sep$list$sep" in
135 *"$sep$top$suffix$sep"*) continue;;
137 list="${list:+$list$sep}$top$suffix"
139 eval "$name=\"$list\""
142 # some Linux systems have deprecated egrep in favor of grep -E
143 # but not everyone supports that
144 case "`echo bmake | egrep 'a|b' 2>&1`" in
146 *) egrep() { grep -E "$@"; }
166 *=*) eval export "$1"; shift;;
167 -a) MACHINE_ARCH=$2; shift 2;;
168 -m) MACHINE=$2; shift 2;;
169 -C) CURDIR=$2; shift 2;;
170 -H) HOST_TARGET=$2; shift 2;;
171 -S) add_list SRCTOPS $2; shift 2;;
172 -O) add_list OBJROOTS $2; shift 2;;
173 -X) add_list EXCLUDES '|' $2; shift 2;;
174 -R) RELDIR=$2; shift 2;;
175 -T) TARGET_SPEC=$2; shift 2;;
182 host) _ht=$HOST_TARGET;;
187 case "$MACHINE,/$o/" in
188 host,*$HOST_TARGET*) ;;
189 *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
190 *) add_list _o $o; continue;;
192 for x in $_ht $TARGET_SPEC $MACHINE
196 */$x/) add_list _o ${o%$x/}; o=;;
197 */$x) add_list _o ${o%$x}; o=;;
198 *$x/) add_list _o ${o%$x/}; o=;;
199 *$x) add_list _o ${o%$x}; o=;;
209 OBJTOP=$o${TARGET_SPEC:-$MACHINE}
216 add_list '|' -s '/*' src_re $SRCTOPS
217 add_list '|' -s '*' obj_re $OBJROOTS
219 [ -z "$RELDIR" ] && unset DPDEPS
222 trap 'rm -f $tf.*; trap 0' 0
235 *) _excludes=_excludes_f;;
244 @*) xargs cat < ${f#@};;
249 *) cat /dev/null "$@";;
251 sed -e 's,^CWD,C C,;/^[CREFLMVX] /!d' -e "s,',,g" |
252 $_excludes | ( version=no epids= xpids=
253 while read op pid path junk
255 : op=$op pid=$pid path=$path
256 # we track cwd and ldir (of interest) per pid
259 ,C) CWD=$path cwd=$path ldir=$path
260 if [ -z "$SB" ]; then
261 SB=`echo $CWD | sed 's,/obj.*,,'`
263 SRCTOP=${SRCTOP:-$SB/src}
266 0) error "no filemon data";;
276 *) eval ldir_$lpid=$ldir;;
278 eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
285 V,*) version=$pid; continue;;
286 W,*srcrel|*.dirdep) continue;;
290 *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
292 # watch out for temp dirs that no longer exist
293 test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
297 F,*) # $path is new pid
298 eval cwd_$path=$cwd ldir_$path=$ldir
303 E) # setid apps get no tracing so we won't see eXit
304 case `'ls' -l $path 2> /dev/null | sed 's, .*,,'` in
306 *) epids="$epids $pid";;
309 X) xpids="$xpids $pid"; continue;;
315 *) for path in $ldir/$path $cwd/$path
317 test -e $path && break
324 # avoid repeating ourselves...
325 case "$DPDEPS,$seensrc," in
331 *,$path,*) continue;;
333 # canonicalize if needed
337 dir=`cd $dir 2> /dev/null && /bin/pwd`
343 ${CURDIR:-.}|"") continue;;
345 # avoid repeating ourselves...
346 case "$DPDEPS,$seensrc," in
360 if [ -d $path ]; then
362 */..) ldir=${dir%/*};;
367 [ -f $path ] || continue
369 $CWD) continue;; # ignore
371 seenit="$seenit,$seen"
372 echo $dir >> $tf.srcdep
373 case "$DPDEPS,$reldir,$seensrc," in
375 *) seensrc="$seensrc,$path"
376 echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
382 # if there is a .dirdep we cannot skip
383 # just because we've seen the dir before.
384 if [ -s $path.dirdep ]; then
385 # this file contains:
386 # '# ${RELDIR}.<machine>'
387 echo $path.dirdep >> $tf.qual
389 elif [ -s $dir.dirdep ]; then
390 echo $dir.dirdep >> $tf.qual
391 seenit="$seenit,$seen"
394 seenit="$seenit,$seen"
402 0) error "no filemon data";;
409 *) error "missing eXit for pid $p";;
413 for f in $tf.dirdep $tf.qual $tf.srcdep
415 [ -s $f ] || continue
417 *qual) # a list of .dirdep files
418 # we can prefix everything with $OBJTOP to
419 # tell gendirdeps.mk that these are
420 # DIRDEP entries, since they are already
421 # qualified with .<machine> as needed.
422 # We strip .$MACHINE though
423 xargs cat < $f | sort -u |
424 sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
430 if [ -s $tf.dpdeps ]; then
433 *) echo > $DPDEPS;; # the echo is needed!
436 sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
438 # ensure we produce _something_ else egrep -v gets upset
443 */meta2dep*) meta2deps "$@";;
444 */meta2dirs*) meta2dirs "$@";;
445 */meta2src*) meta2src "$@";;