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/include
53 # $SB/obj-i386/bsd/lib/csu/i386
54 # $SB/obj-i386/bsd/lib/libc
56 # $SB/src/bsd/sys/i386/include
58 # $SB/src/pan-release/rtsock
59 # $SB/src/pfe-shared/include/jnx
62 # Which can then be further processed by 'gendirdeps.mk'
64 # If we are passed 'DPDEPS='"dpdeps", then for each src file
65 # outside of "CURDIR" we read, we output a line like:
68 # DPDEPS_$path += $RELDIR
71 # with "$path" geting turned into reldir's, so that we can end
72 # up with a list of all the directories which depend on each src
73 # file in another directory. This can allow for efficient yet
74 # complete testing of changes.
78 # $Id: meta2deps.sh,v 1.14 2020/10/02 03:11:17 sjg Exp $
80 # Copyright (c) 2010-2013, Juniper Networks, Inc.
81 # All rights reserved.
83 # Redistribution and use in source and binary forms, with or without
84 # modification, are permitted provided that the following conditions
86 # 1. Redistributions of source code must retain the above copyright
87 # notice, this list of conditions and the following disclaimer.
88 # 2. Redistributions in binary form must reproduce the above copyright
89 # notice, this list of conditions and the following disclaimer in the
90 # documentation and/or other materials provided with the distribution.
92 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
93 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
94 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
95 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
96 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
98 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
99 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
102 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
112 sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
122 "|") sep="$1"; shift;;
123 -s) suffix="$2"; shift 2;;
132 case "$sep$list$sep" in
133 *"$sep$top$suffix$sep"*) continue;;
135 list="${list:+$list$sep}$top$suffix"
137 eval "$name=\"$list\""
157 *=*) eval export "$1"; shift;;
158 -a) MACHINE_ARCH=$2; shift 2;;
159 -m) MACHINE=$2; shift 2;;
160 -C) CURDIR=$2; shift 2;;
161 -H) HOST_TARGET=$2; shift 2;;
162 -S) add_list SRCTOPS $2; shift 2;;
163 -O) add_list OBJROOTS $2; shift 2;;
164 -X) add_list EXCLUDES '|' $2; shift 2;;
165 -R) RELDIR=$2; shift 2;;
166 -T) TARGET_SPEC=$2; shift 2;;
173 host) _ht=$HOST_TARGET;;
178 case "$MACHINE,/$o/" in
179 host,*$HOST_TARGET*) ;;
180 *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
181 *) add_list _o $o; continue;;
183 for x in $_ht $TARGET_SPEC $MACHINE
187 */$x/) add_list _o ${o%$x/}; o=;;
188 */$x) add_list _o ${o%$x}; o=;;
189 *$x/) add_list _o ${o%$x/}; o=;;
190 *$x) add_list _o ${o%$x}; o=;;
200 OBJTOP=$o${TARGET_SPEC:-$MACHINE}
207 add_list '|' -s '/*' src_re $SRCTOPS
208 add_list '|' -s '*' obj_re $OBJROOTS
210 [ -z "$RELDIR" ] && unset DPDEPS
213 trap 'rm -f $tf.*; trap 0' 0
226 *) _excludes=_excludes_f;;
235 @*) xargs cat < ${f#@};;
240 *) cat /dev/null "$@";;
242 sed -e 's,^CWD,C C,;/^[CREFLMV] /!d' -e "s,',,g" |
243 $_excludes | ( version=no
244 while read op pid path junk
246 : op=$op pid=$pid path=$path
247 # we track cwd and ldir (of interest) per pid
250 ,C) CWD=$path cwd=$path ldir=$path
251 if [ -z "$SB" ]; then
252 SB=`echo $CWD | sed 's,/obj.*,,'`
254 SRCTOP=${SRCTOP:-$SB/src}
257 0) error "no filemon data";;
267 *) eval ldir_$lpid=$ldir;;
269 eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
275 V,*) version=$path; continue;;
276 W,*srcrel|*.dirdep) continue;;
280 *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
282 # watch out for temp dirs that no longer exist
283 test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
287 F,*) # $path is new pid
288 eval cwd_$path=$cwd ldir_$path=$ldir
296 *) for path in $ldir/$path $cwd/$path
298 test -e $path && break
305 # avoid repeating ourselves...
306 case "$DPDEPS,$seensrc," in
312 *,$path,*) continue;;
314 # canonicalize if needed
318 dir=`cd $dir 2> /dev/null && /bin/pwd`
324 ${CURDIR:-.}|"") continue;;
326 # avoid repeating ourselves...
327 case "$DPDEPS,$seensrc," in
341 if [ -d $path ]; then
343 */..) ldir=${dir%/*};;
348 [ -f $path ] || continue
350 $CWD) continue;; # ignore
352 seenit="$seenit,$seen"
353 echo $dir >> $tf.srcdep
354 case "$DPDEPS,$reldir,$seensrc," in
356 *) seensrc="$seensrc,$path"
357 echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
363 # if there is a .dirdep we cannot skip
364 # just because we've seen the dir before.
365 if [ -s $path.dirdep ]; then
366 # this file contains:
367 # '# ${RELDIR}.<machine>'
368 echo $path.dirdep >> $tf.qual
370 elif [ -s $dir.dirdep ]; then
371 echo $dir.dirdep >> $tf.qual
372 seenit="$seenit,$seen"
375 seenit="$seenit,$seen"
382 0) error "no filemon data";;
385 for f in $tf.dirdep $tf.qual $tf.srcdep
387 [ -s $f ] || continue
389 *qual) # a list of .dirdep files
390 # we can prefix everything with $OBJTOP to
391 # tell gendirdeps.mk that these are
392 # DIRDEP entries, since they are already
393 # qualified with .<machine> as needed.
394 # We strip .$MACHINE though
395 xargs cat < $f | sort -u |
396 sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
402 if [ -s $tf.dpdeps ]; then
405 *) echo > $DPDEPS;; # the echo is needed!
408 sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
410 # ensure we produce _something_ else egrep -v gets upset
415 */meta2dep*) meta2deps "$@";;
416 */meta2dirs*) meta2dirs "$@";;
417 */meta2src*) meta2src "$@";;