1#!/bin/sh -
2#
3# Copyright (c) 1991, 1993
4#	The Regents of the University of California.  All rights reserved.
5#
6# %sccs.include.redist.sh%
7#
8#	@(#)mkdep.old.compiler	8.1 (Berkeley) 06/06/93
9#
10
11# This is a version of mkdep that works pretty well
12# with compilers that don't have -M.
13MAKE=Makefile			# default makefile name is "Makefile"
14
15PATH=/bin:/usr/bin:/usr/ucb:/lib:/usr/lib
16
17INCL=
18
19while :
20	do case "$1" in
21		# -f allows you to select a makefile name
22		-f)
23			MAKE=$2
24			shift; shift ;;
25
26		# the -p flag produces "program: program.c" style dependencies
27		# so .o's don't get produced
28		-p)
29			SED='s;\.o;;'
30			shift ;;
31		*)
32			break ;;
33	esac
34done
35
36if [ $# = 0 ] ; then
37	echo 'usage: mkdep [-p] [-f makefile] [flags] file ...'
38	exit 1
39fi
40
41if [ ! -w $MAKE ]; then
42	echo "mkdep: no writeable file \"$MAKE\""
43	exit 1
44fi
45
46TMP=/tmp/mkdep$$
47
48trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
49
50cp $MAKE ${MAKE}.bak
51sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
52
53cat << _EOF_ >> $TMP
54# DO NOT DELETE THIS LINE -- mkdep uses it.
55# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
56
57_EOF_
58
59
60for i do
61	case "$i" in
62	-c|-M|-O)
63		;;
64	-I*)
65		INCL="$INCL $i";;
66	-D*|-U*)
67		FLAGS="$FLAGS $i";;
68	*)
69		# assume source file
70		# put '$dep' in front of dependencies
71		dep=`echo "$i" | sed -e 's,/,\\\\/,g' -e 's/\.c$/.o/'`
72
73		# Find includes, remove leading numerics, remove ./,
74		# remove double quotes, and remove trailing numerics.
75		# Sort that, discarding duplicates, and add '$dep'.
76		cpp $INCL $FLAGS "$i" | sed -e '
77			/^#/!d
78			s/# [0-9]* //
79			s,"./,",
80			s/"\(.*\)"/\1/
81			s/ [ 0-9]*$//' |
82		sort -u | sed -e "s/^/$dep: /";;
83	esac
84done |
85sed "
86	s; \./; ;g
87	/\.c:$/d
88	$SED" |
89awk '{
90	if ($1 != prev) {
91		if (rec != "")
92			print rec;
93		rec = $0;
94		prev = $1;
95	}
96	else {
97		if (length(rec $2) > 78) {
98			print rec;
99			rec = $0;
100		}
101		else
102			rec = rec " " $2
103	}
104}
105END {
106	print rec
107}' >> $TMP
108
109cat << _EOF_ >> $TMP
110
111# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
112_EOF_
113
114# copy to preserve permissions
115cp $TMP $MAKE
116rm -f $TMP
117exit 0
118