xref: /freebsd/contrib/bmake/mk/meta2deps.sh (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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/include
53#	$SB/obj-i386/bsd/lib/csu/i386
54#	$SB/obj-i386/bsd/lib/libc
55#	$SB/src/bsd/include
56#	$SB/src/bsd/sys/i386/include
57#	$SB/src/bsd/sys/sys
58#	$SB/src/pan-release/rtsock
59#	$SB/src/pfe-shared/include/jnx
60#.fi
61#
62#	Which can then be further processed by 'gendirdeps.mk'
63#
64#	If we are passed 'DPDEPS='"dpdeps", then for each src file
65#	outside of "CURDIR" we read, we output a line like:
66#.nf
67#
68#	DPDEPS_$path += $RELDIR
69#.fi
70#
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.
75
76
77# RCSid:
78#	$Id: meta2deps.sh,v 1.12 2016/12/13 20:44:16 sjg Exp $
79
80# Copyright (c) 2010-2013, Juniper Networks, Inc.
81# All rights reserved.
82#
83# Redistribution and use in source and binary forms, with or without
84# modification, are permitted provided that the following conditions
85# are met:
86# 1. Redistributions of source code must retain the above copyright
87#    notice, this list of conditions and the following disclaimer.
88# 2. Redistributions in binary form must reproduce the above copyright
89#    notice, this list of conditions and the following disclaimer in the
90#    documentation and/or other materials provided with the distribution.
91#
92# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
93# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
94# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
95# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
96# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
98# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
99# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
102# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103
104meta2src() {
105    cat /dev/null "$@" |
106    sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
107    sort -u
108}
109
110meta2dirs() {
111    cat /dev/null "$@" |
112    sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
113    sort -u
114}
115
116add_list() {
117    sep=' '
118    suffix=
119    while :
120    do
121	case "$1" in
122	"|") sep="$1"; shift;;
123	-s) suffix="$2"; shift 2;;
124	*) break;;
125	esac
126    done
127    name=$1
128    shift
129    eval list="\$$name"
130    for top in "$@"
131    do
132	case "$sep$list$sep" in
133	*"$sep$top$suffix$sep"*) continue;;
134	esac
135	list="${list:+$list$sep}$top$suffix"
136    done
137    eval "$name=\"$list\""
138}
139
140_excludes_f() {
141    egrep -v "$EXCLUDES"
142}
143
144meta2deps() {
145    DPDEPS=
146    SRCTOPS=$SRCTOP
147    OBJROOTS=
148    EXCLUDES=
149    while :
150    do
151	case "$1" in
152	*=*) eval export "$1"; shift;;
153	-a) MACHINE_ARCH=$2; shift 2;;
154	-m) MACHINE=$2; shift 2;;
155	-C) CURDIR=$2; shift 2;;
156	-H) HOST_TARGET=$2; shift 2;;
157	-S) add_list SRCTOPS $2; shift 2;;
158	-O) add_list OBJROOTS $2; shift 2;;
159	-X) add_list EXCLUDES '|' $2; shift 2;;
160	-R) RELDIR=$2; shift 2;;
161	-T) TARGET_SPEC=$2; shift 2;;
162	*) break;;
163	esac
164    done
165
166    _th= _o=
167    case "$MACHINE" in
168    host) _ht=$HOST_TARGET;;
169    esac
170
171    for o in $OBJROOTS
172    do
173	case "$MACHINE,/$o/" in
174	host,*$HOST_TARGET*) ;;
175	*$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
176	*) add_list _o $o; continue;;
177	esac
178	for x in $_ht $TARGET_SPEC $MACHINE
179	do
180	    case "$o" in
181	    "") continue;;
182	    */$x/) add_list _o ${o%$x/}; o=;;
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	    esac
187	done
188    done
189    OBJROOTS="$_o"
190
191    case "$OBJTOP" in
192    "")
193	for o in $OBJROOTS
194	do
195	    OBJTOP=$o${TARGET_SPEC:-$MACHINE}
196	    break
197	done
198	;;
199    esac
200    src_re=
201    obj_re=
202    add_list '|' -s '/*' src_re $SRCTOPS
203    add_list '|' -s '*' obj_re $OBJROOTS
204
205    [ -z "$RELDIR" ] && unset DPDEPS
206    tf=/tmp/m2d$$-$USER
207    rm -f $tf.*
208    trap 'rm -f $tf.*; trap 0' 0
209
210    > $tf.dirdep
211    > $tf.qual
212    > $tf.srcdep
213    > $tf.srcrel
214    > $tf.dpdeps
215
216    seenit=
217    seensrc=
218    lpid=
219    case "$EXCLUDES" in
220    "") _excludes=cat;;
221    *) _excludes=_excludes_f;;
222    esac
223    # handle @list files
224    case "$@" in
225    *@[!.]*)
226	for f in "$@"
227	do
228	    case "$f" in
229	    *.meta) cat $f;;
230	    @*) xargs cat < ${f#@};;
231	    *) cat $f;;
232	    esac
233	done
234	;;
235    *) cat /dev/null "$@";;
236    esac 2> /dev/null |
237    sed -e 's,^CWD,C C,;/^[CREFLM] /!d' -e "s,',,g" |
238    $_excludes |
239    while read op pid path junk
240    do
241	: op=$op pid=$pid path=$path
242	# we track cwd and ldir (of interest) per pid
243	# CWD is bmake's cwd
244	case "$lpid,$pid" in
245	,C) CWD=$path cwd=$path ldir=$path
246	    if [ -z "$SB" ]; then
247		SB=`echo $CWD | sed 's,/obj.*,,'`
248	    fi
249	    SRCTOP=${SRCTOP:-$SB/src}
250	    continue
251	    ;;
252	$pid,$pid) ;;
253	*)
254	    case "$lpid" in
255	    "") ;;
256	    *) eval ldir_$lpid=$ldir;;
257	    esac
258	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
259	    lpid=$pid
260	    ;;
261	esac
262
263	case "$op,$path" in
264	W,*srcrel|*.dirdep) continue;;
265	C,*)
266	    case "$path" in
267	    /*) cwd=$path;;
268	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
269	    esac
270	    # watch out for temp dirs that no longer exist
271	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
272	    eval cwd_$pid=$cwd
273	    continue
274	    ;;
275	F,*) # $path is new pid
276	    eval cwd_$path=$cwd ldir_$path=$ldir
277	    continue
278	    ;;
279	*)  dir=${path%/*}
280	    case "$path" in
281	    $src_re|$obj_re) ;;
282	    /*/stage/*) ;;
283	    /*) continue;;
284	    *)	for path in $ldir/$path $cwd/$path
285		do
286			test -e $path && break
287		done
288		dir=${path%/*}
289		;;
290	    esac
291	    ;;
292	esac
293	# avoid repeating ourselves...
294	case "$DPDEPS,$seensrc," in
295	,*)
296	    case ",$seenit," in
297	    *,$dir,*) continue;;
298	    esac
299	    ;;
300	*,$path,*) continue;;
301	esac
302	# canonicalize if needed
303	case "/$dir/" in
304	*/../*|*/./*)
305	    rdir=$dir
306	    dir=`cd $dir 2> /dev/null && /bin/pwd`
307	    seen="$rdir,$dir"
308	    ;;
309	*)  seen=$dir;;
310	esac
311	case "$dir" in
312	${CURDIR:-.}|"") continue;;
313	$src_re)
314	    # avoid repeating ourselves...
315	    case "$DPDEPS,$seensrc," in
316	    ,*)
317		case ",$seenit," in
318		*,$dir,*) continue;;
319		esac
320		;;
321	    esac
322	    ;;
323	*)
324	    case ",$seenit," in
325	    *,$dir,*) continue;;
326	    esac
327	    ;;
328	esac
329	if [ -d $path ]; then
330	    case "$path" in
331	    */..) ldir=${dir%/*};;
332	    *) ldir=$path;;
333	    esac
334	    continue
335	fi
336	[ -f $path ] || continue
337	case "$dir" in
338	$CWD) continue;;		# ignore
339	$src_re)
340	    seenit="$seenit,$seen"
341	    echo $dir >> $tf.srcdep
342	    case "$DPDEPS,$reldir,$seensrc," in
343	    ,*) ;;
344	    *)	seensrc="$seensrc,$path"
345		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
346		;;
347	    esac
348	    continue
349	    ;;
350	esac
351	# if there is a .dirdep we cannot skip
352	# just because we've seen the dir before.
353	if [ -s $path.dirdep ]; then
354	    # this file contains:
355	    # '# ${RELDIR}.<machine>'
356	    echo $path.dirdep >> $tf.qual
357	    continue
358	elif [ -s $dir.dirdep ]; then
359	    echo $dir.dirdep >> $tf.qual
360	    seenit="$seenit,$seen"
361	    continue
362	fi
363	seenit="$seenit,$seen"
364	case "$dir" in
365	$obj_re)
366	    echo $dir;;
367	esac
368    done > $tf.dirdep
369    _nl=echo
370    for f in $tf.dirdep $tf.qual $tf.srcdep
371    do
372	[ -s $f ] || continue
373	case $f in
374	*qual) # a list of .dirdep files
375	    # we can prefix everything with $OBJTOP to
376	    # tell gendirdeps.mk that these are
377	    # DIRDEP entries, since they are already
378	    # qualified with .<machine> as needed.
379	    # We strip .$MACHINE though
380	    xargs cat < $f | sort -u |
381	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
382	    ;;
383	*)  sort -u $f;;
384	esac
385	_nl=:
386    done
387    if [ -s $tf.dpdeps ]; then
388	case "$DPDEPS" in
389	*/*) ;;
390	*) echo > $DPDEPS;;		# the echo is needed!
391	esac
392	sort -u $tf.dpdeps |
393	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
394    fi
395    # ensure we produce _something_ else egrep -v gets upset
396    $_nl
397}
398
399case /$0 in
400*/meta2dep*) meta2deps "$@";;
401*/meta2dirs*) meta2dirs "$@";;
402*/meta2src*) meta2src "$@";;
403esac
404