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.
79 # $Id: meta2deps.sh,v 1.12 2016/12/13 20:44:16 sjg Exp $
81 # Copyright (c) 2010-2013, Juniper Networks, Inc.
82 # All rights reserved.
84 # Redistribution and use in source and binary forms, with or without
85 # modification, are permitted provided that the following conditions
87 # 1. Redistributions of source code must retain the above copyright
88 # notice, this list of conditions and the following disclaimer.
89 # 2. Redistributions in binary form must reproduce the above copyright
90 # notice, this list of conditions and the following disclaimer in the
91 # documentation and/or other materials provided with the distribution.
93 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
94 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
95 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
96 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
97 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
98 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
99 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
100 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
101 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
102 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
103 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107 sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
113 sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
123 "|") sep="$1"; shift;;
124 -s) suffix="$2"; shift 2;;
133 case "$sep$list$sep" in
134 *"$sep$top$suffix$sep"*) continue;;
136 list="${list:+$list$sep}$top$suffix"
138 eval "$name=\"$list\""
153 *=*) eval export "$1"; shift;;
154 -a) MACHINE_ARCH=$2; shift 2;;
155 -m) MACHINE=$2; shift 2;;
156 -C) CURDIR=$2; shift 2;;
157 -H) HOST_TARGET=$2; shift 2;;
158 -S) add_list SRCTOPS $2; shift 2;;
159 -O) add_list OBJROOTS $2; shift 2;;
160 -X) add_list EXCLUDES '|' $2; shift 2;;
161 -R) RELDIR=$2; shift 2;;
162 -T) TARGET_SPEC=$2; shift 2;;
169 host) _ht=$HOST_TARGET;;
174 case "$MACHINE,/$o/" in
175 host,*$HOST_TARGET*) ;;
176 *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
177 *) add_list _o $o; continue;;
179 for x in $_ht $TARGET_SPEC $MACHINE
183 */$x/) add_list _o ${o%$x/}; o=;;
184 */$x) add_list _o ${o%$x}; o=;;
185 *$x/) add_list _o ${o%$x/}; o=;;
186 *$x) add_list _o ${o%$x}; o=;;
196 OBJTOP=$o${TARGET_SPEC:-$MACHINE}
203 add_list '|' -s '/*' src_re $SRCTOPS
204 add_list '|' -s '*' obj_re $OBJROOTS
206 [ -z "$RELDIR" ] && unset DPDEPS
209 trap 'rm -f $tf.*; trap 0' 0
222 *) _excludes=_excludes_f;;
231 @*) xargs cat < ${f#@};;
236 *) cat /dev/null "$@";;
238 sed -e 's,^CWD,C C,;/^[CREFLM] /!d' -e "s,',,g" |
240 while read op pid path junk
242 : op=$op pid=$pid path=$path
243 # we track cwd and ldir (of interest) per pid
246 ,C) CWD=$path cwd=$path ldir=$path
247 if [ -z "$SB" ]; then
248 SB=`echo $CWD | sed 's,/obj.*,,'`
250 SRCTOP=${SRCTOP:-$SB/src}
257 *) eval ldir_$lpid=$ldir;;
259 eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
265 W,*srcrel|*.dirdep) continue;;
269 *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
271 # watch out for temp dirs that no longer exist
272 test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
276 F,*) # $path is new pid
277 eval cwd_$path=$cwd ldir_$path=$ldir
285 *) for path in $ldir/$path $cwd/$path
287 test -e $path && break
294 # avoid repeating ourselves...
295 case "$DPDEPS,$seensrc," in
301 *,$path,*) continue;;
303 # canonicalize if needed
307 dir=`cd $dir 2> /dev/null && /bin/pwd`
313 ${CURDIR:-.}|"") continue;;
315 # avoid repeating ourselves...
316 case "$DPDEPS,$seensrc," in
330 if [ -d $path ]; then
332 */..) ldir=${dir%/*};;
337 [ -f $path ] || continue
339 $CWD) continue;; # ignore
341 seenit="$seenit,$seen"
342 echo $dir >> $tf.srcdep
343 case "$DPDEPS,$reldir,$seensrc," in
345 *) seensrc="$seensrc,$path"
346 echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
352 # if there is a .dirdep we cannot skip
353 # just because we've seen the dir before.
354 if [ -s $path.dirdep ]; then
355 # this file contains:
356 # '# ${RELDIR}.<machine>'
357 echo $path.dirdep >> $tf.qual
359 elif [ -s $dir.dirdep ]; then
360 echo $dir.dirdep >> $tf.qual
361 seenit="$seenit,$seen"
364 seenit="$seenit,$seen"
371 for f in $tf.dirdep $tf.qual $tf.srcdep
373 [ -s $f ] || continue
375 *qual) # a list of .dirdep files
376 # we can prefix everything with $OBJTOP to
377 # tell gendirdeps.mk that these are
378 # DIRDEP entries, since they are already
379 # qualified with .<machine> as needed.
380 # We strip .$MACHINE though
381 xargs cat < $f | sort -u |
382 sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
388 if [ -s $tf.dpdeps ]; then
391 *) echo > $DPDEPS;; # the echo is needed!
394 sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
396 # ensure we produce _something_ else egrep -v gets upset
401 */meta2dep*) meta2deps "$@";;
402 */meta2dirs*) meta2dirs "$@";;
403 */meta2src*) meta2src "$@";;