xref: /freebsd/contrib/bmake/mk/meta2deps.sh (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1#!/bin/sh
2
3# NAME:
4#	meta2deps.sh - extract useful info from .meta files
5#
6# SYNOPSIS:
7#	meta2deps.sh SB="SB" "meta" ...
8#
9# DESCRIPTION:
10#	This script looks each "meta" file and extracts the
11#	information needed to deduce build and src dependencies.
12#
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
15#	syscalls.
16#
17#	The typical meta file looks like::
18#.nf
19#
20#	# Meta data file "path"
21#	CMD "command-line"
22#	CWD "cwd"
23#	TARGET "target"
24#	-- command output --
25#	-- filemon acquired metadata --
26#	# buildmon version 2
27#	V 2
28#	E "pid" "path"
29#	R "pid" "path"
30#	C "pid" "cwd"
31#	R "pid" "path"
32#	X "pid" "status"
33#.fi
34#
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).
37#
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.
41#
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
44#	interested in.
45#
46#	Any "path" which lies outside of the sandbox "SB" is generally
47#	not of interest and is ignored.
48#
49#	The output, is a set of absolute paths with "SB" like:
50#.nf
51#
52#	$SB/obj-i386/junos/gnu/lib/csu
53#	$SB/obj-i386/junos/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
57#	$SB/src/bsd/include
58#	$SB/src/bsd/sys/i386/include
59#	$SB/src/bsd/sys/sys
60#	$SB/src/pan-release/rtsock
61#	$SB/src/pfe-shared/include/jnx
62#.fi
63#
64#	Which can then be further processed by 'gendirdeps.mk'
65#
66#	If we are passed 'DPDEPS='"dpdeps", then for each src file
67#	outside of "CURDIR" we read, we output a line like:
68#.nf
69#
70#	DPDEPS_$path += $RELDIR
71#.fi
72#
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.
77
78
79# RCSid:
80#	$Id: meta2deps.sh,v 1.5 2013/02/10 19:21:46 sjg Exp $
81
82# Copyright (c) 2010-2012, Juniper Networks, Inc.
83# All rights reserved.
84#
85# Redistribution and use in source and binary forms, with or without
86# modification, are permitted provided that the following conditions
87# are met:
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.
93#
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.
105
106meta2src() {
107    cat /dev/null "$@" |
108    sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
109    sort -u
110}
111
112meta2dirs() {
113    cat /dev/null "$@" |
114    sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
115    sort -u
116}
117
118meta2deps() {
119    DPDEPS=
120    while :
121    do
122	case "$1" in
123	*=*) eval export "$1"; shift;;
124	*) break;;
125	esac
126    done
127
128    [ -z "$RELDIR" ] && unset DPDEPS
129    tf=/tmp/m2d$$-$USER
130    rm -f $tf.*
131    trap 'rm -f $tf.*; trap 0' 0
132
133    > $tf.dirdep
134    > $tf.qual
135    > $tf.srcdep
136    > $tf.srcrel
137    > $tf.dpdeps
138
139    seenit=
140    seensrc=
141    lpid=
142    cat /dev/null "$@" |
143    sed -e 's,^CWD,C C,;/^[CREFL] /!d' -e "s,',,g" |
144    while read op pid path junk
145    do
146	: op=$op pid=$pid path=$path
147	# we track cwd and ldir (of interest) per pid
148	# CWD is bmake's cwd
149	case "$lpid,$pid" in
150	,C) CWD=$path cwd=$path ldir=$path
151	    if [ -z "$SB" ]; then
152		SB=`echo $CWD | sed 's,/obj.*,,'`
153	    fi
154	    SRCTOP=${SRCTOP:-$SB/src}
155	    continue
156	    ;;
157	$pid,$pid) ;;
158	*)
159	    case "$lpid" in
160	    "") ;;
161	    *) eval ldir_$lpid=$ldir cwd_$lpid=$cwd;;
162	    esac
163	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
164	    lpid=$pid
165	    ;;
166	esac
167
168	case "$op,$path" in
169	*.dirdep)  continue;;
170	W,*srcrel) continue;;
171	C,*)
172	    case "$path" in
173	    /*) cwd=$path;;
174	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
175	    esac
176	    # watch out for temp dirs that no longer exist
177	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
178	    continue
179	    ;;
180	F,*)  eval cwd_$path=$cwd ldir_$path=$ldir
181	    continue
182	    ;;
183	*)  dir=${path%/*}
184	    case "$path" in
185	    $SB/*|${SB_BACKING_SB:-$SB}/*) ;;
186	    $SB_OBJROOT*) ;;
187	    /*/stage/*) ;;
188	    /*) continue;;
189	    *)	for path in $ldir/$path $cwd/$path
190		do
191			test -e $path && break
192		done
193		dir=${path%/*}
194		;;
195	    esac
196	    ;;
197	esac
198	# avoid repeating ourselves...
199	case "$DPDEPS,$seensrc," in
200	,*)
201	    case ",$seenit," in
202	    *,$dir,*) continue;;
203	    esac
204	    ;;
205	*,$path,*) continue;;
206	esac
207	# canonicalize if needed
208	case "/$dir/" in
209	*/../*|*/./*)
210	    rdir=$dir
211	    dir=`cd $dir 2> /dev/null && /bin/pwd`
212	    seen="$rdir,$dir"
213	    ;;
214	*)  seen=$dir;;
215	esac
216	case "$dir" in
217	${CURDIR:-.}|${CURDIR:-.}/*|"") continue;;
218	$SRCTOP/*|${SB_BACKING_SB:-$SB}/src/*)
219	    # avoid repeating ourselves...
220	    case "$DPDEPS,$seensrc," in
221	    ,*)
222		case ",$seenit," in
223		*,$dir,*) continue;;
224		esac
225		;;
226	    esac
227	    ;;
228	*)
229	    case ",$seenit," in
230	    *,$dir,*) continue;;
231	    esac
232	    ;;
233	esac
234	if [ -d $path ]; then
235	    case "$path" in
236	    */..) ldir=${dir%/*};;
237	    *) ldir=$path;;
238	    esac
239	    continue
240	fi
241	[ -f $path ] || continue
242	case "$dir" in
243	$CWD) continue;;		# ignore
244	$SRCTOP/*|${SB_BACKING_SB:-$SB}/src/*)
245	    seenit="$seenit,$seen"
246	    echo $dir >> $tf.srcdep
247	    case "$DPDEPS,$reldir,$seensrc," in
248	    ,*) ;;
249	    *)	seensrc="$seensrc,$path"
250		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
251		;;
252	    esac
253	    continue
254	    ;;
255	esac
256	# if there is a .dirdep we cannot skip
257	# just because we've seen the dir before.
258	if [ -s $path.dirdep ]; then
259	    # this file contains:
260	    # '# ${RELDIR}.<machine>'
261	    echo $path.dirdep >> $tf.qual
262	    continue
263	elif [ -s $dir.dirdep ]; then
264	    echo $dir.dirdep >> $tf.qual
265	    seenit="$seenit,$seen"
266	    continue
267	fi
268	seenit="$seenit,$seen"
269	case "$dir" in
270	$SB/*|${SB_OBJROOT:-$SB/}*|${SB_BACKING_SB:-$SB}/*)
271	    echo $dir;;
272	esac
273    done > $tf.dirdep
274    _nl=echo
275    for f in $tf.dirdep $tf.qual $tf.srcdep
276    do
277	[ -s $f ] || continue
278	case $f in
279	*qual) # a list of .dirdep files
280	    # we can prefix everthing with $OBJTOP to
281	    # tell gendirdeps.mk that these are
282	    # DIRDEP entries, since they are already
283	    # qualified with .<machine> as needed.
284	    # We strip .$MACHINE though
285	    xargs cat < $f | sort -u |
286	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.$MACHINE\$,,"
287	    ;;
288	*)  sort -u $f;;
289	esac
290	_nl=:
291    done
292    if [ -s $tf.dpdeps ]; then
293	case "$DPDEPS" in
294	*/*) ;;
295	*) echo > $DPDEPS;;		# the echo is needed!
296	esac
297	sort -u $tf.dpdeps |
298	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
299    fi
300    # ensure we produce _something_ else egrep -v gets upset
301    $_nl
302}
303
304case /$0 in
305*/meta2dep*) meta2deps "$@";;
306*/meta2dirs*) meta2dirs "$@";;
307*/meta2src*) meta2src "$@";;
308esac
309