1 |
#! /usr/bin/env sh |
2 |
# |
3 |
# $TOG: mdepend.cpp /main/13 1997/06/20 21:12:18 kaleb $ |
4 |
# |
5 |
# Do the equivalent of the 'makedepend' program, but do it right. |
6 |
# |
7 |
# Usage: |
8 |
# |
9 |
# makedepend [cpp-flags] [-w width] [-s magic-string] [-f makefile] |
10 |
# [-o object-suffix] |
11 |
# |
12 |
# Notes: |
13 |
# |
14 |
# The C compiler used can be overridden with the environment |
15 |
# variable "CC". |
16 |
# |
17 |
# The "-v" switch of the "makedepend" program is not supported. |
18 |
# |
19 |
# |
20 |
# This script should |
21 |
# work on both USG and BSD systems. However, when System V.4 comes out, |
22 |
# USG users will probably have to change "silent" to "-s" instead of |
23 |
# "-" (at least, that is what the documentation implies). |
24 |
# |
25 |
# $XFree86: xc/config/util/mdepend.cpp,v 3.2 1997/06/29 07:54:20 dawes Exp $ |
26 |
# |
27 |
|
28 |
CPPARGS="-Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -D_XOPEN_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE " |
29 |
|
30 |
CC="cpp $CPPARGS" |
31 |
if test -x /lib/cpp ; then |
32 |
CC="/lib/cpp $CPPARGS" |
33 |
fi |
34 |
|
35 |
silent='-' |
36 |
|
37 |
TMP=./mdep$$ |
38 |
CPPCMD=${TMP}a |
39 |
DEPENDLINES=${TMP}b |
40 |
TMPMAKEFILE=${TMP}c |
41 |
MAGICLINE=${TMP}d |
42 |
ARGS=${TMP}e |
43 |
|
44 |
trap "rm -f ${TMP}*; exit 1" 1 2 15 |
45 |
trap "rm -f ${TMP}*; exit 0" 1 2 13 |
46 |
|
47 |
echo " \c" > $CPPCMD |
48 |
if [ `wc -c < $CPPCMD` -eq 1 ] |
49 |
then |
50 |
c="\c" |
51 |
n= |
52 |
else |
53 |
c= |
54 |
n="-n" |
55 |
fi |
56 |
|
57 |
echo $n "$c" >$ARGS |
58 |
|
59 |
files= |
60 |
makefile= |
61 |
magic_string='# DO NOT DELETE' |
62 |
objsuffix='.o' |
63 |
width=78 |
64 |
endmarker="" |
65 |
verbose=n |
66 |
append=n |
67 |
|
68 |
while [ $# != 0 ] |
69 |
do |
70 |
if [ "$endmarker"x != x ] && [ "$endmarker" = "$1" ]; then |
71 |
endmarker="" |
72 |
else |
73 |
case "$1" in |
74 |
-D*|-I*) |
75 |
echo $n " '$1'$c" >> $ARGS |
76 |
;; |
77 |
|
78 |
-g|-O) # ignore so we can just pass $(CFLAGS) in |
79 |
;; |
80 |
|
81 |
*) |
82 |
if [ "$endmarker"x = x ]; then |
83 |
case "$1" in |
84 |
-w) |
85 |
width="$2" |
86 |
shift |
87 |
;; |
88 |
-s) |
89 |
magic_string="$2" |
90 |
shift |
91 |
;; |
92 |
-f*) |
93 |
if [ "$1" = "-f-" ]; then |
94 |
makefile="-" |
95 |
else |
96 |
makefile="$2" |
97 |
shift |
98 |
fi |
99 |
;; |
100 |
-o) |
101 |
objsuffix="$2" |
102 |
shift |
103 |
;; |
104 |
|
105 |
--*) |
106 |
echo "$1" | sed 's/^\-\-//' >${TMP}end |
107 |
endmarker="`cat ${TMP}end`" |
108 |
rm -f ${TMP}end |
109 |
if [ "$endmarker"x = x ]; then |
110 |
endmarker="--" |
111 |
fi |
112 |
;; |
113 |
-v) |
114 |
verbose="y" |
115 |
;; |
116 |
|
117 |
-a) |
118 |
append="y" |
119 |
;; |
120 |
|
121 |
-cc) |
122 |
CC="$2" |
123 |
shift |
124 |
;; |
125 |
|
126 |
-cpp) |
127 |
CC="$2 $CPPARGS" |
128 |
shift |
129 |
;; |
130 |
|
131 |
-*) |
132 |
echo "Unknown option '$1' ignored" 1>&2 |
133 |
;; |
134 |
*) |
135 |
files="$files $1" |
136 |
;; |
137 |
esac |
138 |
fi |
139 |
;; |
140 |
esac |
141 |
fi |
142 |
shift |
143 |
done |
144 |
echo ' $*' >> $ARGS |
145 |
|
146 |
echo "#!/bin/sh" > $CPPCMD |
147 |
echo "exec $CC `cat $ARGS`" >> $CPPCMD |
148 |
chmod +x $CPPCMD |
149 |
rm $ARGS |
150 |
|
151 |
case "$makefile" in |
152 |
'') |
153 |
if [ -r makefile ] |
154 |
then |
155 |
makefile=makefile |
156 |
elif [ -r Makefile ] |
157 |
then |
158 |
makefile=Makefile |
159 |
else |
160 |
echo 'no makefile or Makefile found' 1>&2 |
161 |
exit 1 |
162 |
fi |
163 |
;; |
164 |
-) |
165 |
makefile=$TMPMAKEFILE |
166 |
;; |
167 |
esac |
168 |
|
169 |
if [ "$verbose"x = "y"x ]; then |
170 |
cat $CPPCMD |
171 |
fi |
172 |
|
173 |
echo '' > $DEPENDLINES |
174 |
for i in $files |
175 |
do |
176 |
$CPPCMD $i | sed -n "/^#/s;^;$i ;p" |
177 |
|
178 |
done | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' -e 's|"||g' -e 's| \./| |' | awk '{ |
179 |
if ($1 != $4 && $2 != "#ident" && $2 != "#pragma") |
180 |
|
181 |
|
182 |
|
183 |
{ |
184 |
ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'" |
185 |
print ofile, $4 |
186 |
} |
187 |
}' | sort -u | awk ' |
188 |
|
189 |
|
190 |
{ |
191 |
newrec = rec " " $2 |
192 |
if ($1 != old1) |
193 |
{ |
194 |
old1 = $1 |
195 |
if (rec != "") |
196 |
print rec |
197 |
rec = $1 ": " $2 |
198 |
} |
199 |
else if (length (newrec) > '"$width"') |
200 |
{ |
201 |
print rec |
202 |
rec = $1 ": " $2 |
203 |
} |
204 |
else |
205 |
rec = newrec |
206 |
} |
207 |
END { |
208 |
|
209 |
if (rec != "") |
210 |
print rec |
211 |
}' | egrep -v '^[^:]*:[ ]*$' >> $DEPENDLINES |
212 |
|
213 |
|
214 |
trap "" 1 2 13 15 # Now we are committed |
215 |
case "$makefile" in |
216 |
$TMPMAKEFILE) |
217 |
;; |
218 |
*) |
219 |
rm -f $makefile.bak |
220 |
cp $makefile $makefile.bak |
221 |
echo "Appending dependencies to $makefile" |
222 |
;; |
223 |
esac |
224 |
|
225 |
# |
226 |
# If not -a, append the magic string and a blank line so that |
227 |
# /^$magic_string/+1,\$d can be used to delete everything from after |
228 |
# the magic string to the end of the file. Then, append a blank |
229 |
# line again and then the dependencies. |
230 |
# |
231 |
if [ "$append" = "n" ] |
232 |
then |
233 |
cat >> $makefile << END_OF_APPEND |
234 |
|
235 |
$magic_string |
236 |
|
237 |
END_OF_APPEND |
238 |
ed $silent $makefile << END_OF_ED_SCRIPT |
239 |
/^$magic_string/+1,\$d |
240 |
w |
241 |
q |
242 |
END_OF_ED_SCRIPT |
243 |
echo '' >>$makefile |
244 |
fi |
245 |
|
246 |
cat $DEPENDLINES >>$makefile |
247 |
|
248 |
case "$makefile" in |
249 |
$TMPMAKEFILE) |
250 |
cat $TMPMAKEFILE |
251 |
;; |
252 |
|
253 |
esac |
254 |
|
255 |
rm -f ${TMP}* |
256 |
exit 0 |