]>
Commit | Line | Data |
---|---|---|
d1b2a10e | 1 | #!/bin/sh |
2 | # | |
3 | # @(#)mdepend.sh 1.3 5/11/88 19:44:36 | |
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's what the documentation implies). | |
24 | # | |
25 | CC=${CC:-cc} | |
26 | ||
27 | silent='-' | |
28 | ||
29 | TMP=${TMPDIR:-/tmp}/mdep$$ | |
30 | CPPCMD=${TMP}a | |
31 | DEPENDLINES=${TMP}b | |
32 | TMPMAKEFILE=${TMP}c | |
33 | MAGICLINE=${TMP}d | |
34 | ||
35 | trap "rm -f ${TMP}*; exit 1" 1 2 15 | |
36 | trap "rm -f ${TMP}*; exit 0" 1 2 13 | |
37 | ||
38 | echo " \c" > $CPPCMD | |
39 | if [ `wc -c < $CPPCMD` -eq 1 ] | |
40 | then | |
41 | c="\c" | |
42 | n= | |
43 | else | |
44 | c= | |
45 | n="-n" | |
46 | fi | |
47 | ||
48 | echo $n "exec $CC -E$c" > $CPPCMD | |
49 | chmod +x $CPPCMD | |
50 | ||
51 | files= | |
52 | makefile= | |
53 | magic_string='# DO NOT DELETE' | |
54 | objsuffix='.o' | |
55 | width=78 | |
56 | while [ $# != 0 ] | |
57 | do | |
58 | case "$1" in | |
59 | -D*|-I*) | |
60 | echo $n " '$1'$c" >> $CPPCMD | |
61 | ;; | |
62 | -w) | |
63 | width="$2" | |
64 | shift | |
65 | ;; | |
66 | -s) | |
67 | magic_string="$2" | |
68 | shift | |
69 | ;; | |
70 | -f) | |
71 | makefile="$2" | |
72 | shift | |
73 | ;; | |
74 | -o) | |
75 | objsuffix="$2" | |
76 | shift | |
77 | ;; | |
78 | -*) | |
79 | echo "Unknown option '$1' ignored" 1>&2 | |
80 | ;; | |
81 | *) | |
82 | files="$files $1" | |
83 | ;; | |
84 | esac | |
85 | shift | |
86 | done | |
87 | echo ' $*' >> $CPPCMD | |
88 | ||
89 | case "$makefile" in | |
90 | '') | |
91 | if [ -r makefile ] | |
92 | then | |
93 | makefile=makefile | |
94 | elif [ -r Makefile ] | |
95 | then | |
96 | makefile=Makefile | |
97 | else | |
98 | echo 'no makefile or Makefile found' 1>&2 | |
99 | exit 1 | |
100 | fi | |
101 | ;; | |
102 | -) | |
103 | makefile=$TMPMAKEFILE | |
104 | ;; | |
105 | esac | |
106 | ||
107 | echo '' > $DEPENDLINES | |
108 | for i in $files | |
109 | do | |
110 | $CPPCMD $i \ | |
111 | | sed -n "/^#/s;^;$i ;p" | |
112 | done \ | |
113 | | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' \ | |
114 | -e 's|"||g' -e 's| \./| |' \ | |
115 | | awk '{ | |
116 | if ($1 != $4 && $2 != "#ident") | |
117 | { | |
118 | ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'" | |
119 | print ofile, $4 | |
120 | } | |
121 | }' \ | |
122 | | sort -u \ | |
123 | | awk ' | |
124 | { | |
125 | newrec = rec " " $2 | |
126 | if ($1 != old1) | |
127 | { | |
128 | old1 = $1 | |
129 | if (rec != "") | |
130 | print rec | |
131 | rec = $1 ": " $2 | |
132 | } | |
133 | else if (length (newrec) > '"$width"') | |
134 | { | |
135 | print rec | |
136 | rec = $1 ": " $2 | |
137 | } | |
138 | else | |
139 | rec = newrec | |
140 | } | |
141 | END \ | |
142 | { | |
143 | if (rec != "") | |
144 | print rec | |
145 | }' \ | |
146 | >> $DEPENDLINES | |
147 | ||
148 | trap "" 1 2 13 15 # Now we are committed | |
149 | case "$makefile" in | |
150 | $TMPMAKEFILE) | |
151 | ;; | |
152 | *) | |
153 | rm -f Makefile.bak | |
154 | cp Makefile Makefile.bak | |
155 | ;; | |
156 | esac | |
157 | ||
158 | # | |
159 | # Before we go edit the Makefile, we must make sure that magic_string | |
160 | # exists, and that it's not the last line. Otherwise, dear old ed | |
161 | # will exit prematurely. What a pain. | |
162 | # | |
163 | cat >> $makefile <<- END_OF_APPEND | |
164 | $magic_string | |
165 | ||
166 | END_OF_APPEND | |
167 | ed $silent $makefile <<- END_OF_ED_SCRIPT | |
168 | /^$magic_string/w $MAGICLINE | |
169 | /^$magic_string/,\$d | |
170 | \$r $MAGICLINE | |
171 | \$r $DEPENDLINES | |
172 | w | |
173 | q | |
174 | END_OF_ED_SCRIPT | |
175 | ||
176 | case "$makefile" in | |
177 | $TMPMAKEFILE) | |
178 | cat $TMPMAKEFILE | |
179 | ;; | |
180 | esac | |
181 | ||
182 | rm -f ${TMP}* |