1f33d31a3Smrg#! /bin/sh
2f33d31a3Smrg# depcomp - compile a program generating dependencies as side-effects
3f33d31a3Smrg
48b5ec993Smrgscriptversion=2024-06-19.01; # UTC
5f33d31a3Smrg
68b5ec993Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
7f33d31a3Smrg
8f33d31a3Smrg# This program is free software; you can redistribute it and/or modify
9f33d31a3Smrg# it under the terms of the GNU General Public License as published by
10f33d31a3Smrg# the Free Software Foundation; either version 2, or (at your option)
11f33d31a3Smrg# any later version.
12f33d31a3Smrg
13f33d31a3Smrg# This program is distributed in the hope that it will be useful,
14f33d31a3Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15f33d31a3Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16f33d31a3Smrg# GNU General Public License for more details.
17f33d31a3Smrg
18f33d31a3Smrg# You should have received a copy of the GNU General Public License
199f1d9476Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20f33d31a3Smrg
21f33d31a3Smrg# As a special exception to the GNU General Public License, if you
22f33d31a3Smrg# distribute this file as part of a program that contains a
23f33d31a3Smrg# configuration script generated by Autoconf, you may include it under
24f33d31a3Smrg# the same distribution terms that you use for the rest of that program.
25f33d31a3Smrg
26f33d31a3Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
27f33d31a3Smrg
28f33d31a3Smrgcase $1 in
29f33d31a3Smrg  '')
30f33d31a3Smrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31f33d31a3Smrg    exit 1;
32f33d31a3Smrg    ;;
33f33d31a3Smrg  -h | --h*)
34f33d31a3Smrg    cat <<\EOF
35f33d31a3SmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
36f33d31a3Smrg
37f33d31a3SmrgRun PROGRAMS ARGS to compile a file, generating dependencies
38f33d31a3Smrgas side-effects.
39f33d31a3Smrg
40f33d31a3SmrgEnvironment variables:
41f33d31a3Smrg  depmode     Dependency tracking mode.
42f33d31a3Smrg  source      Source file read by 'PROGRAMS ARGS'.
43f33d31a3Smrg  object      Object file output by 'PROGRAMS ARGS'.
44f33d31a3Smrg  DEPDIR      directory where to store dependencies.
45f33d31a3Smrg  depfile     Dependency file to output.
46f33d31a3Smrg  tmpdepfile  Temporary file to use when outputting dependencies.
47f33d31a3Smrg  libtool     Whether libtool is used (yes/no).
48f33d31a3Smrg
49f33d31a3SmrgReport bugs to <bug-automake@gnu.org>.
508b5ec993SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
518b5ec993SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
52f33d31a3SmrgEOF
53f33d31a3Smrg    exit $?
54f33d31a3Smrg    ;;
55f33d31a3Smrg  -v | --v*)
568b5ec993Smrg    echo "depcomp (GNU Automake) $scriptversion"
57f33d31a3Smrg    exit $?
58f33d31a3Smrg    ;;
59f33d31a3Smrgesac
60f33d31a3Smrg
61f33d31a3Smrg# Get the directory component of the given path, and save it in the
62f33d31a3Smrg# global variables '$dir'.  Note that this directory component will
63f33d31a3Smrg# be either empty or ending with a '/' character.  This is deliberate.
64f33d31a3Smrgset_dir_from ()
65f33d31a3Smrg{
66f33d31a3Smrg  case $1 in
67f33d31a3Smrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
68f33d31a3Smrg      *) dir=;;
69f33d31a3Smrg  esac
70f33d31a3Smrg}
71f33d31a3Smrg
72f33d31a3Smrg# Get the suffix-stripped basename of the given path, and save it the
73f33d31a3Smrg# global variable '$base'.
74f33d31a3Smrgset_base_from ()
75f33d31a3Smrg{
76f33d31a3Smrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
77f33d31a3Smrg}
78f33d31a3Smrg
79f33d31a3Smrg# If no dependency file was actually created by the compiler invocation,
80f33d31a3Smrg# we still have to create a dummy depfile, to avoid errors with the
81f33d31a3Smrg# Makefile "include basename.Plo" scheme.
82f33d31a3Smrgmake_dummy_depfile ()
83f33d31a3Smrg{
84f33d31a3Smrg  echo "#dummy" > "$depfile"
85f33d31a3Smrg}
86f33d31a3Smrg
87f33d31a3Smrg# Factor out some common post-processing of the generated depfile.
88f33d31a3Smrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
89f33d31a3Smrgaix_post_process_depfile ()
90f33d31a3Smrg{
91f33d31a3Smrg  # If the compiler actually managed to produce a dependency file,
92f33d31a3Smrg  # post-process it.
93f33d31a3Smrg  if test -f "$tmpdepfile"; then
94f33d31a3Smrg    # Each line is of the form 'foo.o: dependency.h'.
95f33d31a3Smrg    # Do two passes, one to just change these to
96f33d31a3Smrg    #   $object: dependency.h
97f33d31a3Smrg    # and one to simply output
98f33d31a3Smrg    #   dependency.h:
99f33d31a3Smrg    # which is needed to avoid the deleted-header problem.
100f33d31a3Smrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
101f33d31a3Smrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
102f33d31a3Smrg    } > "$depfile"
103f33d31a3Smrg    rm -f "$tmpdepfile"
104f33d31a3Smrg  else
105f33d31a3Smrg    make_dummy_depfile
106f33d31a3Smrg  fi
107f33d31a3Smrg}
108f33d31a3Smrg
109f33d31a3Smrg# A tabulation character.
110f33d31a3Smrgtab='	'
111f33d31a3Smrg# A newline character.
112f33d31a3Smrgnl='
113f33d31a3Smrg'
114f33d31a3Smrg# Character ranges might be problematic outside the C locale.
115f33d31a3Smrg# These definitions help.
116f33d31a3Smrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
117f33d31a3Smrglower=abcdefghijklmnopqrstuvwxyz
118f33d31a3Smrgalpha=${upper}${lower}
119f33d31a3Smrg
120f33d31a3Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
121f33d31a3Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
122f33d31a3Smrg  exit 1
123f33d31a3Smrgfi
124f33d31a3Smrg
125f33d31a3Smrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
126f33d31a3Smrgdepfile=${depfile-`echo "$object" |
127f33d31a3Smrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
128f33d31a3Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
129f33d31a3Smrg
130f33d31a3Smrgrm -f "$tmpdepfile"
131f33d31a3Smrg
1328b5ec993Smrg# Avoid interference from the environment.
133f33d31a3Smrggccflag= dashmflag=
134f33d31a3Smrg
135f33d31a3Smrg# Some modes work just like other modes, but use different flags.  We
136f33d31a3Smrg# parameterize here, but still list the modes in the big case below,
137f33d31a3Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
138f33d31a3Smrg# here, because this file can only contain one case statement.
139f33d31a3Smrgif test "$depmode" = hp; then
140f33d31a3Smrg  # HP compiler uses -M and no extra arg.
141f33d31a3Smrg  gccflag=-M
142f33d31a3Smrg  depmode=gcc
143f33d31a3Smrgfi
144f33d31a3Smrg
145f33d31a3Smrgif test "$depmode" = dashXmstdout; then
146f33d31a3Smrg  # This is just like dashmstdout with a different argument.
147f33d31a3Smrg  dashmflag=-xM
148f33d31a3Smrg  depmode=dashmstdout
149f33d31a3Smrgfi
150f33d31a3Smrg
151f33d31a3Smrgcygpath_u="cygpath -u -f -"
152f33d31a3Smrgif test "$depmode" = msvcmsys; then
153f33d31a3Smrg  # This is just like msvisualcpp but w/o cygpath translation.
154f33d31a3Smrg  # Just convert the backslash-escaped backslashes to single forward
155f33d31a3Smrg  # slashes to satisfy depend.m4
156f33d31a3Smrg  cygpath_u='sed s,\\\\,/,g'
157f33d31a3Smrg  depmode=msvisualcpp
158f33d31a3Smrgfi
159f33d31a3Smrg
160f33d31a3Smrgif test "$depmode" = msvc7msys; then
161f33d31a3Smrg  # This is just like msvc7 but w/o cygpath translation.
162f33d31a3Smrg  # Just convert the backslash-escaped backslashes to single forward
163f33d31a3Smrg  # slashes to satisfy depend.m4
164f33d31a3Smrg  cygpath_u='sed s,\\\\,/,g'
165f33d31a3Smrg  depmode=msvc7
166f33d31a3Smrgfi
167f33d31a3Smrg
168f33d31a3Smrgif test "$depmode" = xlc; then
169f33d31a3Smrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
170f33d31a3Smrg  gccflag=-qmakedep=gcc,-MF
171f33d31a3Smrg  depmode=gcc
172f33d31a3Smrgfi
173f33d31a3Smrg
174f33d31a3Smrgcase "$depmode" in
175f33d31a3Smrggcc3)
176f33d31a3Smrg## gcc 3 implements dependency tracking that does exactly what
177f33d31a3Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
178f33d31a3Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
179f33d31a3Smrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
180f33d31a3Smrg## the command line argument order; so add the flags where they
181f33d31a3Smrg## appear in depend2.am.  Note that the slowdown incurred here
182f33d31a3Smrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
183f33d31a3Smrg  for arg
184f33d31a3Smrg  do
185f33d31a3Smrg    case $arg in
186f33d31a3Smrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
187f33d31a3Smrg    *)  set fnord "$@" "$arg" ;;
188f33d31a3Smrg    esac
189f33d31a3Smrg    shift # fnord
190f33d31a3Smrg    shift # $arg
191f33d31a3Smrg  done
192f33d31a3Smrg  "$@"
193f33d31a3Smrg  stat=$?
194f33d31a3Smrg  if test $stat -ne 0; then
195f33d31a3Smrg    rm -f "$tmpdepfile"
196f33d31a3Smrg    exit $stat
197f33d31a3Smrg  fi
198f33d31a3Smrg  mv "$tmpdepfile" "$depfile"
199f33d31a3Smrg  ;;
200f33d31a3Smrg
201f33d31a3Smrggcc)
2028b5ec993Smrg## Note that this doesn't just cater to obsolete pre-3.x GCC compilers.
2038b5ec993Smrg## but also to in-use compilers like IBM xlc/xlC and the HP C compiler.
204f33d31a3Smrg## (see the conditional assignment to $gccflag above).
205f33d31a3Smrg## There are various ways to get dependency output from gcc.  Here's
206f33d31a3Smrg## why we pick this rather obscure method:
207f33d31a3Smrg## - Don't want to use -MD because we'd like the dependencies to end
208f33d31a3Smrg##   up in a subdir.  Having to rename by hand is ugly.
209f33d31a3Smrg##   (We might end up doing this anyway to support other compilers.)
210f33d31a3Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
211f33d31a3Smrg##   -MM, not -M (despite what the docs say).  Also, it might not be
212f33d31a3Smrg##   supported by the other compilers which use the 'gcc' depmode.
213f33d31a3Smrg## - Using -M directly means running the compiler twice (even worse
214f33d31a3Smrg##   than renaming).
215f33d31a3Smrg  if test -z "$gccflag"; then
216f33d31a3Smrg    gccflag=-MD,
217f33d31a3Smrg  fi
218f33d31a3Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
219f33d31a3Smrg  stat=$?
220f33d31a3Smrg  if test $stat -ne 0; then
221f33d31a3Smrg    rm -f "$tmpdepfile"
222f33d31a3Smrg    exit $stat
223f33d31a3Smrg  fi
224f33d31a3Smrg  rm -f "$depfile"
225f33d31a3Smrg  echo "$object : \\" > "$depfile"
226f33d31a3Smrg  # The second -e expression handles DOS-style file names with drive
227f33d31a3Smrg  # letters.
228f33d31a3Smrg  sed -e 's/^[^:]*: / /' \
229f33d31a3Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
230f33d31a3Smrg## This next piece of magic avoids the "deleted header file" problem.
231f33d31a3Smrg## The problem is that when a header file which appears in a .P file
232f33d31a3Smrg## is deleted, the dependency causes make to die (because there is
233f33d31a3Smrg## typically no way to rebuild the header).  We avoid this by adding
234f33d31a3Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
235f33d31a3Smrg## this for us directly.
236f33d31a3Smrg## Some versions of gcc put a space before the ':'.  On the theory
237f33d31a3Smrg## that the space means something, we add a space to the output as
238f33d31a3Smrg## well.  hp depmode also adds that space, but also prefixes the VPATH
239f33d31a3Smrg## to the object.  Take care to not repeat it in the output.
240f33d31a3Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
241f33d31a3Smrg## correctly.  Breaking it into two sed invocations is a workaround.
242f33d31a3Smrg  tr ' ' "$nl" < "$tmpdepfile" \
243f33d31a3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
244f33d31a3Smrg    | sed -e 's/$/ :/' >> "$depfile"
245f33d31a3Smrg  rm -f "$tmpdepfile"
246f33d31a3Smrg  ;;
247f33d31a3Smrg
248f33d31a3Smrghp)
249f33d31a3Smrg  # This case exists only to let depend.m4 do its work.  It works by
250f33d31a3Smrg  # looking at the text of this script.  This case will never be run,
251f33d31a3Smrg  # since it is checked for above.
252f33d31a3Smrg  exit 1
253f33d31a3Smrg  ;;
254f33d31a3Smrg
255f33d31a3Smrgsgi)
256f33d31a3Smrg  if test "$libtool" = yes; then
257f33d31a3Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
258f33d31a3Smrg  else
259f33d31a3Smrg    "$@" -MDupdate "$tmpdepfile"
260f33d31a3Smrg  fi
261f33d31a3Smrg  stat=$?
262f33d31a3Smrg  if test $stat -ne 0; then
263f33d31a3Smrg    rm -f "$tmpdepfile"
264f33d31a3Smrg    exit $stat
265f33d31a3Smrg  fi
266f33d31a3Smrg  rm -f "$depfile"
267f33d31a3Smrg
268f33d31a3Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
269f33d31a3Smrg    echo "$object : \\" > "$depfile"
270f33d31a3Smrg    # Clip off the initial element (the dependent).  Don't try to be
271f33d31a3Smrg    # clever and replace this with sed code, as IRIX sed won't handle
272f33d31a3Smrg    # lines with more than a fixed number of characters (4096 in
273f33d31a3Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
274f33d31a3Smrg    # the IRIX cc adds comments like '#:fec' to the end of the
275f33d31a3Smrg    # dependency line.
276f33d31a3Smrg    tr ' ' "$nl" < "$tmpdepfile" \
277f33d31a3Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
278f33d31a3Smrg      | tr "$nl" ' ' >> "$depfile"
279f33d31a3Smrg    echo >> "$depfile"
280f33d31a3Smrg    # The second pass generates a dummy entry for each header file.
281f33d31a3Smrg    tr ' ' "$nl" < "$tmpdepfile" \
282f33d31a3Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
283f33d31a3Smrg      >> "$depfile"
284f33d31a3Smrg  else
285f33d31a3Smrg    make_dummy_depfile
286f33d31a3Smrg  fi
287f33d31a3Smrg  rm -f "$tmpdepfile"
288f33d31a3Smrg  ;;
289f33d31a3Smrg
290f33d31a3Smrgxlc)
291f33d31a3Smrg  # This case exists only to let depend.m4 do its work.  It works by
292f33d31a3Smrg  # looking at the text of this script.  This case will never be run,
293f33d31a3Smrg  # since it is checked for above.
294f33d31a3Smrg  exit 1
295f33d31a3Smrg  ;;
296f33d31a3Smrg
297f33d31a3Smrgaix)
298f33d31a3Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
299f33d31a3Smrg  # in a .u file.  In older versions, this file always lives in the
300f33d31a3Smrg  # current directory.  Also, the AIX compiler puts '$object:' at the
301f33d31a3Smrg  # start of each line; $object doesn't have directory information.
302f33d31a3Smrg  # Version 6 uses the directory in both cases.
303f33d31a3Smrg  set_dir_from "$object"
304f33d31a3Smrg  set_base_from "$object"
305f33d31a3Smrg  if test "$libtool" = yes; then
306f33d31a3Smrg    tmpdepfile1=$dir$base.u
307f33d31a3Smrg    tmpdepfile2=$base.u
308f33d31a3Smrg    tmpdepfile3=$dir.libs/$base.u
309f33d31a3Smrg    "$@" -Wc,-M
310f33d31a3Smrg  else
311f33d31a3Smrg    tmpdepfile1=$dir$base.u
312f33d31a3Smrg    tmpdepfile2=$dir$base.u
313f33d31a3Smrg    tmpdepfile3=$dir$base.u
314f33d31a3Smrg    "$@" -M
315f33d31a3Smrg  fi
316f33d31a3Smrg  stat=$?
317f33d31a3Smrg  if test $stat -ne 0; then
318f33d31a3Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
319f33d31a3Smrg    exit $stat
320f33d31a3Smrg  fi
321f33d31a3Smrg
322f33d31a3Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
323f33d31a3Smrg  do
324f33d31a3Smrg    test -f "$tmpdepfile" && break
325f33d31a3Smrg  done
326f33d31a3Smrg  aix_post_process_depfile
327f33d31a3Smrg  ;;
328f33d31a3Smrg
329f33d31a3Smrgtcc)
330f33d31a3Smrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
331f33d31a3Smrg  # FIXME: That version still under development at the moment of writing.
332f33d31a3Smrg  #        Make that this statement remains true also for stable, released
333f33d31a3Smrg  #        versions.
334f33d31a3Smrg  # It will wrap lines (doesn't matter whether long or short) with a
335f33d31a3Smrg  # trailing '\', as in:
336f33d31a3Smrg  #
337f33d31a3Smrg  #   foo.o : \
338f33d31a3Smrg  #    foo.c \
339f33d31a3Smrg  #    foo.h \
340f33d31a3Smrg  #
341f33d31a3Smrg  # It will put a trailing '\' even on the last line, and will use leading
342f33d31a3Smrg  # spaces rather than leading tabs (at least since its commit 0394caf7
343f33d31a3Smrg  # "Emit spaces for -MD").
344f33d31a3Smrg  "$@" -MD -MF "$tmpdepfile"
345f33d31a3Smrg  stat=$?
346f33d31a3Smrg  if test $stat -ne 0; then
347f33d31a3Smrg    rm -f "$tmpdepfile"
348f33d31a3Smrg    exit $stat
349f33d31a3Smrg  fi
350f33d31a3Smrg  rm -f "$depfile"
351f33d31a3Smrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
352f33d31a3Smrg  # We have to change lines of the first kind to '$object: \'.
353f33d31a3Smrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
354f33d31a3Smrg  # And for each line of the second kind, we have to emit a 'dep.h:'
355f33d31a3Smrg  # dummy dependency, to avoid the deleted-header problem.
356f33d31a3Smrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
357f33d31a3Smrg  rm -f "$tmpdepfile"
358f33d31a3Smrg  ;;
359f33d31a3Smrg
360f33d31a3Smrg## The order of this option in the case statement is important, since the
361f33d31a3Smrg## shell code in configure will try each of these formats in the order
362f33d31a3Smrg## listed in this file.  A plain '-MD' option would be understood by many
363f33d31a3Smrg## compilers, so we must ensure this comes after the gcc and icc options.
364f33d31a3Smrgpgcc)
365f33d31a3Smrg  # Portland's C compiler understands '-MD'.
366f33d31a3Smrg  # Will always output deps to 'file.d' where file is the root name of the
367f33d31a3Smrg  # source file under compilation, even if file resides in a subdirectory.
368f33d31a3Smrg  # The object file name does not affect the name of the '.d' file.
369f33d31a3Smrg  # pgcc 10.2 will output
370f33d31a3Smrg  #    foo.o: sub/foo.c sub/foo.h
371f33d31a3Smrg  # and will wrap long lines using '\' :
372f33d31a3Smrg  #    foo.o: sub/foo.c ... \
373f33d31a3Smrg  #     sub/foo.h ... \
374f33d31a3Smrg  #     ...
375f33d31a3Smrg  set_dir_from "$object"
376f33d31a3Smrg  # Use the source, not the object, to determine the base name, since
377f33d31a3Smrg  # that's sadly what pgcc will do too.
378f33d31a3Smrg  set_base_from "$source"
379f33d31a3Smrg  tmpdepfile=$base.d
380f33d31a3Smrg
381f33d31a3Smrg  # For projects that build the same source file twice into different object
382f33d31a3Smrg  # files, the pgcc approach of using the *source* file root name can cause
383f33d31a3Smrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
384f33d31a3Smrg  # the same $tmpdepfile.
385f33d31a3Smrg  lockdir=$base.d-lock
386f33d31a3Smrg  trap "
387f33d31a3Smrg    echo '$0: caught signal, cleaning up...' >&2
388f33d31a3Smrg    rmdir '$lockdir'
389f33d31a3Smrg    exit 1
390f33d31a3Smrg  " 1 2 13 15
391f33d31a3Smrg  numtries=100
392f33d31a3Smrg  i=$numtries
393f33d31a3Smrg  while test $i -gt 0; do
394f33d31a3Smrg    # mkdir is a portable test-and-set.
395f33d31a3Smrg    if mkdir "$lockdir" 2>/dev/null; then
396f33d31a3Smrg      # This process acquired the lock.
397f33d31a3Smrg      "$@" -MD
398f33d31a3Smrg      stat=$?
399f33d31a3Smrg      # Release the lock.
400f33d31a3Smrg      rmdir "$lockdir"
401f33d31a3Smrg      break
402f33d31a3Smrg    else
403f33d31a3Smrg      # If the lock is being held by a different process, wait
404f33d31a3Smrg      # until the winning process is done or we timeout.
405f33d31a3Smrg      while test -d "$lockdir" && test $i -gt 0; do
406f33d31a3Smrg        sleep 1
407f33d31a3Smrg        i=`expr $i - 1`
408f33d31a3Smrg      done
409f33d31a3Smrg    fi
410f33d31a3Smrg    i=`expr $i - 1`
411f33d31a3Smrg  done
412f33d31a3Smrg  trap - 1 2 13 15
413f33d31a3Smrg  if test $i -le 0; then
414f33d31a3Smrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
415f33d31a3Smrg    echo "$0: check lockdir '$lockdir'" >&2
416f33d31a3Smrg    exit 1
417f33d31a3Smrg  fi
418f33d31a3Smrg
419f33d31a3Smrg  if test $stat -ne 0; then
420f33d31a3Smrg    rm -f "$tmpdepfile"
421f33d31a3Smrg    exit $stat
422f33d31a3Smrg  fi
423f33d31a3Smrg  rm -f "$depfile"
424f33d31a3Smrg  # Each line is of the form `foo.o: dependent.h',
425f33d31a3Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
426f33d31a3Smrg  # Do two passes, one to just change these to
427f33d31a3Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
428f33d31a3Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
429f33d31a3Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
430f33d31a3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
431f33d31a3Smrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
432f33d31a3Smrg    | sed -e 's/$/ :/' >> "$depfile"
433f33d31a3Smrg  rm -f "$tmpdepfile"
434f33d31a3Smrg  ;;
435f33d31a3Smrg
436f33d31a3Smrghp2)
437f33d31a3Smrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
438f33d31a3Smrg  # compilers, which have integrated preprocessors.  The correct option
439f33d31a3Smrg  # to use with these is +Maked; it writes dependencies to a file named
440f33d31a3Smrg  # 'foo.d', which lands next to the object file, wherever that
441f33d31a3Smrg  # happens to be.
442f33d31a3Smrg  # Much of this is similar to the tru64 case; see comments there.
443f33d31a3Smrg  set_dir_from  "$object"
444f33d31a3Smrg  set_base_from "$object"
445f33d31a3Smrg  if test "$libtool" = yes; then
446f33d31a3Smrg    tmpdepfile1=$dir$base.d
447f33d31a3Smrg    tmpdepfile2=$dir.libs/$base.d
448f33d31a3Smrg    "$@" -Wc,+Maked
449f33d31a3Smrg  else
450f33d31a3Smrg    tmpdepfile1=$dir$base.d
451f33d31a3Smrg    tmpdepfile2=$dir$base.d
452f33d31a3Smrg    "$@" +Maked
453f33d31a3Smrg  fi
454f33d31a3Smrg  stat=$?
455f33d31a3Smrg  if test $stat -ne 0; then
456f33d31a3Smrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
457f33d31a3Smrg     exit $stat
458f33d31a3Smrg  fi
459f33d31a3Smrg
460f33d31a3Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
461f33d31a3Smrg  do
462f33d31a3Smrg    test -f "$tmpdepfile" && break
463f33d31a3Smrg  done
464f33d31a3Smrg  if test -f "$tmpdepfile"; then
465f33d31a3Smrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
466f33d31a3Smrg    # Add 'dependent.h:' lines.
467f33d31a3Smrg    sed -ne '2,${
468f33d31a3Smrg               s/^ *//
469f33d31a3Smrg               s/ \\*$//
470f33d31a3Smrg               s/$/:/
471f33d31a3Smrg               p
472f33d31a3Smrg             }' "$tmpdepfile" >> "$depfile"
473f33d31a3Smrg  else
474f33d31a3Smrg    make_dummy_depfile
475f33d31a3Smrg  fi
476f33d31a3Smrg  rm -f "$tmpdepfile" "$tmpdepfile2"
477f33d31a3Smrg  ;;
478f33d31a3Smrg
479f33d31a3Smrgtru64)
480f33d31a3Smrg  # The Tru64 compiler uses -MD to generate dependencies as a side
481f33d31a3Smrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
482f33d31a3Smrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
483f33d31a3Smrg  # dependencies in 'foo.d' instead, so we check for that too.
484f33d31a3Smrg  # Subdirectories are respected.
485f33d31a3Smrg  set_dir_from  "$object"
486f33d31a3Smrg  set_base_from "$object"
487f33d31a3Smrg
488f33d31a3Smrg  if test "$libtool" = yes; then
489f33d31a3Smrg    # Libtool generates 2 separate objects for the 2 libraries.  These
490f33d31a3Smrg    # two compilations output dependencies in $dir.libs/$base.o.d and
491f33d31a3Smrg    # in $dir$base.o.d.  We have to check for both files, because
492f33d31a3Smrg    # one of the two compilations can be disabled.  We should prefer
493f33d31a3Smrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
494f33d31a3Smrg    # automatically cleaned when .libs/ is deleted, while ignoring
495f33d31a3Smrg    # the former would cause a distcleancheck panic.
496f33d31a3Smrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
497f33d31a3Smrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
498f33d31a3Smrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
499f33d31a3Smrg    "$@" -Wc,-MD
500f33d31a3Smrg  else
501f33d31a3Smrg    tmpdepfile1=$dir$base.d
502f33d31a3Smrg    tmpdepfile2=$dir$base.d
503f33d31a3Smrg    tmpdepfile3=$dir$base.d
504f33d31a3Smrg    "$@" -MD
505f33d31a3Smrg  fi
506f33d31a3Smrg
507f33d31a3Smrg  stat=$?
508f33d31a3Smrg  if test $stat -ne 0; then
509f33d31a3Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
510f33d31a3Smrg    exit $stat
511f33d31a3Smrg  fi
512f33d31a3Smrg
513f33d31a3Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
514f33d31a3Smrg  do
515f33d31a3Smrg    test -f "$tmpdepfile" && break
516f33d31a3Smrg  done
517f33d31a3Smrg  # Same post-processing that is required for AIX mode.
518f33d31a3Smrg  aix_post_process_depfile
519f33d31a3Smrg  ;;
520f33d31a3Smrg
521f33d31a3Smrgmsvc7)
522f33d31a3Smrg  if test "$libtool" = yes; then
523f33d31a3Smrg    showIncludes=-Wc,-showIncludes
524f33d31a3Smrg  else
525f33d31a3Smrg    showIncludes=-showIncludes
526f33d31a3Smrg  fi
527f33d31a3Smrg  "$@" $showIncludes > "$tmpdepfile"
528f33d31a3Smrg  stat=$?
529f33d31a3Smrg  grep -v '^Note: including file: ' "$tmpdepfile"
530f33d31a3Smrg  if test $stat -ne 0; then
531f33d31a3Smrg    rm -f "$tmpdepfile"
532f33d31a3Smrg    exit $stat
533f33d31a3Smrg  fi
534f33d31a3Smrg  rm -f "$depfile"
535f33d31a3Smrg  echo "$object : \\" > "$depfile"
536f33d31a3Smrg  # The first sed program below extracts the file names and escapes
537f33d31a3Smrg  # backslashes for cygpath.  The second sed program outputs the file
538f33d31a3Smrg  # name when reading, but also accumulates all include files in the
539f33d31a3Smrg  # hold buffer in order to output them again at the end.  This only
540f33d31a3Smrg  # works with sed implementations that can handle large buffers.
541f33d31a3Smrg  sed < "$tmpdepfile" -n '
542f33d31a3Smrg/^Note: including file:  *\(.*\)/ {
543f33d31a3Smrg  s//\1/
544f33d31a3Smrg  s/\\/\\\\/g
545f33d31a3Smrg  p
546f33d31a3Smrg}' | $cygpath_u | sort -u | sed -n '
547f33d31a3Smrgs/ /\\ /g
548f33d31a3Smrgs/\(.*\)/'"$tab"'\1 \\/p
549f33d31a3Smrgs/.\(.*\) \\/\1:/
550f33d31a3SmrgH
551f33d31a3Smrg$ {
552f33d31a3Smrg  s/.*/'"$tab"'/
553f33d31a3Smrg  G
554f33d31a3Smrg  p
555f33d31a3Smrg}' >> "$depfile"
556f33d31a3Smrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
557f33d31a3Smrg  rm -f "$tmpdepfile"
558f33d31a3Smrg  ;;
559f33d31a3Smrg
560f33d31a3Smrgmsvc7msys)
561f33d31a3Smrg  # This case exists only to let depend.m4 do its work.  It works by
562f33d31a3Smrg  # looking at the text of this script.  This case will never be run,
563f33d31a3Smrg  # since it is checked for above.
564f33d31a3Smrg  exit 1
565f33d31a3Smrg  ;;
566f33d31a3Smrg
567f33d31a3Smrg#nosideeffect)
568f33d31a3Smrg  # This comment above is used by automake to tell side-effect
569f33d31a3Smrg  # dependency tracking mechanisms from slower ones.
570f33d31a3Smrg
571f33d31a3Smrgdashmstdout)
572f33d31a3Smrg  # Important note: in order to support this mode, a compiler *must*
573f33d31a3Smrg  # always write the preprocessed file to stdout, regardless of -o.
574f33d31a3Smrg  "$@" || exit $?
575f33d31a3Smrg
576f33d31a3Smrg  # Remove the call to Libtool.
577f33d31a3Smrg  if test "$libtool" = yes; then
578f33d31a3Smrg    while test "X$1" != 'X--mode=compile'; do
579f33d31a3Smrg      shift
580f33d31a3Smrg    done
581f33d31a3Smrg    shift
582f33d31a3Smrg  fi
583f33d31a3Smrg
584f33d31a3Smrg  # Remove '-o $object'.
585f33d31a3Smrg  IFS=" "
586f33d31a3Smrg  for arg
587f33d31a3Smrg  do
588f33d31a3Smrg    case $arg in
589f33d31a3Smrg    -o)
590f33d31a3Smrg      shift
591f33d31a3Smrg      ;;
592f33d31a3Smrg    $object)
593f33d31a3Smrg      shift
594f33d31a3Smrg      ;;
595f33d31a3Smrg    *)
596f33d31a3Smrg      set fnord "$@" "$arg"
597f33d31a3Smrg      shift # fnord
598f33d31a3Smrg      shift # $arg
599f33d31a3Smrg      ;;
600f33d31a3Smrg    esac
601f33d31a3Smrg  done
602f33d31a3Smrg
603f33d31a3Smrg  test -z "$dashmflag" && dashmflag=-M
604f33d31a3Smrg  # Require at least two characters before searching for ':'
605f33d31a3Smrg  # in the target name.  This is to cope with DOS-style filenames:
606f33d31a3Smrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
607f33d31a3Smrg  "$@" $dashmflag |
608f33d31a3Smrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
609f33d31a3Smrg  rm -f "$depfile"
610f33d31a3Smrg  cat < "$tmpdepfile" > "$depfile"
611f33d31a3Smrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
612f33d31a3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
613f33d31a3Smrg  tr ' ' "$nl" < "$tmpdepfile" \
614f33d31a3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
615f33d31a3Smrg    | sed -e 's/$/ :/' >> "$depfile"
616f33d31a3Smrg  rm -f "$tmpdepfile"
617f33d31a3Smrg  ;;
618f33d31a3Smrg
619f33d31a3SmrgdashXmstdout)
620f33d31a3Smrg  # This case only exists to satisfy depend.m4.  It is never actually
621f33d31a3Smrg  # run, as this mode is specially recognized in the preamble.
622f33d31a3Smrg  exit 1
623f33d31a3Smrg  ;;
624f33d31a3Smrg
625f33d31a3Smrgmakedepend)
626f33d31a3Smrg  "$@" || exit $?
627f33d31a3Smrg  # Remove any Libtool call
628f33d31a3Smrg  if test "$libtool" = yes; then
629f33d31a3Smrg    while test "X$1" != 'X--mode=compile'; do
630f33d31a3Smrg      shift
631f33d31a3Smrg    done
632f33d31a3Smrg    shift
633f33d31a3Smrg  fi
634f33d31a3Smrg  # X makedepend
635f33d31a3Smrg  shift
636f33d31a3Smrg  cleared=no eat=no
637f33d31a3Smrg  for arg
638f33d31a3Smrg  do
639f33d31a3Smrg    case $cleared in
640f33d31a3Smrg    no)
641f33d31a3Smrg      set ""; shift
642f33d31a3Smrg      cleared=yes ;;
643f33d31a3Smrg    esac
644f33d31a3Smrg    if test $eat = yes; then
645f33d31a3Smrg      eat=no
646f33d31a3Smrg      continue
647f33d31a3Smrg    fi
648f33d31a3Smrg    case "$arg" in
649f33d31a3Smrg    -D*|-I*)
650f33d31a3Smrg      set fnord "$@" "$arg"; shift ;;
651f33d31a3Smrg    # Strip any option that makedepend may not understand.  Remove
652f33d31a3Smrg    # the object too, otherwise makedepend will parse it as a source file.
653f33d31a3Smrg    -arch)
654f33d31a3Smrg      eat=yes ;;
655f33d31a3Smrg    -*|$object)
656f33d31a3Smrg      ;;
657f33d31a3Smrg    *)
658f33d31a3Smrg      set fnord "$@" "$arg"; shift ;;
659f33d31a3Smrg    esac
660f33d31a3Smrg  done
661f33d31a3Smrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
662f33d31a3Smrg  touch "$tmpdepfile"
663f33d31a3Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
664f33d31a3Smrg  rm -f "$depfile"
665f33d31a3Smrg  # makedepend may prepend the VPATH from the source file name to the object.
666f33d31a3Smrg  # No need to regex-escape $object, excess matching of '.' is harmless.
667f33d31a3Smrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
668f33d31a3Smrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
669f33d31a3Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
670f33d31a3Smrg  sed '1,2d' "$tmpdepfile" \
671f33d31a3Smrg    | tr ' ' "$nl" \
672f33d31a3Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
673f33d31a3Smrg    | sed -e 's/$/ :/' >> "$depfile"
674f33d31a3Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
675f33d31a3Smrg  ;;
676f33d31a3Smrg
677f33d31a3Smrgcpp)
678f33d31a3Smrg  # Important note: in order to support this mode, a compiler *must*
679f33d31a3Smrg  # always write the preprocessed file to stdout.
680f33d31a3Smrg  "$@" || exit $?
681f33d31a3Smrg
682f33d31a3Smrg  # Remove the call to Libtool.
683f33d31a3Smrg  if test "$libtool" = yes; then
684f33d31a3Smrg    while test "X$1" != 'X--mode=compile'; do
685f33d31a3Smrg      shift
686f33d31a3Smrg    done
687f33d31a3Smrg    shift
688f33d31a3Smrg  fi
689f33d31a3Smrg
690f33d31a3Smrg  # Remove '-o $object'.
691f33d31a3Smrg  IFS=" "
692f33d31a3Smrg  for arg
693f33d31a3Smrg  do
694f33d31a3Smrg    case $arg in
695f33d31a3Smrg    -o)
696f33d31a3Smrg      shift
697f33d31a3Smrg      ;;
698f33d31a3Smrg    $object)
699f33d31a3Smrg      shift
700f33d31a3Smrg      ;;
701f33d31a3Smrg    *)
702f33d31a3Smrg      set fnord "$@" "$arg"
703f33d31a3Smrg      shift # fnord
704f33d31a3Smrg      shift # $arg
705f33d31a3Smrg      ;;
706f33d31a3Smrg    esac
707f33d31a3Smrg  done
708f33d31a3Smrg
709f33d31a3Smrg  "$@" -E \
710f33d31a3Smrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711f33d31a3Smrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
712f33d31a3Smrg    | sed '$ s: \\$::' > "$tmpdepfile"
713f33d31a3Smrg  rm -f "$depfile"
714f33d31a3Smrg  echo "$object : \\" > "$depfile"
715f33d31a3Smrg  cat < "$tmpdepfile" >> "$depfile"
716f33d31a3Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
717f33d31a3Smrg  rm -f "$tmpdepfile"
718f33d31a3Smrg  ;;
719f33d31a3Smrg
720f33d31a3Smrgmsvisualcpp)
721f33d31a3Smrg  # Important note: in order to support this mode, a compiler *must*
722f33d31a3Smrg  # always write the preprocessed file to stdout.
723f33d31a3Smrg  "$@" || exit $?
724f33d31a3Smrg
725f33d31a3Smrg  # Remove the call to Libtool.
726f33d31a3Smrg  if test "$libtool" = yes; then
727f33d31a3Smrg    while test "X$1" != 'X--mode=compile'; do
728f33d31a3Smrg      shift
729f33d31a3Smrg    done
730f33d31a3Smrg    shift
731f33d31a3Smrg  fi
732f33d31a3Smrg
733f33d31a3Smrg  IFS=" "
734f33d31a3Smrg  for arg
735f33d31a3Smrg  do
736f33d31a3Smrg    case "$arg" in
737f33d31a3Smrg    -o)
738f33d31a3Smrg      shift
739f33d31a3Smrg      ;;
740f33d31a3Smrg    $object)
741f33d31a3Smrg      shift
742f33d31a3Smrg      ;;
743f33d31a3Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
744f33d31a3Smrg        set fnord "$@"
745f33d31a3Smrg        shift
746f33d31a3Smrg        shift
747f33d31a3Smrg        ;;
748f33d31a3Smrg    *)
749f33d31a3Smrg        set fnord "$@" "$arg"
750f33d31a3Smrg        shift
751f33d31a3Smrg        shift
752f33d31a3Smrg        ;;
753f33d31a3Smrg    esac
754f33d31a3Smrg  done
755f33d31a3Smrg  "$@" -E 2>/dev/null |
756f33d31a3Smrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
757f33d31a3Smrg  rm -f "$depfile"
758f33d31a3Smrg  echo "$object : \\" > "$depfile"
759f33d31a3Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
760f33d31a3Smrg  echo "$tab" >> "$depfile"
761f33d31a3Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
762f33d31a3Smrg  rm -f "$tmpdepfile"
763f33d31a3Smrg  ;;
764f33d31a3Smrg
765f33d31a3Smrgmsvcmsys)
766f33d31a3Smrg  # This case exists only to let depend.m4 do its work.  It works by
767f33d31a3Smrg  # looking at the text of this script.  This case will never be run,
768f33d31a3Smrg  # since it is checked for above.
769f33d31a3Smrg  exit 1
770f33d31a3Smrg  ;;
771f33d31a3Smrg
772f33d31a3Smrgnone)
773f33d31a3Smrg  exec "$@"
774f33d31a3Smrg  ;;
775f33d31a3Smrg
776f33d31a3Smrg*)
777f33d31a3Smrg  echo "Unknown depmode $depmode" 1>&2
778f33d31a3Smrg  exit 1
779f33d31a3Smrg  ;;
780f33d31a3Smrgesac
781f33d31a3Smrg
782f33d31a3Smrgexit 0
783f33d31a3Smrg
784f33d31a3Smrg# Local Variables:
785f33d31a3Smrg# mode: shell-script
786f33d31a3Smrg# sh-indentation: 2
7879f1d9476Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
788f33d31a3Smrg# time-stamp-start: "scriptversion="
789f33d31a3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
7909f1d9476Smrg# time-stamp-time-zone: "UTC0"
791f33d31a3Smrg# time-stamp-end: "; # UTC"
792f33d31a3Smrg# End:
793