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.15 2020/11/08 06:31:08 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\""
159 *=*) eval export "$1"; shift;;
160 -a) MACHINE_ARCH=$2; shift 2;;
161 -m) MACHINE=$2; shift 2;;
162 -C) CURDIR=$2; shift 2;;
163 -H) HOST_TARGET=$2; shift 2;;
164 -S) add_list SRCTOPS $2; shift 2;;
165 -O) add_list OBJROOTS $2; shift 2;;
166 -X) add_list EXCLUDES '|' $2; shift 2;;
167 -R) RELDIR=$2; shift 2;;
168 -T) TARGET_SPEC=$2; shift 2;;
175 host) _ht=$HOST_TARGET;;
180 case "$MACHINE,/$o/" in
181 host,*$HOST_TARGET*) ;;
182 *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
183 *) add_list _o $o; continue;;
185 for x in $_ht $TARGET_SPEC $MACHINE
189 */$x/) add_list _o ${o%$x/}; o=;;
190 */$x) add_list _o ${o%$x}; o=;;
191 *$x/) add_list _o ${o%$x/}; o=;;
192 *$x) add_list _o ${o%$x}; o=;;
202 OBJTOP=$o${TARGET_SPEC:-$MACHINE}
209 add_list '|' -s '/*' src_re $SRCTOPS
210 add_list '|' -s '*' obj_re $OBJROOTS
212 [ -z "$RELDIR" ] && unset DPDEPS
215 trap 'rm -f $tf.*; trap 0' 0
228 *) _excludes=_excludes_f;;
237 @*) xargs cat < ${f#@};;
242 *) cat /dev/null "$@";;
244 sed -e 's,^CWD,C C,;/^[CREFLMV] /!d' -e "s,',,g" |
245 $_excludes | ( version=no
246 while read op pid path junk
248 : op=$op pid=$pid path=$path
249 # we track cwd and ldir (of interest) per pid
252 ,C) CWD=$path cwd=$path ldir=$path
253 if [ -z "$SB" ]; then
254 SB=`echo $CWD | sed 's,/obj.*,,'`
256 SRCTOP=${SRCTOP:-$SB/src}
259 0) error "no filemon data";;
269 *) eval ldir_$lpid=$ldir;;
271 eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
277 V,*) version=$path; continue;;
278 W,*srcrel|*.dirdep) continue;;
282 *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
284 # watch out for temp dirs that no longer exist
285 test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
289 F,*) # $path is new pid
290 eval cwd_$path=$cwd ldir_$path=$ldir
298 *) for path in $ldir/$path $cwd/$path
300 test -e $path && break
307 # avoid repeating ourselves...
308 case "$DPDEPS,$seensrc," in
314 *,$path,*) continue;;
316 # canonicalize if needed
320 dir=`cd $dir 2> /dev/null && /bin/pwd`
326 ${CURDIR:-.}|"") continue;;
328 # avoid repeating ourselves...
329 case "$DPDEPS,$seensrc," in
343 if [ -d $path ]; then
345 */..) ldir=${dir%/*};;
350 [ -f $path ] || continue
352 $CWD) continue;; # ignore
354 seenit="$seenit,$seen"
355 echo $dir >> $tf.srcdep
356 case "$DPDEPS,$reldir,$seensrc," in
358 *) seensrc="$seensrc,$path"
359 echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
365 # if there is a .dirdep we cannot skip
366 # just because we've seen the dir before.
367 if [ -s $path.dirdep ]; then
368 # this file contains:
369 # '# ${RELDIR}.<machine>'
370 echo $path.dirdep >> $tf.qual
372 elif [ -s $dir.dirdep ]; then
373 echo $dir.dirdep >> $tf.qual
374 seenit="$seenit,$seen"
377 seenit="$seenit,$seen"
384 0) error "no filemon data";;
387 for f in $tf.dirdep $tf.qual $tf.srcdep
389 [ -s $f ] || continue
391 *qual) # a list of .dirdep files
392 # we can prefix everything with $OBJTOP to
393 # tell gendirdeps.mk that these are
394 # DIRDEP entries, since they are already
395 # qualified with .<machine> as needed.
396 # We strip .$MACHINE though
397 xargs cat < $f | sort -u |
398 sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
404 if [ -s $tf.dpdeps ]; then
407 *) echo > $DPDEPS;; # the echo is needed!
410 sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
412 # ensure we produce _something_ else egrep -v gets upset
417 */meta2dep*) meta2deps "$@";;
418 */meta2dirs*) meta2dirs "$@";;
419 */meta2src*) meta2src "$@";;