xref: /freebsd/share/mk/meta2deps.sh (revision e17f5b1d)
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#	$FreeBSD$
79#	$Id: meta2deps.sh,v 1.12 2016/12/13 20:44:16 sjg Exp $
80
81# Copyright (c) 2010-2013, Juniper Networks, Inc.
82# All rights reserved.
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
141_excludes_f() {
142    egrep -v "$EXCLUDES"
143}
144
145meta2deps() {
146    DPDEPS=
147    SRCTOPS=$SRCTOP
148    OBJROOTS=
149    EXCLUDES=
150    while :
151    do
152	case "$1" in
153	*=*) eval export "$1"; shift;;
154	-a) MACHINE_ARCH=$2; shift 2;;
155	-m) MACHINE=$2; shift 2;;
156	-C) CURDIR=$2; shift 2;;
157	-H) HOST_TARGET=$2; shift 2;;
158	-S) add_list SRCTOPS $2; shift 2;;
159	-O) add_list OBJROOTS $2; shift 2;;
160	-X) add_list EXCLUDES '|' $2; shift 2;;
161	-R) RELDIR=$2; shift 2;;
162	-T) TARGET_SPEC=$2; shift 2;;
163	*) break;;
164	esac
165    done
166
167    _th= _o=
168    case "$MACHINE" in
169    host) _ht=$HOST_TARGET;;
170    esac
171
172    for o in $OBJROOTS
173    do
174	case "$MACHINE,/$o/" in
175	host,*$HOST_TARGET*) ;;
176	*$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
177	*) add_list _o $o; continue;;
178	esac
179	for x in $_ht $TARGET_SPEC $MACHINE
180	do
181	    case "$o" in
182	    "") continue;;
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	    *$x) add_list _o ${o%$x}; o=;;
187	    esac
188	done
189    done
190    OBJROOTS="$_o"
191
192    case "$OBJTOP" in
193    "")
194	for o in $OBJROOTS
195	do
196	    OBJTOP=$o${TARGET_SPEC:-$MACHINE}
197	    break
198	done
199	;;
200    esac
201    src_re=
202    obj_re=
203    add_list '|' -s '/*' src_re $SRCTOPS
204    add_list '|' -s '*' obj_re $OBJROOTS
205
206    [ -z "$RELDIR" ] && unset DPDEPS
207    tf=/tmp/m2d$$-$USER
208    rm -f $tf.*
209    trap 'rm -f $tf.*; trap 0' 0
210
211    > $tf.dirdep
212    > $tf.qual
213    > $tf.srcdep
214    > $tf.srcrel
215    > $tf.dpdeps
216
217    seenit=
218    seensrc=
219    lpid=
220    case "$EXCLUDES" in
221    "") _excludes=cat;;
222    *) _excludes=_excludes_f;;
223    esac
224    # handle @list files
225    case "$@" in
226    *@[!.]*)
227	for f in "$@"
228	do
229	    case "$f" in
230	    *.meta) cat $f;;
231	    @*) xargs cat < ${f#@};;
232	    *) cat $f;;
233	    esac
234	done
235	;;
236    *) cat /dev/null "$@";;
237    esac 2> /dev/null |
238    sed -e 's,^CWD,C C,;/^[CREFLM] /!d' -e "s,',,g" |
239    $_excludes |
240    while read op pid path junk
241    do
242	: op=$op pid=$pid path=$path
243	# we track cwd and ldir (of interest) per pid
244	# CWD is bmake's cwd
245	case "$lpid,$pid" in
246	,C) CWD=$path cwd=$path ldir=$path
247	    if [ -z "$SB" ]; then
248		SB=`echo $CWD | sed 's,/obj.*,,'`
249	    fi
250	    SRCTOP=${SRCTOP:-$SB/src}
251	    continue
252	    ;;
253	$pid,$pid) ;;
254	*)
255	    case "$lpid" in
256	    "") ;;
257	    *) eval ldir_$lpid=$ldir;;
258	    esac
259	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
260	    lpid=$pid
261	    ;;
262	esac
263
264	case "$op,$path" in
265	W,*srcrel|*.dirdep) continue;;
266	C,*)
267	    case "$path" in
268	    /*) cwd=$path;;
269	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
270	    esac
271	    # watch out for temp dirs that no longer exist
272	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
273	    eval cwd_$pid=$cwd
274	    continue
275	    ;;
276	F,*) # $path is new pid
277	    eval cwd_$path=$cwd ldir_$path=$ldir
278	    continue
279	    ;;
280	*)  dir=${path%/*}
281	    case "$path" in
282	    $src_re|$obj_re) ;;
283	    /*/stage/*) ;;
284	    /*) continue;;
285	    *)	for path in $ldir/$path $cwd/$path
286		do
287			test -e $path && break
288		done
289		dir=${path%/*}
290		;;
291	    esac
292	    ;;
293	esac
294	# avoid repeating ourselves...
295	case "$DPDEPS,$seensrc," in
296	,*)
297	    case ",$seenit," in
298	    *,$dir,*) continue;;
299	    esac
300	    ;;
301	*,$path,*) continue;;
302	esac
303	# canonicalize if needed
304	case "/$dir/" in
305	*/../*|*/./*)
306	    rdir=$dir
307	    dir=`cd $dir 2> /dev/null && /bin/pwd`
308	    seen="$rdir,$dir"
309	    ;;
310	*)  seen=$dir;;
311	esac
312	case "$dir" in
313	${CURDIR:-.}|"") continue;;
314	$src_re)
315	    # avoid repeating ourselves...
316	    case "$DPDEPS,$seensrc," in
317	    ,*)
318		case ",$seenit," in
319		*,$dir,*) continue;;
320		esac
321		;;
322	    esac
323	    ;;
324	*)
325	    case ",$seenit," in
326	    *,$dir,*) continue;;
327	    esac
328	    ;;
329	esac
330	if [ -d $path ]; then
331	    case "$path" in
332	    */..) ldir=${dir%/*};;
333	    *) ldir=$path;;
334	    esac
335	    continue
336	fi
337	[ -f $path ] || continue
338	case "$dir" in
339	$CWD) continue;;		# ignore
340	$src_re)
341	    seenit="$seenit,$seen"
342	    echo $dir >> $tf.srcdep
343	    case "$DPDEPS,$reldir,$seensrc," in
344	    ,*) ;;
345	    *)	seensrc="$seensrc,$path"
346		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
347		;;
348	    esac
349	    continue
350	    ;;
351	esac
352	# if there is a .dirdep we cannot skip
353	# just because we've seen the dir before.
354	if [ -s $path.dirdep ]; then
355	    # this file contains:
356	    # '# ${RELDIR}.<machine>'
357	    echo $path.dirdep >> $tf.qual
358	    continue
359	elif [ -s $dir.dirdep ]; then
360	    echo $dir.dirdep >> $tf.qual
361	    seenit="$seenit,$seen"
362	    continue
363	fi
364	seenit="$seenit,$seen"
365	case "$dir" in
366	$obj_re)
367	    echo $dir;;
368	esac
369    done > $tf.dirdep
370    _nl=echo
371    for f in $tf.dirdep $tf.qual $tf.srcdep
372    do
373	[ -s $f ] || continue
374	case $f in
375	*qual) # a list of .dirdep files
376	    # we can prefix everything with $OBJTOP to
377	    # tell gendirdeps.mk that these are
378	    # DIRDEP entries, since they are already
379	    # qualified with .<machine> as needed.
380	    # We strip .$MACHINE though
381	    xargs cat < $f | sort -u |
382	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
383	    ;;
384	*)  sort -u $f;;
385	esac
386	_nl=:
387    done
388    if [ -s $tf.dpdeps ]; then
389	case "$DPDEPS" in
390	*/*) ;;
391	*) echo > $DPDEPS;;		# the echo is needed!
392	esac
393	sort -u $tf.dpdeps |
394	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
395    fi
396    # ensure we produce _something_ else egrep -v gets upset
397    $_nl
398}
399
400case /$0 in
401*/meta2dep*) meta2deps "$@";;
402*/meta2dirs*) meta2dirs "$@";;
403*/meta2src*) meta2src "$@";;
404esac
405