1#!/bin/sh
2
3#
4# makedepend which uses 'gcc -MM'
5#
6# tiny change to the gccmakedep in XFree86
7#
8# Based on mdepend.cpp and code supplied by Hongjiu Lu <hjl@nynexst.com>
9#
10
11TMP=/tmp/mdep$$
12CC="cc"
13RM="rm -f"
14LN="ln -s"
15MV="mv -f"
16
17trap "$RM ${TMP}*; exit 1" 1 2 15
18trap "$RM ${TMP}*; exit 0" 1 2 13
19
20files=
21makefile=
22endmarker=
23magic_string='# DO NOT DELETE'
24append=n
25args=
26asmfiles=
27
28# if we have a gcc version after 3.0, we have to muck with the
29# flags, because post-3.0 gcc breaks (deliberately!) -MM .
30cc --version | head -1 | grep -i 'GCC.* 3\.[1-9]' >/dev/null
31if [ $? = 0 ] ; then lategcc=Y ; else lategcc="" ; fi
32cc --version | head -1 | grep -i 'GCC.* [4-9]\.' >/dev/null
33if [ $? = 0 ] ; then lategcc=Y ; fi
34
35while [ $# != 0 ]; do
36    if [ "$endmarker"x != x -a "$endmarker" = "$1" ]; then
37	endmarker=
38    else
39	case "$1" in
40	    -D*)
41		args="$args '$1'"
42		;;
43	    -I*)
44		if [ "$lategcc" ] ; then
45		    args="$args -isystem '`echo $1 | sed -e s/-I//`'"
46		else
47		    args="$args '$1'"
48		fi
49		;;
50	    -g|-o)
51		;;
52	    *)
53		if [ "$endmarker"x = x ]; then
54		    case $1 in
55# ignore these flags
56			-w|-o|-cc)
57			    shift
58			    ;;
59			-v)
60			    ;;
61			-s)
62			    magic_string="$2"
63			    shift
64			    ;;
65			-f-)
66			    makefile="-"
67			    ;;
68			-f)
69			    makefile="$2"
70			    shift
71			    ;;
72			--*)
73			    endmarker=`echo $1 | sed 's/^\-\-//'`
74			    if [ "$endmarker"x = x ]; then
75				endmarker="--"
76			    fi
77			    ;;
78			-a)
79			    append=y
80			    ;;
81			-*)
82			    echo "Unknown option '$1' ignored" 1>&2
83			    ;;
84			*)
85			    files="$files $1"
86			    ;;
87		    esac
88		fi
89		;;
90	esac
91    fi
92    shift
93done
94
95if [ x"$files" = x ]; then
96# Nothing to do
97    exit 0
98fi
99
100case "$makefile" in
101    '')
102	if [ -r makefile ]; then
103	    makefile=makefile
104	elif [ -r Makefile ]; then
105	    makefile=Makefile
106	else
107	    echo 'no makefile or Makefile found' 1>&2
108	    exit 1
109	fi
110	;;
111esac
112
113if [ X"$makefile" != X- ]; then
114    if [ x"$append" = xn ]; then
115        sed -e "/^$magic_string/,\$d" < $makefile > $TMP
116        echo "$magic_string" >> $TMP
117    else
118        cp $makefile $TMP
119    fi
120fi
121
122# need to link .s files to .S
123for i in $files; do
124    case $i in
125	*.s)
126	    dir=`dirname $i`
127	    base=`basename $i .s`
128	    (cd $dir; $RM ${base}.S; $LN ${base}.s ${base}.S)
129	    asmfiles="$asmfiles ${base}.S"
130	    ;;
131    esac
132done
133
134CMD="$CC -MM $args `echo $files | sed -e 's,\.s$,\.S,g' -e 's,\.s ,\.S ,g'` | 	sed -e 's,\.S$,\.s,g' -e 's,\.S ,\.s ,g'"
135
136if [ X"$makefile" != X- ]; then
137    CMD="$CMD >> $TMP"
138fi
139eval $CMD
140if [ X"$makefile" != X- ]; then
141    $RM ${makefile}.bak
142    $MV $makefile ${makefile}.bak
143    $MV $TMP $makefile
144fi
145
146if [ x"$asmfiles" != x ]; then
147    $RM $asmfiles
148fi
149$RM ${TMP}*
150exit 0
151