17da8b7e3Smrg#! /bin/sh
27da8b7e3Smrg# depcomp - compile a program generating dependencies as side-effects
37da8b7e3Smrg
4e4b60806Smrgscriptversion=2018-03-07.03; # UTC
57da8b7e3Smrg
6e4b60806Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
77da8b7e3Smrg
87da8b7e3Smrg# This program is free software; you can redistribute it and/or modify
97da8b7e3Smrg# it under the terms of the GNU General Public License as published by
107da8b7e3Smrg# the Free Software Foundation; either version 2, or (at your option)
117da8b7e3Smrg# any later version.
127da8b7e3Smrg
137da8b7e3Smrg# This program is distributed in the hope that it will be useful,
147da8b7e3Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
157da8b7e3Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167da8b7e3Smrg# GNU General Public License for more details.
177da8b7e3Smrg
187da8b7e3Smrg# You should have received a copy of the GNU General Public License
19e4b60806Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
207da8b7e3Smrg
217da8b7e3Smrg# As a special exception to the GNU General Public License, if you
227da8b7e3Smrg# distribute this file as part of a program that contains a
237da8b7e3Smrg# configuration script generated by Autoconf, you may include it under
247da8b7e3Smrg# the same distribution terms that you use for the rest of that program.
257da8b7e3Smrg
267da8b7e3Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
277da8b7e3Smrg
287da8b7e3Smrgcase $1 in
297da8b7e3Smrg  '')
30c3d5982aSmrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31c3d5982aSmrg    exit 1;
32c3d5982aSmrg    ;;
337da8b7e3Smrg  -h | --h*)
347da8b7e3Smrg    cat <<\EOF
357da8b7e3SmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
367da8b7e3Smrg
377da8b7e3SmrgRun PROGRAMS ARGS to compile a file, generating dependencies
387da8b7e3Smrgas side-effects.
397da8b7e3Smrg
407da8b7e3SmrgEnvironment variables:
417da8b7e3Smrg  depmode     Dependency tracking mode.
42c3d5982aSmrg  source      Source file read by 'PROGRAMS ARGS'.
43c3d5982aSmrg  object      Object file output by 'PROGRAMS ARGS'.
447da8b7e3Smrg  DEPDIR      directory where to store dependencies.
457da8b7e3Smrg  depfile     Dependency file to output.
46c3d5982aSmrg  tmpdepfile  Temporary file to use when outputting dependencies.
477da8b7e3Smrg  libtool     Whether libtool is used (yes/no).
487da8b7e3Smrg
497da8b7e3SmrgReport bugs to <bug-automake@gnu.org>.
507da8b7e3SmrgEOF
517da8b7e3Smrg    exit $?
527da8b7e3Smrg    ;;
537da8b7e3Smrg  -v | --v*)
547da8b7e3Smrg    echo "depcomp $scriptversion"
557da8b7e3Smrg    exit $?
567da8b7e3Smrg    ;;
577da8b7e3Smrgesac
587da8b7e3Smrg
59c3d5982aSmrg# Get the directory component of the given path, and save it in the
60c3d5982aSmrg# global variables '$dir'.  Note that this directory component will
61c3d5982aSmrg# be either empty or ending with a '/' character.  This is deliberate.
62c3d5982aSmrgset_dir_from ()
63c3d5982aSmrg{
64c3d5982aSmrg  case $1 in
65c3d5982aSmrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
66c3d5982aSmrg      *) dir=;;
67c3d5982aSmrg  esac
68c3d5982aSmrg}
69c3d5982aSmrg
70c3d5982aSmrg# Get the suffix-stripped basename of the given path, and save it the
71c3d5982aSmrg# global variable '$base'.
72c3d5982aSmrgset_base_from ()
73c3d5982aSmrg{
74c3d5982aSmrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
75c3d5982aSmrg}
76c3d5982aSmrg
77c3d5982aSmrg# If no dependency file was actually created by the compiler invocation,
78c3d5982aSmrg# we still have to create a dummy depfile, to avoid errors with the
79c3d5982aSmrg# Makefile "include basename.Plo" scheme.
80c3d5982aSmrgmake_dummy_depfile ()
81c3d5982aSmrg{
82c3d5982aSmrg  echo "#dummy" > "$depfile"
83c3d5982aSmrg}
84c3d5982aSmrg
85c3d5982aSmrg# Factor out some common post-processing of the generated depfile.
86c3d5982aSmrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
87c3d5982aSmrgaix_post_process_depfile ()
88c3d5982aSmrg{
89c3d5982aSmrg  # If the compiler actually managed to produce a dependency file,
90c3d5982aSmrg  # post-process it.
91c3d5982aSmrg  if test -f "$tmpdepfile"; then
92c3d5982aSmrg    # Each line is of the form 'foo.o: dependency.h'.
93c3d5982aSmrg    # Do two passes, one to just change these to
94c3d5982aSmrg    #   $object: dependency.h
95c3d5982aSmrg    # and one to simply output
96c3d5982aSmrg    #   dependency.h:
97c3d5982aSmrg    # which is needed to avoid the deleted-header problem.
98c3d5982aSmrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
99c3d5982aSmrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
100c3d5982aSmrg    } > "$depfile"
101c3d5982aSmrg    rm -f "$tmpdepfile"
102c3d5982aSmrg  else
103c3d5982aSmrg    make_dummy_depfile
104c3d5982aSmrg  fi
105c3d5982aSmrg}
106c3d5982aSmrg
107c3d5982aSmrg# A tabulation character.
108c3d5982aSmrgtab='	'
109c3d5982aSmrg# A newline character.
110c3d5982aSmrgnl='
111c3d5982aSmrg'
112c3d5982aSmrg# Character ranges might be problematic outside the C locale.
113c3d5982aSmrg# These definitions help.
114c3d5982aSmrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
115c3d5982aSmrglower=abcdefghijklmnopqrstuvwxyz
116c3d5982aSmrgdigits=0123456789
117c3d5982aSmrgalpha=${upper}${lower}
118c3d5982aSmrg
1197da8b7e3Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
1207da8b7e3Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
1217da8b7e3Smrg  exit 1
1227da8b7e3Smrgfi
1237da8b7e3Smrg
1247da8b7e3Smrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
1257da8b7e3Smrgdepfile=${depfile-`echo "$object" |
1267da8b7e3Smrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
1277da8b7e3Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
1287da8b7e3Smrg
1297da8b7e3Smrgrm -f "$tmpdepfile"
1307da8b7e3Smrg
131c3d5982aSmrg# Avoid interferences from the environment.
132c3d5982aSmrggccflag= dashmflag=
133c3d5982aSmrg
1347da8b7e3Smrg# Some modes work just like other modes, but use different flags.  We
1357da8b7e3Smrg# parameterize here, but still list the modes in the big case below,
1367da8b7e3Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
1377da8b7e3Smrg# here, because this file can only contain one case statement.
1387da8b7e3Smrgif test "$depmode" = hp; then
1397da8b7e3Smrg  # HP compiler uses -M and no extra arg.
1407da8b7e3Smrg  gccflag=-M
1417da8b7e3Smrg  depmode=gcc
1427da8b7e3Smrgfi
1437da8b7e3Smrg
1447da8b7e3Smrgif test "$depmode" = dashXmstdout; then
145c3d5982aSmrg  # This is just like dashmstdout with a different argument.
146c3d5982aSmrg  dashmflag=-xM
147c3d5982aSmrg  depmode=dashmstdout
1487da8b7e3Smrgfi
1497da8b7e3Smrg
150123e2cc7Smrgcygpath_u="cygpath -u -f -"
151123e2cc7Smrgif test "$depmode" = msvcmsys; then
152c3d5982aSmrg  # This is just like msvisualcpp but w/o cygpath translation.
153c3d5982aSmrg  # Just convert the backslash-escaped backslashes to single forward
154c3d5982aSmrg  # slashes to satisfy depend.m4
155c3d5982aSmrg  cygpath_u='sed s,\\\\,/,g'
156c3d5982aSmrg  depmode=msvisualcpp
157c3d5982aSmrgfi
158c3d5982aSmrg
159c3d5982aSmrgif test "$depmode" = msvc7msys; then
160c3d5982aSmrg  # This is just like msvc7 but w/o cygpath translation.
161c3d5982aSmrg  # Just convert the backslash-escaped backslashes to single forward
162c3d5982aSmrg  # slashes to satisfy depend.m4
163c3d5982aSmrg  cygpath_u='sed s,\\\\,/,g'
164c3d5982aSmrg  depmode=msvc7
165c3d5982aSmrgfi
166c3d5982aSmrg
167c3d5982aSmrgif test "$depmode" = xlc; then
168c3d5982aSmrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
169c3d5982aSmrg  gccflag=-qmakedep=gcc,-MF
170c3d5982aSmrg  depmode=gcc
171123e2cc7Smrgfi
172123e2cc7Smrg
1737da8b7e3Smrgcase "$depmode" in
1747da8b7e3Smrggcc3)
1757da8b7e3Smrg## gcc 3 implements dependency tracking that does exactly what
1767da8b7e3Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
1777da8b7e3Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
178123e2cc7Smrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
179123e2cc7Smrg## the command line argument order; so add the flags where they
180123e2cc7Smrg## appear in depend2.am.  Note that the slowdown incurred here
181123e2cc7Smrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
182123e2cc7Smrg  for arg
183123e2cc7Smrg  do
184123e2cc7Smrg    case $arg in
185123e2cc7Smrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
186123e2cc7Smrg    *)  set fnord "$@" "$arg" ;;
187123e2cc7Smrg    esac
188123e2cc7Smrg    shift # fnord
189123e2cc7Smrg    shift # $arg
190123e2cc7Smrg  done
191123e2cc7Smrg  "$@"
1927da8b7e3Smrg  stat=$?
193c3d5982aSmrg  if test $stat -ne 0; then
1947da8b7e3Smrg    rm -f "$tmpdepfile"
1957da8b7e3Smrg    exit $stat
1967da8b7e3Smrg  fi
1977da8b7e3Smrg  mv "$tmpdepfile" "$depfile"
1987da8b7e3Smrg  ;;
1997da8b7e3Smrg
2007da8b7e3Smrggcc)
201c3d5982aSmrg## Note that this doesn't just cater to obsosete pre-3.x GCC compilers.
202c3d5982aSmrg## but also to in-use compilers like IMB xlc/xlC and the HP C compiler.
203c3d5982aSmrg## (see the conditional assignment to $gccflag above).
2047da8b7e3Smrg## There are various ways to get dependency output from gcc.  Here's
2057da8b7e3Smrg## why we pick this rather obscure method:
2067da8b7e3Smrg## - Don't want to use -MD because we'd like the dependencies to end
2077da8b7e3Smrg##   up in a subdir.  Having to rename by hand is ugly.
2087da8b7e3Smrg##   (We might end up doing this anyway to support other compilers.)
2097da8b7e3Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
210c3d5982aSmrg##   -MM, not -M (despite what the docs say).  Also, it might not be
211c3d5982aSmrg##   supported by the other compilers which use the 'gcc' depmode.
2127da8b7e3Smrg## - Using -M directly means running the compiler twice (even worse
2137da8b7e3Smrg##   than renaming).
2147da8b7e3Smrg  if test -z "$gccflag"; then
2157da8b7e3Smrg    gccflag=-MD,
2167da8b7e3Smrg  fi
2177da8b7e3Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
2187da8b7e3Smrg  stat=$?
219c3d5982aSmrg  if test $stat -ne 0; then
2207da8b7e3Smrg    rm -f "$tmpdepfile"
2217da8b7e3Smrg    exit $stat
2227da8b7e3Smrg  fi
2237da8b7e3Smrg  rm -f "$depfile"
2247da8b7e3Smrg  echo "$object : \\" > "$depfile"
225c3d5982aSmrg  # The second -e expression handles DOS-style file names with drive
226c3d5982aSmrg  # letters.
2277da8b7e3Smrg  sed -e 's/^[^:]*: / /' \
2287da8b7e3Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
229c3d5982aSmrg## This next piece of magic avoids the "deleted header file" problem.
2307da8b7e3Smrg## The problem is that when a header file which appears in a .P file
2317da8b7e3Smrg## is deleted, the dependency causes make to die (because there is
2327da8b7e3Smrg## typically no way to rebuild the header).  We avoid this by adding
2337da8b7e3Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
2347da8b7e3Smrg## this for us directly.
235c3d5982aSmrg## Some versions of gcc put a space before the ':'.  On the theory
2367da8b7e3Smrg## that the space means something, we add a space to the output as
237c3d5982aSmrg## well.  hp depmode also adds that space, but also prefixes the VPATH
238c3d5982aSmrg## to the object.  Take care to not repeat it in the output.
2397da8b7e3Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
2407da8b7e3Smrg## correctly.  Breaking it into two sed invocations is a workaround.
241c3d5982aSmrg  tr ' ' "$nl" < "$tmpdepfile" \
242c3d5982aSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
243c3d5982aSmrg    | sed -e 's/$/ :/' >> "$depfile"
2447da8b7e3Smrg  rm -f "$tmpdepfile"
2457da8b7e3Smrg  ;;
2467da8b7e3Smrg
2477da8b7e3Smrghp)
2487da8b7e3Smrg  # This case exists only to let depend.m4 do its work.  It works by
2497da8b7e3Smrg  # looking at the text of this script.  This case will never be run,
2507da8b7e3Smrg  # since it is checked for above.
2517da8b7e3Smrg  exit 1
2527da8b7e3Smrg  ;;
2537da8b7e3Smrg
2547da8b7e3Smrgsgi)
2557da8b7e3Smrg  if test "$libtool" = yes; then
2567da8b7e3Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
2577da8b7e3Smrg  else
2587da8b7e3Smrg    "$@" -MDupdate "$tmpdepfile"
2597da8b7e3Smrg  fi
2607da8b7e3Smrg  stat=$?
261c3d5982aSmrg  if test $stat -ne 0; then
2627da8b7e3Smrg    rm -f "$tmpdepfile"
2637da8b7e3Smrg    exit $stat
2647da8b7e3Smrg  fi
2657da8b7e3Smrg  rm -f "$depfile"
2667da8b7e3Smrg
2677da8b7e3Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
2687da8b7e3Smrg    echo "$object : \\" > "$depfile"
2697da8b7e3Smrg    # Clip off the initial element (the dependent).  Don't try to be
2707da8b7e3Smrg    # clever and replace this with sed code, as IRIX sed won't handle
2717da8b7e3Smrg    # lines with more than a fixed number of characters (4096 in
2727da8b7e3Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
273c3d5982aSmrg    # the IRIX cc adds comments like '#:fec' to the end of the
2747da8b7e3Smrg    # dependency line.
275c3d5982aSmrg    tr ' ' "$nl" < "$tmpdepfile" \
276c3d5982aSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
277c3d5982aSmrg      | tr "$nl" ' ' >> "$depfile"
278123e2cc7Smrg    echo >> "$depfile"
2797da8b7e3Smrg    # The second pass generates a dummy entry for each header file.
280c3d5982aSmrg    tr ' ' "$nl" < "$tmpdepfile" \
281c3d5982aSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
282c3d5982aSmrg      >> "$depfile"
2837da8b7e3Smrg  else
284c3d5982aSmrg    make_dummy_depfile
2857da8b7e3Smrg  fi
2867da8b7e3Smrg  rm -f "$tmpdepfile"
2877da8b7e3Smrg  ;;
2887da8b7e3Smrg
289c3d5982aSmrgxlc)
290c3d5982aSmrg  # This case exists only to let depend.m4 do its work.  It works by
291c3d5982aSmrg  # looking at the text of this script.  This case will never be run,
292c3d5982aSmrg  # since it is checked for above.
293c3d5982aSmrg  exit 1
294c3d5982aSmrg  ;;
295c3d5982aSmrg
2967da8b7e3Smrgaix)
2977da8b7e3Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
2987da8b7e3Smrg  # in a .u file.  In older versions, this file always lives in the
299c3d5982aSmrg  # current directory.  Also, the AIX compiler puts '$object:' at the
3007da8b7e3Smrg  # start of each line; $object doesn't have directory information.
3017da8b7e3Smrg  # Version 6 uses the directory in both cases.
302c3d5982aSmrg  set_dir_from "$object"
303c3d5982aSmrg  set_base_from "$object"
3047da8b7e3Smrg  if test "$libtool" = yes; then
305123e2cc7Smrg    tmpdepfile1=$dir$base.u
306123e2cc7Smrg    tmpdepfile2=$base.u
307123e2cc7Smrg    tmpdepfile3=$dir.libs/$base.u
3087da8b7e3Smrg    "$@" -Wc,-M
3097da8b7e3Smrg  else
310123e2cc7Smrg    tmpdepfile1=$dir$base.u
311123e2cc7Smrg    tmpdepfile2=$dir$base.u
312123e2cc7Smrg    tmpdepfile3=$dir$base.u
3137da8b7e3Smrg    "$@" -M
3147da8b7e3Smrg  fi
3157da8b7e3Smrg  stat=$?
316c3d5982aSmrg  if test $stat -ne 0; then
317123e2cc7Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
3187da8b7e3Smrg    exit $stat
3197da8b7e3Smrg  fi
3207da8b7e3Smrg
321123e2cc7Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
322123e2cc7Smrg  do
323123e2cc7Smrg    test -f "$tmpdepfile" && break
324123e2cc7Smrg  done
325c3d5982aSmrg  aix_post_process_depfile
326c3d5982aSmrg  ;;
327c3d5982aSmrg
328c3d5982aSmrgtcc)
329c3d5982aSmrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
330c3d5982aSmrg  # FIXME: That version still under development at the moment of writing.
331c3d5982aSmrg  #        Make that this statement remains true also for stable, released
332c3d5982aSmrg  #        versions.
333c3d5982aSmrg  # It will wrap lines (doesn't matter whether long or short) with a
334c3d5982aSmrg  # trailing '\', as in:
335c3d5982aSmrg  #
336c3d5982aSmrg  #   foo.o : \
337c3d5982aSmrg  #    foo.c \
338c3d5982aSmrg  #    foo.h \
339c3d5982aSmrg  #
340c3d5982aSmrg  # It will put a trailing '\' even on the last line, and will use leading
341c3d5982aSmrg  # spaces rather than leading tabs (at least since its commit 0394caf7
342c3d5982aSmrg  # "Emit spaces for -MD").
343c3d5982aSmrg  "$@" -MD -MF "$tmpdepfile"
344c3d5982aSmrg  stat=$?
345c3d5982aSmrg  if test $stat -ne 0; then
346c3d5982aSmrg    rm -f "$tmpdepfile"
347c3d5982aSmrg    exit $stat
3487da8b7e3Smrg  fi
349c3d5982aSmrg  rm -f "$depfile"
350c3d5982aSmrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
351c3d5982aSmrg  # We have to change lines of the first kind to '$object: \'.
352c3d5982aSmrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
353c3d5982aSmrg  # And for each line of the second kind, we have to emit a 'dep.h:'
354c3d5982aSmrg  # dummy dependency, to avoid the deleted-header problem.
355c3d5982aSmrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
3567da8b7e3Smrg  rm -f "$tmpdepfile"
3577da8b7e3Smrg  ;;
3587da8b7e3Smrg
359c3d5982aSmrg## The order of this option in the case statement is important, since the
360c3d5982aSmrg## shell code in configure will try each of these formats in the order
361c3d5982aSmrg## listed in this file.  A plain '-MD' option would be understood by many
362c3d5982aSmrg## compilers, so we must ensure this comes after the gcc and icc options.
363c3d5982aSmrgpgcc)
364c3d5982aSmrg  # Portland's C compiler understands '-MD'.
365c3d5982aSmrg  # Will always output deps to 'file.d' where file is the root name of the
366c3d5982aSmrg  # source file under compilation, even if file resides in a subdirectory.
367c3d5982aSmrg  # The object file name does not affect the name of the '.d' file.
368c3d5982aSmrg  # pgcc 10.2 will output
3697da8b7e3Smrg  #    foo.o: sub/foo.c sub/foo.h
370c3d5982aSmrg  # and will wrap long lines using '\' :
3717da8b7e3Smrg  #    foo.o: sub/foo.c ... \
3727da8b7e3Smrg  #     sub/foo.h ... \
3737da8b7e3Smrg  #     ...
374c3d5982aSmrg  set_dir_from "$object"
375c3d5982aSmrg  # Use the source, not the object, to determine the base name, since
376c3d5982aSmrg  # that's sadly what pgcc will do too.
377c3d5982aSmrg  set_base_from "$source"
378c3d5982aSmrg  tmpdepfile=$base.d
379c3d5982aSmrg
380c3d5982aSmrg  # For projects that build the same source file twice into different object
381c3d5982aSmrg  # files, the pgcc approach of using the *source* file root name can cause
382c3d5982aSmrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
383c3d5982aSmrg  # the same $tmpdepfile.
384c3d5982aSmrg  lockdir=$base.d-lock
385c3d5982aSmrg  trap "
386c3d5982aSmrg    echo '$0: caught signal, cleaning up...' >&2
387c3d5982aSmrg    rmdir '$lockdir'
388c3d5982aSmrg    exit 1
389c3d5982aSmrg  " 1 2 13 15
390c3d5982aSmrg  numtries=100
391c3d5982aSmrg  i=$numtries
392c3d5982aSmrg  while test $i -gt 0; do
393c3d5982aSmrg    # mkdir is a portable test-and-set.
394c3d5982aSmrg    if mkdir "$lockdir" 2>/dev/null; then
395c3d5982aSmrg      # This process acquired the lock.
396c3d5982aSmrg      "$@" -MD
397c3d5982aSmrg      stat=$?
398c3d5982aSmrg      # Release the lock.
399c3d5982aSmrg      rmdir "$lockdir"
400c3d5982aSmrg      break
401c3d5982aSmrg    else
402c3d5982aSmrg      # If the lock is being held by a different process, wait
403c3d5982aSmrg      # until the winning process is done or we timeout.
404c3d5982aSmrg      while test -d "$lockdir" && test $i -gt 0; do
405c3d5982aSmrg        sleep 1
406c3d5982aSmrg        i=`expr $i - 1`
407c3d5982aSmrg      done
408c3d5982aSmrg    fi
409c3d5982aSmrg    i=`expr $i - 1`
410c3d5982aSmrg  done
411c3d5982aSmrg  trap - 1 2 13 15
412c3d5982aSmrg  if test $i -le 0; then
413c3d5982aSmrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
414c3d5982aSmrg    echo "$0: check lockdir '$lockdir'" >&2
415c3d5982aSmrg    exit 1
416c3d5982aSmrg  fi
4177da8b7e3Smrg
418c3d5982aSmrg  if test $stat -ne 0; then
4197da8b7e3Smrg    rm -f "$tmpdepfile"
4207da8b7e3Smrg    exit $stat
4217da8b7e3Smrg  fi
4227da8b7e3Smrg  rm -f "$depfile"
4237da8b7e3Smrg  # Each line is of the form `foo.o: dependent.h',
4247da8b7e3Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
4257da8b7e3Smrg  # Do two passes, one to just change these to
4267da8b7e3Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
4277da8b7e3Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
4287da8b7e3Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
4297da8b7e3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
430c3d5982aSmrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
431c3d5982aSmrg    | sed -e 's/$/ :/' >> "$depfile"
4327da8b7e3Smrg  rm -f "$tmpdepfile"
4337da8b7e3Smrg  ;;
4347da8b7e3Smrg
435123e2cc7Smrghp2)
436123e2cc7Smrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
437123e2cc7Smrg  # compilers, which have integrated preprocessors.  The correct option
438123e2cc7Smrg  # to use with these is +Maked; it writes dependencies to a file named
439123e2cc7Smrg  # 'foo.d', which lands next to the object file, wherever that
440123e2cc7Smrg  # happens to be.
441123e2cc7Smrg  # Much of this is similar to the tru64 case; see comments there.
442c3d5982aSmrg  set_dir_from  "$object"
443c3d5982aSmrg  set_base_from "$object"
444123e2cc7Smrg  if test "$libtool" = yes; then
445123e2cc7Smrg    tmpdepfile1=$dir$base.d
446123e2cc7Smrg    tmpdepfile2=$dir.libs/$base.d
447123e2cc7Smrg    "$@" -Wc,+Maked
448123e2cc7Smrg  else
449123e2cc7Smrg    tmpdepfile1=$dir$base.d
450123e2cc7Smrg    tmpdepfile2=$dir$base.d
451123e2cc7Smrg    "$@" +Maked
452123e2cc7Smrg  fi
453123e2cc7Smrg  stat=$?
454c3d5982aSmrg  if test $stat -ne 0; then
455123e2cc7Smrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
456123e2cc7Smrg     exit $stat
457123e2cc7Smrg  fi
458123e2cc7Smrg
459123e2cc7Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
460123e2cc7Smrg  do
461123e2cc7Smrg    test -f "$tmpdepfile" && break
462123e2cc7Smrg  done
463123e2cc7Smrg  if test -f "$tmpdepfile"; then
464c3d5982aSmrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
465c3d5982aSmrg    # Add 'dependent.h:' lines.
466123e2cc7Smrg    sed -ne '2,${
467c3d5982aSmrg               s/^ *//
468c3d5982aSmrg               s/ \\*$//
469c3d5982aSmrg               s/$/:/
470c3d5982aSmrg               p
471c3d5982aSmrg             }' "$tmpdepfile" >> "$depfile"
472123e2cc7Smrg  else
473c3d5982aSmrg    make_dummy_depfile
474123e2cc7Smrg  fi
475123e2cc7Smrg  rm -f "$tmpdepfile" "$tmpdepfile2"
476123e2cc7Smrg  ;;
477123e2cc7Smrg
4787da8b7e3Smrgtru64)
479c3d5982aSmrg  # The Tru64 compiler uses -MD to generate dependencies as a side
480c3d5982aSmrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
481c3d5982aSmrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
482c3d5982aSmrg  # dependencies in 'foo.d' instead, so we check for that too.
483c3d5982aSmrg  # Subdirectories are respected.
484c3d5982aSmrg  set_dir_from  "$object"
485c3d5982aSmrg  set_base_from "$object"
486c3d5982aSmrg
487c3d5982aSmrg  if test "$libtool" = yes; then
488c3d5982aSmrg    # Libtool generates 2 separate objects for the 2 libraries.  These
489c3d5982aSmrg    # two compilations output dependencies in $dir.libs/$base.o.d and
490c3d5982aSmrg    # in $dir$base.o.d.  We have to check for both files, because
491c3d5982aSmrg    # one of the two compilations can be disabled.  We should prefer
492c3d5982aSmrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
493c3d5982aSmrg    # automatically cleaned when .libs/ is deleted, while ignoring
494c3d5982aSmrg    # the former would cause a distcleancheck panic.
495c3d5982aSmrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
496c3d5982aSmrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
497c3d5982aSmrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
498c3d5982aSmrg    "$@" -Wc,-MD
499c3d5982aSmrg  else
500c3d5982aSmrg    tmpdepfile1=$dir$base.d
501c3d5982aSmrg    tmpdepfile2=$dir$base.d
502c3d5982aSmrg    tmpdepfile3=$dir$base.d
503c3d5982aSmrg    "$@" -MD
504c3d5982aSmrg  fi
505c3d5982aSmrg
506c3d5982aSmrg  stat=$?
507c3d5982aSmrg  if test $stat -ne 0; then
508c3d5982aSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
509c3d5982aSmrg    exit $stat
510c3d5982aSmrg  fi
511c3d5982aSmrg
512c3d5982aSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
513c3d5982aSmrg  do
514c3d5982aSmrg    test -f "$tmpdepfile" && break
515c3d5982aSmrg  done
516c3d5982aSmrg  # Same post-processing that is required for AIX mode.
517c3d5982aSmrg  aix_post_process_depfile
518c3d5982aSmrg  ;;
519c3d5982aSmrg
520c3d5982aSmrgmsvc7)
521c3d5982aSmrg  if test "$libtool" = yes; then
522c3d5982aSmrg    showIncludes=-Wc,-showIncludes
523c3d5982aSmrg  else
524c3d5982aSmrg    showIncludes=-showIncludes
525c3d5982aSmrg  fi
526c3d5982aSmrg  "$@" $showIncludes > "$tmpdepfile"
527c3d5982aSmrg  stat=$?
528c3d5982aSmrg  grep -v '^Note: including file: ' "$tmpdepfile"
529c3d5982aSmrg  if test $stat -ne 0; then
530c3d5982aSmrg    rm -f "$tmpdepfile"
531c3d5982aSmrg    exit $stat
532c3d5982aSmrg  fi
533c3d5982aSmrg  rm -f "$depfile"
534c3d5982aSmrg  echo "$object : \\" > "$depfile"
535c3d5982aSmrg  # The first sed program below extracts the file names and escapes
536c3d5982aSmrg  # backslashes for cygpath.  The second sed program outputs the file
537c3d5982aSmrg  # name when reading, but also accumulates all include files in the
538c3d5982aSmrg  # hold buffer in order to output them again at the end.  This only
539c3d5982aSmrg  # works with sed implementations that can handle large buffers.
540c3d5982aSmrg  sed < "$tmpdepfile" -n '
541c3d5982aSmrg/^Note: including file:  *\(.*\)/ {
542c3d5982aSmrg  s//\1/
543c3d5982aSmrg  s/\\/\\\\/g
544c3d5982aSmrg  p
545c3d5982aSmrg}' | $cygpath_u | sort -u | sed -n '
546c3d5982aSmrgs/ /\\ /g
547c3d5982aSmrgs/\(.*\)/'"$tab"'\1 \\/p
548c3d5982aSmrgs/.\(.*\) \\/\1:/
549c3d5982aSmrgH
550c3d5982aSmrg$ {
551c3d5982aSmrg  s/.*/'"$tab"'/
552c3d5982aSmrg  G
553c3d5982aSmrg  p
554c3d5982aSmrg}' >> "$depfile"
555ff143803Smrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
556c3d5982aSmrg  rm -f "$tmpdepfile"
557c3d5982aSmrg  ;;
558c3d5982aSmrg
559c3d5982aSmrgmsvc7msys)
560c3d5982aSmrg  # This case exists only to let depend.m4 do its work.  It works by
561c3d5982aSmrg  # looking at the text of this script.  This case will never be run,
562c3d5982aSmrg  # since it is checked for above.
563c3d5982aSmrg  exit 1
564c3d5982aSmrg  ;;
5657da8b7e3Smrg
5667da8b7e3Smrg#nosideeffect)
5677da8b7e3Smrg  # This comment above is used by automake to tell side-effect
5687da8b7e3Smrg  # dependency tracking mechanisms from slower ones.
5697da8b7e3Smrg
5707da8b7e3Smrgdashmstdout)
5717da8b7e3Smrg  # Important note: in order to support this mode, a compiler *must*
5727da8b7e3Smrg  # always write the preprocessed file to stdout, regardless of -o.
5737da8b7e3Smrg  "$@" || exit $?
5747da8b7e3Smrg
5757da8b7e3Smrg  # Remove the call to Libtool.
5767da8b7e3Smrg  if test "$libtool" = yes; then
577123e2cc7Smrg    while test "X$1" != 'X--mode=compile'; do
5787da8b7e3Smrg      shift
5797da8b7e3Smrg    done
5807da8b7e3Smrg    shift
5817da8b7e3Smrg  fi
5827da8b7e3Smrg
583c3d5982aSmrg  # Remove '-o $object'.
5847da8b7e3Smrg  IFS=" "
5857da8b7e3Smrg  for arg
5867da8b7e3Smrg  do
5877da8b7e3Smrg    case $arg in
5887da8b7e3Smrg    -o)
5897da8b7e3Smrg      shift
5907da8b7e3Smrg      ;;
5917da8b7e3Smrg    $object)
5927da8b7e3Smrg      shift
5937da8b7e3Smrg      ;;
5947da8b7e3Smrg    *)
5957da8b7e3Smrg      set fnord "$@" "$arg"
5967da8b7e3Smrg      shift # fnord
5977da8b7e3Smrg      shift # $arg
5987da8b7e3Smrg      ;;
5997da8b7e3Smrg    esac
6007da8b7e3Smrg  done
6017da8b7e3Smrg
6027da8b7e3Smrg  test -z "$dashmflag" && dashmflag=-M
603c3d5982aSmrg  # Require at least two characters before searching for ':'
6047da8b7e3Smrg  # in the target name.  This is to cope with DOS-style filenames:
605c3d5982aSmrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
6067da8b7e3Smrg  "$@" $dashmflag |
607c3d5982aSmrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
6087da8b7e3Smrg  rm -f "$depfile"
6097da8b7e3Smrg  cat < "$tmpdepfile" > "$depfile"
610c3d5982aSmrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
611c3d5982aSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
612c3d5982aSmrg  tr ' ' "$nl" < "$tmpdepfile" \
613c3d5982aSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
614c3d5982aSmrg    | sed -e 's/$/ :/' >> "$depfile"
6157da8b7e3Smrg  rm -f "$tmpdepfile"
6167da8b7e3Smrg  ;;
6177da8b7e3Smrg
6187da8b7e3SmrgdashXmstdout)
6197da8b7e3Smrg  # This case only exists to satisfy depend.m4.  It is never actually
6207da8b7e3Smrg  # run, as this mode is specially recognized in the preamble.
6217da8b7e3Smrg  exit 1
6227da8b7e3Smrg  ;;
6237da8b7e3Smrg
6247da8b7e3Smrgmakedepend)
6257da8b7e3Smrg  "$@" || exit $?
6267da8b7e3Smrg  # Remove any Libtool call
6277da8b7e3Smrg  if test "$libtool" = yes; then
628123e2cc7Smrg    while test "X$1" != 'X--mode=compile'; do
6297da8b7e3Smrg      shift
6307da8b7e3Smrg    done
6317da8b7e3Smrg    shift
6327da8b7e3Smrg  fi
6337da8b7e3Smrg  # X makedepend
6347da8b7e3Smrg  shift
635123e2cc7Smrg  cleared=no eat=no
636123e2cc7Smrg  for arg
637123e2cc7Smrg  do
6387da8b7e3Smrg    case $cleared in
6397da8b7e3Smrg    no)
6407da8b7e3Smrg      set ""; shift
6417da8b7e3Smrg      cleared=yes ;;
6427da8b7e3Smrg    esac
643123e2cc7Smrg    if test $eat = yes; then
644123e2cc7Smrg      eat=no
645123e2cc7Smrg      continue
646123e2cc7Smrg    fi
6477da8b7e3Smrg    case "$arg" in
6487da8b7e3Smrg    -D*|-I*)
6497da8b7e3Smrg      set fnord "$@" "$arg"; shift ;;
6507da8b7e3Smrg    # Strip any option that makedepend may not understand.  Remove
6517da8b7e3Smrg    # the object too, otherwise makedepend will parse it as a source file.
652123e2cc7Smrg    -arch)
653123e2cc7Smrg      eat=yes ;;
6547da8b7e3Smrg    -*|$object)
6557da8b7e3Smrg      ;;
6567da8b7e3Smrg    *)
6577da8b7e3Smrg      set fnord "$@" "$arg"; shift ;;
6587da8b7e3Smrg    esac
6597da8b7e3Smrg  done
660123e2cc7Smrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
6617da8b7e3Smrg  touch "$tmpdepfile"
6627da8b7e3Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
6637da8b7e3Smrg  rm -f "$depfile"
664c3d5982aSmrg  # makedepend may prepend the VPATH from the source file name to the object.
665c3d5982aSmrg  # No need to regex-escape $object, excess matching of '.' is harmless.
666c3d5982aSmrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
667c3d5982aSmrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
668c3d5982aSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
669c3d5982aSmrg  sed '1,2d' "$tmpdepfile" \
670c3d5982aSmrg    | tr ' ' "$nl" \
671c3d5982aSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
672c3d5982aSmrg    | sed -e 's/$/ :/' >> "$depfile"
6737da8b7e3Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
6747da8b7e3Smrg  ;;
6757da8b7e3Smrg
6767da8b7e3Smrgcpp)
6777da8b7e3Smrg  # Important note: in order to support this mode, a compiler *must*
6787da8b7e3Smrg  # always write the preprocessed file to stdout.
6797da8b7e3Smrg  "$@" || exit $?
6807da8b7e3Smrg
6817da8b7e3Smrg  # Remove the call to Libtool.
6827da8b7e3Smrg  if test "$libtool" = yes; then
683123e2cc7Smrg    while test "X$1" != 'X--mode=compile'; do
6847da8b7e3Smrg      shift
6857da8b7e3Smrg    done
6867da8b7e3Smrg    shift
6877da8b7e3Smrg  fi
6887da8b7e3Smrg
689c3d5982aSmrg  # Remove '-o $object'.
6907da8b7e3Smrg  IFS=" "
6917da8b7e3Smrg  for arg
6927da8b7e3Smrg  do
6937da8b7e3Smrg    case $arg in
6947da8b7e3Smrg    -o)
6957da8b7e3Smrg      shift
6967da8b7e3Smrg      ;;
6977da8b7e3Smrg    $object)
6987da8b7e3Smrg      shift
6997da8b7e3Smrg      ;;
7007da8b7e3Smrg    *)
7017da8b7e3Smrg      set fnord "$@" "$arg"
7027da8b7e3Smrg      shift # fnord
7037da8b7e3Smrg      shift # $arg
7047da8b7e3Smrg      ;;
7057da8b7e3Smrg    esac
7067da8b7e3Smrg  done
7077da8b7e3Smrg
708c3d5982aSmrg  "$@" -E \
709c3d5982aSmrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
710c3d5982aSmrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711c3d5982aSmrg    | sed '$ s: \\$::' > "$tmpdepfile"
7127da8b7e3Smrg  rm -f "$depfile"
7137da8b7e3Smrg  echo "$object : \\" > "$depfile"
7147da8b7e3Smrg  cat < "$tmpdepfile" >> "$depfile"
7157da8b7e3Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
7167da8b7e3Smrg  rm -f "$tmpdepfile"
7177da8b7e3Smrg  ;;
7187da8b7e3Smrg
7197da8b7e3Smrgmsvisualcpp)
7207da8b7e3Smrg  # Important note: in order to support this mode, a compiler *must*
721123e2cc7Smrg  # always write the preprocessed file to stdout.
7227da8b7e3Smrg  "$@" || exit $?
723123e2cc7Smrg
724123e2cc7Smrg  # Remove the call to Libtool.
725123e2cc7Smrg  if test "$libtool" = yes; then
726123e2cc7Smrg    while test "X$1" != 'X--mode=compile'; do
727123e2cc7Smrg      shift
728123e2cc7Smrg    done
729123e2cc7Smrg    shift
730123e2cc7Smrg  fi
731123e2cc7Smrg
7327da8b7e3Smrg  IFS=" "
7337da8b7e3Smrg  for arg
7347da8b7e3Smrg  do
7357da8b7e3Smrg    case "$arg" in
736123e2cc7Smrg    -o)
737123e2cc7Smrg      shift
738123e2cc7Smrg      ;;
739123e2cc7Smrg    $object)
740123e2cc7Smrg      shift
741123e2cc7Smrg      ;;
7427da8b7e3Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
743c3d5982aSmrg        set fnord "$@"
744c3d5982aSmrg        shift
745c3d5982aSmrg        shift
746c3d5982aSmrg        ;;
7477da8b7e3Smrg    *)
748c3d5982aSmrg        set fnord "$@" "$arg"
749c3d5982aSmrg        shift
750c3d5982aSmrg        shift
751c3d5982aSmrg        ;;
7527da8b7e3Smrg    esac
7537da8b7e3Smrg  done
754123e2cc7Smrg  "$@" -E 2>/dev/null |
755123e2cc7Smrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
7567da8b7e3Smrg  rm -f "$depfile"
7577da8b7e3Smrg  echo "$object : \\" > "$depfile"
758c3d5982aSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
759c3d5982aSmrg  echo "$tab" >> "$depfile"
760123e2cc7Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
7617da8b7e3Smrg  rm -f "$tmpdepfile"
7627da8b7e3Smrg  ;;
7637da8b7e3Smrg
764123e2cc7Smrgmsvcmsys)
765123e2cc7Smrg  # This case exists only to let depend.m4 do its work.  It works by
766123e2cc7Smrg  # looking at the text of this script.  This case will never be run,
767123e2cc7Smrg  # since it is checked for above.
768123e2cc7Smrg  exit 1
769123e2cc7Smrg  ;;
770123e2cc7Smrg
7717da8b7e3Smrgnone)
7727da8b7e3Smrg  exec "$@"
7737da8b7e3Smrg  ;;
7747da8b7e3Smrg
7757da8b7e3Smrg*)
7767da8b7e3Smrg  echo "Unknown depmode $depmode" 1>&2
7777da8b7e3Smrg  exit 1
7787da8b7e3Smrg  ;;
7797da8b7e3Smrgesac
7807da8b7e3Smrg
7817da8b7e3Smrgexit 0
7827da8b7e3Smrg
7837da8b7e3Smrg# Local Variables:
7847da8b7e3Smrg# mode: shell-script
7857da8b7e3Smrg# sh-indentation: 2
786e4b60806Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
7877da8b7e3Smrg# time-stamp-start: "scriptversion="
7887da8b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
789e4b60806Smrg# time-stamp-time-zone: "UTC0"
790123e2cc7Smrg# time-stamp-end: "; # UTC"
7917da8b7e3Smrg# End:
792