1 |
#!/bin/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="-traditional -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 |
if test -r ./genmake_optfile ; then |
31 |
. ./genmake_optfile |
32 |
fi |
33 |
if test "x$CPP" != x0 ; then |
34 |
CC="$CPP $CPPARGS" |
35 |
else |
36 |
CC="cpp $CPPARGS" |
37 |
fi |
38 |
|
39 |
silent='-' |
40 |
|
41 |
TMP=./mdep$$ |
42 |
CPPCMD=${TMP}a |
43 |
DEPENDLINES=${TMP}b |
44 |
TMPMAKEFILE=${TMP}c |
45 |
MAGICLINE=${TMP}d |
46 |
ARGS=${TMP}e |
47 |
|
48 |
trap "rm -f ${TMP}*; exit 1" 1 2 15 |
49 |
trap "rm -f ${TMP}*; exit 0" 1 2 13 |
50 |
|
51 |
echo " \c" > $CPPCMD |
52 |
if [ `wc -c < $CPPCMD` -eq 1 ] |
53 |
then |
54 |
c="\c" |
55 |
n= |
56 |
else |
57 |
c= |
58 |
n="-n" |
59 |
fi |
60 |
|
61 |
echo $n "$c" >$ARGS |
62 |
|
63 |
files= |
64 |
makefile= |
65 |
magic_string='# DO NOT DELETE' |
66 |
objsuffix='.o' |
67 |
width=78 |
68 |
endmarker="" |
69 |
verbose=n |
70 |
append=n |
71 |
|
72 |
while [ $# != 0 ] |
73 |
do |
74 |
if [ "$endmarker"x != x ] && [ "$endmarker" = "$1" ]; then |
75 |
endmarker="" |
76 |
else |
77 |
case "$1" in |
78 |
-D*|-I*) |
79 |
echo $n " '$1'$c" >> $ARGS |
80 |
;; |
81 |
|
82 |
-g|-O) # ignore so we can just pass $(CFLAGS) in |
83 |
;; |
84 |
|
85 |
*) |
86 |
if [ "$endmarker"x = x ]; then |
87 |
case "$1" in |
88 |
-w) |
89 |
width="$2" |
90 |
shift |
91 |
;; |
92 |
-s) |
93 |
magic_string="$2" |
94 |
shift |
95 |
;; |
96 |
-f*) |
97 |
if [ "$1" = "-f-" ]; then |
98 |
makefile="-" |
99 |
else |
100 |
makefile="$2" |
101 |
shift |
102 |
fi |
103 |
;; |
104 |
-o) |
105 |
objsuffix="$2" |
106 |
shift |
107 |
;; |
108 |
|
109 |
--*) |
110 |
echo "$1" | sed 's/^\-\-//' >${TMP}end |
111 |
endmarker="`cat ${TMP}end`" |
112 |
rm -f ${TMP}end |
113 |
if [ "$endmarker"x = x ]; then |
114 |
endmarker="--" |
115 |
fi |
116 |
;; |
117 |
-v) |
118 |
verbose="y" |
119 |
;; |
120 |
|
121 |
-a) |
122 |
append="y" |
123 |
;; |
124 |
|
125 |
-cc) |
126 |
CC="$2" |
127 |
shift |
128 |
;; |
129 |
|
130 |
-cpp) |
131 |
CC="$2 $CPPARGS" |
132 |
shift |
133 |
;; |
134 |
|
135 |
-*) |
136 |
echo "Unknown option '$1' ignored" 1>&2 |
137 |
;; |
138 |
*) |
139 |
files="$files $1" |
140 |
;; |
141 |
esac |
142 |
fi |
143 |
;; |
144 |
esac |
145 |
fi |
146 |
shift |
147 |
done |
148 |
echo ' $*' >> $ARGS |
149 |
|
150 |
echo "#!/bin/sh" > $CPPCMD |
151 |
echo "exec $CC `cat $ARGS`" >> $CPPCMD |
152 |
chmod +x $CPPCMD |
153 |
rm $ARGS |
154 |
|
155 |
case "$makefile" in |
156 |
'') |
157 |
if [ -r makefile ] |
158 |
then |
159 |
makefile=makefile |
160 |
elif [ -r Makefile ] |
161 |
then |
162 |
makefile=Makefile |
163 |
else |
164 |
echo 'no makefile or Makefile found' 1>&2 |
165 |
exit 1 |
166 |
fi |
167 |
;; |
168 |
-) |
169 |
makefile=$TMPMAKEFILE |
170 |
;; |
171 |
esac |
172 |
|
173 |
if [ "$verbose"x = "y"x ]; then |
174 |
cat $CPPCMD |
175 |
fi |
176 |
|
177 |
echo '' > $DEPENDLINES |
178 |
for i in $files |
179 |
do |
180 |
$CPPCMD $i | sed -n "/^#/s;^;$i ;p" |
181 |
|
182 |
done | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' -e 's|"||g' -e 's| \./| |' | awk '{ |
183 |
if ($1 != $4 && $2 != "#ident" && $2 != "#pragma") |
184 |
|
185 |
|
186 |
|
187 |
{ |
188 |
ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'" |
189 |
print ofile, $4 |
190 |
} |
191 |
}' | sort -u | awk ' |
192 |
|
193 |
|
194 |
{ |
195 |
newrec = rec " " $2 |
196 |
if ($1 != old1) |
197 |
{ |
198 |
old1 = $1 |
199 |
if (rec != "") |
200 |
print rec |
201 |
rec = $1 ": " $2 |
202 |
} |
203 |
else if (length (newrec) > '"$width"') |
204 |
{ |
205 |
print rec |
206 |
rec = $1 ": " $2 |
207 |
} |
208 |
else |
209 |
rec = newrec |
210 |
} |
211 |
END { |
212 |
|
213 |
if (rec != "") |
214 |
print rec |
215 |
}' | egrep -v '^[^:]*:[ ]*$' >> $DEPENDLINES |
216 |
|
217 |
|
218 |
trap "" 1 2 13 15 # Now we are committed |
219 |
case "$makefile" in |
220 |
$TMPMAKEFILE) |
221 |
;; |
222 |
*) |
223 |
rm -f $makefile.bak |
224 |
cp $makefile $makefile.bak |
225 |
echo "Appending dependencies to $makefile" |
226 |
;; |
227 |
esac |
228 |
|
229 |
# |
230 |
# If not -a, append the magic string and a blank line so that |
231 |
# /^$magic_string/+1,\$d can be used to delete everything from after |
232 |
# the magic string to the end of the file. Then, append a blank |
233 |
# line again and then the dependencies. |
234 |
# |
235 |
if [ "$append" = "n" ] |
236 |
then |
237 |
cat >> $makefile << END_OF_APPEND |
238 |
|
239 |
$magic_string |
240 |
|
241 |
END_OF_APPEND |
242 |
ed $silent $makefile << END_OF_ED_SCRIPT |
243 |
/^$magic_string/+1,\$d |
244 |
w |
245 |
q |
246 |
END_OF_ED_SCRIPT |
247 |
echo '' >>$makefile |
248 |
fi |
249 |
|
250 |
cat $DEPENDLINES >>$makefile |
251 |
|
252 |
case "$makefile" in |
253 |
$TMPMAKEFILE) |
254 |
cat $TMPMAKEFILE |
255 |
;; |
256 |
|
257 |
esac |
258 |
|
259 |
rm -f ${TMP}* |
260 |
exit 0 |