3 # mkdeps - generate dependencies
6 # mkdeps [options] file ...
9 # This script updates "makefile" with dependencies for
10 # "file"(s). It borrows ideas from various makedepend scripts
11 # and should be compatible with most.
13 # By default we use grep to extract include file names from
14 # source files. We source an "rc" file '$Mydir/.${Myname}rc' which
15 # can contain variable assignments such as:
23 # If the variable 'cpp_$suffix' is set, we use it as our cpp in
24 # place of grep. The program referenced by these variables are
25 # expected to produce output like:
28 # # 10 \"/usr/include/stdio.h\" 1
31 # This allows us to skip most of our processing. For lex,yacc
32 # and other source files, grep is probably just as quick and
33 # certainly more portable.
35 # If the "rc" file does not exist, we create it and attempt to
36 # find cpp or an equivalent cc invocation to assign to 'cpp_c'.
39 # Simon J. Gerraty <sjg@zen.void.oz.au>
43 # $Id: mkdeps.sh,v 1.24 2022/09/09 18:44:56 sjg Exp $
45 # @(#) Copyright (c) 1993 Simon J. Gerraty
47 # This file is provided in the hope that it will
48 # be of use. There is absolutely NO WARRANTY.
49 # Permission to copy, redistribute or otherwise
50 # use this file is hereby granted provided that
51 # the above copyright notice and this notice are
54 # Please send copies of changes and bug-fixes to:
58 Myname=`basename $0 .sh`
66 cc_include=-I/usr/include
73 *-n*) # don't use rc file
77 rc=$Mydir/.${Myname}rc
84 if [ x"$norc" = x -a -f $rc ]; then
87 # if /usr/lib/cpp or equivalent is available it is better than
89 # See what (if anything) works on this system...
91 echo "# pre-processor for .c files" >> $rc
92 # try a couple of sane places first
93 for d in /usr/libexec /usr/lib /usr/bin /lib /usr/ccs/bin
96 [ -x $cpp_c ] && break
99 if [ -x $cpp_c ]; then
100 echo cpp_c=$cpp_c >> $rc
103 # rats see if cc can be used
104 echo "#include <stdio.h>" > /tmp/f$$.c
105 echo "main() { return 0; }" >> /tmp/f$$.c
106 # try some sensible args to cc
109 ok=`${REALCC:-${CC:-cc}} $arg /tmp/f$$.c 2>/dev/null | grep '^#.*stdio.h' | tail -1`
113 cpp_c="${REALCC:-${CC:-cc}} $arg"
114 echo cpp_c="'$cpp_c'" >> $rc
122 # some Linux systems have deprecated egrep in favor of grep -E
123 # but not everyone supports that
124 case "`echo bmake | egrep 'a|b' 2>&1`" in
126 *) egrep() { grep -E "$@"; }
133 egrep -vi "included from|warning" $EF > ${EF}2
134 if [ -s ${EF}2 ]; then
144 # this lot does not work on HPsUX - complain to Hp.
151 # set IGNORE="<" to skip system includes
152 egrep '^#[ ]*include' $* | egrep -v "$IGNORE" | \
153 sed -e 's/^.*include[^"<]*["<]//' -e 's/[">].*//g';;
155 # $cpp (eg. /usr/lib/cpp or cc -E) should produce output like:
156 # 1 "/usr/include/stdio.h" 2
157 # set IGNORE=/usr/include to skip system includes
158 $cpp $cpp_opts $cc_include $* 2>> $EF | egrep '^#.*\.h"' | sed 's,^#.*"\(.*\)".*,\1,' |
159 egrep -v "$IGNORE" | sort -u;;
171 # this lot is not needed if not using grep.
172 for dir in $srcdir $dirlist /usr/include
174 [ -f "$dir/$ifile" ] && break
177 if [ ! -f "$dir/$ifile" ]; then
178 # produce a useful error message (useful to emacs or error)
179 iline=`grep -n ".*include.*[\"<]$ifile[\">]" $file | cut -d: -f1`
180 echo "\"$file\", line $iline: cannot find include file \"$ifile\"" >> $EF
181 # no point adding to dependency list as the resulting makefile
182 # would not work anyway...
187 # check whether we have done it yet
188 case `grep "$ifile" $TF` in
189 "") echo "$ifile" >> $TF;;
190 *) continue;; # no repeats...
195 len=`expr "$ifile " : '.*'`
196 if [ "`expr $llen + $len`" -gt ${width:-76} ]; then
198 echo $N " $C" >> .depend
201 echo $N "$ifile $C" >> .depend
202 llen=`expr $llen + $len`
206 # this lot is not needed unless using grep.
207 ilist=`get_incs $ifile` # recurse needed?
208 [ "$ilist" ] && llen=`gen_deps $llen $ilist`
215 for f in makefile Makefile
217 test -s $f && { MAKEFILE=$f; break; }
220 MAKEFILE=${MAKEFILE:-makefile}
221 IGNORE=${IGNORE:-"^-"} # won't happen
223 cpp_opts= # incase cpp != grep
228 set -- `getopt "AanNV:s:w:o:I:D:b:f:i:p" "$@"`
233 -A) Include=;; # cat .depend >> $MAKEFILE
234 -a) append=yes; shift;;
235 -n) shift;; # ignore rc
236 -N) update=no; shift;; # don't update $MAKEFILE
237 -I) cpp_opts="$cpp_opts$1$2 "; dirlist="$dirlist $2"; shift 2;;
238 -o) obj=$2; shift 2;;
239 -s) shift 2;; # can't handle it anyway...
240 -w) width=$2; shift 2;;
241 -f) MAKEFILE=$2; shift 2;;
242 -b) BASEDIR=$2; shift 2;;
243 -i) IGNORE="$2"; shift 2;; # ignore headers matching this...
244 -D) cpp_opts="$cpp_opts$1$2 "; shift 2;;
245 -V) VPATH="$2"; shift 2;; # where to look for files
246 -p) progDep=yes; shift;;
250 [ "$VPATH" ] && vpath=`IFS=:; set -- $VPATH; echo $*`
252 [ "$append" ] || > .depend
257 suffix=`expr $file : '.*\.\([^.]*\)'`
259 eval cpp=\"\${cpp_${suffix}:-grep}\"
261 if [ ! -f $file -a "$vpath" ]; then
264 [ -f $d/$file ] && { file=$d/$file; break; }
267 srcdir=`dirname $file`
268 base=`basename $file .$suffix`
270 ilist=`get_incs $file`
272 if [ "$ilist" ]; then
274 if [ "$progDep" ]; then
275 echo "$base: $file \\" >> .depend
277 echo "$base.$obj: $file \\" >> .depend
279 echo $N " $C" >> .depend
281 llen=`gen_deps $llen $ilist`
284 elif [ "$progDep" ]; then
285 echo "$base: $file" >> .depend
290 if [ -s .depend ]; then
293 { test "$BASEDIR" && sed -e "s;$BASEDIR;\$(BASEDIR);g" $TF || cat $TF; } |
294 sed 's;\([^.]\)\./;\1;g' > .depend
297 # Save the manually updated section of the makefile
299 if [ x$update != xno ]; then
300 trap "" 2 # don't die if we got this far
302 # if make doesn't support include, then append our deps...
303 depended=`grep 'include.*\.depend' $MAKEFILE`
304 test "$depended" && clean_up
306 sed '/^# DO NOT DELETE.*depend.*$/,$d' < $MAKEFILE > $TF
309 # DO NOT DELETE THIS LINE -- make depend depends on it
310 # Do not edit anything below, it was added automagically by $Myname.
315 "") cat .depend >> $MAKEFILE;;
316 .include) echo '.include ".depend"' >> $MAKEFILE;;
317 include) echo include .depend >> $MAKEFILE;;