17a84e134Smrg#! /bin/sh
27a84e134Smrg# depcomp - compile a program generating dependencies as side-effects
37a84e134Smrg
45ec34c4cSmrgscriptversion=2018-03-07.03; # UTC
57a84e134Smrg
65b16253fSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
77a84e134Smrg
87a84e134Smrg# This program is free software; you can redistribute it and/or modify
97a84e134Smrg# it under the terms of the GNU General Public License as published by
107a84e134Smrg# the Free Software Foundation; either version 2, or (at your option)
117a84e134Smrg# any later version.
127a84e134Smrg
137a84e134Smrg# This program is distributed in the hope that it will be useful,
147a84e134Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
157a84e134Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167a84e134Smrg# GNU General Public License for more details.
177a84e134Smrg
187a84e134Smrg# You should have received a copy of the GNU General Public License
195ec34c4cSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
207a84e134Smrg
217a84e134Smrg# As a special exception to the GNU General Public License, if you
227a84e134Smrg# distribute this file as part of a program that contains a
237a84e134Smrg# configuration script generated by Autoconf, you may include it under
247a84e134Smrg# the same distribution terms that you use for the rest of that program.
257a84e134Smrg
267a84e134Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
277a84e134Smrg
287a84e134Smrgcase $1 in
297a84e134Smrg  '')
30c889a3bfSmrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31c889a3bfSmrg    exit 1;
32c889a3bfSmrg    ;;
337a84e134Smrg  -h | --h*)
347a84e134Smrg    cat <<\EOF
357a84e134SmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
367a84e134Smrg
377a84e134SmrgRun PROGRAMS ARGS to compile a file, generating dependencies
387a84e134Smrgas side-effects.
397a84e134Smrg
407a84e134SmrgEnvironment variables:
417a84e134Smrg  depmode     Dependency tracking mode.
42c889a3bfSmrg  source      Source file read by 'PROGRAMS ARGS'.
43c889a3bfSmrg  object      Object file output by 'PROGRAMS ARGS'.
447a84e134Smrg  DEPDIR      directory where to store dependencies.
457a84e134Smrg  depfile     Dependency file to output.
46421c997bSmrg  tmpdepfile  Temporary file to use when outputting dependencies.
477a84e134Smrg  libtool     Whether libtool is used (yes/no).
487a84e134Smrg
497a84e134SmrgReport bugs to <bug-automake@gnu.org>.
507a84e134SmrgEOF
517a84e134Smrg    exit $?
527a84e134Smrg    ;;
537a84e134Smrg  -v | --v*)
547a84e134Smrg    echo "depcomp $scriptversion"
557a84e134Smrg    exit $?
567a84e134Smrg    ;;
577a84e134Smrgesac
587a84e134Smrg
59c889a3bfSmrg# Get the directory component of the given path, and save it in the
60c889a3bfSmrg# global variables '$dir'.  Note that this directory component will
61c889a3bfSmrg# be either empty or ending with a '/' character.  This is deliberate.
62c889a3bfSmrgset_dir_from ()
63c889a3bfSmrg{
64c889a3bfSmrg  case $1 in
65c889a3bfSmrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
66c889a3bfSmrg      *) dir=;;
67c889a3bfSmrg  esac
68c889a3bfSmrg}
69c889a3bfSmrg
70c889a3bfSmrg# Get the suffix-stripped basename of the given path, and save it the
71c889a3bfSmrg# global variable '$base'.
72c889a3bfSmrgset_base_from ()
73c889a3bfSmrg{
74c889a3bfSmrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
75c889a3bfSmrg}
76c889a3bfSmrg
77c889a3bfSmrg# If no dependency file was actually created by the compiler invocation,
78c889a3bfSmrg# we still have to create a dummy depfile, to avoid errors with the
79c889a3bfSmrg# Makefile "include basename.Plo" scheme.
80c889a3bfSmrgmake_dummy_depfile ()
81c889a3bfSmrg{
82c889a3bfSmrg  echo "#dummy" > "$depfile"
83c889a3bfSmrg}
84c889a3bfSmrg
85c889a3bfSmrg# Factor out some common post-processing of the generated depfile.
86c889a3bfSmrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
87c889a3bfSmrgaix_post_process_depfile ()
88c889a3bfSmrg{
89c889a3bfSmrg  # If the compiler actually managed to produce a dependency file,
90c889a3bfSmrg  # post-process it.
91c889a3bfSmrg  if test -f "$tmpdepfile"; then
92c889a3bfSmrg    # Each line is of the form 'foo.o: dependency.h'.
93c889a3bfSmrg    # Do two passes, one to just change these to
94c889a3bfSmrg    #   $object: dependency.h
95c889a3bfSmrg    # and one to simply output
96c889a3bfSmrg    #   dependency.h:
97c889a3bfSmrg    # which is needed to avoid the deleted-header problem.
98c889a3bfSmrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
99c889a3bfSmrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
100c889a3bfSmrg    } > "$depfile"
101c889a3bfSmrg    rm -f "$tmpdepfile"
102c889a3bfSmrg  else
103c889a3bfSmrg    make_dummy_depfile
104c889a3bfSmrg  fi
105c889a3bfSmrg}
106c889a3bfSmrg
107c889a3bfSmrg# A tabulation character.
108c889a3bfSmrgtab='	'
109c889a3bfSmrg# A newline character.
110c889a3bfSmrgnl='
111c889a3bfSmrg'
112c889a3bfSmrg# Character ranges might be problematic outside the C locale.
113c889a3bfSmrg# These definitions help.
114c889a3bfSmrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
115c889a3bfSmrglower=abcdefghijklmnopqrstuvwxyz
116c889a3bfSmrgdigits=0123456789
117c889a3bfSmrgalpha=${upper}${lower}
118c889a3bfSmrg
1197a84e134Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
1207a84e134Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
1217a84e134Smrg  exit 1
1227a84e134Smrgfi
1237a84e134Smrg
1247a84e134Smrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
1257a84e134Smrgdepfile=${depfile-`echo "$object" |
1267a84e134Smrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
1277a84e134Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
1287a84e134Smrg
1297a84e134Smrgrm -f "$tmpdepfile"
1307a84e134Smrg
131c889a3bfSmrg# Avoid interferences from the environment.
132c889a3bfSmrggccflag= dashmflag=
133c889a3bfSmrg
1347a84e134Smrg# Some modes work just like other modes, but use different flags.  We
1357a84e134Smrg# parameterize here, but still list the modes in the big case below,
1367a84e134Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
1377a84e134Smrg# here, because this file can only contain one case statement.
1387a84e134Smrgif test "$depmode" = hp; then
1397a84e134Smrg  # HP compiler uses -M and no extra arg.
1407a84e134Smrg  gccflag=-M
1417a84e134Smrg  depmode=gcc
1427a84e134Smrgfi
1437a84e134Smrg
1447a84e134Smrgif test "$depmode" = dashXmstdout; then
145c889a3bfSmrg  # This is just like dashmstdout with a different argument.
146c889a3bfSmrg  dashmflag=-xM
147c889a3bfSmrg  depmode=dashmstdout
1487a84e134Smrgfi
1497a84e134Smrg
150e1e1713cSmrgcygpath_u="cygpath -u -f -"
151e1e1713cSmrgif test "$depmode" = msvcmsys; then
152c889a3bfSmrg  # This is just like msvisualcpp but w/o cygpath translation.
153c889a3bfSmrg  # Just convert the backslash-escaped backslashes to single forward
154c889a3bfSmrg  # slashes to satisfy depend.m4
155c889a3bfSmrg  cygpath_u='sed s,\\\\,/,g'
156c889a3bfSmrg  depmode=msvisualcpp
157e1e1713cSmrgfi
158e1e1713cSmrg
159421c997bSmrgif test "$depmode" = msvc7msys; then
160c889a3bfSmrg  # This is just like msvc7 but w/o cygpath translation.
161c889a3bfSmrg  # Just convert the backslash-escaped backslashes to single forward
162c889a3bfSmrg  # slashes to satisfy depend.m4
163c889a3bfSmrg  cygpath_u='sed s,\\\\,/,g'
164c889a3bfSmrg  depmode=msvc7
165c889a3bfSmrgfi
166c889a3bfSmrg
167c889a3bfSmrgif test "$depmode" = xlc; then
168c889a3bfSmrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
169c889a3bfSmrg  gccflag=-qmakedep=gcc,-MF
170c889a3bfSmrg  depmode=gcc
171421c997bSmrgfi
172421c997bSmrg
1737a84e134Smrgcase "$depmode" in
1747a84e134Smrggcc3)
1757a84e134Smrg## gcc 3 implements dependency tracking that does exactly what
1767a84e134Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
1777a84e134Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
1787a84e134Smrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
1797a84e134Smrg## the command line argument order; so add the flags where they
1807a84e134Smrg## appear in depend2.am.  Note that the slowdown incurred here
1817a84e134Smrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
1827a84e134Smrg  for arg
1837a84e134Smrg  do
1847a84e134Smrg    case $arg in
1857a84e134Smrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
1867a84e134Smrg    *)  set fnord "$@" "$arg" ;;
1877a84e134Smrg    esac
1887a84e134Smrg    shift # fnord
1897a84e134Smrg    shift # $arg
1907a84e134Smrg  done
1917a84e134Smrg  "$@"
1927a84e134Smrg  stat=$?
193c889a3bfSmrg  if test $stat -ne 0; then
1947a84e134Smrg    rm -f "$tmpdepfile"
1957a84e134Smrg    exit $stat
1967a84e134Smrg  fi
1977a84e134Smrg  mv "$tmpdepfile" "$depfile"
1987a84e134Smrg  ;;
1997a84e134Smrg
2007a84e134Smrggcc)
201c889a3bfSmrg## Note that this doesn't just cater to obsosete pre-3.x GCC compilers.
202c889a3bfSmrg## but also to in-use compilers like IMB xlc/xlC and the HP C compiler.
203c889a3bfSmrg## (see the conditional assignment to $gccflag above).
2047a84e134Smrg## There are various ways to get dependency output from gcc.  Here's
2057a84e134Smrg## why we pick this rather obscure method:
2067a84e134Smrg## - Don't want to use -MD because we'd like the dependencies to end
2077a84e134Smrg##   up in a subdir.  Having to rename by hand is ugly.
2087a84e134Smrg##   (We might end up doing this anyway to support other compilers.)
2097a84e134Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
210c889a3bfSmrg##   -MM, not -M (despite what the docs say).  Also, it might not be
211c889a3bfSmrg##   supported by the other compilers which use the 'gcc' depmode.
2127a84e134Smrg## - Using -M directly means running the compiler twice (even worse
2137a84e134Smrg##   than renaming).
2147a84e134Smrg  if test -z "$gccflag"; then
2157a84e134Smrg    gccflag=-MD,
2167a84e134Smrg  fi
2177a84e134Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
2187a84e134Smrg  stat=$?
219c889a3bfSmrg  if test $stat -ne 0; then
2207a84e134Smrg    rm -f "$tmpdepfile"
2217a84e134Smrg    exit $stat
2227a84e134Smrg  fi
2237a84e134Smrg  rm -f "$depfile"
2247a84e134Smrg  echo "$object : \\" > "$depfile"
225c889a3bfSmrg  # The second -e expression handles DOS-style file names with drive
226c889a3bfSmrg  # letters.
2277a84e134Smrg  sed -e 's/^[^:]*: / /' \
2287a84e134Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
229c889a3bfSmrg## This next piece of magic avoids the "deleted header file" problem.
2307a84e134Smrg## The problem is that when a header file which appears in a .P file
2317a84e134Smrg## is deleted, the dependency causes make to die (because there is
2327a84e134Smrg## typically no way to rebuild the header).  We avoid this by adding
2337a84e134Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
2347a84e134Smrg## this for us directly.
235c889a3bfSmrg## Some versions of gcc put a space before the ':'.  On the theory
2367a84e134Smrg## that the space means something, we add a space to the output as
237421c997bSmrg## well.  hp depmode also adds that space, but also prefixes the VPATH
238421c997bSmrg## to the object.  Take care to not repeat it in the output.
2397a84e134Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
2407a84e134Smrg## correctly.  Breaking it into two sed invocations is a workaround.
241c889a3bfSmrg  tr ' ' "$nl" < "$tmpdepfile" \
242c889a3bfSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
243c889a3bfSmrg    | sed -e 's/$/ :/' >> "$depfile"
2447a84e134Smrg  rm -f "$tmpdepfile"
2457a84e134Smrg  ;;
2467a84e134Smrg
2477a84e134Smrghp)
2487a84e134Smrg  # This case exists only to let depend.m4 do its work.  It works by
2497a84e134Smrg  # looking at the text of this script.  This case will never be run,
2507a84e134Smrg  # since it is checked for above.
2517a84e134Smrg  exit 1
2527a84e134Smrg  ;;
2537a84e134Smrg
2547a84e134Smrgsgi)
2557a84e134Smrg  if test "$libtool" = yes; then
2567a84e134Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
2577a84e134Smrg  else
2587a84e134Smrg    "$@" -MDupdate "$tmpdepfile"
2597a84e134Smrg  fi
2607a84e134Smrg  stat=$?
261c889a3bfSmrg  if test $stat -ne 0; then
2627a84e134Smrg    rm -f "$tmpdepfile"
2637a84e134Smrg    exit $stat
2647a84e134Smrg  fi
2657a84e134Smrg  rm -f "$depfile"
2667a84e134Smrg
2677a84e134Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
2687a84e134Smrg    echo "$object : \\" > "$depfile"
2697a84e134Smrg    # Clip off the initial element (the dependent).  Don't try to be
2707a84e134Smrg    # clever and replace this with sed code, as IRIX sed won't handle
2717a84e134Smrg    # lines with more than a fixed number of characters (4096 in
2727a84e134Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
273c889a3bfSmrg    # the IRIX cc adds comments like '#:fec' to the end of the
2747a84e134Smrg    # dependency line.
275c889a3bfSmrg    tr ' ' "$nl" < "$tmpdepfile" \
276c889a3bfSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
277c889a3bfSmrg      | tr "$nl" ' ' >> "$depfile"
278e1e1713cSmrg    echo >> "$depfile"
2797a84e134Smrg    # The second pass generates a dummy entry for each header file.
280c889a3bfSmrg    tr ' ' "$nl" < "$tmpdepfile" \
281c889a3bfSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
282c889a3bfSmrg      >> "$depfile"
2837a84e134Smrg  else
284c889a3bfSmrg    make_dummy_depfile
2857a84e134Smrg  fi
2867a84e134Smrg  rm -f "$tmpdepfile"
2877a84e134Smrg  ;;
2887a84e134Smrg
289c889a3bfSmrgxlc)
290c889a3bfSmrg  # This case exists only to let depend.m4 do its work.  It works by
291c889a3bfSmrg  # looking at the text of this script.  This case will never be run,
292c889a3bfSmrg  # since it is checked for above.
293c889a3bfSmrg  exit 1
294c889a3bfSmrg  ;;
295c889a3bfSmrg
2967a84e134Smrgaix)
2977a84e134Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
2987a84e134Smrg  # in a .u file.  In older versions, this file always lives in the
299c889a3bfSmrg  # current directory.  Also, the AIX compiler puts '$object:' at the
3007a84e134Smrg  # start of each line; $object doesn't have directory information.
3017a84e134Smrg  # Version 6 uses the directory in both cases.
302c889a3bfSmrg  set_dir_from "$object"
303c889a3bfSmrg  set_base_from "$object"
3047a84e134Smrg  if test "$libtool" = yes; then
305ab902922Smrg    tmpdepfile1=$dir$base.u
306ab902922Smrg    tmpdepfile2=$base.u
307ab902922Smrg    tmpdepfile3=$dir.libs/$base.u
3087a84e134Smrg    "$@" -Wc,-M
3097a84e134Smrg  else
310ab902922Smrg    tmpdepfile1=$dir$base.u
311ab902922Smrg    tmpdepfile2=$dir$base.u
312ab902922Smrg    tmpdepfile3=$dir$base.u
3137a84e134Smrg    "$@" -M
3147a84e134Smrg  fi
3157a84e134Smrg  stat=$?
316c889a3bfSmrg  if test $stat -ne 0; then
317ab902922Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
3187a84e134Smrg    exit $stat
3197a84e134Smrg  fi
3207a84e134Smrg
321ab902922Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
322ab902922Smrg  do
323ab902922Smrg    test -f "$tmpdepfile" && break
324ab902922Smrg  done
325c889a3bfSmrg  aix_post_process_depfile
326c889a3bfSmrg  ;;
327c889a3bfSmrg
328c889a3bfSmrgtcc)
329c889a3bfSmrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
330c889a3bfSmrg  # FIXME: That version still under development at the moment of writing.
331c889a3bfSmrg  #        Make that this statement remains true also for stable, released
332c889a3bfSmrg  #        versions.
333c889a3bfSmrg  # It will wrap lines (doesn't matter whether long or short) with a
334c889a3bfSmrg  # trailing '\', as in:
335c889a3bfSmrg  #
336c889a3bfSmrg  #   foo.o : \
337c889a3bfSmrg  #    foo.c \
338c889a3bfSmrg  #    foo.h \
339c889a3bfSmrg  #
340c889a3bfSmrg  # It will put a trailing '\' even on the last line, and will use leading
341c889a3bfSmrg  # spaces rather than leading tabs (at least since its commit 0394caf7
342c889a3bfSmrg  # "Emit spaces for -MD").
343c889a3bfSmrg  "$@" -MD -MF "$tmpdepfile"
344c889a3bfSmrg  stat=$?
345c889a3bfSmrg  if test $stat -ne 0; then
346c889a3bfSmrg    rm -f "$tmpdepfile"
347c889a3bfSmrg    exit $stat
3487a84e134Smrg  fi
349c889a3bfSmrg  rm -f "$depfile"
350c889a3bfSmrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
351c889a3bfSmrg  # We have to change lines of the first kind to '$object: \'.
352c889a3bfSmrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
353c889a3bfSmrg  # And for each line of the second kind, we have to emit a 'dep.h:'
354c889a3bfSmrg  # dummy dependency, to avoid the deleted-header problem.
355c889a3bfSmrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
3567a84e134Smrg  rm -f "$tmpdepfile"
3577a84e134Smrg  ;;
3587a84e134Smrg
359c889a3bfSmrg## The order of this option in the case statement is important, since the
360c889a3bfSmrg## shell code in configure will try each of these formats in the order
361c889a3bfSmrg## listed in this file.  A plain '-MD' option would be understood by many
362c889a3bfSmrg## compilers, so we must ensure this comes after the gcc and icc options.
363c889a3bfSmrgpgcc)
364c889a3bfSmrg  # Portland's C compiler understands '-MD'.
365c889a3bfSmrg  # Will always output deps to 'file.d' where file is the root name of the
366c889a3bfSmrg  # source file under compilation, even if file resides in a subdirectory.
367c889a3bfSmrg  # The object file name does not affect the name of the '.d' file.
368c889a3bfSmrg  # pgcc 10.2 will output
3697a84e134Smrg  #    foo.o: sub/foo.c sub/foo.h
370c889a3bfSmrg  # and will wrap long lines using '\' :
3717a84e134Smrg  #    foo.o: sub/foo.c ... \
3727a84e134Smrg  #     sub/foo.h ... \
3737a84e134Smrg  #     ...
374c889a3bfSmrg  set_dir_from "$object"
375c889a3bfSmrg  # Use the source, not the object, to determine the base name, since
376c889a3bfSmrg  # that's sadly what pgcc will do too.
377c889a3bfSmrg  set_base_from "$source"
378c889a3bfSmrg  tmpdepfile=$base.d
379c889a3bfSmrg
380c889a3bfSmrg  # For projects that build the same source file twice into different object
381c889a3bfSmrg  # files, the pgcc approach of using the *source* file root name can cause
382c889a3bfSmrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
383c889a3bfSmrg  # the same $tmpdepfile.
384c889a3bfSmrg  lockdir=$base.d-lock
385c889a3bfSmrg  trap "
386c889a3bfSmrg    echo '$0: caught signal, cleaning up...' >&2
387c889a3bfSmrg    rmdir '$lockdir'
388c889a3bfSmrg    exit 1
389c889a3bfSmrg  " 1 2 13 15
390c889a3bfSmrg  numtries=100
391c889a3bfSmrg  i=$numtries
392c889a3bfSmrg  while test $i -gt 0; do
393c889a3bfSmrg    # mkdir is a portable test-and-set.
394c889a3bfSmrg    if mkdir "$lockdir" 2>/dev/null; then
395c889a3bfSmrg      # This process acquired the lock.
396c889a3bfSmrg      "$@" -MD
397c889a3bfSmrg      stat=$?
398c889a3bfSmrg      # Release the lock.
399c889a3bfSmrg      rmdir "$lockdir"
400c889a3bfSmrg      break
401c889a3bfSmrg    else
402c889a3bfSmrg      # If the lock is being held by a different process, wait
403c889a3bfSmrg      # until the winning process is done or we timeout.
404c889a3bfSmrg      while test -d "$lockdir" && test $i -gt 0; do
405c889a3bfSmrg        sleep 1
406c889a3bfSmrg        i=`expr $i - 1`
407c889a3bfSmrg      done
408c889a3bfSmrg    fi
409c889a3bfSmrg    i=`expr $i - 1`
410c889a3bfSmrg  done
411c889a3bfSmrg  trap - 1 2 13 15
412c889a3bfSmrg  if test $i -le 0; then
413c889a3bfSmrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
414c889a3bfSmrg    echo "$0: check lockdir '$lockdir'" >&2
415c889a3bfSmrg    exit 1
416c889a3bfSmrg  fi
4177a84e134Smrg
418c889a3bfSmrg  if test $stat -ne 0; then
4197a84e134Smrg    rm -f "$tmpdepfile"
4207a84e134Smrg    exit $stat
4217a84e134Smrg  fi
4227a84e134Smrg  rm -f "$depfile"
4237a84e134Smrg  # Each line is of the form `foo.o: dependent.h',
4247a84e134Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
4257a84e134Smrg  # Do two passes, one to just change these to
4267a84e134Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
4277a84e134Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
4287a84e134Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
4297a84e134Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
430c889a3bfSmrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
431c889a3bfSmrg    | sed -e 's/$/ :/' >> "$depfile"
4327a84e134Smrg  rm -f "$tmpdepfile"
4337a84e134Smrg  ;;
4347a84e134Smrg
4357a84e134Smrghp2)
4367a84e134Smrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
4377a84e134Smrg  # compilers, which have integrated preprocessors.  The correct option
4387a84e134Smrg  # to use with these is +Maked; it writes dependencies to a file named
4397a84e134Smrg  # 'foo.d', which lands next to the object file, wherever that
4407a84e134Smrg  # happens to be.
4417a84e134Smrg  # Much of this is similar to the tru64 case; see comments there.
442c889a3bfSmrg  set_dir_from  "$object"
443c889a3bfSmrg  set_base_from "$object"
4447a84e134Smrg  if test "$libtool" = yes; then
4457a84e134Smrg    tmpdepfile1=$dir$base.d
4467a84e134Smrg    tmpdepfile2=$dir.libs/$base.d
4477a84e134Smrg    "$@" -Wc,+Maked
4487a84e134Smrg  else
4497a84e134Smrg    tmpdepfile1=$dir$base.d
4507a84e134Smrg    tmpdepfile2=$dir$base.d
4517a84e134Smrg    "$@" +Maked
4527a84e134Smrg  fi
4537a84e134Smrg  stat=$?
454c889a3bfSmrg  if test $stat -ne 0; then
4557a84e134Smrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
4567a84e134Smrg     exit $stat
4577a84e134Smrg  fi
4587a84e134Smrg
4597a84e134Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
4607a84e134Smrg  do
4617a84e134Smrg    test -f "$tmpdepfile" && break
4627a84e134Smrg  done
4637a84e134Smrg  if test -f "$tmpdepfile"; then
464c889a3bfSmrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
465c889a3bfSmrg    # Add 'dependent.h:' lines.
466e1e1713cSmrg    sed -ne '2,${
467c889a3bfSmrg               s/^ *//
468c889a3bfSmrg               s/ \\*$//
469c889a3bfSmrg               s/$/:/
470c889a3bfSmrg               p
471c889a3bfSmrg             }' "$tmpdepfile" >> "$depfile"
4727a84e134Smrg  else
473c889a3bfSmrg    make_dummy_depfile
4747a84e134Smrg  fi
4757a84e134Smrg  rm -f "$tmpdepfile" "$tmpdepfile2"
4767a84e134Smrg  ;;
4777a84e134Smrg
4787a84e134Smrgtru64)
479c889a3bfSmrg  # The Tru64 compiler uses -MD to generate dependencies as a side
480c889a3bfSmrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
481c889a3bfSmrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
482c889a3bfSmrg  # dependencies in 'foo.d' instead, so we check for that too.
483c889a3bfSmrg  # Subdirectories are respected.
484c889a3bfSmrg  set_dir_from  "$object"
485c889a3bfSmrg  set_base_from "$object"
486c889a3bfSmrg
487c889a3bfSmrg  if test "$libtool" = yes; then
488c889a3bfSmrg    # Libtool generates 2 separate objects for the 2 libraries.  These
489c889a3bfSmrg    # two compilations output dependencies in $dir.libs/$base.o.d and
490c889a3bfSmrg    # in $dir$base.o.d.  We have to check for both files, because
491c889a3bfSmrg    # one of the two compilations can be disabled.  We should prefer
492c889a3bfSmrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
493c889a3bfSmrg    # automatically cleaned when .libs/ is deleted, while ignoring
494c889a3bfSmrg    # the former would cause a distcleancheck panic.
495c889a3bfSmrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
496c889a3bfSmrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
497c889a3bfSmrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
498c889a3bfSmrg    "$@" -Wc,-MD
499c889a3bfSmrg  else
500c889a3bfSmrg    tmpdepfile1=$dir$base.d
501c889a3bfSmrg    tmpdepfile2=$dir$base.d
502c889a3bfSmrg    tmpdepfile3=$dir$base.d
503c889a3bfSmrg    "$@" -MD
504c889a3bfSmrg  fi
505c889a3bfSmrg
506c889a3bfSmrg  stat=$?
507c889a3bfSmrg  if test $stat -ne 0; then
508c889a3bfSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
509c889a3bfSmrg    exit $stat
510c889a3bfSmrg  fi
511c889a3bfSmrg
512c889a3bfSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
513c889a3bfSmrg  do
514c889a3bfSmrg    test -f "$tmpdepfile" && break
515c889a3bfSmrg  done
516c889a3bfSmrg  # Same post-processing that is required for AIX mode.
517c889a3bfSmrg  aix_post_process_depfile
518c889a3bfSmrg  ;;
5197a84e134Smrg
520421c997bSmrgmsvc7)
521421c997bSmrg  if test "$libtool" = yes; then
522421c997bSmrg    showIncludes=-Wc,-showIncludes
523421c997bSmrg  else
524421c997bSmrg    showIncludes=-showIncludes
525421c997bSmrg  fi
526421c997bSmrg  "$@" $showIncludes > "$tmpdepfile"
527421c997bSmrg  stat=$?
528421c997bSmrg  grep -v '^Note: including file: ' "$tmpdepfile"
529c889a3bfSmrg  if test $stat -ne 0; then
530421c997bSmrg    rm -f "$tmpdepfile"
531421c997bSmrg    exit $stat
532421c997bSmrg  fi
533421c997bSmrg  rm -f "$depfile"
534421c997bSmrg  echo "$object : \\" > "$depfile"
535421c997bSmrg  # The first sed program below extracts the file names and escapes
536421c997bSmrg  # backslashes for cygpath.  The second sed program outputs the file
537421c997bSmrg  # name when reading, but also accumulates all include files in the
538421c997bSmrg  # hold buffer in order to output them again at the end.  This only
539421c997bSmrg  # works with sed implementations that can handle large buffers.
540421c997bSmrg  sed < "$tmpdepfile" -n '
541421c997bSmrg/^Note: including file:  *\(.*\)/ {
542421c997bSmrg  s//\1/
543421c997bSmrg  s/\\/\\\\/g
544421c997bSmrg  p
545421c997bSmrg}' | $cygpath_u | sort -u | sed -n '
546421c997bSmrgs/ /\\ /g
547c889a3bfSmrgs/\(.*\)/'"$tab"'\1 \\/p
548421c997bSmrgs/.\(.*\) \\/\1:/
549421c997bSmrgH
550421c997bSmrg$ {
551c889a3bfSmrg  s/.*/'"$tab"'/
552421c997bSmrg  G
553421c997bSmrg  p
554421c997bSmrg}' >> "$depfile"
555c889a3bfSmrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
556421c997bSmrg  rm -f "$tmpdepfile"
557421c997bSmrg  ;;
558421c997bSmrg
559421c997bSmrgmsvc7msys)
560421c997bSmrg  # This case exists only to let depend.m4 do its work.  It works by
561421c997bSmrg  # looking at the text of this script.  This case will never be run,
562421c997bSmrg  # since it is checked for above.
563421c997bSmrg  exit 1
564421c997bSmrg  ;;
565421c997bSmrg
5667a84e134Smrg#nosideeffect)
5677a84e134Smrg  # This comment above is used by automake to tell side-effect
5687a84e134Smrg  # dependency tracking mechanisms from slower ones.
5697a84e134Smrg
5707a84e134Smrgdashmstdout)
5717a84e134Smrg  # Important note: in order to support this mode, a compiler *must*
5727a84e134Smrg  # always write the preprocessed file to stdout, regardless of -o.
5737a84e134Smrg  "$@" || exit $?
5747a84e134Smrg
5757a84e134Smrg  # Remove the call to Libtool.
5767a84e134Smrg  if test "$libtool" = yes; then
577e1e1713cSmrg    while test "X$1" != 'X--mode=compile'; do
5787a84e134Smrg      shift
5797a84e134Smrg    done
5807a84e134Smrg    shift
5817a84e134Smrg  fi
5827a84e134Smrg
583c889a3bfSmrg  # Remove '-o $object'.
5847a84e134Smrg  IFS=" "
5857a84e134Smrg  for arg
5867a84e134Smrg  do
5877a84e134Smrg    case $arg in
5887a84e134Smrg    -o)
5897a84e134Smrg      shift
5907a84e134Smrg      ;;
5917a84e134Smrg    $object)
5927a84e134Smrg      shift
5937a84e134Smrg      ;;
5947a84e134Smrg    *)
5957a84e134Smrg      set fnord "$@" "$arg"
5967a84e134Smrg      shift # fnord
5977a84e134Smrg      shift # $arg
5987a84e134Smrg      ;;
5997a84e134Smrg    esac
6007a84e134Smrg  done
6017a84e134Smrg
6027a84e134Smrg  test -z "$dashmflag" && dashmflag=-M
603c889a3bfSmrg  # Require at least two characters before searching for ':'
6047a84e134Smrg  # in the target name.  This is to cope with DOS-style filenames:
605c889a3bfSmrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
6067a84e134Smrg  "$@" $dashmflag |
607c889a3bfSmrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
6087a84e134Smrg  rm -f "$depfile"
6097a84e134Smrg  cat < "$tmpdepfile" > "$depfile"
610c889a3bfSmrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
611c889a3bfSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
612c889a3bfSmrg  tr ' ' "$nl" < "$tmpdepfile" \
613c889a3bfSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
614c889a3bfSmrg    | sed -e 's/$/ :/' >> "$depfile"
6157a84e134Smrg  rm -f "$tmpdepfile"
6167a84e134Smrg  ;;
6177a84e134Smrg
6187a84e134SmrgdashXmstdout)
6197a84e134Smrg  # This case only exists to satisfy depend.m4.  It is never actually
6207a84e134Smrg  # run, as this mode is specially recognized in the preamble.
6217a84e134Smrg  exit 1
6227a84e134Smrg  ;;
6237a84e134Smrg
6247a84e134Smrgmakedepend)
6257a84e134Smrg  "$@" || exit $?
6267a84e134Smrg  # Remove any Libtool call
6277a84e134Smrg  if test "$libtool" = yes; then
628e1e1713cSmrg    while test "X$1" != 'X--mode=compile'; do
6297a84e134Smrg      shift
6307a84e134Smrg    done
6317a84e134Smrg    shift
6327a84e134Smrg  fi
6337a84e134Smrg  # X makedepend
6347a84e134Smrg  shift
635e1e1713cSmrg  cleared=no eat=no
636e1e1713cSmrg  for arg
637e1e1713cSmrg  do
6387a84e134Smrg    case $cleared in
6397a84e134Smrg    no)
6407a84e134Smrg      set ""; shift
6417a84e134Smrg      cleared=yes ;;
6427a84e134Smrg    esac
643e1e1713cSmrg    if test $eat = yes; then
644e1e1713cSmrg      eat=no
645e1e1713cSmrg      continue
646e1e1713cSmrg    fi
6477a84e134Smrg    case "$arg" in
6487a84e134Smrg    -D*|-I*)
6497a84e134Smrg      set fnord "$@" "$arg"; shift ;;
6507a84e134Smrg    # Strip any option that makedepend may not understand.  Remove
6517a84e134Smrg    # the object too, otherwise makedepend will parse it as a source file.
652e1e1713cSmrg    -arch)
653e1e1713cSmrg      eat=yes ;;
6547a84e134Smrg    -*|$object)
6557a84e134Smrg      ;;
6567a84e134Smrg    *)
6577a84e134Smrg      set fnord "$@" "$arg"; shift ;;
6587a84e134Smrg    esac
6597a84e134Smrg  done
660e1e1713cSmrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
6617a84e134Smrg  touch "$tmpdepfile"
6627a84e134Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
6637a84e134Smrg  rm -f "$depfile"
664421c997bSmrg  # makedepend may prepend the VPATH from the source file name to the object.
665421c997bSmrg  # No need to regex-escape $object, excess matching of '.' is harmless.
666421c997bSmrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
667c889a3bfSmrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
668c889a3bfSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
669c889a3bfSmrg  sed '1,2d' "$tmpdepfile" \
670c889a3bfSmrg    | tr ' ' "$nl" \
671c889a3bfSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
672c889a3bfSmrg    | sed -e 's/$/ :/' >> "$depfile"
6737a84e134Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
6747a84e134Smrg  ;;
6757a84e134Smrg
6767a84e134Smrgcpp)
6777a84e134Smrg  # Important note: in order to support this mode, a compiler *must*
6787a84e134Smrg  # always write the preprocessed file to stdout.
6797a84e134Smrg  "$@" || exit $?
6807a84e134Smrg
6817a84e134Smrg  # Remove the call to Libtool.
6827a84e134Smrg  if test "$libtool" = yes; then
683e1e1713cSmrg    while test "X$1" != 'X--mode=compile'; do
6847a84e134Smrg      shift
6857a84e134Smrg    done
6867a84e134Smrg    shift
6877a84e134Smrg  fi
6887a84e134Smrg
689c889a3bfSmrg  # Remove '-o $object'.
6907a84e134Smrg  IFS=" "
6917a84e134Smrg  for arg
6927a84e134Smrg  do
6937a84e134Smrg    case $arg in
6947a84e134Smrg    -o)
6957a84e134Smrg      shift
6967a84e134Smrg      ;;
6977a84e134Smrg    $object)
6987a84e134Smrg      shift
6997a84e134Smrg      ;;
7007a84e134Smrg    *)
7017a84e134Smrg      set fnord "$@" "$arg"
7027a84e134Smrg      shift # fnord
7037a84e134Smrg      shift # $arg
7047a84e134Smrg      ;;
7057a84e134Smrg    esac
7067a84e134Smrg  done
7077a84e134Smrg
708c889a3bfSmrg  "$@" -E \
709c889a3bfSmrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
710c889a3bfSmrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711c889a3bfSmrg    | sed '$ s: \\$::' > "$tmpdepfile"
7127a84e134Smrg  rm -f "$depfile"
7137a84e134Smrg  echo "$object : \\" > "$depfile"
7147a84e134Smrg  cat < "$tmpdepfile" >> "$depfile"
7157a84e134Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
7167a84e134Smrg  rm -f "$tmpdepfile"
7177a84e134Smrg  ;;
7187a84e134Smrg
7197a84e134Smrgmsvisualcpp)
7207a84e134Smrg  # Important note: in order to support this mode, a compiler *must*
721e1e1713cSmrg  # always write the preprocessed file to stdout.
7227a84e134Smrg  "$@" || exit $?
723e1e1713cSmrg
724e1e1713cSmrg  # Remove the call to Libtool.
725e1e1713cSmrg  if test "$libtool" = yes; then
726e1e1713cSmrg    while test "X$1" != 'X--mode=compile'; do
727e1e1713cSmrg      shift
728e1e1713cSmrg    done
729e1e1713cSmrg    shift
730e1e1713cSmrg  fi
731e1e1713cSmrg
7327a84e134Smrg  IFS=" "
7337a84e134Smrg  for arg
7347a84e134Smrg  do
7357a84e134Smrg    case "$arg" in
736e1e1713cSmrg    -o)
737e1e1713cSmrg      shift
738e1e1713cSmrg      ;;
739e1e1713cSmrg    $object)
740e1e1713cSmrg      shift
741e1e1713cSmrg      ;;
7427a84e134Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
743c889a3bfSmrg        set fnord "$@"
744c889a3bfSmrg        shift
745c889a3bfSmrg        shift
746c889a3bfSmrg        ;;
7477a84e134Smrg    *)
748c889a3bfSmrg        set fnord "$@" "$arg"
749c889a3bfSmrg        shift
750c889a3bfSmrg        shift
751c889a3bfSmrg        ;;
7527a84e134Smrg    esac
7537a84e134Smrg  done
754e1e1713cSmrg  "$@" -E 2>/dev/null |
755e1e1713cSmrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
7567a84e134Smrg  rm -f "$depfile"
7577a84e134Smrg  echo "$object : \\" > "$depfile"
758c889a3bfSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
759c889a3bfSmrg  echo "$tab" >> "$depfile"
760e1e1713cSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
7617a84e134Smrg  rm -f "$tmpdepfile"
7627a84e134Smrg  ;;
7637a84e134Smrg
764e1e1713cSmrgmsvcmsys)
765e1e1713cSmrg  # This case exists only to let depend.m4 do its work.  It works by
766e1e1713cSmrg  # looking at the text of this script.  This case will never be run,
767e1e1713cSmrg  # since it is checked for above.
768e1e1713cSmrg  exit 1
769e1e1713cSmrg  ;;
770e1e1713cSmrg
7717a84e134Smrgnone)
7727a84e134Smrg  exec "$@"
7737a84e134Smrg  ;;
7747a84e134Smrg
7757a84e134Smrg*)
7767a84e134Smrg  echo "Unknown depmode $depmode" 1>&2
7777a84e134Smrg  exit 1
7787a84e134Smrg  ;;
7797a84e134Smrgesac
7807a84e134Smrg
7817a84e134Smrgexit 0
7827a84e134Smrg
7837a84e134Smrg# Local Variables:
7847a84e134Smrg# mode: shell-script
7857a84e134Smrg# sh-indentation: 2
7865ec34c4cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
7877a84e134Smrg# time-stamp-start: "scriptversion="
7887a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
7895ec34c4cSmrg# time-stamp-time-zone: "UTC0"
790e1e1713cSmrg# time-stamp-end: "; # UTC"
7917a84e134Smrg# End:
792