xref: /freebsd/share/mk/meta2deps.sh (revision 4fd0d10e0fe684211328bc148edf89a792425b39)
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/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
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.2 2011/10/02 00:34:47 sjg Exp $
81
82# Copyright (c) 2010, Juniper Networks, Inc.
83#
84# Redistribution and use in source and binary forms, with or without
85# modification, are permitted provided that the following conditions
86# are met:
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.
92#
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.
104
105meta2src() {
106    cat /dev/null "$@" |
107    sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
108    sort -u
109}
110
111meta2dirs() {
112    cat /dev/null "$@" |
113    sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
114    sort -u
115}
116
117add_list() {
118    sep=' '
119    suffix=
120    while :
121    do
122	case "$1" in
123	"|") sep="$1"; shift;;
124	-s) suffix="$2"; shift 2;;
125	*) break;;
126	esac
127    done
128    name=$1
129    shift
130    eval list="\$$name"
131    for top in "$@"
132    do
133	case "$sep$list$sep" in
134	*"$sep$top$suffix$sep"*) continue;;
135	esac
136	list="${list:+$list$sep}$top$suffix"
137    done
138    eval "$name=\"$list\""
139}
140
141meta2deps() {
142    DPDEPS=
143    SRCTOPS=$SRCTOP
144    OBJROOTS=
145    while :
146    do
147	case "$1" in
148	*=*) eval export "$1"; shift;;
149	-a) MACHINE_ARCH=$2; shift 2;;
150	-m) MACHINE=$2; shift 2;;
151	-C) CURDIR=$2; shift 2;;
152	-H) HOST_TARGET=$2; shift 2;;
153	-S) add_list SRCTOPS $2; shift 2;;
154	-O) add_list OBJROOTS $2; shift 2;;
155	-R) RELDIR=$2; shift 2;;
156	-T) TARGET_SPEC=$2; shift 2;;
157	*) break;;
158	esac
159    done
160
161    _th= _o=
162    case "$MACHINE" in
163    host) _ht=$HOST_TARGET;;
164    esac
165
166    for o in $OBJROOTS
167    do
168	case "$MACHINE,/$o/" in
169	host,*$HOST_TARGET*) ;;
170	*$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
171	*) add_list _o $o; continue;;
172	esac
173	for x in $_ht $TARGET_SPEC $MACHINE
174	do
175	    case "$o" in
176	    "") continue;;
177	    */$x/) add_list _o ${o%$x/}; o=;;
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	    esac
182	done
183    done
184    OBJROOTS="$_o"
185
186    case "$OBJTOP" in
187    "")
188	for o in $OBJROOTS
189	do
190	    OBJTOP=$o${TARGET_SPEC:-$MACHINE}
191	    break
192	done
193	;;
194    esac
195    src_re=
196    obj_re=
197    add_list '|' -s '/*' src_re $SRCTOPS
198    add_list '|' -s '*' obj_re $OBJROOTS
199
200    [ -z "$RELDIR" ] && unset DPDEPS
201    tf=/tmp/m2d$$-$USER
202    rm -f $tf.*
203    trap 'rm -f $tf.*; trap 0' 0
204
205    > $tf.dirdep
206    > $tf.qual
207    > $tf.srcdep
208    > $tf.srcrel
209    > $tf.dpdeps
210
211    seenit=
212    seensrc=
213    lpid=
214    cat /dev/null "$@" |
215    sed -e 's,^CWD,C C,;/^[CREFL] /!d' -e "s,',,g" |
216    while read op pid path junk
217    do
218	: op=$op pid=$pid path=$path
219	# we track cwd and ldir (of interest) per pid
220	# CWD is bmake's cwd
221	case "$lpid,$pid" in
222	,C) CWD=$path cwd=$path ldir=$path
223	    if [ -z "$SB" ]; then
224		SB=`echo $CWD | sed 's,/obj.*,,'`
225	    fi
226	    SRCTOP=${SRCTOP:-$SB/src}
227	    continue
228	    ;;
229	$pid,$pid) ;;
230	*)
231	    case "$lpid" in
232	    "") ;;
233	    *) eval ldir_$lpid=$ldir cwd_$lpid=$cwd;;
234	    esac
235	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
236	    lpid=$pid
237	    ;;
238	esac
239
240	case "$op,$path" in
241	W,*srcrel|*.dirdep) continue;;
242	C,*)
243	    case "$path" in
244	    /*) cwd=$path;;
245	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
246	    esac
247	    # watch out for temp dirs that no longer exist
248	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
249	    continue
250	    ;;
251	F,*)  eval cwd_$path=$cwd ldir_$path=$ldir
252	    continue
253	    ;;
254	*)  dir=${path%/*}
255	    case "$path" in
256	    $src_re|$obj_re) ;;
257	    /*/stage/*) ;;
258	    /*) continue;;
259	    *)	for path in $ldir/$path $cwd/$path
260		do
261			test -e $path && break
262		done
263		dir=${path%/*}
264		;;
265	    esac
266	    ;;
267	esac
268	# avoid repeating ourselves...
269	case "$DPDEPS,$seensrc," in
270	,*)
271	    case ",$seenit," in
272	    *,$dir,*) continue;;
273	    esac
274	    ;;
275	*,$path,*) continue;;
276	esac
277	# canonicalize if needed
278	case "/$dir/" in
279	*/../*|*/./*)
280	    rdir=$dir
281	    dir=`cd $dir 2> /dev/null && /bin/pwd`
282	    seen="$rdir,$dir"
283	    ;;
284	*)  seen=$dir;;
285	esac
286	case "$dir" in
287	${CURDIR:-.}|${CURDIR:-.}/*|"") continue;;
288	$src_re)
289	    # avoid repeating ourselves...
290	    case "$DPDEPS,$seensrc," in
291	    ,*)
292		case ",$seenit," in
293		*,$dir,*) continue;;
294		esac
295		;;
296	    esac
297	    ;;
298	*)
299	    case ",$seenit," in
300	    *,$dir,*) continue;;
301	    esac
302	    ;;
303	esac
304	if [ -d $path ]; then
305	    case "$path" in
306	    */..) ldir=${dir%/*};;
307	    *) ldir=$path;;
308	    esac
309	    continue
310	fi
311	[ -f $path ] || continue
312	case "$dir" in
313	$CWD) continue;;		# ignore
314	$src_re)
315	    seenit="$seenit,$seen"
316	    echo $dir >> $tf.srcdep
317	    case "$DPDEPS,$reldir,$seensrc," in
318	    ,*) ;;
319	    *)	seensrc="$seensrc,$path"
320		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
321		;;
322	    esac
323	    continue
324	    ;;
325	esac
326	# if there is a .dirdep we cannot skip
327	# just because we've seen the dir before.
328	if [ -s $path.dirdep ]; then
329	    # this file contains:
330	    # '# ${RELDIR}.<machine>'
331	    echo $path.dirdep >> $tf.qual
332	    continue
333	elif [ -s $dir.dirdep ]; then
334	    echo $dir.dirdep >> $tf.qual
335	    seenit="$seenit,$seen"
336	    continue
337	fi
338	seenit="$seenit,$seen"
339	case "$dir" in
340	$obj_re)
341	    echo $dir;;
342	esac
343    done > $tf.dirdep
344    _nl=echo
345    for f in $tf.dirdep $tf.qual $tf.srcdep
346    do
347	[ -s $f ] || continue
348	case $f in
349	*qual) # a list of .dirdep files
350	    # we can prefix everthing with $OBJTOP to
351	    # tell gendirdeps.mk that these are
352	    # DIRDEP entries, since they are already
353	    # qualified with .<machine> as needed.
354	    # We strip .$MACHINE though
355	    xargs cat < $f | sort -u |
356	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
357	    ;;
358	*)  sort -u $f;;
359	esac
360	_nl=:
361    done
362    if [ -s $tf.dpdeps ]; then
363	case "$DPDEPS" in
364	*/*) ;;
365	*) echo > $DPDEPS;;		# the echo is needed!
366	esac
367	sort -u $tf.dpdeps |
368	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
369    fi
370    # ensure we produce _something_ else egrep -v gets upset
371    $_nl
372}
373
374case /$0 in
375*/meta2dep*) meta2deps "$@";;
376*/meta2dirs*) meta2dirs "$@";;
377*/meta2src*) meta2src "$@";;
378esac
379