First steps of making builds outside the source
[p5sagit/p5-mst-13.2.git] / makedepend.SH
1 #! /bin/sh
2 case $CONFIGDOTSH in
3 '')
4         if test -f config.sh; then TOP=.;
5         elif test -f ../config.sh; then TOP=..;
6         elif test -f ../../config.sh; then TOP=../..;
7         elif test -f ../../../config.sh; then TOP=../../..;
8         elif test -f ../../../../config.sh; then TOP=../../../..;
9         else
10                 echo "Can't find config.sh."; exit 1
11         fi
12         . $TOP/config.sh
13         ;;
14 esac
15 : This forces SH files to create target in same directory as SH file.
16 : This is so that make depend always knows where to find SH derivatives.
17 case "$0" in
18 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
19 esac
20
21 echo "Extracting makedepend (with variable substitutions)"
22 rm -f makedepend
23 $spitshell >makedepend <<!GROK!THIS!
24 $startsh
25 # makedepend.SH
26 #
27 MAKE=$make
28 trnl='$trnl'
29 src=$src
30 !GROK!THIS!
31 $spitshell >>makedepend <<'!NO!SUBS!'
32
33 # This script should be called with 
34 #     sh ./makedepend MAKE=$(MAKE) [src=$(src)]
35 case "$1" in 
36         MAKE=*) eval $1 ; shift ;;
37 esac
38
39 export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh \$0; kill \$\$)
40
41 case $CONFIGDOTSH in
42 '')
43         if test -f config.sh; then TOP=.;
44         elif test -f ../config.sh; then TOP=..;
45         elif test -f ../../config.sh; then TOP=../..;
46         elif test -f ../../../config.sh; then TOP=../../..;
47         elif test -f ../../../../config.sh; then TOP=../../../..;
48         else
49                 echo "Can't find config.sh."; exit 1
50         fi
51         . $TOP/config.sh
52         ;;
53 esac
54
55 case "$1" in 
56         src=*) eval $1 ; shift ;;
57 esac
58
59 # We need .. when we are in the x2p directory if we are using the
60 # cppstdin wrapper script.
61 # Put .. and . first so that we pick up the present cppstdin, not
62 # an older one lying about in /usr/local/bin.
63 PATH=".$path_sep..$path_sep$PATH"
64 export PATH
65
66 $cat /dev/null >.deptmp
67 $rm -f *.c.c c/*.c.c
68 if test -f Makefile; then
69     rm -f $firstmakefile
70     cp Makefile $firstmakefile
71     # On QNX, 'cp' preserves timestamp, so $firstmakefile appears
72     # to be out of date.  I don't know if OS/2 has touch, so do this:
73     case "$osname" in
74     os2) ;;
75     netbsd) ;;
76     *) $touch $firstmakefile ;;
77     esac
78 fi
79 mf=$firstmakefile
80 if test -f $mf; then
81     defrule=`<$mf sed -n                \
82         -e '/^\.c\$(OBJ_EXT):.*;/{'     \
83         -e    's/\$\*\.c//'             \
84         -e    's/^[^;]*;[        ]*//p' \
85         -e    q                         \
86         -e '}'                          \
87         -e '/^\.c\$(OBJ_EXT): *$/{'     \
88         -e    N                         \
89         -e    's/\$\*\.c//'             \
90         -e    's/^.*\n[  ]*//p'         \
91         -e    q                         \
92         -e '}'`
93 fi
94 case "$defrule" in
95 '') defrule='$(CC) -c $(CFLAGS)' ;;
96 esac
97
98 : Create files in UU directory to avoid problems with long filenames
99 : on systems with 14 character filename limits so file.c.c and file.c
100 : might be identical
101 $test -d UU || mkdir UU
102
103 $MAKE clist || (cd $src; $echo "Searching for .c files..."; \
104         $echo *.c | $tr ' ' $trnl | $egrep -v '\*') >.clist
105 for base in `$cat .clist`; do
106     case "$src" in
107     .) file="$base" ;;
108     *) if test -f "$base"; then
109            file="$base"
110        elif test -f "$src/$base"; then
111            file="$src/$base"
112        fi
113        ;;
114     esac
115 # for file in `cat /dev/null`; do
116         if [ "$osname" = uwin ]; then
117                 uwinfix="-e s,\\\\\\\\,/,g -e s,\\([a-zA-Z]\\):/,/\\1/,g"
118         else
119                 if [ "$osname" = os2 ]; then
120                         uwinfix="-e s,\\\\\\\\,/,g"
121                 else
122                         if [ "$archname" = cygwin32 ]; then
123                                 uwinfix="-e s,\\\\\\\\,/,g"
124                         else
125                                 uwinfix=
126                         fi
127                 fi
128         fi
129     case "$file" in
130     */*) finc="-I`echo $file | sed 's#/[^/]*$##`" ;;
131     *)   finc= ;;
132     esac
133     case "$src" in
134     '') ;;
135     *)  finc="-I$src" ;;
136     esac
137     basebase=`basename $base .c`
138     $echo "Finding dependencies for $basebase$_o."
139     ( $echo "#line 1 \"$file\""; \
140       $sed -n <$file \
141         -e "/^${filebase}_init(/q" \
142         -e '/^#line/d' \
143         -e '/^#/{' \
144         -e 's|/\*.*$||' \
145         -e 's|\\$||' \
146         -e p \
147         -e '}' ) >UU/$base.c
148     $cppstdin $finc -I. $cppflags $cppminus <UU/$base.c |
149     $sed \
150         -e '/^#.*<stdin>/d' \
151         -e '/^#.*"-"/d' \
152         -e 's#\.[0-9][0-9]*\.c#'"$file.c#" \
153         -e 's/^[         ]*#[    ]*line/#/' \
154         -e '/^# *[0-9][0-9]* *[".\/]/!d' \
155         -e 's/^.*"\(.*\)".*$/'$base'\$(OBJ_EXT): \1/' \
156         -e 's/^# *[0-9][0-9]* \(.*\)$/'$base'\$(OBJ_EXT): \1/' \
157         -e 's|: \./|: |' \
158         -e 's|\.c\.c|.c|' $uwinfix | \
159     $uniq | $sort | $uniq >> .deptmp
160 done
161
162 $sed <$mf >$mf.new -e '1,/^# AUTOMATICALLY/!d'
163
164 $MAKE shlist || (cd $src; $echo "Searching for .SH files..."; \
165         $echo *.SH | $tr ' ' $trnl | $egrep -v '\*') >.shlist
166
167 # Now extract the dependencies on makedepend.SH and Makefile.SH
168 # (they should reside in the main Makefile):
169 rm -f .shlist.old
170 mv .shlist .shlist.old
171 $egrep -v '^makedepend\.SH' <.shlist.old >.shlist
172 rm -f .shlist.old
173 mv .shlist .shlist.old
174 $egrep -v '^Makefile\.SH' <.shlist.old >.shlist
175 rm -f .shlist.old
176 mv .shlist .shlist.old
177 $egrep -v '^perl_exp\.SH' <.shlist.old >.shlist
178 rm -f .shlist.old
179 mv .shlist .shlist.old
180 $egrep -v '^config_h\.SH' <.shlist.old >.shlist
181 rm .shlist.old
182
183 if $test -s .deptmp; then
184     for file in `cat .shlist`; do
185         $echo `$expr X$file : 'X\(.*\).SH'`: $file $TOP/config.sh \; \
186             $sh $file >> .deptmp
187     done
188     $echo "Updating $mf..."
189     $echo "# If this runs make out of memory, delete /usr/include lines." \
190         >> $mf.new
191     $sed 's|^\(.*\$(OBJ_EXT):\) *\(.*/.*\.c\) *$|\1 \2; '"$defrule \2|" .deptmp \
192        >>$mf.new
193 else
194     $MAKE hlist || (cd $src; $echo "Searching for .h files..."; \
195         $echo *.h | $tr ' ' $trnl | $egrep -v '\*') >.hlist
196     $echo "You don't seem to have a proper C preprocessor.  Using grep instead."
197     $egrep '^#include ' `cat .clist` `cat .hlist`  >.deptmp
198     $echo "Updating $mf..."
199     <.clist $sed -n                                                     \
200         -e '/\//{'                                                      \
201         -e   's|^\(.*\)/\(.*\)\.c|\2\$(OBJ_EXT): \1/\2.c; '"$defrule \1/\2.c|p" \
202         -e   d                                                          \
203         -e '}'                                                          \
204         -e 's|^\(.*\)\.c|\1\$(OBJ_EXT): \1.c|p' >> $mf.new
205     <.hlist $sed -n 's|\(.*/\)\(.*\)|s= \2= \1\2=|p' >.hsed
206     <.deptmp $sed -n 's|c:#include "\(.*\)".*$|o: \1|p' | \
207        $sed 's|^[^;]*/||' | \
208        $sed -f .hsed >> $mf.new
209     <.deptmp $sed -n 's|h:#include "\(.*\)".*$|h: \1|p' | \
210        $sed -f .hsed >> $mf.new
211     for file in `$cat .shlist`; do
212         $echo `$expr X$file : 'X\(.*\).SH'`: $file $TOP/config.sh \; \
213             $sh $file >> $mf.new
214     done
215 fi
216 $rm -f $mf.old
217 $cp $mf $mf.old
218 $rm -f $mf
219 $cp $mf.new $mf
220 $rm $mf.new
221 $echo "# WARNING: Put nothing here or make depend will gobble it up!" >> $mf
222 $rm -rf .deptmp UU .shlist .clist .hlist .hsed
223
224 !NO!SUBS!
225 $eunicefix makedepend
226 chmod +x makedepend
227 case `pwd` in
228 *SH)
229     $rm -f ../makedepend
230     ln makedepend ../makedepend
231     ;;
232 esac