1105236Scharnier#!/bin/sh
2105236Scharnier
3105236Scharnier# NAME:
41590Srgrimes#	meta2deps.sh - extract useful info from .meta files
51590Srgrimes#
61590Srgrimes# SYNOPSIS:
71590Srgrimes#	meta2deps.sh SB="SB" "meta" ...
81590Srgrimes#	
91590Srgrimes# DESCRIPTION:
101590Srgrimes#	This script looks each "meta" file and extracts the
111590Srgrimes#	information needed to deduce build and src dependencies.
121590Srgrimes#	
131590Srgrimes#	To do this, we extract the 'CWD' record as well as all the
141590Srgrimes#	syscall traces which describe 'R'ead, 'C'hdir and 'E'xec
151590Srgrimes#	syscalls.
161590Srgrimes#
171590Srgrimes#	The typical meta file looks like::
181590Srgrimes#.nf
191590Srgrimes#
201590Srgrimes#	# Meta data file "path"
211590Srgrimes#	CMD "command-line"
221590Srgrimes#	CWD "cwd"
231590Srgrimes#	TARGET "target"
241590Srgrimes#	-- command output --
251590Srgrimes#	-- filemon acquired metadata --
261590Srgrimes#	# buildmon version 2
271590Srgrimes#	V 2
281590Srgrimes#	E "pid" "path"
291590Srgrimes#	R "pid" "path"
301590Srgrimes#	C "pid" "cwd"
311590Srgrimes#	R "pid" "path"
321590Srgrimes#	X "pid" "status"
331590Srgrimes#.fi
341590Srgrimes#
351590Srgrimes#	The fact that all the syscall entry lines start with a single
361590Srgrimes#	character make these files quite easy to process using sed(1).
371590Srgrimes#
381590Srgrimes#	To simplify the logic the 'CWD' line is made to look like a
391590Srgrimes#	normal 'C'hdir entry, and "cwd" is remembered so that it can
401590Srgrimes#	be prefixed to any "path" which is not absolute.
411590Srgrimes#
421590Srgrimes#	If the "path" being read ends in '.srcrel' it is the content
431590Srgrimes#	of (actually the first line of) that file that we are
441590Srgrimes#	interested in.
451590Srgrimes#
461590Srgrimes#	Any "path" which lies outside of the sandbox "SB" is generally
471590Srgrimes#	not of interest and is ignored.
481590Srgrimes#
491590Srgrimes#	The output, is a set of absolute paths with "SB" like:
501590Srgrimes#.nf
511590Srgrimes#
521590Srgrimes#	$SB/obj-i386/bsd/gnu/lib/csu
531590Srgrimes#	$SB/obj-i386/bsd/gnu/lib/libgcc
54227458Seadler#	$SB/obj-i386/bsd/include
551590Srgrimes#	$SB/obj-i386/bsd/lib/csu/i386-elf
561590Srgrimes#	$SB/obj-i386/bsd/lib/libc
571590Srgrimes#	$SB/src/bsd/include
581590Srgrimes#	$SB/src/bsd/sys/i386/include
591590Srgrimes#	$SB/src/bsd/sys/sys
601590Srgrimes#	$SB/src/pan-release/rtsock
611590Srgrimes#	$SB/src/pfe-shared/include/jnx
621590Srgrimes#.fi
631590Srgrimes#
641590Srgrimes#	Which can then be further processed by 'gendirdeps.mk'
651590Srgrimes#
661590Srgrimes#	If we are passed 'DPDEPS='"dpdeps", then for each src file
671590Srgrimes#	outside of "CURDIR" we read, we output a line like:
681590Srgrimes#.nf
691590Srgrimes#
701590Srgrimes#	DPDEPS_$path += $RELDIR
711590Srgrimes#.fi
721590Srgrimes#
731590Srgrimes#	with "$path" geting turned into reldir's, so that we can end
741590Srgrimes#	up with a list of all the directories which depend on each src
751590Srgrimes#	file in another directory.  This can allow for efficient yet
761590Srgrimes#	complete testing of changes.
77105236Scharnier
78105236Scharnier
791590Srgrimes# RCSid:
801590Srgrimes#	$Id: meta2deps.sh,v 1.6 2013/05/11 05:16:26 sjg Exp $
811590Srgrimes
821590Srgrimes# Copyright (c) 2010-2013, Juniper Networks, Inc.
831590Srgrimes# All rights reserved.
84105236Scharnier# 
85105236Scharnier# Redistribution and use in source and binary forms, with or without
86105236Scharnier# modification, are permitted provided that the following conditions 
871590Srgrimes# are met: 
881590Srgrimes# 1. Redistributions of source code must retain the above copyright
891590Srgrimes#    notice, this list of conditions and the following disclaimer. 
901590Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
91105236Scharnier#    notice, this list of conditions and the following disclaimer in the
921590Srgrimes#    documentation and/or other materials provided with the distribution.  
93105236Scharnier# 
941590Srgrimes# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
95105236Scharnier# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
96105236Scharnier# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
971590Srgrimes# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
98105236Scharnier# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99105236Scharnier# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
100105236Scharnier# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1011590Srgrimes# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102105236Scharnier# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1031590Srgrimes# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
104105236Scharnier# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
1051590Srgrimes
106105236Scharniermeta2src() {
1071590Srgrimes    cat /dev/null "$@" |
1081590Srgrimes    sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
1091590Srgrimes    sort -u
1101590Srgrimes}
1111590Srgrimes    
1121590Srgrimesmeta2dirs() {
113105236Scharnier    cat /dev/null "$@" |
1141590Srgrimes    sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
1151590Srgrimes    sort -u
1161590Srgrimes}
1171590Srgrimes
118105236Scharnieradd_list() {
1191590Srgrimes    sep=' '
1201590Srgrimes    suffix=
1211590Srgrimes    while :
1221590Srgrimes    do
1231590Srgrimes	case "$1" in
1241590Srgrimes	"|") sep="$1"; shift;;
1251590Srgrimes	-s) suffix="$2"; shift 2;;
126105236Scharnier	*) break;;
1271590Srgrimes	esac
128105236Scharnier    done
1291590Srgrimes    name=$1
1301590Srgrimes    shift
1311590Srgrimes    eval list="\$$name"
1321590Srgrimes    for top in "$@"
1331590Srgrimes    do
1341590Srgrimes	case "$sep$list$sep" in
1351590Srgrimes	*"$sep$top$suffix$sep"*) continue;;
136105236Scharnier	esac
1371590Srgrimes	list="${list:+$list$sep}$top$suffix"
138105236Scharnier    done
1391590Srgrimes    eval "$name=\"$list\""
1401590Srgrimes}
1411590Srgrimes
1421590Srgrimesmeta2deps() {
143    DPDEPS=
144    SRCTOPS=$SRCTOP
145    OBJROOTS=
146    while :
147    do
148	case "$1" in
149	*=*) eval export "$1"; shift;;
150	-a) MACHINE_ARCH=$2; shift 2;;
151	-m) MACHINE=$2; shift 2;;
152	-C) CURDIR=$2; shift 2;;
153	-H) HOST_TARGET=$2; shift 2;;
154	-S) add_list SRCTOPS $2; shift 2;;
155	-O) add_list OBJROOTS $2; shift 2;;
156	-R) RELDIR=$2; shift 2;;
157	-T) TARGET_SPEC=$2; shift 2;;
158	*) break;;
159	esac
160    done
161
162    _th= _o=
163    case "$MACHINE" in
164    host) _ht=$HOST_TARGET;;
165    esac
166    
167    for o in $OBJROOTS
168    do
169	case "$MACHINE,/$o/" in
170	host,*$HOST_TARGET*) ;;
171	*$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
172	*) add_list _o $o; continue;;
173	esac
174	for x in $_ht $TARGET_SPEC $MACHINE
175	do
176	    case "$o" in
177	    "") continue;;
178	    */$x/) add_list _o ${o%$x/}; o=;;
179	    */$x) add_list _o ${o%$x}; o=;;
180	    *$x/) add_list _o ${o%$x/}; o=;;
181	    *$x) add_list _o ${o%$x}; o=;;
182	    esac
183	done
184    done
185    OBJROOTS="$_o"
186
187    case "$OBJTOP" in
188    "")
189	for o in $OBJROOTS
190	do
191	    OBJTOP=$o${TARGET_SPEC:-$MACHINE}
192	    break
193	done
194	;;
195    esac
196    src_re=
197    obj_re=
198    add_list '|' -s '/*' src_re $SRCTOPS
199    add_list '|' -s '*' obj_re $OBJROOTS
200    
201    [ -z "$RELDIR" ] && unset DPDEPS
202    tf=/tmp/m2d$$-$USER
203    rm -f $tf.*
204    trap 'rm -f $tf.*; trap 0' 0
205
206    > $tf.dirdep
207    > $tf.qual
208    > $tf.srcdep
209    > $tf.srcrel
210    > $tf.dpdeps
211
212    seenit=
213    seensrc=
214    lpid=
215    cat /dev/null "$@" |
216    sed -e 's,^CWD,C C,;/^[CREFL] /!d' -e "s,',,g" |
217    while read op pid path junk
218    do
219	: op=$op pid=$pid path=$path
220	# we track cwd and ldir (of interest) per pid
221	# CWD is bmake's cwd
222	case "$lpid,$pid" in
223	,C) CWD=$path cwd=$path ldir=$path
224	    if [ -z "$SB" ]; then
225		SB=`echo $CWD | sed 's,/obj.*,,'`
226	    fi
227	    SRCTOP=${SRCTOP:-$SB/src}
228	    continue
229	    ;;
230	$pid,$pid) ;;
231	*)
232	    case "$lpid" in
233	    "") ;;
234	    *) eval ldir_$lpid=$ldir cwd_$lpid=$cwd;;
235	    esac
236	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
237	    lpid=$pid
238	    ;;
239	esac
240
241	case "$op,$path" in
242	W,*srcrel|*.dirdep) continue;;
243	C,*)
244	    case "$path" in
245	    /*) cwd=$path;;
246	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
247	    esac
248	    # watch out for temp dirs that no longer exist
249	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
250	    continue
251	    ;;
252	F,*)  eval cwd_$path=$cwd ldir_$path=$ldir
253	    continue
254	    ;;	  
255	*)  dir=${path%/*}
256	    case "$path" in
257	    $src_re|$obj_re) ;;
258	    /*/stage/*) ;;
259	    /*) continue;;
260	    *)	for path in $ldir/$path $cwd/$path
261		do
262			test -e $path && break
263		done
264		dir=${path%/*}
265		;;
266	    esac
267	    ;;
268	esac
269	# avoid repeating ourselves...
270	case "$DPDEPS,$seensrc," in
271	,*)
272	    case ",$seenit," in
273	    *,$dir,*) continue;;
274	    esac
275	    ;;
276	*,$path,*) continue;;
277	esac
278	# canonicalize if needed
279	case "/$dir/" in
280	*/../*|*/./*)
281	    rdir=$dir
282	    dir=`cd $dir 2> /dev/null && /bin/pwd`
283	    seen="$rdir,$dir"
284	    ;;
285	*)  seen=$dir;;
286	esac
287	case "$dir" in
288	${CURDIR:-.}|${CURDIR:-.}/*|"") continue;;
289	$src_re)
290	    # avoid repeating ourselves...
291	    case "$DPDEPS,$seensrc," in
292	    ,*)
293		case ",$seenit," in
294		*,$dir,*) continue;;
295		esac
296		;;
297	    esac
298	    ;;
299	*)
300	    case ",$seenit," in
301	    *,$dir,*) continue;;
302	    esac
303	    ;;
304	esac
305	if [ -d $path ]; then
306	    case "$path" in
307	    */..) ldir=${dir%/*};;
308	    *) ldir=$path;;
309	    esac
310	    continue
311	fi
312	[ -f $path ] || continue
313	case "$dir" in
314	$CWD) continue;;		# ignore
315	$src_re)
316	    seenit="$seenit,$seen"
317	    echo $dir >> $tf.srcdep
318	    case "$DPDEPS,$reldir,$seensrc," in
319	    ,*) ;;
320	    *)	seensrc="$seensrc,$path"
321		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
322		;;
323	    esac
324	    continue
325	    ;;
326	esac
327	# if there is a .dirdep we cannot skip
328	# just because we've seen the dir before.
329	if [ -s $path.dirdep ]; then
330	    # this file contains:
331	    # '# ${RELDIR}.<machine>'
332	    echo $path.dirdep >> $tf.qual
333	    continue
334	elif [ -s $dir.dirdep ]; then
335	    echo $dir.dirdep >> $tf.qual
336	    seenit="$seenit,$seen"
337	    continue
338	fi
339	seenit="$seenit,$seen"
340	case "$dir" in
341	$obj_re)
342	    echo $dir;;
343	esac
344    done > $tf.dirdep
345    _nl=echo
346    for f in $tf.dirdep $tf.qual $tf.srcdep
347    do
348	[ -s $f ] || continue
349	case $f in
350	*qual) # a list of .dirdep files
351	    # we can prefix everthing with $OBJTOP to
352	    # tell gendirdeps.mk that these are
353	    # DIRDEP entries, since they are already
354	    # qualified with .<machine> as needed.
355	    # We strip .$MACHINE though
356	    xargs cat < $f | sort -u |
357	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
358	    ;;
359	*)  sort -u $f;;
360	esac
361	_nl=:
362    done
363    if [ -s $tf.dpdeps ]; then
364	case "$DPDEPS" in
365	*/*) ;;
366	*) echo > $DPDEPS;;		# the echo is needed!
367	esac
368	sort -u $tf.dpdeps |
369	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
370    fi
371    # ensure we produce _something_ else egrep -v gets upset
372    $_nl
373}
374
375case /$0 in
376*/meta2dep*) meta2deps "$@";;
377*/meta2dirs*) meta2dirs "$@";;
378*/meta2src*) meta2src "$@";;
379esac
380