summaryrefslogtreecommitdiff
path: root/util/imake/mdepend.cpp
blob: a8311fc77c72aa4014e661e9e02c7090682db84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
XCOMM!/bin/sh
XCOMM
XCOMM $Xorg: mdepend.cpp,v 1.3 2000/08/17 19:41:52 cpqbld Exp $
XCOMM $XdotOrg: util/imake/mdepend.cpp,v 1.4 2005/08/26 05:01:37 daniels Exp $
XCOMM
XCOMM	Do the equivalent of the 'makedepend' program, but do it right.
XCOMM
XCOMM	Usage:
XCOMM
XCOMM	makedepend [cpp-flags] [-w width] [-s magic-string] [-f makefile]
XCOMM	  [-o object-suffix] [-v] [-a] [-cc compiler] [-d dependencyflag]
XCOMM
XCOMM	Notes:
XCOMM
XCOMM	The C compiler used can be overridden with the environment
XCOMM	variable "CC" or the command line flag -cc.
XCOMM
XCOMM	The "-v" switch of the "makedepend" program is not supported.
XCOMM
XCOMM
XCOMM	This script should
XCOMM	work on both USG and BSD systems.  However, when System V.4 comes out,
XCOMM	USG users will probably have to change "silent" to "-s" instead of
XCOMM	"-" (at least, that is what the documentation implies).
XCOMM
XCOMM $XFree86: xc/config/util/mdepend.cpp,v 3.9 2001/04/26 20:55:10 dawes Exp $
XCOMM

CC=PREPROC

silent='-'

TMP=`pwd`/.mdep$$

rm -rf ${TMP}
if ! mkdir -p ${TMP}; then
  echo "$0: cannot create ${TMP}, exit." >&2
fi

CPPCMD=${TMP}/a
DEPENDLINES=${TMP}/b
TMPMAKEFILE=${TMP}/c
MAGICLINE=${TMP}/d
ARGS=${TMP}/e

trap "rm -rf ${TMP}; exit 1" 1 2 15
trap "rm -rf ${TMP}; exit 0" 1 2 13

echo " \c" > $CPPCMD
if [ `wc -c < $CPPCMD` -eq 1 ]
then
    c="\c"
    n=
else
    c=
    n="-n"
fi

echo $n "$c" >$ARGS

files=
makefile=
magic_string='# DO NOT DELETE'
objsuffix='.o'
width=78
endmarker=""
verbose=n
append=n
compilerlistsdepends=n

while [ $# != 0 ]
do
    if [ "$endmarker"x != x ] && [ "$endmarker" = "$1" ]; then
	endmarker=""
    else
	case "$1" in
	    -D*|-I*|-U*)
		echo $n " '$1'$c" >> $ARGS
		;;

	    -g|-O)	# ignore so we can just pass $(CFLAGS) in
		;;

	    *)
		if [ "$endmarker"x = x ]; then
		    case "$1" in 	
			-w)
			    width="$2"
			    shift
			    ;;
			-s)
			    magic_string="$2"
			    shift
			    ;;
			-f*)
			    if [ "$1" = "-f-" ]; then
				makefile="-"
			    elif [ "$1" = "-f" ]; then
				makefile="$2"
				shift
			    else
				echo "$1" | sed 's/^\-f//' >${TMP}arg
				makefile="`cat ${TMP}arg`"
				rm -f ${TMP}arg
			    fi
			    ;;
			-o)
			    objsuffix="$2"
			    shift
			    ;;
			
			--*)
			    echo "$1" | sed 's/^\-\-//' >${TMP}end
			    endmarker="`cat ${TMP}end`"
			    rm -f ${TMP}end
			    if [ "$endmarker"x = x ]; then
				endmarker="--"
			    fi
			    ;;
			-v)
			    verbose="y"
			    ;;

			-a)
			    append="y"
			    ;;

			-cc)
			    CC="$2"
			    shift
			    ;;

			# Flag to tell compiler to output dependencies directly
			# For example, with Sun compilers, -xM or -xM1 or
			# with gcc, -M
		        -d)
			    compilerlistsdepends="y"
			    compilerlistdependsflag="$2"
			    shift
			    ;;

			-*)
			    echo "Unknown option '$1' ignored" 1>&2
			    ;;
			*)
			    files="$files $1"
			    ;;
		    esac
		fi
		;;
	esac
    fi
    shift
done
echo ' $*' >> $ARGS

if [ "$compilerlistsdepends"x = "y"x ] ; then
  CC="$CC $compilerlistdependsflag"
fi

echo "#!/bin/sh" > $CPPCMD
echo "exec $CC `cat $ARGS`" >> $CPPCMD
chmod +x $CPPCMD
rm $ARGS

case "$makefile" in
    '')
	if [ -r makefile ]
	then
	    makefile=makefile
	elif [ -r Makefile ]
	then
	    makefile=Makefile
	else
	    echo 'no makefile or Makefile found' 1>&2
	    exit 1
	fi
	;;
    -)
	makefile=$TMPMAKEFILE
	;;
esac

if [ "$verbose"x = "y"x ]; then 
    cat $CPPCMD
fi

echo '' > $DEPENDLINES

if [ "$compilerlistsdepends"x = "y"x ] ; then
    for i in $files
    do
	$CPPCMD $i >> $DEPENDLINES
    done
else
for i in $files
do
    $CPPCMD $i \
      | sed -n "/^#/s;^;$i ;p"
done \
  | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' \
    -e 's|"||g' -e 's| \./| |' \
  | awk '{
	if ($1 != $4  &&  $2 != "#ident" && $2 != "#pragma")
	    {
	    numparts = split( $1, ofileparts, "\." )
	    ofile = ""
	    for ( i = 1; i < numparts; i = i+1 )
		{
		if (i != 1 )
		    ofile = ofile "."
		ofile = ofile ofileparts[i]
		}
	    print ofile "'"$objsuffix"'", $4
	    }
	}' \
  | sort -u \
  | awk '
	    {
	    newrec = rec " " $2
	    if ($1 != old1)
		{
		old1 = $1
		if (rec != "")
		    print rec
		rec = $1 ": " $2
		}
	    else if (length (newrec) > '"$width"')
		{
		print rec
		rec = $1 ": " $2
		}
	    else
		rec = newrec
	    }
	END \
	    {
	    if (rec != "")
		print rec
	    }' \
  | egrep -v '^[^:]*:[ 	]*$' >> $DEPENDLINES
fi

trap "" 1 2 13 15	# Now we are committed
case "$makefile" in
    $TMPMAKEFILE)
	;;
    *)
	rm -f $makefile.bak
	cp $makefile $makefile.bak
	echo "Appending dependencies to $makefile"
	;;
esac

XCOMM
XCOMM If not -a, append the magic string and a blank line so that
XCOMM /^$magic_string/+1,\$d can be used to delete everything from after
XCOMM the magic string to the end of the file.  Then, append a blank
XCOMM line again and then the dependencies.
XCOMM
if [ "$append" = "n" ]
then
    cat >> $makefile << END_OF_APPEND

$magic_string

END_OF_APPEND
    ed $silent $makefile << END_OF_ED_SCRIPT
/^$magic_string/+1,\$d
w
q
END_OF_ED_SCRIPT
    echo '' >>$makefile
fi

cat $DEPENDLINES >>$makefile

case "$makefile" in
    $TMPMAKEFILE)
	cat $TMPMAKEFILE
	;;

esac

rm -rf ${TMP}*
exit 0