1fdb3d228Smrg#! /bin/sh
2fdb3d228Smrg# depcomp - compile a program generating dependencies as side-effects
39b41ff1aSmrg
474b35aa8Smrgscriptversion=2018-03-07.03; # UTC
59b41ff1aSmrg
674b35aa8Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7fdb3d228Smrg
8fdb3d228Smrg# This program is free software; you can redistribute it and/or modify
9fdb3d228Smrg# it under the terms of the GNU General Public License as published by
10fdb3d228Smrg# the Free Software Foundation; either version 2, or (at your option)
11fdb3d228Smrg# any later version.
12fdb3d228Smrg
13fdb3d228Smrg# This program is distributed in the hope that it will be useful,
14fdb3d228Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15fdb3d228Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16fdb3d228Smrg# GNU General Public License for more details.
17fdb3d228Smrg
18fdb3d228Smrg# You should have received a copy of the GNU General Public License
1974b35aa8Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20fdb3d228Smrg
21fdb3d228Smrg# As a special exception to the GNU General Public License, if you
22fdb3d228Smrg# distribute this file as part of a program that contains a
23fdb3d228Smrg# configuration script generated by Autoconf, you may include it under
24fdb3d228Smrg# the same distribution terms that you use for the rest of that program.
25fdb3d228Smrg
26fdb3d228Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
27fdb3d228Smrg
289b41ff1aSmrgcase $1 in
299b41ff1aSmrg  '')
30585aa3f7Smrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31585aa3f7Smrg    exit 1;
32585aa3f7Smrg    ;;
339b41ff1aSmrg  -h | --h*)
349b41ff1aSmrg    cat <<\EOF
359b41ff1aSmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
369b41ff1aSmrg
379b41ff1aSmrgRun PROGRAMS ARGS to compile a file, generating dependencies
389b41ff1aSmrgas side-effects.
399b41ff1aSmrg
409b41ff1aSmrgEnvironment variables:
419b41ff1aSmrg  depmode     Dependency tracking mode.
42585aa3f7Smrg  source      Source file read by 'PROGRAMS ARGS'.
43585aa3f7Smrg  object      Object file output by 'PROGRAMS ARGS'.
449b41ff1aSmrg  DEPDIR      directory where to store dependencies.
459b41ff1aSmrg  depfile     Dependency file to output.
467d575c90Smrg  tmpdepfile  Temporary file to use when outputting dependencies.
479b41ff1aSmrg  libtool     Whether libtool is used (yes/no).
489b41ff1aSmrg
499b41ff1aSmrgReport bugs to <bug-automake@gnu.org>.
509b41ff1aSmrgEOF
519b41ff1aSmrg    exit $?
529b41ff1aSmrg    ;;
539b41ff1aSmrg  -v | --v*)
549b41ff1aSmrg    echo "depcomp $scriptversion"
559b41ff1aSmrg    exit $?
569b41ff1aSmrg    ;;
579b41ff1aSmrgesac
589b41ff1aSmrg
59585aa3f7Smrg# Get the directory component of the given path, and save it in the
60585aa3f7Smrg# global variables '$dir'.  Note that this directory component will
61585aa3f7Smrg# be either empty or ending with a '/' character.  This is deliberate.
62585aa3f7Smrgset_dir_from ()
63585aa3f7Smrg{
64585aa3f7Smrg  case $1 in
65585aa3f7Smrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
66585aa3f7Smrg      *) dir=;;
67585aa3f7Smrg  esac
68585aa3f7Smrg}
69585aa3f7Smrg
70585aa3f7Smrg# Get the suffix-stripped basename of the given path, and save it the
71585aa3f7Smrg# global variable '$base'.
72585aa3f7Smrgset_base_from ()
73585aa3f7Smrg{
74585aa3f7Smrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
75585aa3f7Smrg}
76585aa3f7Smrg
77585aa3f7Smrg# If no dependency file was actually created by the compiler invocation,
78585aa3f7Smrg# we still have to create a dummy depfile, to avoid errors with the
79585aa3f7Smrg# Makefile "include basename.Plo" scheme.
80585aa3f7Smrgmake_dummy_depfile ()
81585aa3f7Smrg{
82585aa3f7Smrg  echo "#dummy" > "$depfile"
83585aa3f7Smrg}
84585aa3f7Smrg
85585aa3f7Smrg# Factor out some common post-processing of the generated depfile.
86585aa3f7Smrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
87585aa3f7Smrgaix_post_process_depfile ()
88585aa3f7Smrg{
89585aa3f7Smrg  # If the compiler actually managed to produce a dependency file,
90585aa3f7Smrg  # post-process it.
91585aa3f7Smrg  if test -f "$tmpdepfile"; then
92585aa3f7Smrg    # Each line is of the form 'foo.o: dependency.h'.
93585aa3f7Smrg    # Do two passes, one to just change these to
94585aa3f7Smrg    #   $object: dependency.h
95585aa3f7Smrg    # and one to simply output
96585aa3f7Smrg    #   dependency.h:
97585aa3f7Smrg    # which is needed to avoid the deleted-header problem.
98585aa3f7Smrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
99585aa3f7Smrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
100585aa3f7Smrg    } > "$depfile"
101585aa3f7Smrg    rm -f "$tmpdepfile"
102585aa3f7Smrg  else
103585aa3f7Smrg    make_dummy_depfile
104585aa3f7Smrg  fi
105585aa3f7Smrg}
106585aa3f7Smrg
107585aa3f7Smrg# A tabulation character.
108585aa3f7Smrgtab='	'
109585aa3f7Smrg# A newline character.
110585aa3f7Smrgnl='
111585aa3f7Smrg'
112585aa3f7Smrg# Character ranges might be problematic outside the C locale.
113585aa3f7Smrg# These definitions help.
114585aa3f7Smrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
115585aa3f7Smrglower=abcdefghijklmnopqrstuvwxyz
116585aa3f7Smrgdigits=0123456789
117585aa3f7Smrgalpha=${upper}${lower}
118585aa3f7Smrg
119fdb3d228Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
120fdb3d228Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
121fdb3d228Smrg  exit 1
122fdb3d228Smrgfi
123fdb3d228Smrg
1249b41ff1aSmrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
1259b41ff1aSmrgdepfile=${depfile-`echo "$object" |
1269b41ff1aSmrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
127fdb3d228Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
128fdb3d228Smrg
129fdb3d228Smrgrm -f "$tmpdepfile"
130fdb3d228Smrg
131585aa3f7Smrg# Avoid interferences from the environment.
132585aa3f7Smrggccflag= dashmflag=
133585aa3f7Smrg
134fdb3d228Smrg# Some modes work just like other modes, but use different flags.  We
135fdb3d228Smrg# parameterize here, but still list the modes in the big case below,
136fdb3d228Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
137fdb3d228Smrg# here, because this file can only contain one case statement.
138fdb3d228Smrgif test "$depmode" = hp; then
139fdb3d228Smrg  # HP compiler uses -M and no extra arg.
140fdb3d228Smrg  gccflag=-M
141fdb3d228Smrg  depmode=gcc
142fdb3d228Smrgfi
143fdb3d228Smrg
144fdb3d228Smrgif test "$depmode" = dashXmstdout; then
145585aa3f7Smrg  # This is just like dashmstdout with a different argument.
146585aa3f7Smrg  dashmflag=-xM
147585aa3f7Smrg  depmode=dashmstdout
148fdb3d228Smrgfi
149fdb3d228Smrg
1509b41ff1aSmrgcygpath_u="cygpath -u -f -"
1519b41ff1aSmrgif test "$depmode" = msvcmsys; then
152585aa3f7Smrg  # This is just like msvisualcpp but w/o cygpath translation.
153585aa3f7Smrg  # Just convert the backslash-escaped backslashes to single forward
154585aa3f7Smrg  # slashes to satisfy depend.m4
155585aa3f7Smrg  cygpath_u='sed s,\\\\,/,g'
156585aa3f7Smrg  depmode=msvisualcpp
1579b41ff1aSmrgfi
1589b41ff1aSmrg
1597d575c90Smrgif test "$depmode" = msvc7msys; then
160585aa3f7Smrg  # This is just like msvc7 but w/o cygpath translation.
161585aa3f7Smrg  # Just convert the backslash-escaped backslashes to single forward
162585aa3f7Smrg  # slashes to satisfy depend.m4
163585aa3f7Smrg  cygpath_u='sed s,\\\\,/,g'
164585aa3f7Smrg  depmode=msvc7
165585aa3f7Smrgfi
166585aa3f7Smrg
167585aa3f7Smrgif test "$depmode" = xlc; then
168585aa3f7Smrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
169585aa3f7Smrg  gccflag=-qmakedep=gcc,-MF
170585aa3f7Smrg  depmode=gcc
1717d575c90Smrgfi
1727d575c90Smrg
173fdb3d228Smrgcase "$depmode" in
174fdb3d228Smrggcc3)
175fdb3d228Smrg## gcc 3 implements dependency tracking that does exactly what
176fdb3d228Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
177fdb3d228Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
1789b41ff1aSmrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
1799b41ff1aSmrg## the command line argument order; so add the flags where they
1809b41ff1aSmrg## appear in depend2.am.  Note that the slowdown incurred here
1819b41ff1aSmrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
1829b41ff1aSmrg  for arg
1839b41ff1aSmrg  do
1849b41ff1aSmrg    case $arg in
1859b41ff1aSmrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
1869b41ff1aSmrg    *)  set fnord "$@" "$arg" ;;
1879b41ff1aSmrg    esac
1889b41ff1aSmrg    shift # fnord
1899b41ff1aSmrg    shift # $arg
1909b41ff1aSmrg  done
1919b41ff1aSmrg  "$@"
192fdb3d228Smrg  stat=$?
193585aa3f7Smrg  if test $stat -ne 0; then
194fdb3d228Smrg    rm -f "$tmpdepfile"
195fdb3d228Smrg    exit $stat
196fdb3d228Smrg  fi
197fdb3d228Smrg  mv "$tmpdepfile" "$depfile"
198fdb3d228Smrg  ;;
199fdb3d228Smrg
200fdb3d228Smrggcc)
201585aa3f7Smrg## Note that this doesn't just cater to obsosete pre-3.x GCC compilers.
202585aa3f7Smrg## but also to in-use compilers like IMB xlc/xlC and the HP C compiler.
203585aa3f7Smrg## (see the conditional assignment to $gccflag above).
204fdb3d228Smrg## There are various ways to get dependency output from gcc.  Here's
205fdb3d228Smrg## why we pick this rather obscure method:
206fdb3d228Smrg## - Don't want to use -MD because we'd like the dependencies to end
207fdb3d228Smrg##   up in a subdir.  Having to rename by hand is ugly.
208fdb3d228Smrg##   (We might end up doing this anyway to support other compilers.)
209fdb3d228Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
210585aa3f7Smrg##   -MM, not -M (despite what the docs say).  Also, it might not be
211585aa3f7Smrg##   supported by the other compilers which use the 'gcc' depmode.
212fdb3d228Smrg## - Using -M directly means running the compiler twice (even worse
213fdb3d228Smrg##   than renaming).
214fdb3d228Smrg  if test -z "$gccflag"; then
215fdb3d228Smrg    gccflag=-MD,
216fdb3d228Smrg  fi
217fdb3d228Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
218fdb3d228Smrg  stat=$?
219585aa3f7Smrg  if test $stat -ne 0; then
220fdb3d228Smrg    rm -f "$tmpdepfile"
221fdb3d228Smrg    exit $stat
222fdb3d228Smrg  fi
223fdb3d228Smrg  rm -f "$depfile"
224fdb3d228Smrg  echo "$object : \\" > "$depfile"
225585aa3f7Smrg  # The second -e expression handles DOS-style file names with drive
226585aa3f7Smrg  # letters.
227fdb3d228Smrg  sed -e 's/^[^:]*: / /' \
228fdb3d228Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
229585aa3f7Smrg## This next piece of magic avoids the "deleted header file" problem.
230fdb3d228Smrg## The problem is that when a header file which appears in a .P file
231fdb3d228Smrg## is deleted, the dependency causes make to die (because there is
232fdb3d228Smrg## typically no way to rebuild the header).  We avoid this by adding
233fdb3d228Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
234fdb3d228Smrg## this for us directly.
235585aa3f7Smrg## Some versions of gcc put a space before the ':'.  On the theory
236fdb3d228Smrg## that the space means something, we add a space to the output as
2377d575c90Smrg## well.  hp depmode also adds that space, but also prefixes the VPATH
2387d575c90Smrg## to the object.  Take care to not repeat it in the output.
239fdb3d228Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
240fdb3d228Smrg## correctly.  Breaking it into two sed invocations is a workaround.
241585aa3f7Smrg  tr ' ' "$nl" < "$tmpdepfile" \
242585aa3f7Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
243585aa3f7Smrg    | sed -e 's/$/ :/' >> "$depfile"
244fdb3d228Smrg  rm -f "$tmpdepfile"
245fdb3d228Smrg  ;;
246fdb3d228Smrg
247fdb3d228Smrghp)
248fdb3d228Smrg  # This case exists only to let depend.m4 do its work.  It works by
249fdb3d228Smrg  # looking at the text of this script.  This case will never be run,
250fdb3d228Smrg  # since it is checked for above.
251fdb3d228Smrg  exit 1
252fdb3d228Smrg  ;;
253fdb3d228Smrg
254fdb3d228Smrgsgi)
255fdb3d228Smrg  if test "$libtool" = yes; then
256fdb3d228Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
257fdb3d228Smrg  else
258fdb3d228Smrg    "$@" -MDupdate "$tmpdepfile"
259fdb3d228Smrg  fi
260fdb3d228Smrg  stat=$?
261585aa3f7Smrg  if test $stat -ne 0; then
262fdb3d228Smrg    rm -f "$tmpdepfile"
263fdb3d228Smrg    exit $stat
264fdb3d228Smrg  fi
265fdb3d228Smrg  rm -f "$depfile"
266fdb3d228Smrg
267fdb3d228Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
268fdb3d228Smrg    echo "$object : \\" > "$depfile"
269fdb3d228Smrg    # Clip off the initial element (the dependent).  Don't try to be
270fdb3d228Smrg    # clever and replace this with sed code, as IRIX sed won't handle
271fdb3d228Smrg    # lines with more than a fixed number of characters (4096 in
272fdb3d228Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
273585aa3f7Smrg    # the IRIX cc adds comments like '#:fec' to the end of the
274fdb3d228Smrg    # dependency line.
275585aa3f7Smrg    tr ' ' "$nl" < "$tmpdepfile" \
276585aa3f7Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
277585aa3f7Smrg      | tr "$nl" ' ' >> "$depfile"
2789b41ff1aSmrg    echo >> "$depfile"
279fdb3d228Smrg    # The second pass generates a dummy entry for each header file.
280585aa3f7Smrg    tr ' ' "$nl" < "$tmpdepfile" \
281585aa3f7Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
282585aa3f7Smrg      >> "$depfile"
283fdb3d228Smrg  else
284585aa3f7Smrg    make_dummy_depfile
285fdb3d228Smrg  fi
286fdb3d228Smrg  rm -f "$tmpdepfile"
287fdb3d228Smrg  ;;
288fdb3d228Smrg
289585aa3f7Smrgxlc)
290585aa3f7Smrg  # This case exists only to let depend.m4 do its work.  It works by
291585aa3f7Smrg  # looking at the text of this script.  This case will never be run,
292585aa3f7Smrg  # since it is checked for above.
293585aa3f7Smrg  exit 1
294585aa3f7Smrg  ;;
295585aa3f7Smrg
296fdb3d228Smrgaix)
297fdb3d228Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
298fdb3d228Smrg  # in a .u file.  In older versions, this file always lives in the
299585aa3f7Smrg  # current directory.  Also, the AIX compiler puts '$object:' at the
300fdb3d228Smrg  # start of each line; $object doesn't have directory information.
301fdb3d228Smrg  # Version 6 uses the directory in both cases.
302585aa3f7Smrg  set_dir_from "$object"
303585aa3f7Smrg  set_base_from "$object"
304fdb3d228Smrg  if test "$libtool" = yes; then
3059b41ff1aSmrg    tmpdepfile1=$dir$base.u
3069b41ff1aSmrg    tmpdepfile2=$base.u
3079b41ff1aSmrg    tmpdepfile3=$dir.libs/$base.u
308fdb3d228Smrg    "$@" -Wc,-M
309fdb3d228Smrg  else
3109b41ff1aSmrg    tmpdepfile1=$dir$base.u
3119b41ff1aSmrg    tmpdepfile2=$dir$base.u
3129b41ff1aSmrg    tmpdepfile3=$dir$base.u
313fdb3d228Smrg    "$@" -M
314fdb3d228Smrg  fi
315fdb3d228Smrg  stat=$?
316585aa3f7Smrg  if test $stat -ne 0; then
3179b41ff1aSmrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
318fdb3d228Smrg    exit $stat
319fdb3d228Smrg  fi
320fdb3d228Smrg
3219b41ff1aSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
3229b41ff1aSmrg  do
3239b41ff1aSmrg    test -f "$tmpdepfile" && break
3249b41ff1aSmrg  done
325585aa3f7Smrg  aix_post_process_depfile
326585aa3f7Smrg  ;;
327585aa3f7Smrg
328585aa3f7Smrgtcc)
329585aa3f7Smrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
330585aa3f7Smrg  # FIXME: That version still under development at the moment of writing.
331585aa3f7Smrg  #        Make that this statement remains true also for stable, released
332585aa3f7Smrg  #        versions.
333585aa3f7Smrg  # It will wrap lines (doesn't matter whether long or short) with a
334585aa3f7Smrg  # trailing '\', as in:
335585aa3f7Smrg  #
336585aa3f7Smrg  #   foo.o : \
337585aa3f7Smrg  #    foo.c \
338585aa3f7Smrg  #    foo.h \
339585aa3f7Smrg  #
340585aa3f7Smrg  # It will put a trailing '\' even on the last line, and will use leading
341585aa3f7Smrg  # spaces rather than leading tabs (at least since its commit 0394caf7
342585aa3f7Smrg  # "Emit spaces for -MD").
343585aa3f7Smrg  "$@" -MD -MF "$tmpdepfile"
344585aa3f7Smrg  stat=$?
345585aa3f7Smrg  if test $stat -ne 0; then
346585aa3f7Smrg    rm -f "$tmpdepfile"
347585aa3f7Smrg    exit $stat
348fdb3d228Smrg  fi
349585aa3f7Smrg  rm -f "$depfile"
350585aa3f7Smrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
351585aa3f7Smrg  # We have to change lines of the first kind to '$object: \'.
352585aa3f7Smrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
353585aa3f7Smrg  # And for each line of the second kind, we have to emit a 'dep.h:'
354585aa3f7Smrg  # dummy dependency, to avoid the deleted-header problem.
355585aa3f7Smrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
356fdb3d228Smrg  rm -f "$tmpdepfile"
357fdb3d228Smrg  ;;
358fdb3d228Smrg
359585aa3f7Smrg## The order of this option in the case statement is important, since the
360585aa3f7Smrg## shell code in configure will try each of these formats in the order
361585aa3f7Smrg## listed in this file.  A plain '-MD' option would be understood by many
362585aa3f7Smrg## compilers, so we must ensure this comes after the gcc and icc options.
363585aa3f7Smrgpgcc)
364585aa3f7Smrg  # Portland's C compiler understands '-MD'.
365585aa3f7Smrg  # Will always output deps to 'file.d' where file is the root name of the
366585aa3f7Smrg  # source file under compilation, even if file resides in a subdirectory.
367585aa3f7Smrg  # The object file name does not affect the name of the '.d' file.
368585aa3f7Smrg  # pgcc 10.2 will output
369fdb3d228Smrg  #    foo.o: sub/foo.c sub/foo.h
370585aa3f7Smrg  # and will wrap long lines using '\' :
371fdb3d228Smrg  #    foo.o: sub/foo.c ... \
372fdb3d228Smrg  #     sub/foo.h ... \
373fdb3d228Smrg  #     ...
374585aa3f7Smrg  set_dir_from "$object"
375585aa3f7Smrg  # Use the source, not the object, to determine the base name, since
376585aa3f7Smrg  # that's sadly what pgcc will do too.
377585aa3f7Smrg  set_base_from "$source"
378585aa3f7Smrg  tmpdepfile=$base.d
379585aa3f7Smrg
380585aa3f7Smrg  # For projects that build the same source file twice into different object
381585aa3f7Smrg  # files, the pgcc approach of using the *source* file root name can cause
382585aa3f7Smrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
383585aa3f7Smrg  # the same $tmpdepfile.
384585aa3f7Smrg  lockdir=$base.d-lock
385585aa3f7Smrg  trap "
386585aa3f7Smrg    echo '$0: caught signal, cleaning up...' >&2
387585aa3f7Smrg    rmdir '$lockdir'
388585aa3f7Smrg    exit 1
389585aa3f7Smrg  " 1 2 13 15
390585aa3f7Smrg  numtries=100
391585aa3f7Smrg  i=$numtries
392585aa3f7Smrg  while test $i -gt 0; do
393585aa3f7Smrg    # mkdir is a portable test-and-set.
394585aa3f7Smrg    if mkdir "$lockdir" 2>/dev/null; then
395585aa3f7Smrg      # This process acquired the lock.
396585aa3f7Smrg      "$@" -MD
397585aa3f7Smrg      stat=$?
398585aa3f7Smrg      # Release the lock.
399585aa3f7Smrg      rmdir "$lockdir"
400585aa3f7Smrg      break
401585aa3f7Smrg    else
402585aa3f7Smrg      # If the lock is being held by a different process, wait
403585aa3f7Smrg      # until the winning process is done or we timeout.
404585aa3f7Smrg      while test -d "$lockdir" && test $i -gt 0; do
405585aa3f7Smrg        sleep 1
406585aa3f7Smrg        i=`expr $i - 1`
407585aa3f7Smrg      done
408585aa3f7Smrg    fi
409585aa3f7Smrg    i=`expr $i - 1`
410585aa3f7Smrg  done
411585aa3f7Smrg  trap - 1 2 13 15
412585aa3f7Smrg  if test $i -le 0; then
413585aa3f7Smrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
414585aa3f7Smrg    echo "$0: check lockdir '$lockdir'" >&2
415585aa3f7Smrg    exit 1
416585aa3f7Smrg  fi
417fdb3d228Smrg
418585aa3f7Smrg  if test $stat -ne 0; then
419fdb3d228Smrg    rm -f "$tmpdepfile"
420fdb3d228Smrg    exit $stat
421fdb3d228Smrg  fi
422fdb3d228Smrg  rm -f "$depfile"
423fdb3d228Smrg  # Each line is of the form `foo.o: dependent.h',
424fdb3d228Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
425fdb3d228Smrg  # Do two passes, one to just change these to
426fdb3d228Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
427fdb3d228Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
428fdb3d228Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
429fdb3d228Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
430585aa3f7Smrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
431585aa3f7Smrg    | sed -e 's/$/ :/' >> "$depfile"
432fdb3d228Smrg  rm -f "$tmpdepfile"
433fdb3d228Smrg  ;;
434fdb3d228Smrg
4359b41ff1aSmrghp2)
4369b41ff1aSmrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
4379b41ff1aSmrg  # compilers, which have integrated preprocessors.  The correct option
4389b41ff1aSmrg  # to use with these is +Maked; it writes dependencies to a file named
4399b41ff1aSmrg  # 'foo.d', which lands next to the object file, wherever that
4409b41ff1aSmrg  # happens to be.
4419b41ff1aSmrg  # Much of this is similar to the tru64 case; see comments there.
442585aa3f7Smrg  set_dir_from  "$object"
443585aa3f7Smrg  set_base_from "$object"
4449b41ff1aSmrg  if test "$libtool" = yes; then
4459b41ff1aSmrg    tmpdepfile1=$dir$base.d
4469b41ff1aSmrg    tmpdepfile2=$dir.libs/$base.d
4479b41ff1aSmrg    "$@" -Wc,+Maked
4489b41ff1aSmrg  else
4499b41ff1aSmrg    tmpdepfile1=$dir$base.d
4509b41ff1aSmrg    tmpdepfile2=$dir$base.d
4519b41ff1aSmrg    "$@" +Maked
4529b41ff1aSmrg  fi
4539b41ff1aSmrg  stat=$?
454585aa3f7Smrg  if test $stat -ne 0; then
4559b41ff1aSmrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
4569b41ff1aSmrg     exit $stat
4579b41ff1aSmrg  fi
4589b41ff1aSmrg
4599b41ff1aSmrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
4609b41ff1aSmrg  do
4619b41ff1aSmrg    test -f "$tmpdepfile" && break
4629b41ff1aSmrg  done
4639b41ff1aSmrg  if test -f "$tmpdepfile"; then
464585aa3f7Smrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
465585aa3f7Smrg    # Add 'dependent.h:' lines.
4669b41ff1aSmrg    sed -ne '2,${
467585aa3f7Smrg               s/^ *//
468585aa3f7Smrg               s/ \\*$//
469585aa3f7Smrg               s/$/:/
470585aa3f7Smrg               p
471585aa3f7Smrg             }' "$tmpdepfile" >> "$depfile"
4729b41ff1aSmrg  else
473585aa3f7Smrg    make_dummy_depfile
4749b41ff1aSmrg  fi
4759b41ff1aSmrg  rm -f "$tmpdepfile" "$tmpdepfile2"
4769b41ff1aSmrg  ;;
4779b41ff1aSmrg
478fdb3d228Smrgtru64)
479585aa3f7Smrg  # The Tru64 compiler uses -MD to generate dependencies as a side
480585aa3f7Smrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
481585aa3f7Smrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
482585aa3f7Smrg  # dependencies in 'foo.d' instead, so we check for that too.
483585aa3f7Smrg  # Subdirectories are respected.
484585aa3f7Smrg  set_dir_from  "$object"
485585aa3f7Smrg  set_base_from "$object"
486585aa3f7Smrg
487585aa3f7Smrg  if test "$libtool" = yes; then
488585aa3f7Smrg    # Libtool generates 2 separate objects for the 2 libraries.  These
489585aa3f7Smrg    # two compilations output dependencies in $dir.libs/$base.o.d and
490585aa3f7Smrg    # in $dir$base.o.d.  We have to check for both files, because
491585aa3f7Smrg    # one of the two compilations can be disabled.  We should prefer
492585aa3f7Smrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
493585aa3f7Smrg    # automatically cleaned when .libs/ is deleted, while ignoring
494585aa3f7Smrg    # the former would cause a distcleancheck panic.
495585aa3f7Smrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
496585aa3f7Smrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
497585aa3f7Smrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
498585aa3f7Smrg    "$@" -Wc,-MD
499585aa3f7Smrg  else
500585aa3f7Smrg    tmpdepfile1=$dir$base.d
501585aa3f7Smrg    tmpdepfile2=$dir$base.d
502585aa3f7Smrg    tmpdepfile3=$dir$base.d
503585aa3f7Smrg    "$@" -MD
504585aa3f7Smrg  fi
505585aa3f7Smrg
506585aa3f7Smrg  stat=$?
507585aa3f7Smrg  if test $stat -ne 0; then
508585aa3f7Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
509585aa3f7Smrg    exit $stat
510585aa3f7Smrg  fi
511585aa3f7Smrg
512585aa3f7Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
513585aa3f7Smrg  do
514585aa3f7Smrg    test -f "$tmpdepfile" && break
515585aa3f7Smrg  done
516585aa3f7Smrg  # Same post-processing that is required for AIX mode.
517585aa3f7Smrg  aix_post_process_depfile
518585aa3f7Smrg  ;;
519fdb3d228Smrg
5207d575c90Smrgmsvc7)
5217d575c90Smrg  if test "$libtool" = yes; then
5227d575c90Smrg    showIncludes=-Wc,-showIncludes
5237d575c90Smrg  else
5247d575c90Smrg    showIncludes=-showIncludes
5257d575c90Smrg  fi
5267d575c90Smrg  "$@" $showIncludes > "$tmpdepfile"
5277d575c90Smrg  stat=$?
5287d575c90Smrg  grep -v '^Note: including file: ' "$tmpdepfile"
529585aa3f7Smrg  if test $stat -ne 0; then
5307d575c90Smrg    rm -f "$tmpdepfile"
5317d575c90Smrg    exit $stat
5327d575c90Smrg  fi
5337d575c90Smrg  rm -f "$depfile"
5347d575c90Smrg  echo "$object : \\" > "$depfile"
5357d575c90Smrg  # The first sed program below extracts the file names and escapes
5367d575c90Smrg  # backslashes for cygpath.  The second sed program outputs the file
5377d575c90Smrg  # name when reading, but also accumulates all include files in the
5387d575c90Smrg  # hold buffer in order to output them again at the end.  This only
5397d575c90Smrg  # works with sed implementations that can handle large buffers.
5407d575c90Smrg  sed < "$tmpdepfile" -n '
5417d575c90Smrg/^Note: including file:  *\(.*\)/ {
5427d575c90Smrg  s//\1/
5437d575c90Smrg  s/\\/\\\\/g
5447d575c90Smrg  p
5457d575c90Smrg}' | $cygpath_u | sort -u | sed -n '
5467d575c90Smrgs/ /\\ /g
547585aa3f7Smrgs/\(.*\)/'"$tab"'\1 \\/p
5487d575c90Smrgs/.\(.*\) \\/\1:/
5497d575c90SmrgH
5507d575c90Smrg$ {
551585aa3f7Smrg  s/.*/'"$tab"'/
5527d575c90Smrg  G
5537d575c90Smrg  p
5547d575c90Smrg}' >> "$depfile"
555585aa3f7Smrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
5567d575c90Smrg  rm -f "$tmpdepfile"
5577d575c90Smrg  ;;
5587d575c90Smrg
5597d575c90Smrgmsvc7msys)
5607d575c90Smrg  # This case exists only to let depend.m4 do its work.  It works by
5617d575c90Smrg  # looking at the text of this script.  This case will never be run,
5627d575c90Smrg  # since it is checked for above.
5637d575c90Smrg  exit 1
5647d575c90Smrg  ;;
5657d575c90Smrg
566fdb3d228Smrg#nosideeffect)
567fdb3d228Smrg  # This comment above is used by automake to tell side-effect
568fdb3d228Smrg  # dependency tracking mechanisms from slower ones.
569fdb3d228Smrg
570fdb3d228Smrgdashmstdout)
571fdb3d228Smrg  # Important note: in order to support this mode, a compiler *must*
572fdb3d228Smrg  # always write the preprocessed file to stdout, regardless of -o.
573fdb3d228Smrg  "$@" || exit $?
574fdb3d228Smrg
575fdb3d228Smrg  # Remove the call to Libtool.
576fdb3d228Smrg  if test "$libtool" = yes; then
5779b41ff1aSmrg    while test "X$1" != 'X--mode=compile'; do
578fdb3d228Smrg      shift
579fdb3d228Smrg    done
580fdb3d228Smrg    shift
581fdb3d228Smrg  fi
582fdb3d228Smrg
583585aa3f7Smrg  # Remove '-o $object'.
584fdb3d228Smrg  IFS=" "
585fdb3d228Smrg  for arg
586fdb3d228Smrg  do
587fdb3d228Smrg    case $arg in
588fdb3d228Smrg    -o)
589fdb3d228Smrg      shift
590fdb3d228Smrg      ;;
591fdb3d228Smrg    $object)
592fdb3d228Smrg      shift
593fdb3d228Smrg      ;;
594fdb3d228Smrg    *)
595fdb3d228Smrg      set fnord "$@" "$arg"
596fdb3d228Smrg      shift # fnord
597fdb3d228Smrg      shift # $arg
598fdb3d228Smrg      ;;
599fdb3d228Smrg    esac
600fdb3d228Smrg  done
601fdb3d228Smrg
602fdb3d228Smrg  test -z "$dashmflag" && dashmflag=-M
603585aa3f7Smrg  # Require at least two characters before searching for ':'
604fdb3d228Smrg  # in the target name.  This is to cope with DOS-style filenames:
605585aa3f7Smrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
606fdb3d228Smrg  "$@" $dashmflag |
607585aa3f7Smrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
608fdb3d228Smrg  rm -f "$depfile"
609fdb3d228Smrg  cat < "$tmpdepfile" > "$depfile"
610585aa3f7Smrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
611585aa3f7Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
612585aa3f7Smrg  tr ' ' "$nl" < "$tmpdepfile" \
613585aa3f7Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
614585aa3f7Smrg    | sed -e 's/$/ :/' >> "$depfile"
615fdb3d228Smrg  rm -f "$tmpdepfile"
616fdb3d228Smrg  ;;
617fdb3d228Smrg
618fdb3d228SmrgdashXmstdout)
619fdb3d228Smrg  # This case only exists to satisfy depend.m4.  It is never actually
620fdb3d228Smrg  # run, as this mode is specially recognized in the preamble.
621fdb3d228Smrg  exit 1
622fdb3d228Smrg  ;;
623fdb3d228Smrg
624fdb3d228Smrgmakedepend)
625fdb3d228Smrg  "$@" || exit $?
626fdb3d228Smrg  # Remove any Libtool call
627fdb3d228Smrg  if test "$libtool" = yes; then
6289b41ff1aSmrg    while test "X$1" != 'X--mode=compile'; do
629fdb3d228Smrg      shift
630fdb3d228Smrg    done
631fdb3d228Smrg    shift
632fdb3d228Smrg  fi
633fdb3d228Smrg  # X makedepend
634fdb3d228Smrg  shift
6359b41ff1aSmrg  cleared=no eat=no
6369b41ff1aSmrg  for arg
6379b41ff1aSmrg  do
638fdb3d228Smrg    case $cleared in
639fdb3d228Smrg    no)
640fdb3d228Smrg      set ""; shift
641fdb3d228Smrg      cleared=yes ;;
642fdb3d228Smrg    esac
6439b41ff1aSmrg    if test $eat = yes; then
6449b41ff1aSmrg      eat=no
6459b41ff1aSmrg      continue
6469b41ff1aSmrg    fi
647fdb3d228Smrg    case "$arg" in
648fdb3d228Smrg    -D*|-I*)
649fdb3d228Smrg      set fnord "$@" "$arg"; shift ;;
650fdb3d228Smrg    # Strip any option that makedepend may not understand.  Remove
651fdb3d228Smrg    # the object too, otherwise makedepend will parse it as a source file.
6529b41ff1aSmrg    -arch)
6539b41ff1aSmrg      eat=yes ;;
654fdb3d228Smrg    -*|$object)
655fdb3d228Smrg      ;;
656fdb3d228Smrg    *)
657fdb3d228Smrg      set fnord "$@" "$arg"; shift ;;
658fdb3d228Smrg    esac
659fdb3d228Smrg  done
6609b41ff1aSmrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
661fdb3d228Smrg  touch "$tmpdepfile"
662fdb3d228Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
663fdb3d228Smrg  rm -f "$depfile"
6647d575c90Smrg  # makedepend may prepend the VPATH from the source file name to the object.
6657d575c90Smrg  # No need to regex-escape $object, excess matching of '.' is harmless.
6667d575c90Smrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
667585aa3f7Smrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
668585aa3f7Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
669585aa3f7Smrg  sed '1,2d' "$tmpdepfile" \
670585aa3f7Smrg    | tr ' ' "$nl" \
671585aa3f7Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
672585aa3f7Smrg    | sed -e 's/$/ :/' >> "$depfile"
673fdb3d228Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
674fdb3d228Smrg  ;;
675fdb3d228Smrg
676fdb3d228Smrgcpp)
677fdb3d228Smrg  # Important note: in order to support this mode, a compiler *must*
678fdb3d228Smrg  # always write the preprocessed file to stdout.
679fdb3d228Smrg  "$@" || exit $?
680fdb3d228Smrg
681fdb3d228Smrg  # Remove the call to Libtool.
682fdb3d228Smrg  if test "$libtool" = yes; then
6839b41ff1aSmrg    while test "X$1" != 'X--mode=compile'; do
684fdb3d228Smrg      shift
685fdb3d228Smrg    done
686fdb3d228Smrg    shift
687fdb3d228Smrg  fi
688fdb3d228Smrg
689585aa3f7Smrg  # Remove '-o $object'.
690fdb3d228Smrg  IFS=" "
691fdb3d228Smrg  for arg
692fdb3d228Smrg  do
693fdb3d228Smrg    case $arg in
694fdb3d228Smrg    -o)
695fdb3d228Smrg      shift
696fdb3d228Smrg      ;;
697fdb3d228Smrg    $object)
698fdb3d228Smrg      shift
699fdb3d228Smrg      ;;
700fdb3d228Smrg    *)
701fdb3d228Smrg      set fnord "$@" "$arg"
702fdb3d228Smrg      shift # fnord
703fdb3d228Smrg      shift # $arg
704fdb3d228Smrg      ;;
705fdb3d228Smrg    esac
706fdb3d228Smrg  done
707fdb3d228Smrg
708585aa3f7Smrg  "$@" -E \
709585aa3f7Smrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
710585aa3f7Smrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711585aa3f7Smrg    | sed '$ s: \\$::' > "$tmpdepfile"
712fdb3d228Smrg  rm -f "$depfile"
713fdb3d228Smrg  echo "$object : \\" > "$depfile"
714fdb3d228Smrg  cat < "$tmpdepfile" >> "$depfile"
715fdb3d228Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
716fdb3d228Smrg  rm -f "$tmpdepfile"
717fdb3d228Smrg  ;;
718fdb3d228Smrg
719fdb3d228Smrgmsvisualcpp)
720fdb3d228Smrg  # Important note: in order to support this mode, a compiler *must*
7219b41ff1aSmrg  # always write the preprocessed file to stdout.
722fdb3d228Smrg  "$@" || exit $?
7239b41ff1aSmrg
7249b41ff1aSmrg  # Remove the call to Libtool.
7259b41ff1aSmrg  if test "$libtool" = yes; then
7269b41ff1aSmrg    while test "X$1" != 'X--mode=compile'; do
7279b41ff1aSmrg      shift
7289b41ff1aSmrg    done
7299b41ff1aSmrg    shift
7309b41ff1aSmrg  fi
7319b41ff1aSmrg
732fdb3d228Smrg  IFS=" "
733fdb3d228Smrg  for arg
734fdb3d228Smrg  do
735fdb3d228Smrg    case "$arg" in
7369b41ff1aSmrg    -o)
7379b41ff1aSmrg      shift
7389b41ff1aSmrg      ;;
7399b41ff1aSmrg    $object)
7409b41ff1aSmrg      shift
7419b41ff1aSmrg      ;;
742fdb3d228Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
743585aa3f7Smrg        set fnord "$@"
744585aa3f7Smrg        shift
745585aa3f7Smrg        shift
746585aa3f7Smrg        ;;
747fdb3d228Smrg    *)
748585aa3f7Smrg        set fnord "$@" "$arg"
749585aa3f7Smrg        shift
750585aa3f7Smrg        shift
751585aa3f7Smrg        ;;
752fdb3d228Smrg    esac
753fdb3d228Smrg  done
7549b41ff1aSmrg  "$@" -E 2>/dev/null |
7559b41ff1aSmrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
756fdb3d228Smrg  rm -f "$depfile"
757fdb3d228Smrg  echo "$object : \\" > "$depfile"
758585aa3f7Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
759585aa3f7Smrg  echo "$tab" >> "$depfile"
7609b41ff1aSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
761fdb3d228Smrg  rm -f "$tmpdepfile"
762fdb3d228Smrg  ;;
763fdb3d228Smrg
7649b41ff1aSmrgmsvcmsys)
7659b41ff1aSmrg  # This case exists only to let depend.m4 do its work.  It works by
7669b41ff1aSmrg  # looking at the text of this script.  This case will never be run,
7679b41ff1aSmrg  # since it is checked for above.
7689b41ff1aSmrg  exit 1
7699b41ff1aSmrg  ;;
7709b41ff1aSmrg
771fdb3d228Smrgnone)
772fdb3d228Smrg  exec "$@"
773fdb3d228Smrg  ;;
774fdb3d228Smrg
775fdb3d228Smrg*)
776fdb3d228Smrg  echo "Unknown depmode $depmode" 1>&2
777fdb3d228Smrg  exit 1
778fdb3d228Smrg  ;;
779fdb3d228Smrgesac
780fdb3d228Smrg
781fdb3d228Smrgexit 0
7829b41ff1aSmrg
7839b41ff1aSmrg# Local Variables:
7849b41ff1aSmrg# mode: shell-script
7859b41ff1aSmrg# sh-indentation: 2
78674b35aa8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
7879b41ff1aSmrg# time-stamp-start: "scriptversion="
7889b41ff1aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
78974b35aa8Smrg# time-stamp-time-zone: "UTC0"
7909b41ff1aSmrg# time-stamp-end: "; # UTC"
7919b41ff1aSmrg# End:
792