1ea6ae205Smrg#! /bin/sh
2ea6ae205Smrg# depcomp - compile a program generating dependencies as side-effects
3ea6ae205Smrg
4764c86d1Smrgscriptversion=2018-03-07.03; # UTC
5ea6ae205Smrg
6764c86d1Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7ea6ae205Smrg
8ea6ae205Smrg# This program is free software; you can redistribute it and/or modify
9ea6ae205Smrg# it under the terms of the GNU General Public License as published by
10ea6ae205Smrg# the Free Software Foundation; either version 2, or (at your option)
11ea6ae205Smrg# any later version.
12ea6ae205Smrg
13ea6ae205Smrg# This program is distributed in the hope that it will be useful,
14ea6ae205Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15ea6ae205Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16ea6ae205Smrg# GNU General Public License for more details.
17ea6ae205Smrg
18ea6ae205Smrg# You should have received a copy of the GNU General Public License
19764c86d1Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20ea6ae205Smrg
21ea6ae205Smrg# As a special exception to the GNU General Public License, if you
22ea6ae205Smrg# distribute this file as part of a program that contains a
23ea6ae205Smrg# configuration script generated by Autoconf, you may include it under
24ea6ae205Smrg# the same distribution terms that you use for the rest of that program.
25ea6ae205Smrg
26ea6ae205Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
27ea6ae205Smrg
28ea6ae205Smrgcase $1 in
29ea6ae205Smrg  '')
3087aef7c3Smrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
3187aef7c3Smrg    exit 1;
3287aef7c3Smrg    ;;
33ea6ae205Smrg  -h | --h*)
34ea6ae205Smrg    cat <<\EOF
35ea6ae205SmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
36ea6ae205Smrg
37ea6ae205SmrgRun PROGRAMS ARGS to compile a file, generating dependencies
38ea6ae205Smrgas side-effects.
39ea6ae205Smrg
40ea6ae205SmrgEnvironment variables:
41ea6ae205Smrg  depmode     Dependency tracking mode.
4287aef7c3Smrg  source      Source file read by 'PROGRAMS ARGS'.
4387aef7c3Smrg  object      Object file output by 'PROGRAMS ARGS'.
44ea6ae205Smrg  DEPDIR      directory where to store dependencies.
45ea6ae205Smrg  depfile     Dependency file to output.
4687aef7c3Smrg  tmpdepfile  Temporary file to use when outputting dependencies.
47ea6ae205Smrg  libtool     Whether libtool is used (yes/no).
48ea6ae205Smrg
49ea6ae205SmrgReport bugs to <bug-automake@gnu.org>.
50ea6ae205SmrgEOF
51ea6ae205Smrg    exit $?
52ea6ae205Smrg    ;;
53ea6ae205Smrg  -v | --v*)
54ea6ae205Smrg    echo "depcomp $scriptversion"
55ea6ae205Smrg    exit $?
56ea6ae205Smrg    ;;
57ea6ae205Smrgesac
58ea6ae205Smrg
5987aef7c3Smrg# Get the directory component of the given path, and save it in the
6087aef7c3Smrg# global variables '$dir'.  Note that this directory component will
6187aef7c3Smrg# be either empty or ending with a '/' character.  This is deliberate.
6287aef7c3Smrgset_dir_from ()
6387aef7c3Smrg{
6487aef7c3Smrg  case $1 in
6587aef7c3Smrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
6687aef7c3Smrg      *) dir=;;
6787aef7c3Smrg  esac
6887aef7c3Smrg}
6987aef7c3Smrg
7087aef7c3Smrg# Get the suffix-stripped basename of the given path, and save it the
7187aef7c3Smrg# global variable '$base'.
7287aef7c3Smrgset_base_from ()
7387aef7c3Smrg{
7487aef7c3Smrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
7587aef7c3Smrg}
7687aef7c3Smrg
7787aef7c3Smrg# If no dependency file was actually created by the compiler invocation,
7887aef7c3Smrg# we still have to create a dummy depfile, to avoid errors with the
7987aef7c3Smrg# Makefile "include basename.Plo" scheme.
8087aef7c3Smrgmake_dummy_depfile ()
8187aef7c3Smrg{
8287aef7c3Smrg  echo "#dummy" > "$depfile"
8387aef7c3Smrg}
8487aef7c3Smrg
8587aef7c3Smrg# Factor out some common post-processing of the generated depfile.
8687aef7c3Smrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
8787aef7c3Smrgaix_post_process_depfile ()
8887aef7c3Smrg{
8987aef7c3Smrg  # If the compiler actually managed to produce a dependency file,
9087aef7c3Smrg  # post-process it.
9187aef7c3Smrg  if test -f "$tmpdepfile"; then
9287aef7c3Smrg    # Each line is of the form 'foo.o: dependency.h'.
9387aef7c3Smrg    # Do two passes, one to just change these to
9487aef7c3Smrg    #   $object: dependency.h
9587aef7c3Smrg    # and one to simply output
9687aef7c3Smrg    #   dependency.h:
9787aef7c3Smrg    # which is needed to avoid the deleted-header problem.
9887aef7c3Smrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
9987aef7c3Smrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
10087aef7c3Smrg    } > "$depfile"
10187aef7c3Smrg    rm -f "$tmpdepfile"
10287aef7c3Smrg  else
10387aef7c3Smrg    make_dummy_depfile
10487aef7c3Smrg  fi
10587aef7c3Smrg}
10687aef7c3Smrg
10787aef7c3Smrg# A tabulation character.
10887aef7c3Smrgtab='	'
10987aef7c3Smrg# A newline character.
11087aef7c3Smrgnl='
11187aef7c3Smrg'
11287aef7c3Smrg# Character ranges might be problematic outside the C locale.
11387aef7c3Smrg# These definitions help.
11487aef7c3Smrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
11587aef7c3Smrglower=abcdefghijklmnopqrstuvwxyz
11687aef7c3Smrgdigits=0123456789
11787aef7c3Smrgalpha=${upper}${lower}
11887aef7c3Smrg
119ea6ae205Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
120ea6ae205Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
121ea6ae205Smrg  exit 1
122ea6ae205Smrgfi
123ea6ae205Smrg
124ea6ae205Smrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
125ea6ae205Smrgdepfile=${depfile-`echo "$object" |
126ea6ae205Smrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
127ea6ae205Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
128ea6ae205Smrg
129ea6ae205Smrgrm -f "$tmpdepfile"
130ea6ae205Smrg
13187aef7c3Smrg# Avoid interferences from the environment.
13287aef7c3Smrggccflag= dashmflag=
13387aef7c3Smrg
134ea6ae205Smrg# Some modes work just like other modes, but use different flags.  We
135ea6ae205Smrg# parameterize here, but still list the modes in the big case below,
136ea6ae205Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
137ea6ae205Smrg# here, because this file can only contain one case statement.
138ea6ae205Smrgif test "$depmode" = hp; then
139ea6ae205Smrg  # HP compiler uses -M and no extra arg.
140ea6ae205Smrg  gccflag=-M
141ea6ae205Smrg  depmode=gcc
142ea6ae205Smrgfi
143ea6ae205Smrg
144ea6ae205Smrgif test "$depmode" = dashXmstdout; then
14587aef7c3Smrg  # This is just like dashmstdout with a different argument.
14687aef7c3Smrg  dashmflag=-xM
14787aef7c3Smrg  depmode=dashmstdout
148ea6ae205Smrgfi
149ea6ae205Smrg
150e83ac88aSmrgcygpath_u="cygpath -u -f -"
151e83ac88aSmrgif test "$depmode" = msvcmsys; then
15287aef7c3Smrg  # This is just like msvisualcpp but w/o cygpath translation.
15387aef7c3Smrg  # Just convert the backslash-escaped backslashes to single forward
15487aef7c3Smrg  # slashes to satisfy depend.m4
15587aef7c3Smrg  cygpath_u='sed s,\\\\,/,g'
15687aef7c3Smrg  depmode=msvisualcpp
15787aef7c3Smrgfi
15887aef7c3Smrg
15987aef7c3Smrgif test "$depmode" = msvc7msys; then
16087aef7c3Smrg  # This is just like msvc7 but w/o cygpath translation.
16187aef7c3Smrg  # Just convert the backslash-escaped backslashes to single forward
16287aef7c3Smrg  # slashes to satisfy depend.m4
16387aef7c3Smrg  cygpath_u='sed s,\\\\,/,g'
16487aef7c3Smrg  depmode=msvc7
16587aef7c3Smrgfi
16687aef7c3Smrg
16787aef7c3Smrgif test "$depmode" = xlc; then
16887aef7c3Smrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
16987aef7c3Smrg  gccflag=-qmakedep=gcc,-MF
17087aef7c3Smrg  depmode=gcc
171e83ac88aSmrgfi
172e83ac88aSmrg
173ea6ae205Smrgcase "$depmode" in
174ea6ae205Smrggcc3)
175ea6ae205Smrg## gcc 3 implements dependency tracking that does exactly what
176ea6ae205Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
177ea6ae205Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
178ea6ae205Smrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
179ea6ae205Smrg## the command line argument order; so add the flags where they
180ea6ae205Smrg## appear in depend2.am.  Note that the slowdown incurred here
181ea6ae205Smrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
182ea6ae205Smrg  for arg
183ea6ae205Smrg  do
184ea6ae205Smrg    case $arg in
185ea6ae205Smrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
186ea6ae205Smrg    *)  set fnord "$@" "$arg" ;;
187ea6ae205Smrg    esac
188ea6ae205Smrg    shift # fnord
189ea6ae205Smrg    shift # $arg
190ea6ae205Smrg  done
191ea6ae205Smrg  "$@"
192ea6ae205Smrg  stat=$?
19387aef7c3Smrg  if test $stat -ne 0; then
194ea6ae205Smrg    rm -f "$tmpdepfile"
195ea6ae205Smrg    exit $stat
196ea6ae205Smrg  fi
197ea6ae205Smrg  mv "$tmpdepfile" "$depfile"
198ea6ae205Smrg  ;;
199ea6ae205Smrg
200ea6ae205Smrggcc)
20187aef7c3Smrg## Note that this doesn't just cater to obsosete pre-3.x GCC compilers.
20287aef7c3Smrg## but also to in-use compilers like IMB xlc/xlC and the HP C compiler.
20387aef7c3Smrg## (see the conditional assignment to $gccflag above).
204ea6ae205Smrg## There are various ways to get dependency output from gcc.  Here's
205ea6ae205Smrg## why we pick this rather obscure method:
206ea6ae205Smrg## - Don't want to use -MD because we'd like the dependencies to end
207ea6ae205Smrg##   up in a subdir.  Having to rename by hand is ugly.
208ea6ae205Smrg##   (We might end up doing this anyway to support other compilers.)
209ea6ae205Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
21087aef7c3Smrg##   -MM, not -M (despite what the docs say).  Also, it might not be
21187aef7c3Smrg##   supported by the other compilers which use the 'gcc' depmode.
212ea6ae205Smrg## - Using -M directly means running the compiler twice (even worse
213ea6ae205Smrg##   than renaming).
214ea6ae205Smrg  if test -z "$gccflag"; then
215ea6ae205Smrg    gccflag=-MD,
216ea6ae205Smrg  fi
217ea6ae205Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
218ea6ae205Smrg  stat=$?
21987aef7c3Smrg  if test $stat -ne 0; then
220ea6ae205Smrg    rm -f "$tmpdepfile"
221ea6ae205Smrg    exit $stat
222ea6ae205Smrg  fi
223ea6ae205Smrg  rm -f "$depfile"
224ea6ae205Smrg  echo "$object : \\" > "$depfile"
22587aef7c3Smrg  # The second -e expression handles DOS-style file names with drive
22687aef7c3Smrg  # letters.
227ea6ae205Smrg  sed -e 's/^[^:]*: / /' \
228ea6ae205Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
22987aef7c3Smrg## This next piece of magic avoids the "deleted header file" problem.
230ea6ae205Smrg## The problem is that when a header file which appears in a .P file
231ea6ae205Smrg## is deleted, the dependency causes make to die (because there is
232ea6ae205Smrg## typically no way to rebuild the header).  We avoid this by adding
233ea6ae205Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
234ea6ae205Smrg## this for us directly.
23587aef7c3Smrg## Some versions of gcc put a space before the ':'.  On the theory
236ea6ae205Smrg## that the space means something, we add a space to the output as
23787aef7c3Smrg## well.  hp depmode also adds that space, but also prefixes the VPATH
23887aef7c3Smrg## to the object.  Take care to not repeat it in the output.
239ea6ae205Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
240ea6ae205Smrg## correctly.  Breaking it into two sed invocations is a workaround.
24187aef7c3Smrg  tr ' ' "$nl" < "$tmpdepfile" \
24287aef7c3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
24387aef7c3Smrg    | sed -e 's/$/ :/' >> "$depfile"
244ea6ae205Smrg  rm -f "$tmpdepfile"
245ea6ae205Smrg  ;;
246ea6ae205Smrg
247ea6ae205Smrghp)
248ea6ae205Smrg  # This case exists only to let depend.m4 do its work.  It works by
249ea6ae205Smrg  # looking at the text of this script.  This case will never be run,
250ea6ae205Smrg  # since it is checked for above.
251ea6ae205Smrg  exit 1
252ea6ae205Smrg  ;;
253ea6ae205Smrg
254ea6ae205Smrgsgi)
255ea6ae205Smrg  if test "$libtool" = yes; then
256ea6ae205Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
257ea6ae205Smrg  else
258ea6ae205Smrg    "$@" -MDupdate "$tmpdepfile"
259ea6ae205Smrg  fi
260ea6ae205Smrg  stat=$?
26187aef7c3Smrg  if test $stat -ne 0; then
262ea6ae205Smrg    rm -f "$tmpdepfile"
263ea6ae205Smrg    exit $stat
264ea6ae205Smrg  fi
265ea6ae205Smrg  rm -f "$depfile"
266ea6ae205Smrg
267ea6ae205Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
268ea6ae205Smrg    echo "$object : \\" > "$depfile"
269ea6ae205Smrg    # Clip off the initial element (the dependent).  Don't try to be
270ea6ae205Smrg    # clever and replace this with sed code, as IRIX sed won't handle
271ea6ae205Smrg    # lines with more than a fixed number of characters (4096 in
272ea6ae205Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
27387aef7c3Smrg    # the IRIX cc adds comments like '#:fec' to the end of the
274ea6ae205Smrg    # dependency line.
27587aef7c3Smrg    tr ' ' "$nl" < "$tmpdepfile" \
27687aef7c3Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
27787aef7c3Smrg      | tr "$nl" ' ' >> "$depfile"
278e83ac88aSmrg    echo >> "$depfile"
279ea6ae205Smrg    # The second pass generates a dummy entry for each header file.
28087aef7c3Smrg    tr ' ' "$nl" < "$tmpdepfile" \
28187aef7c3Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
28287aef7c3Smrg      >> "$depfile"
283ea6ae205Smrg  else
28487aef7c3Smrg    make_dummy_depfile
285ea6ae205Smrg  fi
286ea6ae205Smrg  rm -f "$tmpdepfile"
287ea6ae205Smrg  ;;
288ea6ae205Smrg
28987aef7c3Smrgxlc)
29087aef7c3Smrg  # This case exists only to let depend.m4 do its work.  It works by
29187aef7c3Smrg  # looking at the text of this script.  This case will never be run,
29287aef7c3Smrg  # since it is checked for above.
29387aef7c3Smrg  exit 1
29487aef7c3Smrg  ;;
29587aef7c3Smrg
296ea6ae205Smrgaix)
297ea6ae205Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
298ea6ae205Smrg  # in a .u file.  In older versions, this file always lives in the
29987aef7c3Smrg  # current directory.  Also, the AIX compiler puts '$object:' at the
300ea6ae205Smrg  # start of each line; $object doesn't have directory information.
301ea6ae205Smrg  # Version 6 uses the directory in both cases.
30287aef7c3Smrg  set_dir_from "$object"
30387aef7c3Smrg  set_base_from "$object"
304ea6ae205Smrg  if test "$libtool" = yes; then
305e83ac88aSmrg    tmpdepfile1=$dir$base.u
306e83ac88aSmrg    tmpdepfile2=$base.u
307e83ac88aSmrg    tmpdepfile3=$dir.libs/$base.u
308ea6ae205Smrg    "$@" -Wc,-M
309ea6ae205Smrg  else
310e83ac88aSmrg    tmpdepfile1=$dir$base.u
311e83ac88aSmrg    tmpdepfile2=$dir$base.u
312e83ac88aSmrg    tmpdepfile3=$dir$base.u
313ea6ae205Smrg    "$@" -M
314ea6ae205Smrg  fi
315ea6ae205Smrg  stat=$?
31687aef7c3Smrg  if test $stat -ne 0; then
317e83ac88aSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
318ea6ae205Smrg    exit $stat
319ea6ae205Smrg  fi
320ea6ae205Smrg
321e83ac88aSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
322e83ac88aSmrg  do
323e83ac88aSmrg    test -f "$tmpdepfile" && break
324e83ac88aSmrg  done
32587aef7c3Smrg  aix_post_process_depfile
32687aef7c3Smrg  ;;
32787aef7c3Smrg
32887aef7c3Smrgtcc)
32987aef7c3Smrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
33087aef7c3Smrg  # FIXME: That version still under development at the moment of writing.
33187aef7c3Smrg  #        Make that this statement remains true also for stable, released
33287aef7c3Smrg  #        versions.
33387aef7c3Smrg  # It will wrap lines (doesn't matter whether long or short) with a
33487aef7c3Smrg  # trailing '\', as in:
33587aef7c3Smrg  #
33687aef7c3Smrg  #   foo.o : \
33787aef7c3Smrg  #    foo.c \
33887aef7c3Smrg  #    foo.h \
33987aef7c3Smrg  #
34087aef7c3Smrg  # It will put a trailing '\' even on the last line, and will use leading
34187aef7c3Smrg  # spaces rather than leading tabs (at least since its commit 0394caf7
34287aef7c3Smrg  # "Emit spaces for -MD").
34387aef7c3Smrg  "$@" -MD -MF "$tmpdepfile"
34487aef7c3Smrg  stat=$?
34587aef7c3Smrg  if test $stat -ne 0; then
34687aef7c3Smrg    rm -f "$tmpdepfile"
34787aef7c3Smrg    exit $stat
348ea6ae205Smrg  fi
34987aef7c3Smrg  rm -f "$depfile"
35087aef7c3Smrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
35187aef7c3Smrg  # We have to change lines of the first kind to '$object: \'.
35287aef7c3Smrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
35387aef7c3Smrg  # And for each line of the second kind, we have to emit a 'dep.h:'
35487aef7c3Smrg  # dummy dependency, to avoid the deleted-header problem.
35587aef7c3Smrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
356ea6ae205Smrg  rm -f "$tmpdepfile"
357ea6ae205Smrg  ;;
358ea6ae205Smrg
35987aef7c3Smrg## The order of this option in the case statement is important, since the
36087aef7c3Smrg## shell code in configure will try each of these formats in the order
36187aef7c3Smrg## listed in this file.  A plain '-MD' option would be understood by many
36287aef7c3Smrg## compilers, so we must ensure this comes after the gcc and icc options.
36387aef7c3Smrgpgcc)
36487aef7c3Smrg  # Portland's C compiler understands '-MD'.
36587aef7c3Smrg  # Will always output deps to 'file.d' where file is the root name of the
36687aef7c3Smrg  # source file under compilation, even if file resides in a subdirectory.
36787aef7c3Smrg  # The object file name does not affect the name of the '.d' file.
36887aef7c3Smrg  # pgcc 10.2 will output
369ea6ae205Smrg  #    foo.o: sub/foo.c sub/foo.h
37087aef7c3Smrg  # and will wrap long lines using '\' :
371ea6ae205Smrg  #    foo.o: sub/foo.c ... \
372ea6ae205Smrg  #     sub/foo.h ... \
373ea6ae205Smrg  #     ...
37487aef7c3Smrg  set_dir_from "$object"
37587aef7c3Smrg  # Use the source, not the object, to determine the base name, since
37687aef7c3Smrg  # that's sadly what pgcc will do too.
37787aef7c3Smrg  set_base_from "$source"
37887aef7c3Smrg  tmpdepfile=$base.d
37987aef7c3Smrg
38087aef7c3Smrg  # For projects that build the same source file twice into different object
38187aef7c3Smrg  # files, the pgcc approach of using the *source* file root name can cause
38287aef7c3Smrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
38387aef7c3Smrg  # the same $tmpdepfile.
38487aef7c3Smrg  lockdir=$base.d-lock
38587aef7c3Smrg  trap "
38687aef7c3Smrg    echo '$0: caught signal, cleaning up...' >&2
38787aef7c3Smrg    rmdir '$lockdir'
38887aef7c3Smrg    exit 1
38987aef7c3Smrg  " 1 2 13 15
39087aef7c3Smrg  numtries=100
39187aef7c3Smrg  i=$numtries
39287aef7c3Smrg  while test $i -gt 0; do
39387aef7c3Smrg    # mkdir is a portable test-and-set.
39487aef7c3Smrg    if mkdir "$lockdir" 2>/dev/null; then
39587aef7c3Smrg      # This process acquired the lock.
39687aef7c3Smrg      "$@" -MD
39787aef7c3Smrg      stat=$?
39887aef7c3Smrg      # Release the lock.
39987aef7c3Smrg      rmdir "$lockdir"
40087aef7c3Smrg      break
40187aef7c3Smrg    else
40287aef7c3Smrg      # If the lock is being held by a different process, wait
40387aef7c3Smrg      # until the winning process is done or we timeout.
40487aef7c3Smrg      while test -d "$lockdir" && test $i -gt 0; do
40587aef7c3Smrg        sleep 1
40687aef7c3Smrg        i=`expr $i - 1`
40787aef7c3Smrg      done
40887aef7c3Smrg    fi
40987aef7c3Smrg    i=`expr $i - 1`
41087aef7c3Smrg  done
41187aef7c3Smrg  trap - 1 2 13 15
41287aef7c3Smrg  if test $i -le 0; then
41387aef7c3Smrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
41487aef7c3Smrg    echo "$0: check lockdir '$lockdir'" >&2
41587aef7c3Smrg    exit 1
41687aef7c3Smrg  fi
417ea6ae205Smrg
41887aef7c3Smrg  if test $stat -ne 0; then
419ea6ae205Smrg    rm -f "$tmpdepfile"
420ea6ae205Smrg    exit $stat
421ea6ae205Smrg  fi
422ea6ae205Smrg  rm -f "$depfile"
423ea6ae205Smrg  # Each line is of the form `foo.o: dependent.h',
424ea6ae205Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
425ea6ae205Smrg  # Do two passes, one to just change these to
426ea6ae205Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
427ea6ae205Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
428ea6ae205Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
429ea6ae205Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
43087aef7c3Smrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
43187aef7c3Smrg    | sed -e 's/$/ :/' >> "$depfile"
432ea6ae205Smrg  rm -f "$tmpdepfile"
433ea6ae205Smrg  ;;
434ea6ae205Smrg
435ea6ae205Smrghp2)
436ea6ae205Smrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
437ea6ae205Smrg  # compilers, which have integrated preprocessors.  The correct option
438ea6ae205Smrg  # to use with these is +Maked; it writes dependencies to a file named
439ea6ae205Smrg  # 'foo.d', which lands next to the object file, wherever that
440ea6ae205Smrg  # happens to be.
441ea6ae205Smrg  # Much of this is similar to the tru64 case; see comments there.
44287aef7c3Smrg  set_dir_from  "$object"
44387aef7c3Smrg  set_base_from "$object"
444ea6ae205Smrg  if test "$libtool" = yes; then
445ea6ae205Smrg    tmpdepfile1=$dir$base.d
446ea6ae205Smrg    tmpdepfile2=$dir.libs/$base.d
447ea6ae205Smrg    "$@" -Wc,+Maked
448ea6ae205Smrg  else
449ea6ae205Smrg    tmpdepfile1=$dir$base.d
450ea6ae205Smrg    tmpdepfile2=$dir$base.d
451ea6ae205Smrg    "$@" +Maked
452ea6ae205Smrg  fi
453ea6ae205Smrg  stat=$?
45487aef7c3Smrg  if test $stat -ne 0; then
455ea6ae205Smrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
456ea6ae205Smrg     exit $stat
457ea6ae205Smrg  fi
458ea6ae205Smrg
459ea6ae205Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
460ea6ae205Smrg  do
461ea6ae205Smrg    test -f "$tmpdepfile" && break
462ea6ae205Smrg  done
463ea6ae205Smrg  if test -f "$tmpdepfile"; then
46487aef7c3Smrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
46587aef7c3Smrg    # Add 'dependent.h:' lines.
466e83ac88aSmrg    sed -ne '2,${
46787aef7c3Smrg               s/^ *//
46887aef7c3Smrg               s/ \\*$//
46987aef7c3Smrg               s/$/:/
47087aef7c3Smrg               p
47187aef7c3Smrg             }' "$tmpdepfile" >> "$depfile"
472ea6ae205Smrg  else
47387aef7c3Smrg    make_dummy_depfile
474ea6ae205Smrg  fi
475ea6ae205Smrg  rm -f "$tmpdepfile" "$tmpdepfile2"
476ea6ae205Smrg  ;;
477ea6ae205Smrg
478ea6ae205Smrgtru64)
47987aef7c3Smrg  # The Tru64 compiler uses -MD to generate dependencies as a side
48087aef7c3Smrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
48187aef7c3Smrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
48287aef7c3Smrg  # dependencies in 'foo.d' instead, so we check for that too.
48387aef7c3Smrg  # Subdirectories are respected.
48487aef7c3Smrg  set_dir_from  "$object"
48587aef7c3Smrg  set_base_from "$object"
48687aef7c3Smrg
48787aef7c3Smrg  if test "$libtool" = yes; then
48887aef7c3Smrg    # Libtool generates 2 separate objects for the 2 libraries.  These
48987aef7c3Smrg    # two compilations output dependencies in $dir.libs/$base.o.d and
49087aef7c3Smrg    # in $dir$base.o.d.  We have to check for both files, because
49187aef7c3Smrg    # one of the two compilations can be disabled.  We should prefer
49287aef7c3Smrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
49387aef7c3Smrg    # automatically cleaned when .libs/ is deleted, while ignoring
49487aef7c3Smrg    # the former would cause a distcleancheck panic.
49587aef7c3Smrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
49687aef7c3Smrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
49787aef7c3Smrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
49887aef7c3Smrg    "$@" -Wc,-MD
49987aef7c3Smrg  else
50087aef7c3Smrg    tmpdepfile1=$dir$base.d
50187aef7c3Smrg    tmpdepfile2=$dir$base.d
50287aef7c3Smrg    tmpdepfile3=$dir$base.d
50387aef7c3Smrg    "$@" -MD
50487aef7c3Smrg  fi
50587aef7c3Smrg
50687aef7c3Smrg  stat=$?
50787aef7c3Smrg  if test $stat -ne 0; then
50887aef7c3Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
50987aef7c3Smrg    exit $stat
51087aef7c3Smrg  fi
51187aef7c3Smrg
51287aef7c3Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
51387aef7c3Smrg  do
51487aef7c3Smrg    test -f "$tmpdepfile" && break
51587aef7c3Smrg  done
51687aef7c3Smrg  # Same post-processing that is required for AIX mode.
51787aef7c3Smrg  aix_post_process_depfile
51887aef7c3Smrg  ;;
51987aef7c3Smrg
52087aef7c3Smrgmsvc7)
52187aef7c3Smrg  if test "$libtool" = yes; then
52287aef7c3Smrg    showIncludes=-Wc,-showIncludes
52387aef7c3Smrg  else
52487aef7c3Smrg    showIncludes=-showIncludes
52587aef7c3Smrg  fi
52687aef7c3Smrg  "$@" $showIncludes > "$tmpdepfile"
52787aef7c3Smrg  stat=$?
52887aef7c3Smrg  grep -v '^Note: including file: ' "$tmpdepfile"
52987aef7c3Smrg  if test $stat -ne 0; then
53087aef7c3Smrg    rm -f "$tmpdepfile"
53187aef7c3Smrg    exit $stat
53287aef7c3Smrg  fi
53387aef7c3Smrg  rm -f "$depfile"
53487aef7c3Smrg  echo "$object : \\" > "$depfile"
53587aef7c3Smrg  # The first sed program below extracts the file names and escapes
53687aef7c3Smrg  # backslashes for cygpath.  The second sed program outputs the file
53787aef7c3Smrg  # name when reading, but also accumulates all include files in the
53887aef7c3Smrg  # hold buffer in order to output them again at the end.  This only
53987aef7c3Smrg  # works with sed implementations that can handle large buffers.
54087aef7c3Smrg  sed < "$tmpdepfile" -n '
54187aef7c3Smrg/^Note: including file:  *\(.*\)/ {
54287aef7c3Smrg  s//\1/
54387aef7c3Smrg  s/\\/\\\\/g
54487aef7c3Smrg  p
54587aef7c3Smrg}' | $cygpath_u | sort -u | sed -n '
54687aef7c3Smrgs/ /\\ /g
54787aef7c3Smrgs/\(.*\)/'"$tab"'\1 \\/p
54887aef7c3Smrgs/.\(.*\) \\/\1:/
54987aef7c3SmrgH
55087aef7c3Smrg$ {
55187aef7c3Smrg  s/.*/'"$tab"'/
55287aef7c3Smrg  G
55387aef7c3Smrg  p
55487aef7c3Smrg}' >> "$depfile"
55587aef7c3Smrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
55687aef7c3Smrg  rm -f "$tmpdepfile"
55787aef7c3Smrg  ;;
55887aef7c3Smrg
55987aef7c3Smrgmsvc7msys)
56087aef7c3Smrg  # This case exists only to let depend.m4 do its work.  It works by
56187aef7c3Smrg  # looking at the text of this script.  This case will never be run,
56287aef7c3Smrg  # since it is checked for above.
56387aef7c3Smrg  exit 1
56487aef7c3Smrg  ;;
565ea6ae205Smrg
566ea6ae205Smrg#nosideeffect)
567ea6ae205Smrg  # This comment above is used by automake to tell side-effect
568ea6ae205Smrg  # dependency tracking mechanisms from slower ones.
569ea6ae205Smrg
570ea6ae205Smrgdashmstdout)
571ea6ae205Smrg  # Important note: in order to support this mode, a compiler *must*
572ea6ae205Smrg  # always write the preprocessed file to stdout, regardless of -o.
573ea6ae205Smrg  "$@" || exit $?
574ea6ae205Smrg
575ea6ae205Smrg  # Remove the call to Libtool.
576ea6ae205Smrg  if test "$libtool" = yes; then
577e83ac88aSmrg    while test "X$1" != 'X--mode=compile'; do
578ea6ae205Smrg      shift
579ea6ae205Smrg    done
580ea6ae205Smrg    shift
581ea6ae205Smrg  fi
582ea6ae205Smrg
58387aef7c3Smrg  # Remove '-o $object'.
584ea6ae205Smrg  IFS=" "
585ea6ae205Smrg  for arg
586ea6ae205Smrg  do
587ea6ae205Smrg    case $arg in
588ea6ae205Smrg    -o)
589ea6ae205Smrg      shift
590ea6ae205Smrg      ;;
591ea6ae205Smrg    $object)
592ea6ae205Smrg      shift
593ea6ae205Smrg      ;;
594ea6ae205Smrg    *)
595ea6ae205Smrg      set fnord "$@" "$arg"
596ea6ae205Smrg      shift # fnord
597ea6ae205Smrg      shift # $arg
598ea6ae205Smrg      ;;
599ea6ae205Smrg    esac
600ea6ae205Smrg  done
601ea6ae205Smrg
602ea6ae205Smrg  test -z "$dashmflag" && dashmflag=-M
60387aef7c3Smrg  # Require at least two characters before searching for ':'
604ea6ae205Smrg  # in the target name.  This is to cope with DOS-style filenames:
60587aef7c3Smrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
606ea6ae205Smrg  "$@" $dashmflag |
60787aef7c3Smrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
608ea6ae205Smrg  rm -f "$depfile"
609ea6ae205Smrg  cat < "$tmpdepfile" > "$depfile"
61087aef7c3Smrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
61187aef7c3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
61287aef7c3Smrg  tr ' ' "$nl" < "$tmpdepfile" \
61387aef7c3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
61487aef7c3Smrg    | sed -e 's/$/ :/' >> "$depfile"
615ea6ae205Smrg  rm -f "$tmpdepfile"
616ea6ae205Smrg  ;;
617ea6ae205Smrg
618ea6ae205SmrgdashXmstdout)
619ea6ae205Smrg  # This case only exists to satisfy depend.m4.  It is never actually
620ea6ae205Smrg  # run, as this mode is specially recognized in the preamble.
621ea6ae205Smrg  exit 1
622ea6ae205Smrg  ;;
623ea6ae205Smrg
624ea6ae205Smrgmakedepend)
625ea6ae205Smrg  "$@" || exit $?
626ea6ae205Smrg  # Remove any Libtool call
627ea6ae205Smrg  if test "$libtool" = yes; then
628e83ac88aSmrg    while test "X$1" != 'X--mode=compile'; do
629ea6ae205Smrg      shift
630ea6ae205Smrg    done
631ea6ae205Smrg    shift
632ea6ae205Smrg  fi
633ea6ae205Smrg  # X makedepend
634ea6ae205Smrg  shift
635e83ac88aSmrg  cleared=no eat=no
636e83ac88aSmrg  for arg
637e83ac88aSmrg  do
638ea6ae205Smrg    case $cleared in
639ea6ae205Smrg    no)
640ea6ae205Smrg      set ""; shift
641ea6ae205Smrg      cleared=yes ;;
642ea6ae205Smrg    esac
643e83ac88aSmrg    if test $eat = yes; then
644e83ac88aSmrg      eat=no
645e83ac88aSmrg      continue
646e83ac88aSmrg    fi
647ea6ae205Smrg    case "$arg" in
648ea6ae205Smrg    -D*|-I*)
649ea6ae205Smrg      set fnord "$@" "$arg"; shift ;;
650ea6ae205Smrg    # Strip any option that makedepend may not understand.  Remove
651ea6ae205Smrg    # the object too, otherwise makedepend will parse it as a source file.
652e83ac88aSmrg    -arch)
653e83ac88aSmrg      eat=yes ;;
654ea6ae205Smrg    -*|$object)
655ea6ae205Smrg      ;;
656ea6ae205Smrg    *)
657ea6ae205Smrg      set fnord "$@" "$arg"; shift ;;
658ea6ae205Smrg    esac
659ea6ae205Smrg  done
660e83ac88aSmrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
661ea6ae205Smrg  touch "$tmpdepfile"
662ea6ae205Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
663ea6ae205Smrg  rm -f "$depfile"
66487aef7c3Smrg  # makedepend may prepend the VPATH from the source file name to the object.
66587aef7c3Smrg  # No need to regex-escape $object, excess matching of '.' is harmless.
66687aef7c3Smrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
66787aef7c3Smrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
66887aef7c3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
66987aef7c3Smrg  sed '1,2d' "$tmpdepfile" \
67087aef7c3Smrg    | tr ' ' "$nl" \
67187aef7c3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
67287aef7c3Smrg    | sed -e 's/$/ :/' >> "$depfile"
673ea6ae205Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
674ea6ae205Smrg  ;;
675ea6ae205Smrg
676ea6ae205Smrgcpp)
677ea6ae205Smrg  # Important note: in order to support this mode, a compiler *must*
678ea6ae205Smrg  # always write the preprocessed file to stdout.
679ea6ae205Smrg  "$@" || exit $?
680ea6ae205Smrg
681ea6ae205Smrg  # Remove the call to Libtool.
682ea6ae205Smrg  if test "$libtool" = yes; then
683e83ac88aSmrg    while test "X$1" != 'X--mode=compile'; do
684ea6ae205Smrg      shift
685ea6ae205Smrg    done
686ea6ae205Smrg    shift
687ea6ae205Smrg  fi
688ea6ae205Smrg
68987aef7c3Smrg  # Remove '-o $object'.
690ea6ae205Smrg  IFS=" "
691ea6ae205Smrg  for arg
692ea6ae205Smrg  do
693ea6ae205Smrg    case $arg in
694ea6ae205Smrg    -o)
695ea6ae205Smrg      shift
696ea6ae205Smrg      ;;
697ea6ae205Smrg    $object)
698ea6ae205Smrg      shift
699ea6ae205Smrg      ;;
700ea6ae205Smrg    *)
701ea6ae205Smrg      set fnord "$@" "$arg"
702ea6ae205Smrg      shift # fnord
703ea6ae205Smrg      shift # $arg
704ea6ae205Smrg      ;;
705ea6ae205Smrg    esac
706ea6ae205Smrg  done
707ea6ae205Smrg
70887aef7c3Smrg  "$@" -E \
70987aef7c3Smrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
71087aef7c3Smrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
71187aef7c3Smrg    | sed '$ s: \\$::' > "$tmpdepfile"
712ea6ae205Smrg  rm -f "$depfile"
713ea6ae205Smrg  echo "$object : \\" > "$depfile"
714ea6ae205Smrg  cat < "$tmpdepfile" >> "$depfile"
715ea6ae205Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
716ea6ae205Smrg  rm -f "$tmpdepfile"
717ea6ae205Smrg  ;;
718ea6ae205Smrg
719ea6ae205Smrgmsvisualcpp)
720ea6ae205Smrg  # Important note: in order to support this mode, a compiler *must*
721e83ac88aSmrg  # always write the preprocessed file to stdout.
722ea6ae205Smrg  "$@" || exit $?
723e83ac88aSmrg
724e83ac88aSmrg  # Remove the call to Libtool.
725e83ac88aSmrg  if test "$libtool" = yes; then
726e83ac88aSmrg    while test "X$1" != 'X--mode=compile'; do
727e83ac88aSmrg      shift
728e83ac88aSmrg    done
729e83ac88aSmrg    shift
730e83ac88aSmrg  fi
731e83ac88aSmrg
732ea6ae205Smrg  IFS=" "
733ea6ae205Smrg  for arg
734ea6ae205Smrg  do
735ea6ae205Smrg    case "$arg" in
736e83ac88aSmrg    -o)
737e83ac88aSmrg      shift
738e83ac88aSmrg      ;;
739e83ac88aSmrg    $object)
740e83ac88aSmrg      shift
741e83ac88aSmrg      ;;
742ea6ae205Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
74387aef7c3Smrg        set fnord "$@"
74487aef7c3Smrg        shift
74587aef7c3Smrg        shift
74687aef7c3Smrg        ;;
747ea6ae205Smrg    *)
74887aef7c3Smrg        set fnord "$@" "$arg"
74987aef7c3Smrg        shift
75087aef7c3Smrg        shift
75187aef7c3Smrg        ;;
752ea6ae205Smrg    esac
753ea6ae205Smrg  done
754e83ac88aSmrg  "$@" -E 2>/dev/null |
755e83ac88aSmrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
756ea6ae205Smrg  rm -f "$depfile"
757ea6ae205Smrg  echo "$object : \\" > "$depfile"
75887aef7c3Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
75987aef7c3Smrg  echo "$tab" >> "$depfile"
760e83ac88aSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
761ea6ae205Smrg  rm -f "$tmpdepfile"
762ea6ae205Smrg  ;;
763ea6ae205Smrg
764e83ac88aSmrgmsvcmsys)
765e83ac88aSmrg  # This case exists only to let depend.m4 do its work.  It works by
766e83ac88aSmrg  # looking at the text of this script.  This case will never be run,
767e83ac88aSmrg  # since it is checked for above.
768e83ac88aSmrg  exit 1
769e83ac88aSmrg  ;;
770e83ac88aSmrg
771ea6ae205Smrgnone)
772ea6ae205Smrg  exec "$@"
773ea6ae205Smrg  ;;
774ea6ae205Smrg
775ea6ae205Smrg*)
776ea6ae205Smrg  echo "Unknown depmode $depmode" 1>&2
777ea6ae205Smrg  exit 1
778ea6ae205Smrg  ;;
779ea6ae205Smrgesac
780ea6ae205Smrg
781ea6ae205Smrgexit 0
782ea6ae205Smrg
783ea6ae205Smrg# Local Variables:
784ea6ae205Smrg# mode: shell-script
785ea6ae205Smrg# sh-indentation: 2
786764c86d1Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
787ea6ae205Smrg# time-stamp-start: "scriptversion="
788ea6ae205Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
789764c86d1Smrg# time-stamp-time-zone: "UTC0"
790e83ac88aSmrg# time-stamp-end: "; # UTC"
791ea6ae205Smrg# End:
792