1335640Shselasky#!/bin/sh -
2335640Shselasky#
3335640Shselasky# Copyright (c) 1994, 1996
4335640Shselasky#	The Regents of the University of California.  All rights reserved.
5335640Shselasky#
6335640Shselasky# Redistribution and use in source and binary forms are permitted
7335640Shselasky# provided that this notice is preserved and that due credit is given
8335640Shselasky# to the University of California at Berkeley. The name of the University
9335640Shselasky# may not be used to endorse or promote products derived from this
10335640Shselasky# software without specific prior written permission. This software
11335640Shselasky# is provided ``as is'' without express or implied warranty.
12335640Shselasky#
13335640Shselasky#	@(#)mkdep.sh	5.11 (Berkeley) 5/5/88
14335640Shselasky#
15335640Shselasky
16335640ShselaskyMAKE=Makefile			# default makefile name is "Makefile"
17335640ShselaskyCC=cc				# default C compiler is "cc"
18335640ShselaskyDEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
19335640Shselasky
20335640Shselaskywhile :
21335640Shselasky	do case "$1" in
22335640Shselasky		# -c allows you to specify the C compiler
23335640Shselasky		-c)
24335640Shselasky			CC=$2
25335640Shselasky			shift; shift ;;
26335640Shselasky
27335640Shselasky		# -f allows you to select a makefile name
28335640Shselasky		-f)
29335640Shselasky			MAKE=$2
30335640Shselasky			shift; shift ;;
31335640Shselasky
32335640Shselasky		# -m allows you to specify the dependency-generation flag
33335640Shselasky		-m)
34335640Shselasky			DEPENDENCY_CFLAG=$2
35335640Shselasky			shift; shift ;;
36335640Shselasky
37335640Shselasky		# the -p flag produces "program: program.c" style dependencies
38335640Shselasky		# so .o's don't get produced
39335640Shselasky		-p)
40335640Shselasky			SED='s;\.o;;'
41335640Shselasky			shift ;;
42335640Shselasky		*)
43335640Shselasky			break ;;
44335640Shselasky	esac
45335640Shselaskydone
46335640Shselasky
47335640Shselaskyif [ $# = 0 ] ; then
48335640Shselasky	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [flags] file ...'
49335640Shselasky	exit 1
50335640Shselaskyfi
51335640Shselasky
52335640Shselaskyif [ ! -w $MAKE ]; then
53335640Shselasky	echo "mkdep: no writeable file \"$MAKE\""
54335640Shselasky	exit 1
55335640Shselaskyfi
56335640Shselasky
57335640ShselaskyTMP=/tmp/mkdep$$
58335640Shselasky
59335640Shselaskytrap 'rm -f $TMP ; exit 1' 1 2 3 13 15
60335640Shselasky
61335640Shselaskycp $MAKE ${MAKE}.bak
62335640Shselasky
63335640Shselaskysed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
64335640Shselasky
65335640Shselaskycat << _EOF_ >> $TMP
66335640Shselasky# DO NOT DELETE THIS LINE -- mkdep uses it.
67335640Shselasky# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
68335640Shselasky
69335640Shselasky_EOF_
70335640Shselasky
71335640Shselasky# If your compiler doesn't have -M, add it.  If you can't, the next two
72335640Shselasky# lines will try and replace the "cc -M".  The real problem is that this
73335640Shselasky# hack can't deal with anything that requires a search path, and doesn't
74335640Shselasky# even try for anything using bracket (<>) syntax.
75335640Shselasky#
76335640Shselasky# egrep '^#include[ 	]*".*"' /dev/null $* |
77335640Shselasky# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
78335640Shselasky
79335640Shselasky# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
80335640Shselasky$CC $DEPENDENCY_CFLAG $* |
81335640Shselaskysed "
82335640Shselasky	s; \./; ;g
83335640Shselasky	$SED" |
84335640Shselaskyawk '{
85335640Shselasky	if ($1 != prev) {
86335640Shselasky		if (rec != "")
87335640Shselasky			print rec;
88335640Shselasky		rec = $0;
89335640Shselasky		prev = $1;
90335640Shselasky	}
91335640Shselasky	else {
92335640Shselasky		if (length(rec $2) > 78) {
93335640Shselasky			print rec;
94335640Shselasky			rec = $0;
95335640Shselasky		}
96335640Shselasky		else
97335640Shselasky			rec = rec " " $2
98335640Shselasky	}
99335640Shselasky}
100335640ShselaskyEND {
101335640Shselasky	print rec
102335640Shselasky}' >> $TMP
103335640Shselasky
104335640Shselaskycat << _EOF_ >> $TMP
105335640Shselasky
106335640Shselasky# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
107335640Shselasky_EOF_
108335640Shselasky
109335640Shselasky# copy to preserve permissions
110335640Shselaskycp $TMP $MAKE
111335640Shselaskyrm -f ${MAKE}.bak $TMP
112335640Shselaskyexit 0
113