1692f60a7Smrg#! /bin/sh
2692f60a7Smrg# depcomp - compile a program generating dependencies as side-effects
3692f60a7Smrg
4efb46889Smrgscriptversion=2018-03-07.03; # UTC
5692f60a7Smrg
6efb46889Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7692f60a7Smrg
8692f60a7Smrg# This program is free software; you can redistribute it and/or modify
9692f60a7Smrg# it under the terms of the GNU General Public License as published by
10692f60a7Smrg# the Free Software Foundation; either version 2, or (at your option)
11692f60a7Smrg# any later version.
12692f60a7Smrg
13692f60a7Smrg# This program is distributed in the hope that it will be useful,
14692f60a7Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15692f60a7Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16692f60a7Smrg# GNU General Public License for more details.
17692f60a7Smrg
18692f60a7Smrg# You should have received a copy of the GNU General Public License
19efb46889Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20692f60a7Smrg
21692f60a7Smrg# As a special exception to the GNU General Public License, if you
22692f60a7Smrg# distribute this file as part of a program that contains a
23692f60a7Smrg# configuration script generated by Autoconf, you may include it under
24692f60a7Smrg# the same distribution terms that you use for the rest of that program.
25692f60a7Smrg
26692f60a7Smrg# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
27692f60a7Smrg
28692f60a7Smrgcase $1 in
29692f60a7Smrg  '')
30eaa3dbe0Smrg    echo "$0: No command.  Try '$0 --help' for more information." 1>&2
31eaa3dbe0Smrg    exit 1;
32eaa3dbe0Smrg    ;;
33692f60a7Smrg  -h | --h*)
34692f60a7Smrg    cat <<\EOF
35692f60a7SmrgUsage: depcomp [--help] [--version] PROGRAM [ARGS]
36692f60a7Smrg
37692f60a7SmrgRun PROGRAMS ARGS to compile a file, generating dependencies
38692f60a7Smrgas side-effects.
39692f60a7Smrg
40692f60a7SmrgEnvironment variables:
41692f60a7Smrg  depmode     Dependency tracking mode.
42eaa3dbe0Smrg  source      Source file read by 'PROGRAMS ARGS'.
43eaa3dbe0Smrg  object      Object file output by 'PROGRAMS ARGS'.
44692f60a7Smrg  DEPDIR      directory where to store dependencies.
45692f60a7Smrg  depfile     Dependency file to output.
463f6d0e1dSmrg  tmpdepfile  Temporary file to use when outputting dependencies.
47692f60a7Smrg  libtool     Whether libtool is used (yes/no).
48692f60a7Smrg
49692f60a7SmrgReport bugs to <bug-automake@gnu.org>.
50692f60a7SmrgEOF
51692f60a7Smrg    exit $?
52692f60a7Smrg    ;;
53692f60a7Smrg  -v | --v*)
54692f60a7Smrg    echo "depcomp $scriptversion"
55692f60a7Smrg    exit $?
56692f60a7Smrg    ;;
57692f60a7Smrgesac
58692f60a7Smrg
59eaa3dbe0Smrg# Get the directory component of the given path, and save it in the
60eaa3dbe0Smrg# global variables '$dir'.  Note that this directory component will
61eaa3dbe0Smrg# be either empty or ending with a '/' character.  This is deliberate.
62eaa3dbe0Smrgset_dir_from ()
63eaa3dbe0Smrg{
64eaa3dbe0Smrg  case $1 in
65eaa3dbe0Smrg    */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;;
66eaa3dbe0Smrg      *) dir=;;
67eaa3dbe0Smrg  esac
68eaa3dbe0Smrg}
69eaa3dbe0Smrg
70eaa3dbe0Smrg# Get the suffix-stripped basename of the given path, and save it the
71eaa3dbe0Smrg# global variable '$base'.
72eaa3dbe0Smrgset_base_from ()
73eaa3dbe0Smrg{
74eaa3dbe0Smrg  base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'`
75eaa3dbe0Smrg}
76eaa3dbe0Smrg
77eaa3dbe0Smrg# If no dependency file was actually created by the compiler invocation,
78eaa3dbe0Smrg# we still have to create a dummy depfile, to avoid errors with the
79eaa3dbe0Smrg# Makefile "include basename.Plo" scheme.
80eaa3dbe0Smrgmake_dummy_depfile ()
81eaa3dbe0Smrg{
82eaa3dbe0Smrg  echo "#dummy" > "$depfile"
83eaa3dbe0Smrg}
84eaa3dbe0Smrg
85eaa3dbe0Smrg# Factor out some common post-processing of the generated depfile.
86eaa3dbe0Smrg# Requires the auxiliary global variable '$tmpdepfile' to be set.
87eaa3dbe0Smrgaix_post_process_depfile ()
88eaa3dbe0Smrg{
89eaa3dbe0Smrg  # If the compiler actually managed to produce a dependency file,
90eaa3dbe0Smrg  # post-process it.
91eaa3dbe0Smrg  if test -f "$tmpdepfile"; then
92eaa3dbe0Smrg    # Each line is of the form 'foo.o: dependency.h'.
93eaa3dbe0Smrg    # Do two passes, one to just change these to
94eaa3dbe0Smrg    #   $object: dependency.h
95eaa3dbe0Smrg    # and one to simply output
96eaa3dbe0Smrg    #   dependency.h:
97eaa3dbe0Smrg    # which is needed to avoid the deleted-header problem.
98eaa3dbe0Smrg    { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile"
99eaa3dbe0Smrg      sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile"
100eaa3dbe0Smrg    } > "$depfile"
101eaa3dbe0Smrg    rm -f "$tmpdepfile"
102eaa3dbe0Smrg  else
103eaa3dbe0Smrg    make_dummy_depfile
104eaa3dbe0Smrg  fi
105eaa3dbe0Smrg}
106eaa3dbe0Smrg
107eaa3dbe0Smrg# A tabulation character.
108eaa3dbe0Smrgtab='	'
109eaa3dbe0Smrg# A newline character.
110eaa3dbe0Smrgnl='
111eaa3dbe0Smrg'
112eaa3dbe0Smrg# Character ranges might be problematic outside the C locale.
113eaa3dbe0Smrg# These definitions help.
114eaa3dbe0Smrgupper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
115eaa3dbe0Smrglower=abcdefghijklmnopqrstuvwxyz
116eaa3dbe0Smrgdigits=0123456789
117eaa3dbe0Smrgalpha=${upper}${lower}
118eaa3dbe0Smrg
119692f60a7Smrgif test -z "$depmode" || test -z "$source" || test -z "$object"; then
120692f60a7Smrg  echo "depcomp: Variables source, object and depmode must be set" 1>&2
121692f60a7Smrg  exit 1
122692f60a7Smrgfi
123692f60a7Smrg
124692f60a7Smrg# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
125692f60a7Smrgdepfile=${depfile-`echo "$object" |
126692f60a7Smrg  sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
127692f60a7Smrgtmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
128692f60a7Smrg
129692f60a7Smrgrm -f "$tmpdepfile"
130692f60a7Smrg
131eaa3dbe0Smrg# Avoid interferences from the environment.
132eaa3dbe0Smrggccflag= dashmflag=
133eaa3dbe0Smrg
134692f60a7Smrg# Some modes work just like other modes, but use different flags.  We
135692f60a7Smrg# parameterize here, but still list the modes in the big case below,
136692f60a7Smrg# to make depend.m4 easier to write.  Note that we *cannot* use a case
137692f60a7Smrg# here, because this file can only contain one case statement.
138692f60a7Smrgif test "$depmode" = hp; then
139692f60a7Smrg  # HP compiler uses -M and no extra arg.
140692f60a7Smrg  gccflag=-M
141692f60a7Smrg  depmode=gcc
142692f60a7Smrgfi
143692f60a7Smrg
144692f60a7Smrgif test "$depmode" = dashXmstdout; then
145eaa3dbe0Smrg  # This is just like dashmstdout with a different argument.
146eaa3dbe0Smrg  dashmflag=-xM
147eaa3dbe0Smrg  depmode=dashmstdout
148692f60a7Smrgfi
149692f60a7Smrg
1503f6d0e1dSmrgcygpath_u="cygpath -u -f -"
1513f6d0e1dSmrgif test "$depmode" = msvcmsys; then
152eaa3dbe0Smrg  # This is just like msvisualcpp but w/o cygpath translation.
153eaa3dbe0Smrg  # Just convert the backslash-escaped backslashes to single forward
154eaa3dbe0Smrg  # slashes to satisfy depend.m4
155eaa3dbe0Smrg  cygpath_u='sed s,\\\\,/,g'
156eaa3dbe0Smrg  depmode=msvisualcpp
1573f6d0e1dSmrgfi
1583f6d0e1dSmrg
1593f6d0e1dSmrgif test "$depmode" = msvc7msys; then
160eaa3dbe0Smrg  # This is just like msvc7 but w/o cygpath translation.
161eaa3dbe0Smrg  # Just convert the backslash-escaped backslashes to single forward
162eaa3dbe0Smrg  # slashes to satisfy depend.m4
163eaa3dbe0Smrg  cygpath_u='sed s,\\\\,/,g'
164eaa3dbe0Smrg  depmode=msvc7
165eaa3dbe0Smrgfi
166eaa3dbe0Smrg
167eaa3dbe0Smrgif test "$depmode" = xlc; then
168eaa3dbe0Smrg  # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information.
169eaa3dbe0Smrg  gccflag=-qmakedep=gcc,-MF
170eaa3dbe0Smrg  depmode=gcc
1713f6d0e1dSmrgfi
1723f6d0e1dSmrg
173692f60a7Smrgcase "$depmode" in
174692f60a7Smrggcc3)
175692f60a7Smrg## gcc 3 implements dependency tracking that does exactly what
176692f60a7Smrg## we want.  Yay!  Note: for some reason libtool 1.4 doesn't like
177692f60a7Smrg## it if -MD -MP comes after the -MF stuff.  Hmm.
17854569438Smrg## Unfortunately, FreeBSD c89 acceptance of flags depends upon
17954569438Smrg## the command line argument order; so add the flags where they
18054569438Smrg## appear in depend2.am.  Note that the slowdown incurred here
18154569438Smrg## affects only configure: in makefiles, %FASTDEP% shortcuts this.
18254569438Smrg  for arg
18354569438Smrg  do
18454569438Smrg    case $arg in
18554569438Smrg    -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
18654569438Smrg    *)  set fnord "$@" "$arg" ;;
18754569438Smrg    esac
18854569438Smrg    shift # fnord
18954569438Smrg    shift # $arg
19054569438Smrg  done
19154569438Smrg  "$@"
192692f60a7Smrg  stat=$?
193eaa3dbe0Smrg  if test $stat -ne 0; then
194692f60a7Smrg    rm -f "$tmpdepfile"
195692f60a7Smrg    exit $stat
196692f60a7Smrg  fi
197692f60a7Smrg  mv "$tmpdepfile" "$depfile"
198692f60a7Smrg  ;;
199692f60a7Smrg
200692f60a7Smrggcc)
201eaa3dbe0Smrg## Note that this doesn't just cater to obsosete pre-3.x GCC compilers.
202eaa3dbe0Smrg## but also to in-use compilers like IMB xlc/xlC and the HP C compiler.
203eaa3dbe0Smrg## (see the conditional assignment to $gccflag above).
204692f60a7Smrg## There are various ways to get dependency output from gcc.  Here's
205692f60a7Smrg## why we pick this rather obscure method:
206692f60a7Smrg## - Don't want to use -MD because we'd like the dependencies to end
207692f60a7Smrg##   up in a subdir.  Having to rename by hand is ugly.
208692f60a7Smrg##   (We might end up doing this anyway to support other compilers.)
209692f60a7Smrg## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
210eaa3dbe0Smrg##   -MM, not -M (despite what the docs say).  Also, it might not be
211eaa3dbe0Smrg##   supported by the other compilers which use the 'gcc' depmode.
212692f60a7Smrg## - Using -M directly means running the compiler twice (even worse
213692f60a7Smrg##   than renaming).
214692f60a7Smrg  if test -z "$gccflag"; then
215692f60a7Smrg    gccflag=-MD,
216692f60a7Smrg  fi
217692f60a7Smrg  "$@" -Wp,"$gccflag$tmpdepfile"
218692f60a7Smrg  stat=$?
219eaa3dbe0Smrg  if test $stat -ne 0; then
220692f60a7Smrg    rm -f "$tmpdepfile"
221692f60a7Smrg    exit $stat
222692f60a7Smrg  fi
223692f60a7Smrg  rm -f "$depfile"
224692f60a7Smrg  echo "$object : \\" > "$depfile"
225eaa3dbe0Smrg  # The second -e expression handles DOS-style file names with drive
226eaa3dbe0Smrg  # letters.
227692f60a7Smrg  sed -e 's/^[^:]*: / /' \
228692f60a7Smrg      -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
229eaa3dbe0Smrg## This next piece of magic avoids the "deleted header file" problem.
230692f60a7Smrg## The problem is that when a header file which appears in a .P file
231692f60a7Smrg## is deleted, the dependency causes make to die (because there is
232692f60a7Smrg## typically no way to rebuild the header).  We avoid this by adding
233692f60a7Smrg## dummy dependencies for each header file.  Too bad gcc doesn't do
234692f60a7Smrg## this for us directly.
235eaa3dbe0Smrg## Some versions of gcc put a space before the ':'.  On the theory
236692f60a7Smrg## that the space means something, we add a space to the output as
2373f6d0e1dSmrg## well.  hp depmode also adds that space, but also prefixes the VPATH
2383f6d0e1dSmrg## to the object.  Take care to not repeat it in the output.
239692f60a7Smrg## Some versions of the HPUX 10.20 sed can't process this invocation
240692f60a7Smrg## correctly.  Breaking it into two sed invocations is a workaround.
241eaa3dbe0Smrg  tr ' ' "$nl" < "$tmpdepfile" \
242eaa3dbe0Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
243eaa3dbe0Smrg    | sed -e 's/$/ :/' >> "$depfile"
244692f60a7Smrg  rm -f "$tmpdepfile"
245692f60a7Smrg  ;;
246692f60a7Smrg
247692f60a7Smrghp)
248692f60a7Smrg  # This case exists only to let depend.m4 do its work.  It works by
249692f60a7Smrg  # looking at the text of this script.  This case will never be run,
250692f60a7Smrg  # since it is checked for above.
251692f60a7Smrg  exit 1
252692f60a7Smrg  ;;
253692f60a7Smrg
254692f60a7Smrgsgi)
255692f60a7Smrg  if test "$libtool" = yes; then
256692f60a7Smrg    "$@" "-Wp,-MDupdate,$tmpdepfile"
257692f60a7Smrg  else
258692f60a7Smrg    "$@" -MDupdate "$tmpdepfile"
259692f60a7Smrg  fi
260692f60a7Smrg  stat=$?
261eaa3dbe0Smrg  if test $stat -ne 0; then
262692f60a7Smrg    rm -f "$tmpdepfile"
263692f60a7Smrg    exit $stat
264692f60a7Smrg  fi
265692f60a7Smrg  rm -f "$depfile"
266692f60a7Smrg
267692f60a7Smrg  if test -f "$tmpdepfile"; then  # yes, the sourcefile depend on other files
268692f60a7Smrg    echo "$object : \\" > "$depfile"
269692f60a7Smrg    # Clip off the initial element (the dependent).  Don't try to be
270692f60a7Smrg    # clever and replace this with sed code, as IRIX sed won't handle
271692f60a7Smrg    # lines with more than a fixed number of characters (4096 in
272692f60a7Smrg    # IRIX 6.2 sed, 8192 in IRIX 6.5).  We also remove comment lines;
273eaa3dbe0Smrg    # the IRIX cc adds comments like '#:fec' to the end of the
274692f60a7Smrg    # dependency line.
275eaa3dbe0Smrg    tr ' ' "$nl" < "$tmpdepfile" \
276eaa3dbe0Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \
277eaa3dbe0Smrg      | tr "$nl" ' ' >> "$depfile"
2783f6d0e1dSmrg    echo >> "$depfile"
279692f60a7Smrg    # The second pass generates a dummy entry for each header file.
280eaa3dbe0Smrg    tr ' ' "$nl" < "$tmpdepfile" \
281eaa3dbe0Smrg      | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
282eaa3dbe0Smrg      >> "$depfile"
283692f60a7Smrg  else
284eaa3dbe0Smrg    make_dummy_depfile
285692f60a7Smrg  fi
286692f60a7Smrg  rm -f "$tmpdepfile"
287692f60a7Smrg  ;;
288692f60a7Smrg
289eaa3dbe0Smrgxlc)
290eaa3dbe0Smrg  # This case exists only to let depend.m4 do its work.  It works by
291eaa3dbe0Smrg  # looking at the text of this script.  This case will never be run,
292eaa3dbe0Smrg  # since it is checked for above.
293eaa3dbe0Smrg  exit 1
294eaa3dbe0Smrg  ;;
295eaa3dbe0Smrg
296692f60a7Smrgaix)
297692f60a7Smrg  # The C for AIX Compiler uses -M and outputs the dependencies
298692f60a7Smrg  # in a .u file.  In older versions, this file always lives in the
299eaa3dbe0Smrg  # current directory.  Also, the AIX compiler puts '$object:' at the
300692f60a7Smrg  # start of each line; $object doesn't have directory information.
301692f60a7Smrg  # Version 6 uses the directory in both cases.
302eaa3dbe0Smrg  set_dir_from "$object"
303eaa3dbe0Smrg  set_base_from "$object"
304692f60a7Smrg  if test "$libtool" = yes; then
30554569438Smrg    tmpdepfile1=$dir$base.u
30654569438Smrg    tmpdepfile2=$base.u
30754569438Smrg    tmpdepfile3=$dir.libs/$base.u
308692f60a7Smrg    "$@" -Wc,-M
309692f60a7Smrg  else
31054569438Smrg    tmpdepfile1=$dir$base.u
31154569438Smrg    tmpdepfile2=$dir$base.u
31254569438Smrg    tmpdepfile3=$dir$base.u
313692f60a7Smrg    "$@" -M
314692f60a7Smrg  fi
315692f60a7Smrg  stat=$?
316eaa3dbe0Smrg  if test $stat -ne 0; then
31754569438Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
318692f60a7Smrg    exit $stat
319692f60a7Smrg  fi
320692f60a7Smrg
32154569438Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
32254569438Smrg  do
32354569438Smrg    test -f "$tmpdepfile" && break
32454569438Smrg  done
325eaa3dbe0Smrg  aix_post_process_depfile
326eaa3dbe0Smrg  ;;
327eaa3dbe0Smrg
328eaa3dbe0Smrgtcc)
329eaa3dbe0Smrg  # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26
330eaa3dbe0Smrg  # FIXME: That version still under development at the moment of writing.
331eaa3dbe0Smrg  #        Make that this statement remains true also for stable, released
332eaa3dbe0Smrg  #        versions.
333eaa3dbe0Smrg  # It will wrap lines (doesn't matter whether long or short) with a
334eaa3dbe0Smrg  # trailing '\', as in:
335eaa3dbe0Smrg  #
336eaa3dbe0Smrg  #   foo.o : \
337eaa3dbe0Smrg  #    foo.c \
338eaa3dbe0Smrg  #    foo.h \
339eaa3dbe0Smrg  #
340eaa3dbe0Smrg  # It will put a trailing '\' even on the last line, and will use leading
341eaa3dbe0Smrg  # spaces rather than leading tabs (at least since its commit 0394caf7
342eaa3dbe0Smrg  # "Emit spaces for -MD").
343eaa3dbe0Smrg  "$@" -MD -MF "$tmpdepfile"
344eaa3dbe0Smrg  stat=$?
345eaa3dbe0Smrg  if test $stat -ne 0; then
346eaa3dbe0Smrg    rm -f "$tmpdepfile"
347eaa3dbe0Smrg    exit $stat
348692f60a7Smrg  fi
349eaa3dbe0Smrg  rm -f "$depfile"
350eaa3dbe0Smrg  # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'.
351eaa3dbe0Smrg  # We have to change lines of the first kind to '$object: \'.
352eaa3dbe0Smrg  sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile"
353eaa3dbe0Smrg  # And for each line of the second kind, we have to emit a 'dep.h:'
354eaa3dbe0Smrg  # dummy dependency, to avoid the deleted-header problem.
355eaa3dbe0Smrg  sed -n -e 's|^  *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile"
356692f60a7Smrg  rm -f "$tmpdepfile"
357692f60a7Smrg  ;;
358692f60a7Smrg
359eaa3dbe0Smrg## The order of this option in the case statement is important, since the
360eaa3dbe0Smrg## shell code in configure will try each of these formats in the order
361eaa3dbe0Smrg## listed in this file.  A plain '-MD' option would be understood by many
362eaa3dbe0Smrg## compilers, so we must ensure this comes after the gcc and icc options.
363eaa3dbe0Smrgpgcc)
364eaa3dbe0Smrg  # Portland's C compiler understands '-MD'.
365eaa3dbe0Smrg  # Will always output deps to 'file.d' where file is the root name of the
366eaa3dbe0Smrg  # source file under compilation, even if file resides in a subdirectory.
367eaa3dbe0Smrg  # The object file name does not affect the name of the '.d' file.
368eaa3dbe0Smrg  # pgcc 10.2 will output
369692f60a7Smrg  #    foo.o: sub/foo.c sub/foo.h
370eaa3dbe0Smrg  # and will wrap long lines using '\' :
371692f60a7Smrg  #    foo.o: sub/foo.c ... \
372692f60a7Smrg  #     sub/foo.h ... \
373692f60a7Smrg  #     ...
374eaa3dbe0Smrg  set_dir_from "$object"
375eaa3dbe0Smrg  # Use the source, not the object, to determine the base name, since
376eaa3dbe0Smrg  # that's sadly what pgcc will do too.
377eaa3dbe0Smrg  set_base_from "$source"
378eaa3dbe0Smrg  tmpdepfile=$base.d
379eaa3dbe0Smrg
380eaa3dbe0Smrg  # For projects that build the same source file twice into different object
381eaa3dbe0Smrg  # files, the pgcc approach of using the *source* file root name can cause
382eaa3dbe0Smrg  # problems in parallel builds.  Use a locking strategy to avoid stomping on
383eaa3dbe0Smrg  # the same $tmpdepfile.
384eaa3dbe0Smrg  lockdir=$base.d-lock
385eaa3dbe0Smrg  trap "
386eaa3dbe0Smrg    echo '$0: caught signal, cleaning up...' >&2
387eaa3dbe0Smrg    rmdir '$lockdir'
388eaa3dbe0Smrg    exit 1
389eaa3dbe0Smrg  " 1 2 13 15
390eaa3dbe0Smrg  numtries=100
391eaa3dbe0Smrg  i=$numtries
392eaa3dbe0Smrg  while test $i -gt 0; do
393eaa3dbe0Smrg    # mkdir is a portable test-and-set.
394eaa3dbe0Smrg    if mkdir "$lockdir" 2>/dev/null; then
395eaa3dbe0Smrg      # This process acquired the lock.
396eaa3dbe0Smrg      "$@" -MD
397eaa3dbe0Smrg      stat=$?
398eaa3dbe0Smrg      # Release the lock.
399eaa3dbe0Smrg      rmdir "$lockdir"
400eaa3dbe0Smrg      break
401eaa3dbe0Smrg    else
402eaa3dbe0Smrg      # If the lock is being held by a different process, wait
403eaa3dbe0Smrg      # until the winning process is done or we timeout.
404eaa3dbe0Smrg      while test -d "$lockdir" && test $i -gt 0; do
405eaa3dbe0Smrg        sleep 1
406eaa3dbe0Smrg        i=`expr $i - 1`
407eaa3dbe0Smrg      done
408eaa3dbe0Smrg    fi
409eaa3dbe0Smrg    i=`expr $i - 1`
410eaa3dbe0Smrg  done
411eaa3dbe0Smrg  trap - 1 2 13 15
412eaa3dbe0Smrg  if test $i -le 0; then
413eaa3dbe0Smrg    echo "$0: failed to acquire lock after $numtries attempts" >&2
414eaa3dbe0Smrg    echo "$0: check lockdir '$lockdir'" >&2
415eaa3dbe0Smrg    exit 1
416eaa3dbe0Smrg  fi
417692f60a7Smrg
418eaa3dbe0Smrg  if test $stat -ne 0; then
419692f60a7Smrg    rm -f "$tmpdepfile"
420692f60a7Smrg    exit $stat
421692f60a7Smrg  fi
422692f60a7Smrg  rm -f "$depfile"
423692f60a7Smrg  # Each line is of the form `foo.o: dependent.h',
424692f60a7Smrg  # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
425692f60a7Smrg  # Do two passes, one to just change these to
426692f60a7Smrg  # `$object: dependent.h' and one to simply `dependent.h:'.
427692f60a7Smrg  sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
428692f60a7Smrg  # Some versions of the HPUX 10.20 sed can't process this invocation
429692f60a7Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
430eaa3dbe0Smrg  sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \
431eaa3dbe0Smrg    | sed -e 's/$/ :/' >> "$depfile"
432692f60a7Smrg  rm -f "$tmpdepfile"
433692f60a7Smrg  ;;
434692f60a7Smrg
43554569438Smrghp2)
43654569438Smrg  # The "hp" stanza above does not work with aCC (C++) and HP's ia64
43754569438Smrg  # compilers, which have integrated preprocessors.  The correct option
43854569438Smrg  # to use with these is +Maked; it writes dependencies to a file named
43954569438Smrg  # 'foo.d', which lands next to the object file, wherever that
44054569438Smrg  # happens to be.
44154569438Smrg  # Much of this is similar to the tru64 case; see comments there.
442eaa3dbe0Smrg  set_dir_from  "$object"
443eaa3dbe0Smrg  set_base_from "$object"
44454569438Smrg  if test "$libtool" = yes; then
44554569438Smrg    tmpdepfile1=$dir$base.d
44654569438Smrg    tmpdepfile2=$dir.libs/$base.d
44754569438Smrg    "$@" -Wc,+Maked
44854569438Smrg  else
44954569438Smrg    tmpdepfile1=$dir$base.d
45054569438Smrg    tmpdepfile2=$dir$base.d
45154569438Smrg    "$@" +Maked
45254569438Smrg  fi
45354569438Smrg  stat=$?
454eaa3dbe0Smrg  if test $stat -ne 0; then
45554569438Smrg     rm -f "$tmpdepfile1" "$tmpdepfile2"
45654569438Smrg     exit $stat
45754569438Smrg  fi
45854569438Smrg
45954569438Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
46054569438Smrg  do
46154569438Smrg    test -f "$tmpdepfile" && break
46254569438Smrg  done
46354569438Smrg  if test -f "$tmpdepfile"; then
464eaa3dbe0Smrg    sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile"
465eaa3dbe0Smrg    # Add 'dependent.h:' lines.
4663f6d0e1dSmrg    sed -ne '2,${
467eaa3dbe0Smrg               s/^ *//
468eaa3dbe0Smrg               s/ \\*$//
469eaa3dbe0Smrg               s/$/:/
470eaa3dbe0Smrg               p
471eaa3dbe0Smrg             }' "$tmpdepfile" >> "$depfile"
47254569438Smrg  else
473eaa3dbe0Smrg    make_dummy_depfile
47454569438Smrg  fi
47554569438Smrg  rm -f "$tmpdepfile" "$tmpdepfile2"
47654569438Smrg  ;;
47754569438Smrg
478692f60a7Smrgtru64)
479eaa3dbe0Smrg  # The Tru64 compiler uses -MD to generate dependencies as a side
480eaa3dbe0Smrg  # effect.  'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'.
481eaa3dbe0Smrg  # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
482eaa3dbe0Smrg  # dependencies in 'foo.d' instead, so we check for that too.
483eaa3dbe0Smrg  # Subdirectories are respected.
484eaa3dbe0Smrg  set_dir_from  "$object"
485eaa3dbe0Smrg  set_base_from "$object"
486eaa3dbe0Smrg
487eaa3dbe0Smrg  if test "$libtool" = yes; then
488eaa3dbe0Smrg    # Libtool generates 2 separate objects for the 2 libraries.  These
489eaa3dbe0Smrg    # two compilations output dependencies in $dir.libs/$base.o.d and
490eaa3dbe0Smrg    # in $dir$base.o.d.  We have to check for both files, because
491eaa3dbe0Smrg    # one of the two compilations can be disabled.  We should prefer
492eaa3dbe0Smrg    # $dir$base.o.d over $dir.libs/$base.o.d because the latter is
493eaa3dbe0Smrg    # automatically cleaned when .libs/ is deleted, while ignoring
494eaa3dbe0Smrg    # the former would cause a distcleancheck panic.
495eaa3dbe0Smrg    tmpdepfile1=$dir$base.o.d          # libtool 1.5
496eaa3dbe0Smrg    tmpdepfile2=$dir.libs/$base.o.d    # Likewise.
497eaa3dbe0Smrg    tmpdepfile3=$dir.libs/$base.d      # Compaq CCC V6.2-504
498eaa3dbe0Smrg    "$@" -Wc,-MD
499eaa3dbe0Smrg  else
500eaa3dbe0Smrg    tmpdepfile1=$dir$base.d
501eaa3dbe0Smrg    tmpdepfile2=$dir$base.d
502eaa3dbe0Smrg    tmpdepfile3=$dir$base.d
503eaa3dbe0Smrg    "$@" -MD
504eaa3dbe0Smrg  fi
505eaa3dbe0Smrg
506eaa3dbe0Smrg  stat=$?
507eaa3dbe0Smrg  if test $stat -ne 0; then
508eaa3dbe0Smrg    rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
509eaa3dbe0Smrg    exit $stat
510eaa3dbe0Smrg  fi
511eaa3dbe0Smrg
512eaa3dbe0Smrg  for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
513eaa3dbe0Smrg  do
514eaa3dbe0Smrg    test -f "$tmpdepfile" && break
515eaa3dbe0Smrg  done
516eaa3dbe0Smrg  # Same post-processing that is required for AIX mode.
517eaa3dbe0Smrg  aix_post_process_depfile
518eaa3dbe0Smrg  ;;
519692f60a7Smrg
5203f6d0e1dSmrgmsvc7)
5213f6d0e1dSmrg  if test "$libtool" = yes; then
5223f6d0e1dSmrg    showIncludes=-Wc,-showIncludes
5233f6d0e1dSmrg  else
5243f6d0e1dSmrg    showIncludes=-showIncludes
5253f6d0e1dSmrg  fi
5263f6d0e1dSmrg  "$@" $showIncludes > "$tmpdepfile"
5273f6d0e1dSmrg  stat=$?
5283f6d0e1dSmrg  grep -v '^Note: including file: ' "$tmpdepfile"
529eaa3dbe0Smrg  if test $stat -ne 0; then
5303f6d0e1dSmrg    rm -f "$tmpdepfile"
5313f6d0e1dSmrg    exit $stat
5323f6d0e1dSmrg  fi
5333f6d0e1dSmrg  rm -f "$depfile"
5343f6d0e1dSmrg  echo "$object : \\" > "$depfile"
5353f6d0e1dSmrg  # The first sed program below extracts the file names and escapes
5363f6d0e1dSmrg  # backslashes for cygpath.  The second sed program outputs the file
5373f6d0e1dSmrg  # name when reading, but also accumulates all include files in the
5383f6d0e1dSmrg  # hold buffer in order to output them again at the end.  This only
5393f6d0e1dSmrg  # works with sed implementations that can handle large buffers.
5403f6d0e1dSmrg  sed < "$tmpdepfile" -n '
5413f6d0e1dSmrg/^Note: including file:  *\(.*\)/ {
5423f6d0e1dSmrg  s//\1/
5433f6d0e1dSmrg  s/\\/\\\\/g
5443f6d0e1dSmrg  p
5453f6d0e1dSmrg}' | $cygpath_u | sort -u | sed -n '
5463f6d0e1dSmrgs/ /\\ /g
547eaa3dbe0Smrgs/\(.*\)/'"$tab"'\1 \\/p
5483f6d0e1dSmrgs/.\(.*\) \\/\1:/
5493f6d0e1dSmrgH
5503f6d0e1dSmrg$ {
551eaa3dbe0Smrg  s/.*/'"$tab"'/
5523f6d0e1dSmrg  G
5533f6d0e1dSmrg  p
5543f6d0e1dSmrg}' >> "$depfile"
555eaa3dbe0Smrg  echo >> "$depfile" # make sure the fragment doesn't end with a backslash
5563f6d0e1dSmrg  rm -f "$tmpdepfile"
5573f6d0e1dSmrg  ;;
5583f6d0e1dSmrg
5593f6d0e1dSmrgmsvc7msys)
5603f6d0e1dSmrg  # This case exists only to let depend.m4 do its work.  It works by
5613f6d0e1dSmrg  # looking at the text of this script.  This case will never be run,
5623f6d0e1dSmrg  # since it is checked for above.
5633f6d0e1dSmrg  exit 1
5643f6d0e1dSmrg  ;;
5653f6d0e1dSmrg
566692f60a7Smrg#nosideeffect)
567692f60a7Smrg  # This comment above is used by automake to tell side-effect
568692f60a7Smrg  # dependency tracking mechanisms from slower ones.
569692f60a7Smrg
570692f60a7Smrgdashmstdout)
571692f60a7Smrg  # Important note: in order to support this mode, a compiler *must*
572692f60a7Smrg  # always write the preprocessed file to stdout, regardless of -o.
573692f60a7Smrg  "$@" || exit $?
574692f60a7Smrg
575692f60a7Smrg  # Remove the call to Libtool.
576692f60a7Smrg  if test "$libtool" = yes; then
5773f6d0e1dSmrg    while test "X$1" != 'X--mode=compile'; do
578692f60a7Smrg      shift
579692f60a7Smrg    done
580692f60a7Smrg    shift
581692f60a7Smrg  fi
582692f60a7Smrg
583eaa3dbe0Smrg  # Remove '-o $object'.
584692f60a7Smrg  IFS=" "
585692f60a7Smrg  for arg
586692f60a7Smrg  do
587692f60a7Smrg    case $arg in
588692f60a7Smrg    -o)
589692f60a7Smrg      shift
590692f60a7Smrg      ;;
591692f60a7Smrg    $object)
592692f60a7Smrg      shift
593692f60a7Smrg      ;;
594692f60a7Smrg    *)
595692f60a7Smrg      set fnord "$@" "$arg"
596692f60a7Smrg      shift # fnord
597692f60a7Smrg      shift # $arg
598692f60a7Smrg      ;;
599692f60a7Smrg    esac
600692f60a7Smrg  done
601692f60a7Smrg
602692f60a7Smrg  test -z "$dashmflag" && dashmflag=-M
603eaa3dbe0Smrg  # Require at least two characters before searching for ':'
604692f60a7Smrg  # in the target name.  This is to cope with DOS-style filenames:
605eaa3dbe0Smrg  # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise.
606692f60a7Smrg  "$@" $dashmflag |
607eaa3dbe0Smrg    sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile"
608692f60a7Smrg  rm -f "$depfile"
609692f60a7Smrg  cat < "$tmpdepfile" > "$depfile"
610eaa3dbe0Smrg  # Some versions of the HPUX 10.20 sed can't process this sed invocation
611eaa3dbe0Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
612eaa3dbe0Smrg  tr ' ' "$nl" < "$tmpdepfile" \
613eaa3dbe0Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
614eaa3dbe0Smrg    | sed -e 's/$/ :/' >> "$depfile"
615692f60a7Smrg  rm -f "$tmpdepfile"
616692f60a7Smrg  ;;
617692f60a7Smrg
618692f60a7SmrgdashXmstdout)
619692f60a7Smrg  # This case only exists to satisfy depend.m4.  It is never actually
620692f60a7Smrg  # run, as this mode is specially recognized in the preamble.
621692f60a7Smrg  exit 1
622692f60a7Smrg  ;;
623692f60a7Smrg
624692f60a7Smrgmakedepend)
625692f60a7Smrg  "$@" || exit $?
626692f60a7Smrg  # Remove any Libtool call
627692f60a7Smrg  if test "$libtool" = yes; then
6283f6d0e1dSmrg    while test "X$1" != 'X--mode=compile'; do
629692f60a7Smrg      shift
630692f60a7Smrg    done
631692f60a7Smrg    shift
632692f60a7Smrg  fi
633692f60a7Smrg  # X makedepend
634692f60a7Smrg  shift
6353f6d0e1dSmrg  cleared=no eat=no
6363f6d0e1dSmrg  for arg
6373f6d0e1dSmrg  do
638692f60a7Smrg    case $cleared in
639692f60a7Smrg    no)
640692f60a7Smrg      set ""; shift
641692f60a7Smrg      cleared=yes ;;
642692f60a7Smrg    esac
6433f6d0e1dSmrg    if test $eat = yes; then
6443f6d0e1dSmrg      eat=no
6453f6d0e1dSmrg      continue
6463f6d0e1dSmrg    fi
647692f60a7Smrg    case "$arg" in
648692f60a7Smrg    -D*|-I*)
649692f60a7Smrg      set fnord "$@" "$arg"; shift ;;
650692f60a7Smrg    # Strip any option that makedepend may not understand.  Remove
651692f60a7Smrg    # the object too, otherwise makedepend will parse it as a source file.
6523f6d0e1dSmrg    -arch)
6533f6d0e1dSmrg      eat=yes ;;
654692f60a7Smrg    -*|$object)
655692f60a7Smrg      ;;
656692f60a7Smrg    *)
657692f60a7Smrg      set fnord "$@" "$arg"; shift ;;
658692f60a7Smrg    esac
659692f60a7Smrg  done
6603f6d0e1dSmrg  obj_suffix=`echo "$object" | sed 's/^.*\././'`
661692f60a7Smrg  touch "$tmpdepfile"
662692f60a7Smrg  ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
663692f60a7Smrg  rm -f "$depfile"
6643f6d0e1dSmrg  # makedepend may prepend the VPATH from the source file name to the object.
6653f6d0e1dSmrg  # No need to regex-escape $object, excess matching of '.' is harmless.
6663f6d0e1dSmrg  sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
667eaa3dbe0Smrg  # Some versions of the HPUX 10.20 sed can't process the last invocation
668eaa3dbe0Smrg  # correctly.  Breaking it into two sed invocations is a workaround.
669eaa3dbe0Smrg  sed '1,2d' "$tmpdepfile" \
670eaa3dbe0Smrg    | tr ' ' "$nl" \
671eaa3dbe0Smrg    | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \
672eaa3dbe0Smrg    | sed -e 's/$/ :/' >> "$depfile"
673692f60a7Smrg  rm -f "$tmpdepfile" "$tmpdepfile".bak
674692f60a7Smrg  ;;
675692f60a7Smrg
676692f60a7Smrgcpp)
677692f60a7Smrg  # Important note: in order to support this mode, a compiler *must*
678692f60a7Smrg  # always write the preprocessed file to stdout.
679692f60a7Smrg  "$@" || exit $?
680692f60a7Smrg
681692f60a7Smrg  # Remove the call to Libtool.
682692f60a7Smrg  if test "$libtool" = yes; then
6833f6d0e1dSmrg    while test "X$1" != 'X--mode=compile'; do
684692f60a7Smrg      shift
685692f60a7Smrg    done
686692f60a7Smrg    shift
687692f60a7Smrg  fi
688692f60a7Smrg
689eaa3dbe0Smrg  # Remove '-o $object'.
690692f60a7Smrg  IFS=" "
691692f60a7Smrg  for arg
692692f60a7Smrg  do
693692f60a7Smrg    case $arg in
694692f60a7Smrg    -o)
695692f60a7Smrg      shift
696692f60a7Smrg      ;;
697692f60a7Smrg    $object)
698692f60a7Smrg      shift
699692f60a7Smrg      ;;
700692f60a7Smrg    *)
701692f60a7Smrg      set fnord "$@" "$arg"
702692f60a7Smrg      shift # fnord
703692f60a7Smrg      shift # $arg
704692f60a7Smrg      ;;
705692f60a7Smrg    esac
706692f60a7Smrg  done
707692f60a7Smrg
708eaa3dbe0Smrg  "$@" -E \
709eaa3dbe0Smrg    | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
710eaa3dbe0Smrg             -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
711eaa3dbe0Smrg    | sed '$ s: \\$::' > "$tmpdepfile"
712692f60a7Smrg  rm -f "$depfile"
713692f60a7Smrg  echo "$object : \\" > "$depfile"
714692f60a7Smrg  cat < "$tmpdepfile" >> "$depfile"
715692f60a7Smrg  sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
716692f60a7Smrg  rm -f "$tmpdepfile"
717692f60a7Smrg  ;;
718692f60a7Smrg
719692f60a7Smrgmsvisualcpp)
720692f60a7Smrg  # Important note: in order to support this mode, a compiler *must*
7213f6d0e1dSmrg  # always write the preprocessed file to stdout.
722692f60a7Smrg  "$@" || exit $?
7233f6d0e1dSmrg
7243f6d0e1dSmrg  # Remove the call to Libtool.
7253f6d0e1dSmrg  if test "$libtool" = yes; then
7263f6d0e1dSmrg    while test "X$1" != 'X--mode=compile'; do
7273f6d0e1dSmrg      shift
7283f6d0e1dSmrg    done
7293f6d0e1dSmrg    shift
7303f6d0e1dSmrg  fi
7313f6d0e1dSmrg
732692f60a7Smrg  IFS=" "
733692f60a7Smrg  for arg
734692f60a7Smrg  do
735692f60a7Smrg    case "$arg" in
7363f6d0e1dSmrg    -o)
7373f6d0e1dSmrg      shift
7383f6d0e1dSmrg      ;;
7393f6d0e1dSmrg    $object)
7403f6d0e1dSmrg      shift
7413f6d0e1dSmrg      ;;
742692f60a7Smrg    "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
743eaa3dbe0Smrg        set fnord "$@"
744eaa3dbe0Smrg        shift
745eaa3dbe0Smrg        shift
746eaa3dbe0Smrg        ;;
747692f60a7Smrg    *)
748eaa3dbe0Smrg        set fnord "$@" "$arg"
749eaa3dbe0Smrg        shift
750eaa3dbe0Smrg        shift
751eaa3dbe0Smrg        ;;
752692f60a7Smrg    esac
753692f60a7Smrg  done
7543f6d0e1dSmrg  "$@" -E 2>/dev/null |
7553f6d0e1dSmrg  sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
756692f60a7Smrg  rm -f "$depfile"
757692f60a7Smrg  echo "$object : \\" > "$depfile"
758eaa3dbe0Smrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile"
759eaa3dbe0Smrg  echo "$tab" >> "$depfile"
7603f6d0e1dSmrg  sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
761692f60a7Smrg  rm -f "$tmpdepfile"
762692f60a7Smrg  ;;
763692f60a7Smrg
7643f6d0e1dSmrgmsvcmsys)
7653f6d0e1dSmrg  # This case exists only to let depend.m4 do its work.  It works by
7663f6d0e1dSmrg  # looking at the text of this script.  This case will never be run,
7673f6d0e1dSmrg  # since it is checked for above.
7683f6d0e1dSmrg  exit 1
7693f6d0e1dSmrg  ;;
7703f6d0e1dSmrg
771692f60a7Smrgnone)
772692f60a7Smrg  exec "$@"
773692f60a7Smrg  ;;
774692f60a7Smrg
775692f60a7Smrg*)
776692f60a7Smrg  echo "Unknown depmode $depmode" 1>&2
777692f60a7Smrg  exit 1
778692f60a7Smrg  ;;
779692f60a7Smrgesac
780692f60a7Smrg
781692f60a7Smrgexit 0
782692f60a7Smrg
783692f60a7Smrg# Local Variables:
784692f60a7Smrg# mode: shell-script
785692f60a7Smrg# sh-indentation: 2
786efb46889Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
787692f60a7Smrg# time-stamp-start: "scriptversion="
788692f60a7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
789efb46889Smrg# time-stamp-time-zone: "UTC0"
7903f6d0e1dSmrg# time-stamp-end: "; # UTC"
791692f60a7Smrg# End:
792