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