1fc89c0fbSmrg#! /bin/sh
2fc89c0fbSmrg# depcomp - compile a program generating dependencies as side-effects
3fc89c0fbSmrg
4c8df0c59Smrgscriptversion=2024-06-19.01; # UTC
5fc89c0fbSmrg
6c8df0c59Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
7fc89c0fbSmrg
8fc89c0fbSmrg# This program is free software; you can redistribute it and/or modify
9fc89c0fbSmrg# it under the terms of the GNU General Public License as published by
10fc89c0fbSmrg# the Free Software Foundation; either version 2, or (at your option)
11fc89c0fbSmrg# any later version.
12fc89c0fbSmrg
13fc89c0fbSmrg# This program is distributed in the hope that it will be useful,
14fc89c0fbSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15fc89c0fbSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16fc89c0fbSmrg# GNU General Public License for more details.
17fc89c0fbSmrg
18fc89c0fbSmrg# You should have received a copy of the GNU General Public License
19f9c28e31Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20fc89c0fbSmrg
21fc89c0fbSmrg# As a special exception to the GNU General Public License, if you
22fc89c0fbSmrg# distribute this file as part of a program that contains a
23fc89c0fbSmrg# configuration script generated by Autoconf, you may include it under
24fc89c0fbSmrg# the same distribution terms that you use for the rest of that program.
25fc89c0fbSmrg
26fc89c0fbSmrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
27fc89c0fbSmrg
28fc89c0fbSmrgcase $1 in
29fc89c0fbSmrg  '')
30576bbdfcSmrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31576bbdfcSmrg    exit 1;
32576bbdfcSmrg    ;;
33fc89c0fbSmrg  -h | --h*)
34fc89c0fbSmrg    cat <<\EOF
35fc89c0fbSmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
36fc89c0fbSmrg
37fc89c0fbSmrgRun PROGRAMS ARGS to compile a file, generating dependencies
38fc89c0fbSmrgas side-effects.
39fc89c0fbSmrg
40fc89c0fbSmrgEnvironment variables:
41fc89c0fbSmrg  depmode     Dependency tracking mode.
42576bbdfcSmrg  source      Source file read by 'PROGRAMS ARGS'.
43576bbdfcSmrg  object      Object file output by 'PROGRAMS ARGS'.
44fc89c0fbSmrg  DEPDIR      directory where to store dependencies.
45fc89c0fbSmrg  depfile     Dependency file to output.
46576bbdfcSmrg  tmpdepfile  Temporary file to use when outputting dependencies.
47fc89c0fbSmrg  libtool     Whether libtool is used (yes/no).
48fc89c0fbSmrg
49fc89c0fbSmrgReport bugs to <bug-automake@gnu.org>.
50c8df0c59SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
51c8df0c59SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
52fc89c0fbSmrgEOF
53fc89c0fbSmrg    exit $?
54fc89c0fbSmrg    ;;
55fc89c0fbSmrg  -v | --v*)
56c8df0c59Smrg    echo "depcomp (GNU Automake) $scriptversion"
57fc89c0fbSmrg    exit $?
58fc89c0fbSmrg    ;;
59fc89c0fbSmrgesac
60fc89c0fbSmrg
61576bbdfcSmrg# Get the directory component of the given path, and save it in the
62576bbdfcSmrg# global variables '$dir'.  Note that this directory component will
63576bbdfcSmrg# be either empty or ending with a '/' character.  This is deliberate.
64576bbdfcSmrgset_dir_from ()
65576bbdfcSmrg{
66576bbdfcSmrg  case $1 in
67576bbdfcSmrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
68576bbdfcSmrg      *) dir=;;
69576bbdfcSmrg  esac
70576bbdfcSmrg}
71576bbdfcSmrg
72576bbdfcSmrg# Get the suffix-stripped basename of the given path, and save it the
73576bbdfcSmrg# global variable '$base'.
74576bbdfcSmrgset_base_from ()
75576bbdfcSmrg{
76576bbdfcSmrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
77576bbdfcSmrg}
78576bbdfcSmrg
79576bbdfcSmrg# If no dependency file was actually created by the compiler invocation,
80576bbdfcSmrg# we still have to create a dummy depfile, to avoid errors with the
81576bbdfcSmrg# Makefile "include basename.Plo" scheme.
82576bbdfcSmrgmake_dummy_depfile ()
83576bbdfcSmrg{
84576bbdfcSmrg  echo "#dummy" > "$depfile"
85576bbdfcSmrg}
86576bbdfcSmrg
87576bbdfcSmrg# Factor out some common post-processing of the generated depfile.
88576bbdfcSmrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
89576bbdfcSmrgaix_post_process_depfile ()
90576bbdfcSmrg{
91576bbdfcSmrg  # If the compiler actually managed to produce a dependency file,
92576bbdfcSmrg  # post-process it.
93576bbdfcSmrg  if test -f "$tmpdepfile"; then
94576bbdfcSmrg    # Each line is of the form 'foo.o: dependency.h'.
95576bbdfcSmrg    # Do two passes, one to just change these to
96576bbdfcSmrg    #   $object: dependency.h
97576bbdfcSmrg    # and one to simply output
98576bbdfcSmrg    #   dependency.h:
99576bbdfcSmrg    # which is needed to avoid the deleted-header problem.
100576bbdfcSmrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
101576bbdfcSmrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
102576bbdfcSmrg    } > "$depfile"
103576bbdfcSmrg    rm -f "$tmpdepfile"
104576bbdfcSmrg  else
105576bbdfcSmrg    make_dummy_depfile
106576bbdfcSmrg  fi
107576bbdfcSmrg}
108576bbdfcSmrg
109576bbdfcSmrg# A tabulation character.
110576bbdfcSmrgtab='	'
111576bbdfcSmrg# A newline character.
112576bbdfcSmrgnl='
113576bbdfcSmrg'
114576bbdfcSmrg# Character ranges might be problematic outside the C locale.
115576bbdfcSmrg# These definitions help.
116576bbdfcSmrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
117576bbdfcSmrglower=abcdefghijklmnopqrstuvwxyz
118576bbdfcSmrgalpha=${upper}${lower}
119576bbdfcSmrg
120fc89c0fbSmrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
121fc89c0fbSmrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
122fc89c0fbSmrg  exit 1
123fc89c0fbSmrgfi
124fc89c0fbSmrg
125fc89c0fbSmrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
126fc89c0fbSmrgdepfile=${depfile-`echo "$object" |
127fc89c0fbSmrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
128fc89c0fbSmrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
129fc89c0fbSmrg
130fc89c0fbSmrgrm -f "$tmpdepfile"
131fc89c0fbSmrg
132c8df0c59Smrg# Avoid interference from the environment.
133576bbdfcSmrggccflag= dashmflag=
134576bbdfcSmrg
135fc89c0fbSmrg# Some modes work just like other modes, but use different flags.  We
136fc89c0fbSmrg# parameterize here, but still list the modes in the big case below,
137fc89c0fbSmrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
138fc89c0fbSmrg# here, because this file can only contain one case statement.
139fc89c0fbSmrgif test "$depmode" = hp; then
140fc89c0fbSmrg  # HP compiler uses -M and no extra arg.
141fc89c0fbSmrg  gccflag=-M
142fc89c0fbSmrg  depmode=gcc
143fc89c0fbSmrgfi
144fc89c0fbSmrg
145fc89c0fbSmrgif test "$depmode" = dashXmstdout; then
146576bbdfcSmrg  # This is just like dashmstdout with a different argument.
147576bbdfcSmrg  dashmflag=-xM
148576bbdfcSmrg  depmode=dashmstdout
149fc89c0fbSmrgfi
150fc89c0fbSmrg
15191ec45ceSmrgcygpath_u="cygpath -u -f -"
15291ec45ceSmrgif test "$depmode" = msvcmsys; then
153576bbdfcSmrg  # This is just like msvisualcpp but w/o cygpath translation.
154576bbdfcSmrg  # Just convert the backslash-escaped backslashes to single forward
155576bbdfcSmrg  # slashes to satisfy depend.m4
156576bbdfcSmrg  cygpath_u='sed s,\\\\,/,g'
157576bbdfcSmrg  depmode=msvisualcpp
158576bbdfcSmrgfi
159576bbdfcSmrg
160576bbdfcSmrgif test "$depmode" = msvc7msys; then
161576bbdfcSmrg  # This is just like msvc7 but w/o cygpath translation.
162576bbdfcSmrg  # Just convert the backslash-escaped backslashes to single forward
163576bbdfcSmrg  # slashes to satisfy depend.m4
164576bbdfcSmrg  cygpath_u='sed s,\\\\,/,g'
165576bbdfcSmrg  depmode=msvc7
166576bbdfcSmrgfi
167576bbdfcSmrg
168576bbdfcSmrgif test "$depmode" = xlc; then
169576bbdfcSmrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
170576bbdfcSmrg  gccflag=-qmakedep=gcc,-MF
171576bbdfcSmrg  depmode=gcc
17291ec45ceSmrgfi
17391ec45ceSmrg
174fc89c0fbSmrgcase "$depmode" in
175fc89c0fbSmrggcc3)
176fc89c0fbSmrg## gcc 3 implements dependency tracking that does exactly what
177fc89c0fbSmrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
178fc89c0fbSmrg## it if -MD -MP comes after the -MF stuff.  Hmm.
179fc89c0fbSmrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
180fc89c0fbSmrg## the command line argument order; so add the flags where they
181fc89c0fbSmrg## appear in depend2.am.  Note that the slowdown incurred here
182fc89c0fbSmrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
183fc89c0fbSmrg  for arg
184fc89c0fbSmrg  do
185fc89c0fbSmrg    case $arg in
186fc89c0fbSmrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
187fc89c0fbSmrg    *)  set fnord "$@" "$arg" ;;
188fc89c0fbSmrg    esac
189fc89c0fbSmrg    shift # fnord
190fc89c0fbSmrg    shift # $arg
191fc89c0fbSmrg  done
192fc89c0fbSmrg  "$@"
193fc89c0fbSmrg  stat=$?
194576bbdfcSmrg  if test $stat -ne 0; then
195fc89c0fbSmrg    rm -f "$tmpdepfile"
196fc89c0fbSmrg    exit $stat
197fc89c0fbSmrg  fi
198fc89c0fbSmrg  mv "$tmpdepfile" "$depfile"
199fc89c0fbSmrg  ;;
200fc89c0fbSmrg
201fc89c0fbSmrggcc)
202c8df0c59Smrg## Note that this doesn't just cater to obsolete pre-3.x GCC compilers.
203c8df0c59Smrg## but also to in-use compilers like IBM xlc/xlC and the HP C compiler.
204576bbdfcSmrg## (see the conditional assignment to $gccflag above).
205fc89c0fbSmrg## There are various ways to get dependency output from gcc.  Here's
206fc89c0fbSmrg## why we pick this rather obscure method:
207fc89c0fbSmrg## - Don't want to use -MD because we'd like the dependencies to end
208fc89c0fbSmrg##   up in a subdir.  Having to rename by hand is ugly.
209fc89c0fbSmrg##   (We might end up doing this anyway to support other compilers.)
210fc89c0fbSmrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
211576bbdfcSmrg##   -MM, not -M (despite what the docs say).  Also, it might not be
212576bbdfcSmrg##   supported by the other compilers which use the 'gcc' depmode.
213fc89c0fbSmrg## - Using -M directly means running the compiler twice (even worse
214fc89c0fbSmrg##   than renaming).
215fc89c0fbSmrg  if test -z "$gccflag"; then
216fc89c0fbSmrg    gccflag=-MD,
217fc89c0fbSmrg  fi
218fc89c0fbSmrg  "$@" -Wp,"$gccflag$tmpdepfile"
219fc89c0fbSmrg  stat=$?
220576bbdfcSmrg  if test $stat -ne 0; then
221fc89c0fbSmrg    rm -f "$tmpdepfile"
222fc89c0fbSmrg    exit $stat
223fc89c0fbSmrg  fi
224fc89c0fbSmrg  rm -f "$depfile"
225fc89c0fbSmrg  echo "$object : \\" > "$depfile"
226576bbdfcSmrg  # The second -e expression handles DOS-style file names with drive
227576bbdfcSmrg  # letters.
228fc89c0fbSmrg  sed -e 's/^[^:]*: / /' \
229fc89c0fbSmrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
230576bbdfcSmrg## This next piece of magic avoids the "deleted header file" problem.
231fc89c0fbSmrg## The problem is that when a header file which appears in a .P file
232fc89c0fbSmrg## is deleted, the dependency causes make to die (because there is
233fc89c0fbSmrg## typically no way to rebuild the header).  We avoid this by adding
234fc89c0fbSmrg## dummy dependencies for each header file.  Too bad gcc doesn't do
235fc89c0fbSmrg## this for us directly.
236576bbdfcSmrg## Some versions of gcc put a space before the ':'.  On the theory
237fc89c0fbSmrg## that the space means something, we add a space to the output as
238576bbdfcSmrg## well.  hp depmode also adds that space, but also prefixes the VPATH
239576bbdfcSmrg## to the object.  Take care to not repeat it in the output.
240fc89c0fbSmrg## Some versions of the HPUX 10.20 sed can't process this invocation
241fc89c0fbSmrg## correctly.  Breaking it into two sed invocations is a workaround.
242576bbdfcSmrg  tr ' ' "$nl" < "$tmpdepfile" \
243576bbdfcSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
244576bbdfcSmrg    | sed -e 's/$/ :/' >> "$depfile"
245fc89c0fbSmrg  rm -f "$tmpdepfile"
246fc89c0fbSmrg  ;;
247fc89c0fbSmrg
248fc89c0fbSmrghp)
249fc89c0fbSmrg  # This case exists only to let depend.m4 do its work.  It works by
250fc89c0fbSmrg  # looking at the text of this script.  This case will never be run,
251fc89c0fbSmrg  # since it is checked for above.
252fc89c0fbSmrg  exit 1
253fc89c0fbSmrg  ;;
254fc89c0fbSmrg
255fc89c0fbSmrgsgi)
256fc89c0fbSmrg  if test "$libtool" = yes; then
257fc89c0fbSmrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
258fc89c0fbSmrg  else
259fc89c0fbSmrg    "$@" -MDupdate "$tmpdepfile"
260fc89c0fbSmrg  fi
261fc89c0fbSmrg  stat=$?
262576bbdfcSmrg  if test $stat -ne 0; then
263fc89c0fbSmrg    rm -f "$tmpdepfile"
264fc89c0fbSmrg    exit $stat
265fc89c0fbSmrg  fi
266fc89c0fbSmrg  rm -f "$depfile"
267fc89c0fbSmrg
268fc89c0fbSmrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
269fc89c0fbSmrg    echo "$object : \\" > "$depfile"
270fc89c0fbSmrg    # Clip off the initial element (the dependent).  Don't try to be
271fc89c0fbSmrg    # clever and replace this with sed code, as IRIX sed won't handle
272fc89c0fbSmrg    # lines with more than a fixed number of characters (4096 in
273fc89c0fbSmrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
274576bbdfcSmrg    # the IRIX cc adds comments like '#:fec' to the end of the
275fc89c0fbSmrg    # dependency line.
276576bbdfcSmrg    tr ' ' "$nl" < "$tmpdepfile" \
277576bbdfcSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
278576bbdfcSmrg      | tr "$nl" ' ' >> "$depfile"
27991ec45ceSmrg    echo >> "$depfile"
280fc89c0fbSmrg    # The second pass generates a dummy entry for each header file.
281576bbdfcSmrg    tr ' ' "$nl" < "$tmpdepfile" \
282576bbdfcSmrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
283576bbdfcSmrg      >> "$depfile"
284fc89c0fbSmrg  else
285576bbdfcSmrg    make_dummy_depfile
286fc89c0fbSmrg  fi
287fc89c0fbSmrg  rm -f "$tmpdepfile"
288fc89c0fbSmrg  ;;
289fc89c0fbSmrg
290576bbdfcSmrgxlc)
291576bbdfcSmrg  # This case exists only to let depend.m4 do its work.  It works by
292576bbdfcSmrg  # looking at the text of this script.  This case will never be run,
293576bbdfcSmrg  # since it is checked for above.
294576bbdfcSmrg  exit 1
295576bbdfcSmrg  ;;
296576bbdfcSmrg
297fc89c0fbSmrgaix)
298fc89c0fbSmrg  # The C for AIX Compiler uses -M and outputs the dependencies
299fc89c0fbSmrg  # in a .u file.  In older versions, this file always lives in the
300576bbdfcSmrg  # current directory.  Also, the AIX compiler puts '$object:' at the
301fc89c0fbSmrg  # start of each line; $object doesn't have directory information.
302fc89c0fbSmrg  # Version 6 uses the directory in both cases.
303576bbdfcSmrg  set_dir_from "$object"
304576bbdfcSmrg  set_base_from "$object"
305fc89c0fbSmrg  if test "$libtool" = yes; then
30691ec45ceSmrg    tmpdepfile1=$dir$base.u
30791ec45ceSmrg    tmpdepfile2=$base.u
30891ec45ceSmrg    tmpdepfile3=$dir.libs/$base.u
309fc89c0fbSmrg    "$@" -Wc,-M
310fc89c0fbSmrg  else
31191ec45ceSmrg    tmpdepfile1=$dir$base.u
31291ec45ceSmrg    tmpdepfile2=$dir$base.u
31391ec45ceSmrg    tmpdepfile3=$dir$base.u
314fc89c0fbSmrg    "$@" -M
315fc89c0fbSmrg  fi
316fc89c0fbSmrg  stat=$?
317576bbdfcSmrg  if test $stat -ne 0; then
31891ec45ceSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
319fc89c0fbSmrg    exit $stat
320fc89c0fbSmrg  fi
321fc89c0fbSmrg
32291ec45ceSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
32391ec45ceSmrg  do
32491ec45ceSmrg    test -f "$tmpdepfile" && break
32591ec45ceSmrg  done
326576bbdfcSmrg  aix_post_process_depfile
327576bbdfcSmrg  ;;
328576bbdfcSmrg
329576bbdfcSmrgtcc)
330576bbdfcSmrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
331576bbdfcSmrg  # FIXME: That version still under development at the moment of writing.
332576bbdfcSmrg  #        Make that this statement remains true also for stable, released
333576bbdfcSmrg  #        versions.
334576bbdfcSmrg  # It will wrap lines (doesn't matter whether long or short) with a
335576bbdfcSmrg  # trailing '\', as in:
336576bbdfcSmrg  #
337576bbdfcSmrg  #   foo.o : \
338576bbdfcSmrg  #    foo.c \
339576bbdfcSmrg  #    foo.h \
340576bbdfcSmrg  #
341576bbdfcSmrg  # It will put a trailing '\' even on the last line, and will use leading
342576bbdfcSmrg  # spaces rather than leading tabs (at least since its commit 0394caf7
343576bbdfcSmrg  # "Emit spaces for -MD").
344576bbdfcSmrg  "$@" -MD -MF "$tmpdepfile"
345576bbdfcSmrg  stat=$?
346576bbdfcSmrg  if test $stat -ne 0; then
347576bbdfcSmrg    rm -f "$tmpdepfile"
348576bbdfcSmrg    exit $stat
349fc89c0fbSmrg  fi
350576bbdfcSmrg  rm -f "$depfile"
351576bbdfcSmrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
352576bbdfcSmrg  # We have to change lines of the first kind to '$object: \'.
353576bbdfcSmrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
354576bbdfcSmrg  # And for each line of the second kind, we have to emit a 'dep.h:'
355576bbdfcSmrg  # dummy dependency, to avoid the deleted-header problem.
356576bbdfcSmrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
357fc89c0fbSmrg  rm -f "$tmpdepfile"
358fc89c0fbSmrg  ;;
359fc89c0fbSmrg
360576bbdfcSmrg## The order of this option in the case statement is important, since the
361576bbdfcSmrg## shell code in configure will try each of these formats in the order
362576bbdfcSmrg## listed in this file.  A plain '-MD' option would be understood by many
363576bbdfcSmrg## compilers, so we must ensure this comes after the gcc and icc options.
364576bbdfcSmrgpgcc)
365576bbdfcSmrg  # Portland's C compiler understands '-MD'.
366576bbdfcSmrg  # Will always output deps to 'file.d' where file is the root name of the
367576bbdfcSmrg  # source file under compilation, even if file resides in a subdirectory.
368576bbdfcSmrg  # The object file name does not affect the name of the '.d' file.
369576bbdfcSmrg  # pgcc 10.2 will output
370fc89c0fbSmrg  #    foo.o: sub/foo.c sub/foo.h
371576bbdfcSmrg  # and will wrap long lines using '\' :
372fc89c0fbSmrg  #    foo.o: sub/foo.c ... \
373fc89c0fbSmrg  #     sub/foo.h ... \
374fc89c0fbSmrg  #     ...
375576bbdfcSmrg  set_dir_from "$object"
376576bbdfcSmrg  # Use the source, not the object, to determine the base name, since
377576bbdfcSmrg  # that's sadly what pgcc will do too.
378576bbdfcSmrg  set_base_from "$source"
379576bbdfcSmrg  tmpdepfile=$base.d
380576bbdfcSmrg
381576bbdfcSmrg  # For projects that build the same source file twice into different object
382576bbdfcSmrg  # files, the pgcc approach of using the *source* file root name can cause
383576bbdfcSmrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
384576bbdfcSmrg  # the same $tmpdepfile.
385576bbdfcSmrg  lockdir=$base.d-lock
386576bbdfcSmrg  trap "
387576bbdfcSmrg    echo '$0: caught signal, cleaning up...' >&2
388576bbdfcSmrg    rmdir '$lockdir'
389576bbdfcSmrg    exit 1
390576bbdfcSmrg  " 1 2 13 15
391576bbdfcSmrg  numtries=100
392576bbdfcSmrg  i=$numtries
393576bbdfcSmrg  while test $i -gt 0; do
394576bbdfcSmrg    # mkdir is a portable test-and-set.
395576bbdfcSmrg    if mkdir "$lockdir" 2>/dev/null; then
396576bbdfcSmrg      # This process acquired the lock.
397576bbdfcSmrg      "$@" -MD
398576bbdfcSmrg      stat=$?
399576bbdfcSmrg      # Release the lock.
400576bbdfcSmrg      rmdir "$lockdir"
401576bbdfcSmrg      break
402576bbdfcSmrg    else
403576bbdfcSmrg      # If the lock is being held by a different process, wait
404576bbdfcSmrg      # until the winning process is done or we timeout.
405576bbdfcSmrg      while test -d "$lockdir" && test $i -gt 0; do
406576bbdfcSmrg        sleep 1
407576bbdfcSmrg        i=`expr $i - 1`
408576bbdfcSmrg      done
409576bbdfcSmrg    fi
410576bbdfcSmrg    i=`expr $i - 1`
411576bbdfcSmrg  done
412576bbdfcSmrg  trap - 1 2 13 15
413576bbdfcSmrg  if test $i -le 0; then
414576bbdfcSmrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
415576bbdfcSmrg    echo "$0: check lockdir '$lockdir'" >&2
416576bbdfcSmrg    exit 1
417576bbdfcSmrg  fi
418fc89c0fbSmrg
419576bbdfcSmrg  if test $stat -ne 0; then
420fc89c0fbSmrg    rm -f "$tmpdepfile"
421fc89c0fbSmrg    exit $stat
422fc89c0fbSmrg  fi
423fc89c0fbSmrg  rm -f "$depfile"
424fc89c0fbSmrg  # Each line is of the form `foo.o: dependent.h',
425fc89c0fbSmrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
426fc89c0fbSmrg  # Do two passes, one to just change these to
427fc89c0fbSmrg  # `$object: dependent.h' and one to simply `dependent.h:'.
428fc89c0fbSmrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
429fc89c0fbSmrg  # Some versions of the HPUX 10.20 sed can't process this invocation
430fc89c0fbSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
431576bbdfcSmrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
432576bbdfcSmrg    | sed -e 's/$/ :/' >> "$depfile"
433fc89c0fbSmrg  rm -f "$tmpdepfile"
434fc89c0fbSmrg  ;;
435fc89c0fbSmrg
436fc89c0fbSmrghp2)
437fc89c0fbSmrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
438fc89c0fbSmrg  # compilers, which have integrated preprocessors.  The correct option
439fc89c0fbSmrg  # to use with these is +Maked; it writes dependencies to a file named
440fc89c0fbSmrg  # 'foo.d', which lands next to the object file, wherever that
441fc89c0fbSmrg  # happens to be.
442fc89c0fbSmrg  # Much of this is similar to the tru64 case; see comments there.
443576bbdfcSmrg  set_dir_from  "$object"
444576bbdfcSmrg  set_base_from "$object"
445fc89c0fbSmrg  if test "$libtool" = yes; then
446fc89c0fbSmrg    tmpdepfile1=$dir$base.d
447fc89c0fbSmrg    tmpdepfile2=$dir.libs/$base.d
448fc89c0fbSmrg    "$@" -Wc,+Maked
449fc89c0fbSmrg  else
450fc89c0fbSmrg    tmpdepfile1=$dir$base.d
451fc89c0fbSmrg    tmpdepfile2=$dir$base.d
452fc89c0fbSmrg    "$@" +Maked
453fc89c0fbSmrg  fi
454fc89c0fbSmrg  stat=$?
455576bbdfcSmrg  if test $stat -ne 0; then
456fc89c0fbSmrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
457fc89c0fbSmrg     exit $stat
458fc89c0fbSmrg  fi
459fc89c0fbSmrg
460fc89c0fbSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
461fc89c0fbSmrg  do
462fc89c0fbSmrg    test -f "$tmpdepfile" && break
463fc89c0fbSmrg  done
464fc89c0fbSmrg  if test -f "$tmpdepfile"; then
465576bbdfcSmrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
466576bbdfcSmrg    # Add 'dependent.h:' lines.
46791ec45ceSmrg    sed -ne '2,${
468576bbdfcSmrg               s/^ *//
469576bbdfcSmrg               s/ \\*$//
470576bbdfcSmrg               s/$/:/
471576bbdfcSmrg               p
472576bbdfcSmrg             }' "$tmpdepfile" >> "$depfile"
473fc89c0fbSmrg  else
474576bbdfcSmrg    make_dummy_depfile
475fc89c0fbSmrg  fi
476fc89c0fbSmrg  rm -f "$tmpdepfile" "$tmpdepfile2"
477fc89c0fbSmrg  ;;
478fc89c0fbSmrg
479fc89c0fbSmrgtru64)
480576bbdfcSmrg  # The Tru64 compiler uses -MD to generate dependencies as a side
481576bbdfcSmrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
482576bbdfcSmrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
483576bbdfcSmrg  # dependencies in 'foo.d' instead, so we check for that too.
484576bbdfcSmrg  # Subdirectories are respected.
485576bbdfcSmrg  set_dir_from  "$object"
486576bbdfcSmrg  set_base_from "$object"
487576bbdfcSmrg
488576bbdfcSmrg  if test "$libtool" = yes; then
489576bbdfcSmrg    # Libtool generates 2 separate objects for the 2 libraries.  These
490576bbdfcSmrg    # two compilations output dependencies in $dir.libs/$base.o.d and
491576bbdfcSmrg    # in $dir$base.o.d.  We have to check for both files, because
492576bbdfcSmrg    # one of the two compilations can be disabled.  We should prefer
493576bbdfcSmrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
494576bbdfcSmrg    # automatically cleaned when .libs/ is deleted, while ignoring
495576bbdfcSmrg    # the former would cause a distcleancheck panic.
496576bbdfcSmrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
497576bbdfcSmrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
498576bbdfcSmrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
499576bbdfcSmrg    "$@" -Wc,-MD
500576bbdfcSmrg  else
501576bbdfcSmrg    tmpdepfile1=$dir$base.d
502576bbdfcSmrg    tmpdepfile2=$dir$base.d
503576bbdfcSmrg    tmpdepfile3=$dir$base.d
504576bbdfcSmrg    "$@" -MD
505576bbdfcSmrg  fi
506576bbdfcSmrg
507576bbdfcSmrg  stat=$?
508576bbdfcSmrg  if test $stat -ne 0; then
509576bbdfcSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
510576bbdfcSmrg    exit $stat
511576bbdfcSmrg  fi
512576bbdfcSmrg
513576bbdfcSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
514576bbdfcSmrg  do
515576bbdfcSmrg    test -f "$tmpdepfile" && break
516576bbdfcSmrg  done
517576bbdfcSmrg  # Same post-processing that is required for AIX mode.
518576bbdfcSmrg  aix_post_process_depfile
519576bbdfcSmrg  ;;
520576bbdfcSmrg
521576bbdfcSmrgmsvc7)
522576bbdfcSmrg  if test "$libtool" = yes; then
523576bbdfcSmrg    showIncludes=-Wc,-showIncludes
524576bbdfcSmrg  else
525576bbdfcSmrg    showIncludes=-showIncludes
526576bbdfcSmrg  fi
527576bbdfcSmrg  "$@" $showIncludes > "$tmpdepfile"
528576bbdfcSmrg  stat=$?
529576bbdfcSmrg  grep -v '^Note: including file: ' "$tmpdepfile"
530576bbdfcSmrg  if test $stat -ne 0; then
531576bbdfcSmrg    rm -f "$tmpdepfile"
532576bbdfcSmrg    exit $stat
533576bbdfcSmrg  fi
534576bbdfcSmrg  rm -f "$depfile"
535576bbdfcSmrg  echo "$object : \\" > "$depfile"
536576bbdfcSmrg  # The first sed program below extracts the file names and escapes
537576bbdfcSmrg  # backslashes for cygpath.  The second sed program outputs the file
538576bbdfcSmrg  # name when reading, but also accumulates all include files in the
539576bbdfcSmrg  # hold buffer in order to output them again at the end.  This only
540576bbdfcSmrg  # works with sed implementations that can handle large buffers.
541576bbdfcSmrg  sed < "$tmpdepfile" -n '
542576bbdfcSmrg/^Note: including file:  *\(.*\)/ {
543576bbdfcSmrg  s//\1/
544576bbdfcSmrg  s/\\/\\\\/g
545576bbdfcSmrg  p
546576bbdfcSmrg}' | $cygpath_u | sort -u | sed -n '
547576bbdfcSmrgs/ /\\ /g
548576bbdfcSmrgs/\(.*\)/'"$tab"'\1 \\/p
549576bbdfcSmrgs/.\(.*\) \\/\1:/
550576bbdfcSmrgH
551576bbdfcSmrg$ {
552576bbdfcSmrg  s/.*/'"$tab"'/
553576bbdfcSmrg  G
554576bbdfcSmrg  p
555576bbdfcSmrg}' >> "$depfile"
556576bbdfcSmrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
557576bbdfcSmrg  rm -f "$tmpdepfile"
558576bbdfcSmrg  ;;
559576bbdfcSmrg
560576bbdfcSmrgmsvc7msys)
561576bbdfcSmrg  # This case exists only to let depend.m4 do its work.  It works by
562576bbdfcSmrg  # looking at the text of this script.  This case will never be run,
563576bbdfcSmrg  # since it is checked for above.
564576bbdfcSmrg  exit 1
565576bbdfcSmrg  ;;
566fc89c0fbSmrg
567fc89c0fbSmrg#nosideeffect)
568fc89c0fbSmrg  # This comment above is used by automake to tell side-effect
569fc89c0fbSmrg  # dependency tracking mechanisms from slower ones.
570fc89c0fbSmrg
571fc89c0fbSmrgdashmstdout)
572fc89c0fbSmrg  # Important note: in order to support this mode, a compiler *must*
573fc89c0fbSmrg  # always write the preprocessed file to stdout, regardless of -o.
574fc89c0fbSmrg  "$@" || exit $?
575fc89c0fbSmrg
576fc89c0fbSmrg  # Remove the call to Libtool.
577fc89c0fbSmrg  if test "$libtool" = yes; then
57891ec45ceSmrg    while test "X$1" != 'X--mode=compile'; do
579fc89c0fbSmrg      shift
580fc89c0fbSmrg    done
581fc89c0fbSmrg    shift
582fc89c0fbSmrg  fi
583fc89c0fbSmrg
584576bbdfcSmrg  # Remove '-o $object'.
585fc89c0fbSmrg  IFS=" "
586fc89c0fbSmrg  for arg
587fc89c0fbSmrg  do
588fc89c0fbSmrg    case $arg in
589fc89c0fbSmrg    -o)
590fc89c0fbSmrg      shift
591fc89c0fbSmrg      ;;
592fc89c0fbSmrg    $object)
593fc89c0fbSmrg      shift
594fc89c0fbSmrg      ;;
595fc89c0fbSmrg    *)
596fc89c0fbSmrg      set fnord "$@" "$arg"
597fc89c0fbSmrg      shift # fnord
598fc89c0fbSmrg      shift # $arg
599fc89c0fbSmrg      ;;
600fc89c0fbSmrg    esac
601fc89c0fbSmrg  done
602fc89c0fbSmrg
603fc89c0fbSmrg  test -z "$dashmflag" && dashmflag=-M
604576bbdfcSmrg  # Require at least two characters before searching for ':'
605fc89c0fbSmrg  # in the target name.  This is to cope with DOS-style filenames:
606576bbdfcSmrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
607fc89c0fbSmrg  "$@" $dashmflag |
608576bbdfcSmrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
609fc89c0fbSmrg  rm -f "$depfile"
610fc89c0fbSmrg  cat < "$tmpdepfile" > "$depfile"
611576bbdfcSmrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
612576bbdfcSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
613576bbdfcSmrg  tr ' ' "$nl" < "$tmpdepfile" \
614576bbdfcSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
615576bbdfcSmrg    | sed -e 's/$/ :/' >> "$depfile"
616fc89c0fbSmrg  rm -f "$tmpdepfile"
617fc89c0fbSmrg  ;;
618fc89c0fbSmrg
619fc89c0fbSmrgdashXmstdout)
620fc89c0fbSmrg  # This case only exists to satisfy depend.m4.  It is never actually
621fc89c0fbSmrg  # run, as this mode is specially recognized in the preamble.
622fc89c0fbSmrg  exit 1
623fc89c0fbSmrg  ;;
624fc89c0fbSmrg
625fc89c0fbSmrgmakedepend)
626fc89c0fbSmrg  "$@" || exit $?
627fc89c0fbSmrg  # Remove any Libtool call
628fc89c0fbSmrg  if test "$libtool" = yes; then
62991ec45ceSmrg    while test "X$1" != 'X--mode=compile'; do
630fc89c0fbSmrg      shift
631fc89c0fbSmrg    done
632fc89c0fbSmrg    shift
633fc89c0fbSmrg  fi
634fc89c0fbSmrg  # X makedepend
635fc89c0fbSmrg  shift
63691ec45ceSmrg  cleared=no eat=no
63791ec45ceSmrg  for arg
63891ec45ceSmrg  do
639fc89c0fbSmrg    case $cleared in
640fc89c0fbSmrg    no)
641fc89c0fbSmrg      set ""; shift
642fc89c0fbSmrg      cleared=yes ;;
643fc89c0fbSmrg    esac
64491ec45ceSmrg    if test $eat = yes; then
64591ec45ceSmrg      eat=no
64691ec45ceSmrg      continue
64791ec45ceSmrg    fi
648fc89c0fbSmrg    case "$arg" in
649fc89c0fbSmrg    -D*|-I*)
650fc89c0fbSmrg      set fnord "$@" "$arg"; shift ;;
651fc89c0fbSmrg    # Strip any option that makedepend may not understand.  Remove
652fc89c0fbSmrg    # the object too, otherwise makedepend will parse it as a source file.
65391ec45ceSmrg    -arch)
65491ec45ceSmrg      eat=yes ;;
655fc89c0fbSmrg    -*|$object)
656fc89c0fbSmrg      ;;
657fc89c0fbSmrg    *)
658fc89c0fbSmrg      set fnord "$@" "$arg"; shift ;;
659fc89c0fbSmrg    esac
660fc89c0fbSmrg  done
66191ec45ceSmrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
662fc89c0fbSmrg  touch "$tmpdepfile"
663fc89c0fbSmrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
664fc89c0fbSmrg  rm -f "$depfile"
665576bbdfcSmrg  # makedepend may prepend the VPATH from the source file name to the object.
666576bbdfcSmrg  # No need to regex-escape $object, excess matching of '.' is harmless.
667576bbdfcSmrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
668576bbdfcSmrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
669576bbdfcSmrg  # correctly.  Breaking it into two sed invocations is a workaround.
670576bbdfcSmrg  sed '1,2d' "$tmpdepfile" \
671576bbdfcSmrg    | tr ' ' "$nl" \
672576bbdfcSmrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
673576bbdfcSmrg    | sed -e 's/$/ :/' >> "$depfile"
674fc89c0fbSmrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
675fc89c0fbSmrg  ;;
676fc89c0fbSmrg
677fc89c0fbSmrgcpp)
678fc89c0fbSmrg  # Important note: in order to support this mode, a compiler *must*
679fc89c0fbSmrg  # always write the preprocessed file to stdout.
680fc89c0fbSmrg  "$@" || exit $?
681fc89c0fbSmrg
682fc89c0fbSmrg  # Remove the call to Libtool.
683fc89c0fbSmrg  if test "$libtool" = yes; then
68491ec45ceSmrg    while test "X$1" != 'X--mode=compile'; do
685fc89c0fbSmrg      shift
686fc89c0fbSmrg    done
687fc89c0fbSmrg    shift
688fc89c0fbSmrg  fi
689fc89c0fbSmrg
690576bbdfcSmrg  # Remove '-o $object'.
691fc89c0fbSmrg  IFS=" "
692fc89c0fbSmrg  for arg
693fc89c0fbSmrg  do
694fc89c0fbSmrg    case $arg in
695fc89c0fbSmrg    -o)
696fc89c0fbSmrg      shift
697fc89c0fbSmrg      ;;
698fc89c0fbSmrg    $object)
699fc89c0fbSmrg      shift
700fc89c0fbSmrg      ;;
701fc89c0fbSmrg    *)
702fc89c0fbSmrg      set fnord "$@" "$arg"
703fc89c0fbSmrg      shift # fnord
704fc89c0fbSmrg      shift # $arg
705fc89c0fbSmrg      ;;
706fc89c0fbSmrg    esac
707fc89c0fbSmrg  done
708fc89c0fbSmrg
709576bbdfcSmrg  "$@" -E \
710576bbdfcSmrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711576bbdfcSmrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
712576bbdfcSmrg    | sed '$ s: \\$::' > "$tmpdepfile"
713fc89c0fbSmrg  rm -f "$depfile"
714fc89c0fbSmrg  echo "$object : \\" > "$depfile"
715fc89c0fbSmrg  cat < "$tmpdepfile" >> "$depfile"
716fc89c0fbSmrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
717fc89c0fbSmrg  rm -f "$tmpdepfile"
718fc89c0fbSmrg  ;;
719fc89c0fbSmrg
720fc89c0fbSmrgmsvisualcpp)
721fc89c0fbSmrg  # Important note: in order to support this mode, a compiler *must*
72291ec45ceSmrg  # always write the preprocessed file to stdout.
723fc89c0fbSmrg  "$@" || exit $?
72491ec45ceSmrg
72591ec45ceSmrg  # Remove the call to Libtool.
72691ec45ceSmrg  if test "$libtool" = yes; then
72791ec45ceSmrg    while test "X$1" != 'X--mode=compile'; do
72891ec45ceSmrg      shift
72991ec45ceSmrg    done
73091ec45ceSmrg    shift
73191ec45ceSmrg  fi
73291ec45ceSmrg
733fc89c0fbSmrg  IFS=" "
734fc89c0fbSmrg  for arg
735fc89c0fbSmrg  do
736fc89c0fbSmrg    case "$arg" in
73791ec45ceSmrg    -o)
73891ec45ceSmrg      shift
73991ec45ceSmrg      ;;
74091ec45ceSmrg    $object)
74191ec45ceSmrg      shift
74291ec45ceSmrg      ;;
743fc89c0fbSmrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
744576bbdfcSmrg        set fnord "$@"
745576bbdfcSmrg        shift
746576bbdfcSmrg        shift
747576bbdfcSmrg        ;;
748fc89c0fbSmrg    *)
749576bbdfcSmrg        set fnord "$@" "$arg"
750576bbdfcSmrg        shift
751576bbdfcSmrg        shift
752576bbdfcSmrg        ;;
753fc89c0fbSmrg    esac
754fc89c0fbSmrg  done
75591ec45ceSmrg  "$@" -E 2>/dev/null |
75691ec45ceSmrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
757fc89c0fbSmrg  rm -f "$depfile"
758fc89c0fbSmrg  echo "$object : \\" > "$depfile"
759576bbdfcSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
760576bbdfcSmrg  echo "$tab" >> "$depfile"
76191ec45ceSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
762fc89c0fbSmrg  rm -f "$tmpdepfile"
763fc89c0fbSmrg  ;;
764fc89c0fbSmrg
76591ec45ceSmrgmsvcmsys)
76691ec45ceSmrg  # This case exists only to let depend.m4 do its work.  It works by
76791ec45ceSmrg  # looking at the text of this script.  This case will never be run,
76891ec45ceSmrg  # since it is checked for above.
76991ec45ceSmrg  exit 1
77091ec45ceSmrg  ;;
77191ec45ceSmrg
772fc89c0fbSmrgnone)
773fc89c0fbSmrg  exec "$@"
774fc89c0fbSmrg  ;;
775fc89c0fbSmrg
776fc89c0fbSmrg*)
777fc89c0fbSmrg  echo "Unknown depmode $depmode" 1>&2
778fc89c0fbSmrg  exit 1
779fc89c0fbSmrg  ;;
780fc89c0fbSmrgesac
781fc89c0fbSmrg
782fc89c0fbSmrgexit 0
783fc89c0fbSmrg
784fc89c0fbSmrg# Local Variables:
785fc89c0fbSmrg# mode: shell-script
786fc89c0fbSmrg# sh-indentation: 2
787f9c28e31Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
788fc89c0fbSmrg# time-stamp-start: "scriptversion="
789fc89c0fbSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
790f9c28e31Smrg# time-stamp-time-zone: "UTC0"
79191ec45ceSmrg# time-stamp-end: "; # UTC"
792fc89c0fbSmrg# End:
793