1#! /bin/sh -
2# $OpenLDAP$
3## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4##
5## Copyright 1998-2011 The OpenLDAP Foundation.
6## All rights reserved.
7##
8## Redistribution and use in source and binary forms, with or without
9## modification, are permitted only as authorized by the OpenLDAP
10## Public License.
11##
12## A copy of this license is available in the file LICENSE in the
13## top-level directory of the distribution or, alternatively, at
14## <http://www.OpenLDAP.org/license.html>.
15#
16## Portions Copyright (c) 1987 Regents of the University of California.
17## All rights reserved.
18##
19## Redistribution and use in source and binary forms are permitted
20## provided that the above copyright notice and this paragraph are
21## duplicated in all such forms and that any documentation,
22## advertising materials, and other materials related to such
23## distribution and use acknowledge that the software was developed
24## by the University of California, Berkeley.  The name of the
25## University may not be used to endorse or promote products derived
26## from this software without specific prior written permission.
27## THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
28## IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
29## WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30#
31#	@(#)mkdep.sh	5.12 (Berkeley) 6/30/88
32#
33# We now use whatever path is already set by the invoker
34#PATH=/bin:/usr/bin:/usr/ucb
35#export PATH
36
37set -e				# exit immediately if any errors occur
38
39MAKE=Makefile			# default makefile name is "Makefile"
40NOSLASH="no"			# by default, / dependencies are included
41SRCDIR=""
42SED=cat
43
44: ${CC=cc}					# use cc by default
45
46# We generally set these via the command line options
47: ${MKDEP_CC=$CC}			# select default compiler to generate dependencies
48: ${MKDEP_CFLAGS="-M"}	# cc -M usually produces dependencies
49
50while :
51	do case "$1" in
52		# the -s flag removes dependencies to files that begin with /
53		-s)
54			NOSLASH=yes;
55			shift ;;
56
57		# -f allows you to select a makefile name
58		-f)
59			MAKE=$2
60			shift; shift ;;
61
62		# -d allows you to select a VPATH directory
63		-d)
64			SRCDIR=$2
65			shift; shift ;;
66
67		# -c allows you to override the compiler used to generate dependencies
68		-c)
69			MKDEP_CC=$2
70			shift; shift ;;
71
72		# -m allows you to override the compiler flags used to generate
73		# dependencies.
74		-m)
75			MKDEP_CFLAGS=$2
76			shift; shift ;;
77
78		# the -p flag produces "program: program.c" style dependencies
79		# so .o's don't get produced
80		-p)
81			SED='sed -e s;\.o;;'
82			shift ;;
83
84		# the -l flag produces libtool compatible dependencies
85		-l)
86			SED='sed -e s;\.o:;.lo:;'
87			shift ;;
88
89#		-*)	shift ;;
90
91		*)
92			break ;;
93	esac
94done
95
96if test $# = 0 ; then
97	echo 'usage: mkdep [-p] [-s] [-c cc] [-m flags] [-f makefile] [-d srcdir] [cppflags] file ...'
98	exit 1
99fi
100
101if test ! -w $MAKE ; then
102	echo "mkdep: no writeable file \"$MAKE\""
103	exit 1
104fi
105
106TMP=/tmp/mkdep$$
107
108trap 'rm -f $TMP.sed $TMP ; exit 1' 1 2 3 13 15
109
110cp $MAKE ${MAKE}.bak
111
112sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
113
114cat << _EOF_ >> $TMP
115# DO NOT DELETE THIS LINE -- mkdep uses it.
116# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
117
118_EOF_
119
120# If your compiler doesn't have -M, you may be able to use -E instead.
121# The preprocessor must generate lines of the form
122#   #.* [0-9]* "dependent file" .*
123# This script will parse out the "dependent file"s to generate the
124# dependency list.
125
126if test "x$SRCDIR" = "x" ; then
127	files=$*
128else
129	files=
130	for i in $* ; do
131		if test -f $i ; then
132			files="$files $i"
133		elif test -f $SRCDIR/$i ; then
134			files="$files $SRCDIR/$i"
135		else
136			files="$files $i"
137		fi
138	done
139
140	MKDEP_CFLAGS="$MKDEP_CFLAGS -I$SRCDIR"
141fi
142
143cat << _EOF_ >> $TMP
144
145#
146# files: $*
147# command: $MKDEP_CC $MKDEP_CFLAGS $files
148#
149
150_EOF_
151
152case $MKDEP_CFLAGS in
153# Using regular preprocessor output
154	-E*)
155FLAGS=""
156FILES=""
157for i in $files; do
158	case $i in
159	-*)	FLAGS="$FLAGS $i" ;;
160	*)	FILES="$FILES $i" ;;
161	esac
162done
163for i in $FILES; do
164	$MKDEP_CC $MKDEP_CFLAGS $FLAGS $i | grep '^#.*"' > $TMP.sed
165awk '
166BEGIN {
167	file = "'$i'"
168	n = split(file, parts, "/")
169	filenm = substr(parts[n], 0, length(parts[n])-1) "o"
170}
171{
172	dep = split($3, parts, "\"")
173	dep = parts[2]
174	if (dep ~ "^\./.*") dep = substr(dep, 3, length(dep)-2)
175	if (( noslash == "yes") && (dep ~ /^\// )) continue
176	if (deps[dep] == 0) printf "%s: %s\n", filenm, dep
177	deps[dep] = 1
178}' noslash="$NOSLASH" $TMP.sed >> $TMP
179done
180	;;
181
182	*)
183# Using -M or some other specific dependency-generating option
184$MKDEP_CC $MKDEP_CFLAGS $files | \
185	sed -e 's; \./; ;g' -e 's/ :/:/' | \
186	$SED > $TMP.sed
187# do not pipe to awk.  SGI awk wants a filename as argument.
188# (or '-', but I do not know if all other awks support that.)
189awk '
190$1 ~ /:$/ {
191	filenm=$1
192	dep=substr($0, length(filenm)+1)
193}
194$1 !~ /:$/ {
195	dep=$0
196}
197/.*/ {
198	if ( length(filenm) < 2 ) next
199	if ( filenm ~ /:.*:$/ ) next
200	split(dep, depends, " ")
201	for(d in depends) {
202		dfile = depends[d]
203		if ( length(dfile) < 2 ) continue
204		if ( dfile ~ /:/ ) continue
205		if (( noslash == "yes") && (dfile ~ /^\// )) continue
206		rec = filenm " " dfile
207		print rec
208	}
209}
210' noslash="$NOSLASH" $TMP.sed >> $TMP
211	;;
212esac
213
214
215cat << _EOF_ >> $TMP
216
217# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
218_EOF_
219
220# copy to preserve permissions
221cp $TMP $MAKE
222rm -f ${MAKE}.bak $TMP.sed $TMP
223exit 0
224