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