1241519Sattilio#!/bin/sh -
2241519Sattilio#
3241519Sattilio# Copyright (c) 1994, 1996
4241519Sattilio#	The Regents of the University of California.  All rights reserved.
5241519Sattilio#
6241519Sattilio# Redistribution and use in source and binary forms are permitted
7241519Sattilio# provided that this notice is preserved and that due credit is given
8241519Sattilio# to the University of California at Berkeley. The name of the University
9241519Sattilio# may not be used to endorse or promote products derived from this
10241519Sattilio# software without specific prior written permission. This software
11241519Sattilio# is provided ``as is'' without express or implied warranty.
12241519Sattilio#
13241519Sattilio#	@(#)mkdep.sh	5.11 (Berkeley) 5/5/88
14241519Sattilio#
15241519Sattilio
16241519SattilioMAKE=Makefile			# default makefile name is "Makefile"
17241519SattilioCC=cc				# default C compiler is "cc"
18241519SattilioDEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
19241519SattilioSOURCE_DIRECTORY=.		# default source directory is the current directory
20241519Sattilio
21241519Sattilio# No command-line flags seen yet.
22241519Sattilioflags=""
23241519Sattiliowhile :
24241519Sattilio	do case "$1" in
25241519Sattilio		# -c allows you to specify the C compiler
26241519Sattilio		-c)
27241519Sattilio			CC=$2
28241519Sattilio			shift; shift ;;
29241519Sattilio
30241519Sattilio		# -f allows you to select a makefile name
31241519Sattilio		-f)
32241519Sattilio			MAKE=$2
33241519Sattilio			shift; shift ;;
34241519Sattilio
35241519Sattilio		# -m allows you to specify the dependency-generation flag
36241519Sattilio		-m)
37241519Sattilio			DEPENDENCY_CFLAG=$2
38241519Sattilio			shift; shift ;;
39241519Sattilio
40241519Sattilio		# the -p flag produces "program: program.c" style dependencies
41241519Sattilio		# so .o's don't get produced
42241519Sattilio		-p)
43241519Sattilio			SED='s;\.o;;'
44241519Sattilio			shift ;;
45241519Sattilio
46241519Sattilio		# -s allows you to specify the source directory
47241519Sattilio		-s)
48241519Sattilio			SOURCE_DIRECTORY=$2
49241519Sattilio			shift; shift ;;
50241519Sattilio
51241519Sattilio		# other command-line flag
52241519Sattilio		-*)
53241519Sattilio			flags="$flags $1"
54241519Sattilio			shift ;;
55241519Sattilio
56241519Sattilio		*)
57241519Sattilio			break ;;
58241519Sattilio	esac
59241519Sattiliodone
60241519Sattilio
61241519Sattilioif [ $# = 0 ] ; then
62241519Sattilio	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...'
63241519Sattilio	exit 1
64241519Sattiliofi
65280258Srwatson
66241519Sattilioif [ ! -w $MAKE ]; then
67241519Sattilio	echo "mkdep: no writeable file \"$MAKE\""
68241519Sattilio	exit 1
69241519Sattiliofi
70241519Sattilio
71241519SattilioTMP=/tmp/mkdep$$
72241519Sattilio
73241519Sattiliotrap 'rm -f $TMP ; exit 1' 1 2 3 13 15
74241519Sattilio
75241519Sattiliocp $MAKE ${MAKE}.bak
76241519Sattilio
77241519Sattiliosed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
78241519Sattilio
79241519Sattiliocat << _EOF_ >> $TMP
80241519Sattilio# DO NOT DELETE THIS LINE -- mkdep uses it.
81241519Sattilio# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
82241519Sattilio
83241519Sattilio_EOF_
84241519Sattilio
85241519Sattilio# If your compiler doesn't have -M, add it.  If you can't, the next two
86241519Sattilio# lines will try and replace the "cc -M".  The real problem is that this
87241519Sattilio# hack can't deal with anything that requires a search path, and doesn't
88241519Sattilio# even try for anything using bracket (<>) syntax.
89241519Sattilio#
90241519Sattilio# grep -E '^#include[[:blank:]]*".*"' /dev/null $* |
91241519Sattilio# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
92241519Sattilio
93241519Sattilio#
94241519Sattilio# Construct a list of source files with paths relative to the source directory.
95241519Sattilio#
96241519Sattiliosources=""
97241519Sattiliofor srcfile in $*
98241519Sattiliodo
99241519Sattilio	sources="$sources $SOURCE_DIRECTORY/$srcfile"
100241519Sattiliodone
101241519Sattilio
102241519Sattilio# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
103241519Sattilio$CC $DEPENDENCY_CFLAG $flags $sources |
104241519Sattiliosed "
105241519Sattilio	s; \./; ;g
106241519Sattilio	$SED" >> $TMP
107241519Sattilio
108241519Sattiliocat << _EOF_ >> $TMP
109241519Sattilio
110241519Sattilio# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
111241519Sattilio_EOF_
112241519Sattilio
113241519Sattilio# copy to preserve permissions
114241519Sattiliocp $TMP $MAKE
115241519Sattiliorm -f ${MAKE}.bak $TMP
116241519Sattilioexit 0
117273736Shselasky