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