117683Spst#!/bin/sh -
217683Spst#
317683Spst# Copyright (c) 1994, 1996
417683Spst#	The Regents of the University of California.  All rights reserved.
517683Spst#
617683Spst# Redistribution and use in source and binary forms are permitted
717683Spst# provided that this notice is preserved and that due credit is given
817683Spst# to the University of California at Berkeley. The name of the University
917683Spst# may not be used to endorse or promote products derived from this
1017683Spst# software without specific prior written permission. This software
1117683Spst# is provided ``as is'' without express or implied warranty.
1217683Spst#
1317683Spst#	@(#)mkdep.sh	5.11 (Berkeley) 5/5/88
1417683Spst#
1517683Spst
16190225SrpauloPATH=/bin:/usr/bin:/usr/ucb:/usr/local:/usr/local/bin:/usr/sfw/bin
1717683Spstexport PATH
1817683Spst
1917683SpstMAKE=Makefile			# default makefile name is "Makefile"
2017683SpstCC=cc				# default C compiler is "cc"
21276768SdelphijDEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
2217683Spst
2317683Spstwhile :
2417683Spst	do case "$1" in
2517683Spst		# -c allows you to specify the C compiler
2617683Spst		-c)
2717683Spst			CC=$2
2817683Spst			shift; shift ;;
2917683Spst
3017683Spst		# -f allows you to select a makefile name
3117683Spst		-f)
3217683Spst			MAKE=$2
3317683Spst			shift; shift ;;
3417683Spst
35276768Sdelphij		# -m allows you to specify the dependency-generation flag
36276768Sdelphij		-m)
37276768Sdelphij			DEPENDENCY_CFLAG=$2
38276768Sdelphij			shift; shift ;;
39276768Sdelphij
4017683Spst		# the -p flag produces "program: program.c" style dependencies
4117683Spst		# so .o's don't get produced
4217683Spst		-p)
4317683Spst			SED='s;\.o;;'
4417683Spst			shift ;;
4517683Spst		*)
4617683Spst			break ;;
4717683Spst	esac
4817683Spstdone
4917683Spst
5017683Spstif [ $# = 0 ] ; then
51276768Sdelphij	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [flags] file ...'
5217683Spst	exit 1
5317683Spstfi
5417683Spst
5517683Spstif [ ! -w $MAKE ]; then
5617683Spst	echo "mkdep: no writeable file \"$MAKE\""
5717683Spst	exit 1
5817683Spstfi
5917683Spst
6017683SpstTMP=/tmp/mkdep$$
6117683Spst
6217683Spsttrap 'rm -f $TMP ; exit 1' 1 2 3 13 15
6317683Spst
6417683Spstcp $MAKE ${MAKE}.bak
6517683Spst
6617683Spstsed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
6717683Spst
6817683Spstcat << _EOF_ >> $TMP
6917683Spst# DO NOT DELETE THIS LINE -- mkdep uses it.
7017683Spst# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
7117683Spst
7217683Spst_EOF_
7317683Spst
7417683Spst# If your compiler doesn't have -M, add it.  If you can't, the next two
7517683Spst# lines will try and replace the "cc -M".  The real problem is that this
7617683Spst# hack can't deal with anything that requires a search path, and doesn't
7717683Spst# even try for anything using bracket (<>) syntax.
7817683Spst#
7917683Spst# egrep '^#include[ 	]*".*"' /dev/null $* |
8017683Spst# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
8117683Spst
8217683Spst# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
83276768Sdelphij$CC $DEPENDENCY_CFLAG $* |
8417683Spstsed "
8517683Spst	s; \./; ;g
8617683Spst	$SED" |
8717683Spstawk '{
8817683Spst	if ($1 != prev) {
8917683Spst		if (rec != "")
9017683Spst			print rec;
9117683Spst		rec = $0;
9217683Spst		prev = $1;
9317683Spst	}
9417683Spst	else {
9517683Spst		if (length(rec $2) > 78) {
9617683Spst			print rec;
9717683Spst			rec = $0;
9817683Spst		}
9917683Spst		else
10017683Spst			rec = rec " " $2
10117683Spst	}
10217683Spst}
10317683SpstEND {
10417683Spst	print rec
10517683Spst}' >> $TMP
10617683Spst
10717683Spstcat << _EOF_ >> $TMP
10817683Spst
10917683Spst# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
11017683Spst_EOF_
11117683Spst
11217683Spst# copy to preserve permissions
11317683Spstcp $TMP $MAKE
11417683Spstrm -f ${MAKE}.bak $TMP
11517683Spstexit 0
116