xref: /freebsd/contrib/bmake/mk/meta2deps.sh (revision 9f0f30bc1f5f08d25243952bad3fdc6e13a75c2a)
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.22 2025/05/16 20:03:43 sjg Exp $
79
80# SPDX-License-Identifier: BSD-2-Clause
81#
82# Copyright (c) 2011-2025, Simon J. Gerraty
83# Copyright (c) 2010-2013, Juniper Networks, Inc.
84# All rights reserved.
85#
86# Redistribution and use in source and binary forms, with or without
87# modification, are permitted provided that the following conditions
88# are met:
89# 1. Redistributions of source code must retain the above copyright
90#    notice, this list of conditions and the following disclaimer.
91# 2. Redistributions in binary form must reproduce the above copyright
92#    notice, this list of conditions and the following disclaimer in the
93#    documentation and/or other materials provided with the distribution.
94#
95# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
96# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
97# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
98# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
99# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
100# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
101# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
105# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106
107meta2src() {
108    cat /dev/null "$@" |
109    sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
110    sort -u
111}
112
113meta2dirs() {
114    cat /dev/null "$@" |
115    sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
116    sort -u
117}
118
119add_list() {
120    sep=' '
121    suffix=
122    while :
123    do
124	case "$1" in
125	"|") sep="$1"; shift;;
126	-s) suffix="$2"; shift 2;;
127	*) break;;
128	esac
129    done
130    name=$1
131    shift
132    eval list="\$$name"
133    for top in "$@"
134    do
135	case "$sep$list$sep" in
136	*"$sep$top$suffix$sep"*) continue;;
137	esac
138	list="${list:+$list$sep}$top$suffix"
139    done
140    eval "$name=\"$list\""
141}
142
143# some Linux systems have deprecated egrep in favor of grep -E
144# but not everyone supports that
145case "`echo bmake | egrep 'a|b' 2>&1`" in
146bmake) ;;
147*) egrep() { grep -E "$@"; }
148esac
149
150_excludes_f() {
151    egrep -v "$EXCLUDES"
152}
153
154error() {
155    echo "ERROR: $@" >&2
156    exit 1
157}
158
159meta2deps() {
160    DPDEPS=
161    SRCTOPS=$SRCTOP
162    OBJROOTS=
163    EXCLUDES=
164    while :
165    do
166	case "$1" in
167	*=*) eval export "$1"; shift;;
168	-a) MACHINE_ARCH=$2; shift 2;;
169	-m) MACHINE=$2; shift 2;;
170	-C) CURDIR=$2; shift 2;;
171	-H) HOST_TARGET=$2; shift 2;;
172	-S) add_list SRCTOPS $2; shift 2;;
173	-O) add_list OBJROOTS $2; shift 2;;
174	-X) add_list EXCLUDES '|' $2; shift 2;;
175	-R) RELDIR=$2; shift 2;;
176	-T) TARGET_SPEC=$2; shift 2;;
177	*) break;;
178	esac
179    done
180
181    _th= _o=
182    case "$MACHINE" in
183    host) _ht=$HOST_TARGET;;
184    esac
185
186    for o in $OBJROOTS
187    do
188	case "$MACHINE,/$o/" in
189	host,*$HOST_TARGET*) ;;
190	*$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
191	*) add_list _o $o; continue;;
192	esac
193	for x in $_ht $TARGET_SPEC $MACHINE
194	do
195	    case "$o" in
196	    "") continue;;
197	    */$x/) add_list _o ${o%$x/}; o=;;
198	    */$x) add_list _o ${o%$x}; o=;;
199	    *$x/) add_list _o ${o%$x/}; o=;;
200	    *$x) add_list _o ${o%$x}; o=;;
201	    esac
202	done
203    done
204    OBJROOTS="$_o"
205
206    case "$OBJTOP" in
207    "")
208	for o in $OBJROOTS
209	do
210	    OBJTOP=$o${TARGET_SPEC:-$MACHINE}
211	    break
212	done
213	;;
214    esac
215    src_re=
216    obj_re=
217    add_list '|' -s '/*' src_re $SRCTOPS
218    add_list '|' -s '*' obj_re $OBJROOTS
219
220    [ -z "$RELDIR" ] && unset DPDEPS
221    tf=/tmp/m2d$$-$USER
222    rm -f $tf.*
223    trap 'rm -f $tf.*; trap 0' 0
224
225    > $tf.dirdep
226    > $tf.qual
227    > $tf.srcdep
228    > $tf.srcrel
229    > $tf.dpdeps
230
231    seenit=
232    seensrc=
233    lpid=
234    case "$EXCLUDES" in
235    "") _excludes=cat;;
236    *) _excludes=_excludes_f;;
237    esac
238    # handle @list files
239    case "$@" in
240    *@[!.]*)
241	for f in "$@"
242	do
243	    case "$f" in
244	    *.meta) cat $f;;
245	    @*) xargs cat < ${f#@};;
246	    *) cat $f;;
247	    esac
248	done
249	;;
250    *) cat /dev/null "$@";;
251    esac 2> /dev/null |
252    sed -e 's,^CWD,C C,;/^[#CREFLMVX] /!d' -e "s,',,g" |
253    $_excludes | ( version=no epids= xpids= eof_token=no
254    while read op pid path path2
255    do
256	: op=$op pid=$pid path=$path path2=$path2
257	# we track cwd and ldir (of interest) per pid
258	# CWD is bmake's cwd
259	case "$lpid,$pid" in
260	,C) CWD=$path cwd=$path ldir=$path
261	    if [ -z "$SB" ]; then
262		SB=`echo $CWD | sed 's,/obj.*,,'`
263	    fi
264	    SRCTOP=${SRCTOP:-$SB/src}
265	    case "$verion" in
266	    no) ;;		# ignore
267	    0) error "no filemon data";;
268	    *) ;;
269	    esac
270	    version=0
271	    case "$eof_token" in
272	    no) ;;		# ignore
273	    0) error "truncated filemon data";;
274	    esac
275	    eof_token=0
276	    continue
277	    ;;
278	$pid,$pid) ;;
279	[1-9]*)
280	    case "$lpid" in
281	    "") ;;
282	    *) eval ldir_$lpid=$ldir;;
283	    esac
284	    eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
285	    lpid=$pid
286	    ;;
287	esac
288
289	: op=$op path=$path
290	case "$op,$path" in
291	V,*) version=$pid; continue;;
292	W,*srcrel|*.dirdep) continue;;
293	C,*)
294	    case "$path" in
295	    /*) cwd=$path;;
296	    *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
297	    esac
298	    # watch out for temp dirs that no longer exist
299	    test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
300	    eval cwd_$pid=$cwd
301	    continue
302	    ;;
303	F,*) # $path is new pid
304	    eval cwd_$path=$cwd ldir_$path=$ldir
305	    continue
306	    ;;
307	\#,bye) eof_token=1; continue;;
308	\#*) continue;;
309	*)  dir=${path%/*}
310	    case "$op" in
311	    E)	# setid apps get no tracing so we won't see eXit
312		case `'ls' -l $path 2> /dev/null | sed 's, .*,,'` in
313		*s*) ;;
314		*) epids="$epids $pid";;
315		esac
316		;;
317	    X) xpids="$xpids $pid"; continue;;
318	    esac
319	    case "$path" in
320	    $src_re|$obj_re) ;;
321	    /*/stage/*) ;;
322	    /*) continue;;
323	    *)
324		rlist="$ldir/$path $cwd/$path"
325		case "$op,$path" in
326		[ML],../*) rlist="$rlist $path2/$path `dirname $path2`/$path";;
327		esac
328		for path in $rlist
329		do
330		    test -e $path && break
331		done
332		dir=${path%/*}
333		;;
334	    esac
335	    ;;
336	esac
337	# avoid repeating ourselves...
338	case "$DPDEPS,$seensrc," in
339	,*)
340	    case ",$seenit," in
341	    *,$dir,*) continue;;
342	    esac
343	    ;;
344	*,$path,*) continue;;
345	esac
346	# canonicalize if needed
347	case "/$dir/" in
348	*/../*|*/./*)
349	    rdir=$dir
350	    dir=`cd $dir 2> /dev/null && /bin/pwd`
351	    seen="$rdir,$dir"
352	    ;;
353	*)  seen=$dir;;
354	esac
355	case "$dir" in
356	${CURDIR:-.}|"") continue;;
357	$src_re)
358	    # avoid repeating ourselves...
359	    case "$DPDEPS,$seensrc," in
360	    ,*)
361		case ",$seenit," in
362		*,$dir,*) continue;;
363		esac
364		;;
365	    esac
366	    ;;
367	*)
368	    case ",$seenit," in
369	    *,$dir,*) continue;;
370	    esac
371	    ;;
372	esac
373	if [ -d $path ]; then
374	    case "$path" in
375	    */..) ldir=${dir%/*};;
376	    *) ldir=$path;;
377	    esac
378	    continue
379	fi
380	[ -f $path ] || continue
381	case "$dir" in
382	$CWD) continue;;		# ignore
383	$src_re)
384	    seenit="$seenit,$seen"
385	    echo $dir >> $tf.srcdep
386	    case "$DPDEPS,$reldir,$seensrc," in
387	    ,*) ;;
388	    *)	seensrc="$seensrc,$path"
389		echo "DPDEPS_$dir/${path##*/} += $RELDIR" >> $tf.dpdeps
390		;;
391	    esac
392	    continue
393	    ;;
394	esac
395	# if there is a .dirdep we cannot skip
396	# just because we've seen the dir before.
397	if [ -s $path.dirdep ]; then
398	    # this file contains:
399	    # '# ${RELDIR}.<machine>'
400	    echo $path.dirdep >> $tf.qual
401	    continue
402	elif [ -s $dir.dirdep ]; then
403	    echo $dir.dirdep >> $tf.qual
404	    seenit="$seenit,$seen"
405	    continue
406	fi
407	seenit="$seenit,$seen"
408	case "$dir" in
409	$obj_re)
410	    echo $dir;;
411	esac
412    done > $tf.dirdep
413    : version=$version
414    case "$version" in
415    0) error "no filemon data";;
416    esac
417    : eof_token=$eof_token
418    case "$eof_token" in
419    0) error "truncated filemon data";;
420    esac
421    for p in $epids
422    do
423	: p=$p
424	case " $xpids " in
425	*" $p "*) ;;
426	*) error "missing eXit for pid $p";;
427	esac
428    done ) || exit 1
429    _nl=echo
430    for f in $tf.dirdep $tf.qual $tf.srcdep
431    do
432	[ -s $f ] || continue
433	case $f in
434	*qual) # a list of .dirdep files
435	    # we can prefix everything with $OBJTOP to
436	    # tell gendirdeps.mk that these are
437	    # DIRDEP entries, since they are already
438	    # qualified with .<machine> as needed.
439	    # We strip .$MACHINE though
440	    xargs cat < $f | sort -u |
441	    sed "s,^# ,,;s,^,$OBJTOP/,;s,\.${TARGET_SPEC:-$MACHINE}\$,,;s,\.$MACHINE\$,,"
442	    ;;
443	*)  sort -u $f;;
444	esac
445	_nl=:
446    done
447    if [ -s $tf.dpdeps ]; then
448	case "$DPDEPS" in
449	*/*) ;;
450	*) echo > $DPDEPS;;		# the echo is needed!
451	esac
452	sort -u $tf.dpdeps |
453	sed "s,${SRCTOP}/,,;s,${SB_BACKING_SB:-$SB}/src/,," >> $DPDEPS
454    fi
455    # ensure we produce _something_ else egrep -v gets upset
456    $_nl
457}
458
459case /$0 in
460*/meta2dep*) meta2deps "$@";;
461*/meta2dirs*) meta2dirs "$@";;
462*/meta2src*) meta2src "$@";;
463esac
464