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