93ab261a81387c9e66fd166f07a5c77b8e95a185
[p5sagit/p5-mst-13.2.git] / makedepend.SH
1 #! /bin/sh
2 case $CONFIG 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 : ${bin_sh=/bin/sh}
22
23 echo "Extracting makedepend (with variable substitutions)"
24 rm -f makedepend
25 $spitshell >makedepend <<!GROK!THIS!
26 $startsh
27 # makedepend.SH
28 #
29 ## To use an alternate make, set \$altmake in config.sh.
30 MAKE=${altmake-make}
31 !GROK!THIS!
32 $spitshell >>makedepend <<'!NO!SUBS!'
33
34 export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh \$0; kill \$\$)
35
36 case $CONFIG in
37 '')
38         if test -f config.sh; then TOP=.;
39         elif test -f ../config.sh; then TOP=..;
40         elif test -f ../../config.sh; then TOP=../..;
41         elif test -f ../../../config.sh; then TOP=../../..;
42         elif test -f ../../../../config.sh; then TOP=../../../..;
43         else
44                 echo "Can't find config.sh."; exit 1
45         fi
46         . $TOP/config.sh
47         ;;
48 esac
49
50 # We need .. when we are in the x2p directory if we are using the
51 # cppstdin wrapper script.
52 # Put .. and . first so that we pick up the present cppstdin, not
53 # an older one lying about in /usr/local/bin.
54 PATH=".:..:$PATH"
55 export PATH
56
57 $cat /dev/null >.deptmp
58 $rm -f *.c.c c/*.c.c
59 if test -f Makefile; then
60     rm -f $firstmakefile
61     cp Makefile $firstmakefile
62 fi
63 mf=$firstmakefile
64 if test -f $mf; then
65     defrule=`<$mf sed -n                \
66         -e '/^\.c\(\$(OBJ_EXT)\|\.o\):.*;/{'    \
67         -e    's/\$\*\.c//'             \
68         -e    's/^[^;]*;[        ]*//p' \
69         -e    q                         \
70         -e '}'                          \
71         -e '/^\.c\(\$(OBJ_EXT)\|\.o\): *$/{'    \
72         -e    N                         \
73         -e    's/\$\*\.c//'             \
74         -e    's/^.*\n[  ]*//p'         \
75         -e    q                         \
76         -e '}'`
77 fi
78 case "$defrule" in
79 '') defrule='$(CC) -c $(CFLAGS)' ;;
80 esac
81
82 : Create files in UU directory to avoid problems with long filenames
83 : on systems with 14 character filename limits so file.c.c and file.c
84 : might be identical
85 $test -d UU || mkdir UU
86
87 $MAKE clist || ($echo "Searching for .c files..."; \
88         $echo *.c | $tr ' ' '\012' | $egrep -v '\*' >.clist)
89 for file in `$cat .clist`; do
90 # for file in `cat /dev/null`; do
91     case "$file" in
92     *.c) filebase=`basename $file .c` ;;
93     *.y) filebase=`basename $file .y` ;;
94     esac
95     case "$file" in
96     */*) finc="-I`echo $file | sed 's#/[^/]*$##`" ;;
97     *)   finc= ;;
98     esac
99     $echo "Finding dependencies for $filebase$obj_ext."
100     ( $echo "#line 1 \"$file\""; \
101       $sed -n <$file \
102         -e "/^${filebase}_init(/q" \
103         -e '/^#line/d' \
104         -e '/^#/{' \
105         -e 's|/\*.*$||' \
106         -e 's|\\$||' \
107         -e p \
108         -e '}' ) >UU/$file.c
109     $cppstdin $finc -I/usr/local/include -I. $cppflags $cppminus <UU/$file.c |
110     $sed \
111         -e '/^#.*<stdin>/d' \
112         -e 's#\.[0-9][0-9]*\.c#'"$file.c#" \
113         -e 's/^[         ]*#[    ]*line/#/' \
114         -e '/^# *[0-9][0-9]* *[".\/]/!d' \
115         -e 's/^.*"\(.*\)".*$/'$filebase'\$(OBJ_EXT): \1/' \
116         -e 's/^# *[0-9][0-9]* \(.*\)$/'$filebase'\$(OBJ_EXT): \1/' \
117         -e 's|: \./|: |' \
118         -e 's|\.c\.c|.c|' | \
119     $uniq | $sort | $uniq >> .deptmp
120 done
121
122 $sed <$mf >$mf.new -e '1,/^# AUTOMATICALLY/!d'
123
124 $MAKE shlist || ($echo "Searching for .SH files..."; \
125         $echo *.SH | $tr ' ' '\012' | $egrep -v '\*' >.shlist)
126
127 # Now extract the dependencies on makedepend.SH and Makefile.SH
128 # (they should reside in the main Makefile):
129 mv .shlist .shlist.old
130 $egrep -v '^makedepend\.SH' <.shlist.old >.shlist
131 mv .shlist .shlist.old
132 $egrep -v '^Makefile\.SH' <.shlist.old >.shlist
133 rm .shlist.old
134
135 if $test -s .deptmp; then
136     for file in `cat .shlist`; do
137         $echo `$expr X$file : 'X\(.*\).SH'`: $file $TOP/config.sh \; \
138             $bin_sh $file >> .deptmp
139     done
140     $echo "Updating $mf..."
141     $echo "# If this runs make out of memory, delete /usr/include lines." \
142         >> $mf.new
143     $sed 's|^\(.*\(\$(OBJ_EXT)\|\.o\):\) *\(.*/.*\.c\) *$|\1 \3; '"$defrule \2|" .deptmp \
144        >>$mf.new
145 else
146     $MAKE hlist || ($echo "Searching for .h files..."; \
147         $echo *.h | $tr ' ' '\012' | $egrep -v '\*' >.hlist)
148     $echo "You don't seem to have a proper C preprocessor.  Using grep instead."
149     $egrep '^#include ' `cat .clist` `cat .hlist`  >.deptmp
150     $echo "Updating $mf..."
151     <.clist $sed -n                                                     \
152         -e '/\//{'                                                      \
153         -e   's|^\(.*\)/\(.*\)\.c|\2\$(OBJ_EXT): \1/\2.c; '"$defrule \1/\2.c|p" \
154         -e   d                                                          \
155         -e '}'                                                          \
156         -e 's|^\(.*\)\.c|\1\$(OBJ_EXT): \1.c|p' >> $mf.new
157     <.hlist $sed -n 's|\(.*/\)\(.*\)|s= \2= \1\2=|p' >.hsed
158     <.deptmp $sed -n 's|c:#include "\(.*\)".*$|o: \1|p' | \
159        $sed 's|^[^;]*/||' | \
160        $sed -f .hsed >> $mf.new
161     <.deptmp $sed -n 's|h:#include "\(.*\)".*$|h: \1|p' | \
162        $sed -f .hsed >> $mf.new
163     for file in `$cat .shlist`; do
164         $echo `$expr X$file : 'X\(.*\).SH'`: $file $TOP/config.sh \; \
165             $bin_sh $file >> $mf.new
166     done
167 fi
168 $rm -f $mf.old
169 $cp $mf $mf.old
170 $rm -f $mf
171 $cp $mf.new $mf
172 $rm $mf.new
173 $echo "# WARNING: Put nothing here or make depend will gobble it up!" >> $mf
174 $rm -rf .deptmp UU .shlist .clist .hlist .hsed
175
176 !NO!SUBS!
177 $eunicefix makedepend
178 chmod +x makedepend
179 case `pwd` in
180 *SH)
181     $rm -f ../makedepend
182     ln makedepend ../makedepend
183     ;;
184 esac