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