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