]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/mk/meta2deps.sh
Import tzdata 2019c.
[FreeBSD/FreeBSD.git] / share / mk / meta2deps.sh
1 # $FreeBSD$
2 #!/bin/sh
3
4 # NAME:
5 #       meta2deps.sh - extract useful info from .meta files
6 #
7 # SYNOPSIS:
8 #       meta2deps.sh SB="SB" "meta" ...
9 #       
10 # DESCRIPTION:
11 #       This script looks each "meta" file and extracts the
12 #       information needed to deduce build and src dependencies.
13 #       
14 #       To do this, we extract the 'CWD' record as well as all the
15 #       syscall traces which describe 'R'ead, 'C'hdir and 'E'xec
16 #       syscalls.
17 #
18 #       The typical meta file looks like::
19 #.nf
20 #
21 #       # Meta data file "path"
22 #       CMD "command-line"
23 #       CWD "cwd"
24 #       TARGET "target"
25 #       -- command output --
26 #       -- filemon acquired metadata --
27 #       # buildmon version 2
28 #       V 2
29 #       E "pid" "path"
30 #       R "pid" "path"
31 #       C "pid" "cwd"
32 #       R "pid" "path"
33 #       X "pid" "status"
34 #.fi
35 #
36 #       The fact that all the syscall entry lines start with a single
37 #       character make these files quite easy to process using sed(1).
38 #
39 #       To simplify the logic the 'CWD' line is made to look like a
40 #       normal 'C'hdir entry, and "cwd" is remembered so that it can
41 #       be prefixed to any "path" which is not absolute.
42 #
43 #       If the "path" being read ends in '.srcrel' it is the content
44 #       of (actually the first line of) that file that we are
45 #       interested in.
46 #
47 #       Any "path" which lies outside of the sandbox "SB" is generally
48 #       not of interest and is ignored.
49 #
50 #       The output, is a set of absolute paths with "SB" like:
51 #.nf
52 #
53 #       $SB/obj-i386/bsd/gnu/lib/csu
54 #       $SB/obj-i386/bsd/gnu/lib/libgcc
55 #       $SB/obj-i386/bsd/include
56 #       $SB/obj-i386/bsd/lib/csu/i386-elf
57 #       $SB/obj-i386/bsd/lib/libc
58 #       $SB/src/bsd/include
59 #       $SB/src/bsd/sys/i386/include
60 #       $SB/src/bsd/sys/sys
61 #       $SB/src/pan-release/rtsock
62 #       $SB/src/pfe-shared/include/jnx
63 #.fi
64 #
65 #       Which can then be further processed by 'gendirdeps.mk'
66 #
67 #       If we are passed 'DPDEPS='"dpdeps", then for each src file
68 #       outside of "CURDIR" we read, we output a line like:
69 #.nf
70 #
71 #       DPDEPS_$path += $RELDIR
72 #.fi
73 #
74 #       with "$path" geting turned into reldir's, so that we can end
75 #       up with a list of all the directories which depend on each src
76 #       file in another directory.  This can allow for efficient yet
77 #       complete testing of changes.
78
79
80 # RCSid:
81 #       $Id: meta2deps.sh,v 1.9 2015/04/03 18:23:25 sjg Exp $
82
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
107 meta2src() {
108     cat /dev/null "$@" |
109     sed -n '/^R .*\.[chyl]$/s,^..[0-9]* ,,p' |
110     sort -u
111 }
112     
113 meta2dirs() {
114     cat /dev/null "$@" |
115     sed -n '/^R .*\/.*\.[a-z0-9][^\/]*$/s,^..[0-9]* \(.*\)/[^/]*$,\1,p' |
116     sort -u
117 }
118
119 add_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 _excludes_f() {
144     egrep -v "$EXCLUDES"
145 }
146
147 meta2deps() {
148     DPDEPS=
149     SRCTOPS=$SRCTOP
150     OBJROOTS=
151     EXCLUDES=
152     while :
153     do
154         case "$1" in
155         *=*) eval export "$1"; shift;;
156         -a) MACHINE_ARCH=$2; shift 2;;
157         -m) MACHINE=$2; shift 2;;
158         -C) CURDIR=$2; shift 2;;
159         -H) HOST_TARGET=$2; shift 2;;
160         -S) add_list SRCTOPS $2; shift 2;;
161         -O) add_list OBJROOTS $2; shift 2;;
162         -X) add_list EXCLUDES '|' $2; shift 2;;
163         -R) RELDIR=$2; shift 2;;
164         -T) TARGET_SPEC=$2; shift 2;;
165         *) break;;
166         esac
167     done
168
169     _th= _o=
170     case "$MACHINE" in
171     host) _ht=$HOST_TARGET;;
172     esac
173     
174     for o in $OBJROOTS
175     do
176         case "$MACHINE,/$o/" in
177         host,*$HOST_TARGET*) ;;
178         *$MACHINE*|*${TARGET_SPEC:-$MACHINE}*) ;;
179         *) add_list _o $o; continue;;
180         esac
181         for x in $_ht $TARGET_SPEC $MACHINE
182         do
183             case "$o" in
184             "") continue;;
185             */$x/) add_list _o ${o%$x/}; o=;;
186             */$x) add_list _o ${o%$x}; o=;;
187             *$x/) add_list _o ${o%$x/}; o=;;
188             *$x) add_list _o ${o%$x}; o=;;
189             esac
190         done
191     done
192     OBJROOTS="$_o"
193
194     case "$OBJTOP" in
195     "")
196         for o in $OBJROOTS
197         do
198             OBJTOP=$o${TARGET_SPEC:-$MACHINE}
199             break
200         done
201         ;;
202     esac
203     src_re=
204     obj_re=
205     add_list '|' -s '/*' src_re $SRCTOPS
206     add_list '|' -s '*' obj_re $OBJROOTS
207     
208     [ -z "$RELDIR" ] && unset DPDEPS
209     tf=/tmp/m2d$$-$USER
210     rm -f $tf.*
211     trap 'rm -f $tf.*; trap 0' 0
212
213     > $tf.dirdep
214     > $tf.qual
215     > $tf.srcdep
216     > $tf.srcrel
217     > $tf.dpdeps
218
219     seenit=
220     seensrc=
221     lpid=
222     case "$EXCLUDES" in
223     "") _excludes=cat;;
224     *) _excludes=_excludes_f;;
225     esac
226     # handle @list files
227     case "$@" in
228     *@[!.]*)
229         for f in "$@"
230         do
231             case "$f" in
232             *.meta) cat $f;;
233             @*) xargs cat < ${f#@};;
234             *) cat $f;;
235             esac
236         done
237         ;;
238     *) cat /dev/null "$@";;
239     esac 2> /dev/null |
240     sed -e 's,^CWD,C C,;/^[CREFLM] /!d' -e "s,',,g" |
241     $_excludes |
242     while read op pid path junk
243     do
244         : op=$op pid=$pid path=$path
245         # we track cwd and ldir (of interest) per pid
246         # CWD is bmake's cwd
247         case "$lpid,$pid" in
248         ,C) CWD=$path cwd=$path ldir=$path
249             if [ -z "$SB" ]; then
250                 SB=`echo $CWD | sed 's,/obj.*,,'`
251             fi
252             SRCTOP=${SRCTOP:-$SB/src}
253             continue
254             ;;
255         $pid,$pid) ;;
256         *)
257             case "$lpid" in
258             "") ;;
259             *) eval ldir_$lpid=$ldir cwd_$lpid=$cwd;;
260             esac
261             eval ldir=\${ldir_$pid:-$CWD} cwd=\${cwd_$pid:-$CWD}
262             lpid=$pid
263             ;;
264         esac
265
266         case "$op,$path" in
267         W,*srcrel|*.dirdep) continue;;
268         C,*)
269             case "$path" in
270             /*) cwd=$path;;
271             *) cwd=`cd $cwd/$path 2> /dev/null && /bin/pwd`;;
272             esac
273             # watch out for temp dirs that no longer exist
274             test -d ${cwd:-/dev/null/no/such} || cwd=$CWD
275             continue
276             ;;
277         F,*)  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
400 case /$0 in
401 */meta2dep*) meta2deps "$@";;
402 */meta2dirs*) meta2dirs "$@";;
403 */meta2src*) meta2src "$@";;
404 esac