02458b8923a3c782231a882a322e2141e6c2c177
[alexxy/gromacs.git] / config / depcomp
1 #! /bin/sh
2
3 # depcomp - compile a program generating dependencies as side-effects
4 # Copyright 1999, 2000 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20
21 # Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
22
23 if test -z "$depmode" || test -z "$source" || test -z "$object"; then
24   echo "depcomp: Variables source, object and depmode must be set" 1>&2
25   exit 1
26 fi
27 # `libtool' can also be set to `yes' or `no'.
28
29 depfile=${depfile-`echo "$object" | sed 's,\([^/]*\)$,.deps/\1,;s/\.\([^.]*\)$/.P\1/'`}
30 tmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
31
32 rm -f "$tmpdepfile"
33
34 # Some modes work just like other modes, but use different flags.  We
35 # parameterize here, but still list the modes in the big case below,
36 # to make depend.m4 easier to write.  Note that we *cannot* use a case
37 # here, because this file can only contain one case statement.
38 if test "$depmode" = hp; then
39   # HP compiler uses -M and no extra arg.
40   gccflag=-M
41   depmode=gcc
42 fi
43
44 if test "$depmode" = dashXmstdout; then
45    # This is just like dashmstdout with a different argument.
46    dashmflag=-xM
47    depmode=dashmstdout
48 fi
49
50 case "$depmode" in
51 gcc3)
52 ## gcc 3 implements dependency tracking that does exactly what
53 ## we want.  Yay!
54   "$@" -MT "$object" -MF "$tmpdepfile" -MD -MP
55   stat=$?
56   if test $stat -eq 0; then :
57   else
58     rm -f "$tmpdepfile"
59     exit $stat
60   fi
61   mv "$tmpdepfile" "$depfile"
62   ;;
63
64 gcc)
65 ## There are various ways to get dependency output from gcc.  Here's
66 ## why we pick this rather obscure method:
67 ## - Don't want to use -MD because we'd like the dependencies to end
68 ##   up in a subdir.  Having to rename by hand is ugly.
69 ##   (We might end up doing this anyway to support other compilers.)
70 ## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
71 ##   -MM, not -M (despite what the docs say).
72 ## - Using -M directly means running the compiler twice (even worse
73 ##   than renaming).
74   if test -z "$gccflag"; then
75     gccflag=-MD,
76   fi
77   "$@" -Wp,"$gccflag$tmpdepfile"
78   stat=$?
79   if test $stat -eq 0; then :
80   else
81     rm -f "$tmpdepfile"
82     exit $stat
83   fi
84   rm -f "$depfile"
85   echo "$object : \\" > "$depfile"
86   alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
87 ## The second -e expression handles DOS-style file names with drive letters.
88   sed -e 's/^[^:]*: / /' \
89       -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
90 ## This next piece of magic avoids the `deleted header file' problem.
91 ## The problem is that when a header file which appears in a .P file
92 ## is deleted, the dependency causes make to die (because there is
93 ## typically no way to rebuild the header).  We avoid this by adding
94 ## dummy dependencies for each header file.  Too bad gcc doesn't do
95 ## this for us directly.
96   tr ' ' '
97 ' < "$tmpdepfile" |
98 ## Some versions of gcc put a space before the `:'.  On the theory
99 ## that the space means something, we add a space to the output as
100 ## well.
101 ## Some versions of the HPUX 10.20 sed can't process this invocation
102 ## correctly.  Breaking it into two sed invocations is a workaround.
103     sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile"
104   rm -f "$tmpdepfile"
105   ;;
106
107 hp)
108   # This case exists only to let depend.m4 do its work.  It works by
109   # looking at the text of this script.  This case will never be run,
110   # since it is checked for above.
111   exit 1
112   ;;
113
114 sgi)
115   if test "$libtool" = yes; then
116     "$@" "-Wp,-MDupdate,$tmpdepfile"
117   else
118     "$@" -MDupdate "$tmpdepfile"
119   fi
120   stat=$?
121   if test $stat -eq 0; then :
122   else
123     rm -f "$tmpdepfile"
124     exit $stat
125   fi
126   rm -f "$depfile"
127
128   if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
129     echo "$object : \\" > "$depfile"
130
131     # Clip off the initial element (the dependent).  Don't try to be
132     # clever and replace this with sed code, as IRIX sed won't handle
133     # lines with more than a fixed number of characters (4096 in
134     # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
135     # the IRIX cc adds comments like `#:fec' to the end of the
136     # dependency line.
137     tr ' ' '
138 ' < "$tmpdepfile" \
139     | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' | \
140     tr '
141 ' ' ' >> $depfile
142     echo >> $depfile
143
144     # The second pass generates a dummy entry for each header file.
145     tr ' ' '
146 ' < "$tmpdepfile" \
147    | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
148    >> $depfile
149   else
150     # The sourcefile does not contain any dependencies, so just
151     # store a dummy comment line, to avoid errors with the Makefile
152     # "include basename.Plo" scheme.
153     echo "#dummy" > "$depfile"
154   fi
155   rm -f "$tmpdepfile"
156   ;;
157
158 aix)
159   # The C for AIX Compiler uses -M and outputs the dependencies
160   # in a .u file.  This file always lives in the current directory.
161   # Also, the AIX compiler puts `$object:' at the start of each line;
162   # $object doesn't have directory information.
163   stripped=`echo "$object" | sed -e 's,^.*/,,' -e 's/\(.*\)\..*$/\1/'`
164   tmpdepfile="$stripped.u"
165   outname="$stripped.o"
166   if test "$libtool" = yes; then
167     "$@" -Wc,-M
168   else
169     "$@" -M
170   fi
171
172   stat=$?
173   if test $stat -eq 0; then :
174   else
175     rm -f "$tmpdepfile"
176     exit $stat
177   fi
178
179   if test -f "$tmpdepfile"; then
180     # Each line is of the form `foo.o: dependent.h'.
181     # Do two passes, one to just change these to
182     # `$object: dependent.h' and one to simply `dependent.h:'.
183     sed -e "s,^$outname:,$object :," < "$tmpdepfile" > "$depfile"
184     sed -e "s,^$outname: \(.*\)$,\1:," < "$tmpdepfile" >> "$depfile"
185   else
186     # The sourcefile does not contain any dependencies, so just
187     # store a dummy comment line, to avoid errors with the Makefile
188     # "include basename.Plo" scheme.
189     echo "#dummy" > "$depfile"
190   fi
191   rm -f "$tmpdepfile"
192   ;;
193
194 tru64)
195    # The Tru64 AIX compiler uses -MD to generate dependencies as a side
196    # effect.  `cc -MD -o foo.o ...' puts the dependencies into `foo.o.d'.
197    # Subdirectories are respected.
198
199    tmpdepfile="$object.d"
200    if test "$libtool" = yes; then
201       "$@" -Wc,-MD
202    else
203       "$@" -MD
204    fi
205
206    stat=$?
207    if test $stat -eq 0; then :
208    else
209       rm -f "$tmpdepfile"
210       exit $stat
211    fi
212
213    if test -f "$tmpdepfile"; then
214       sed -e "s,^.*\.[a-z]*:,$object:," < "$tmpdepfile" > "$depfile"
215       # That's a space and a tab in the [].
216       sed -e 's,^.*\.[a-z]*:[   ]*,,' -e 's,$,:,' < "$tmpdepfile" >> "$depfile"
217    else
218       echo "#dummy" > "$depfile"
219    fi
220    rm -f "$tmpdepfile"
221    ;;
222
223 #nosideeffect)
224   # This comment above is used by automake to tell side-effect
225   # dependency tracking mechanisms from slower ones.
226
227 dashmstdout)
228   # Important note: in order to support this mode, a compiler *must*
229   # always write the proprocessed file to stdout, regardless of -o,
230   # because we must use -o when running libtool.
231   test -z "$dashmflag" && dashmflag=-M
232   ( IFS=" "
233     case " $* " in
234     *" --mode=compile "*) # this is libtool, let us make it quiet
235       for arg
236       do # cycle over the arguments
237         case "$arg" in
238         "--mode=compile")
239           # insert --quiet before "--mode=compile"
240           set fnord "$@" --quiet
241           shift # fnord
242           ;;
243         esac
244         set fnord "$@" "$arg"
245         shift # fnord
246         shift # "$arg"
247       done
248       ;;
249     esac
250     "$@" $dashmflag | sed 's:^[^:]*\:[  ]*:'"$object"'\: :' > "$tmpdepfile"
251   ) &
252   proc=$!
253   "$@"
254   stat=$?
255   wait "$proc"
256   if test "$stat" != 0; then exit $stat; fi
257   rm -f "$depfile"
258   cat < "$tmpdepfile" > "$depfile"
259   tr ' ' '
260 ' < "$tmpdepfile" | \
261 ## Some versions of the HPUX 10.20 sed can't process this invocation
262 ## correctly.  Breaking it into two sed invocations is a workaround.
263     sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile"
264   rm -f "$tmpdepfile"
265   ;;
266
267 dashXmstdout)
268   # This case only exists to satisfy depend.m4.  It is never actually
269   # run, as this mode is specially recognized in the preamble.
270   exit 1
271   ;;
272
273 makedepend)
274   # X makedepend
275   (
276     shift
277     cleared=no
278     for arg in "$@"; do
279       case $cleared in no)
280         set ""; shift
281         cleared=yes
282       esac
283       case "$arg" in
284         -D*|-I*)
285           set fnord "$@" "$arg"; shift;;
286         -*)
287           ;;
288         *)
289           set fnord "$@" "$arg"; shift;;
290       esac
291     done
292     obj_suffix="`echo $object | sed 's/^.*\././'`"
293     touch "$tmpdepfile"
294     ${MAKEDEPEND-makedepend} 2>/dev/null -o"$obj_suffix" -f"$tmpdepfile" "$@"
295   ) &
296   proc=$!
297   "$@"
298   stat=$?
299   wait "$proc"
300   if test "$stat" != 0; then exit $stat; fi
301   rm -f "$depfile"
302   cat < "$tmpdepfile" > "$depfile"
303   tail +3 "$tmpdepfile" | tr ' ' '
304 ' | \
305 ## Some versions of the HPUX 10.20 sed can't process this invocation
306 ## correctly.  Breaking it into two sed invocations is a workaround.
307     sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile"
308   rm -f "$tmpdepfile" "$tmpdepfile".bak
309   ;;
310
311 cpp)
312   # Important note: in order to support this mode, a compiler *must*
313   # always write the proprocessed file to stdout, regardless of -o,
314   # because we must use -o when running libtool.
315   ( IFS=" "
316     case " $* " in
317     *" --mode=compile "*)
318       for arg
319       do # cycle over the arguments
320         case $arg in
321         "--mode=compile")
322           # insert --quiet before "--mode=compile"
323           set fnord "$@" --quiet
324           shift # fnord
325           ;;
326         esac
327         set fnord "$@" "$arg"
328         shift # fnord
329         shift # "$arg"
330       done
331       ;;
332     esac
333     "$@" -E |
334     sed -n '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' |
335     sed '$ s: \\$::' > "$tmpdepfile"
336   ) &
337   proc=$!
338   "$@"
339   stat=$?
340   wait "$proc"
341   if test "$stat" != 0; then exit $stat; fi
342   rm -f "$depfile"
343   echo "$object : \\" > "$depfile"
344   cat < "$tmpdepfile" >> "$depfile"
345   sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
346   rm -f "$tmpdepfile"
347   ;;
348
349 msvisualcpp)
350   # Important note: in order to support this mode, a compiler *must*
351   # always write the proprocessed file to stdout, regardless of -o,
352   # because we must use -o when running libtool.
353   ( IFS=" "
354     case " $* " in
355     *" --mode=compile "*)
356       for arg
357       do # cycle over the arguments
358         case $arg in
359         "--mode=compile")
360           # insert --quiet before "--mode=compile"
361           set fnord "$@" --quiet
362           shift # fnord
363           ;;
364         esac
365         set fnord "$@" "$arg"
366         shift # fnord
367         shift # "$arg"
368       done
369       ;;
370     esac
371     "$@" -E |
372     sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::echo "`cygpath -u \\"\1\\"`":p' | sort | uniq > "$tmpdepfile"
373   ) &
374   proc=$!
375   "$@"
376   stat=$?
377   wait "$proc"
378   if test "$stat" != 0; then exit $stat; fi
379   rm -f "$depfile"
380   echo "$object : \\" > "$depfile"
381   . "$tmpdepfile" | sed 's% %\\ %g' | sed -n '/^\(.*\)$/ s::    \1 \\:p' >> "$depfile"
382   echo "        " >> "$depfile"
383   . "$tmpdepfile" | sed 's% %\\ %g' | sed -n '/^\(.*\)$/ s::\1\::p' >> "$depfile"
384   rm -f "$tmpdepfile"
385   ;;
386
387 none)
388   exec "$@"
389   ;;
390
391 *)
392   echo "Unknown depmode $depmode" 1>&2
393   exit 1
394   ;;
395 esac
396
397 exit 0