1275970Scy#! /bin/sh
2275970Scy# Wrapper for compilers which do not understand `-c -o'.
3275970Scy
4275970Scyscriptversion=2009-10-06.20; # UTC
5275970Scy
6275970Scy# Copyright (C) 1999, 2000, 2003, 2004, 2005, 2009  Free Software
7275970Scy# Foundation, Inc.
8275970Scy# Written by Tom Tromey <tromey@cygnus.com>.
9275970Scy#
10275970Scy# This program is free software; you can redistribute it and/or modify
11275970Scy# it under the terms of the GNU General Public License as published by
12275970Scy# the Free Software Foundation; either version 2, or (at your option)
13275970Scy# any later version.
14275970Scy#
15275970Scy# This program is distributed in the hope that it will be useful,
16275970Scy# but WITHOUT ANY WARRANTY; without even the implied warranty of
17275970Scy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18275970Scy# GNU General Public License for more details.
19275970Scy#
20275970Scy# You should have received a copy of the GNU General Public License
21275970Scy# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22275970Scy
23275970Scy# As a special exception to the GNU General Public License, if you
24275970Scy# distribute this file as part of a program that contains a
25275970Scy# configuration script generated by Autoconf, you may include it under
26275970Scy# the same distribution terms that you use for the rest of that program.
27275970Scy
28275970Scy# This file is maintained in Automake, please report
29275970Scy# bugs to <bug-automake@gnu.org> or send patches to
30275970Scy# <automake-patches@gnu.org>.
31275970Scy
32275970Scycase $1 in
33275970Scy  '')
34275970Scy     echo "$0: No command.  Try \`$0 --help' for more information." 1>&2
35275970Scy     exit 1;
36275970Scy     ;;
37275970Scy  -h | --h*)
38275970Scy    cat <<\EOF
39275970ScyUsage: compile [--help] [--version] PROGRAM [ARGS]
40275970Scy
41275970ScyWrapper for compilers which do not understand `-c -o'.
42275970ScyRemove `-o dest.o' from ARGS, run PROGRAM with the remaining
43275970Scyarguments, and rename the output as expected.
44275970Scy
45275970ScyIf you are trying to build a whole package this is not the
46275970Scyright script to run: please start by reading the file `INSTALL'.
47275970Scy
48275970ScyReport bugs to <bug-automake@gnu.org>.
49275970ScyEOF
50275970Scy    exit $?
51275970Scy    ;;
52275970Scy  -v | --v*)
53275970Scy    echo "compile $scriptversion"
54275970Scy    exit $?
55275970Scy    ;;
56275970Scyesac
57275970Scy
58275970Scyofile=
59275970Scycfile=
60275970Scyeat=
61275970Scy
62275970Scyfor arg
63275970Scydo
64275970Scy  if test -n "$eat"; then
65275970Scy    eat=
66275970Scy  else
67275970Scy    case $1 in
68275970Scy      -o)
69275970Scy	# configure might choose to run compile as `compile cc -o foo foo.c'.
70275970Scy	# So we strip `-o arg' only if arg is an object.
71275970Scy	eat=1
72275970Scy	case $2 in
73275970Scy	  *.o | *.obj)
74275970Scy	    ofile=$2
75275970Scy	    ;;
76275970Scy	  *)
77275970Scy	    set x "$@" -o "$2"
78275970Scy	    shift
79275970Scy	    ;;
80275970Scy	esac
81275970Scy	;;
82275970Scy      *.c)
83275970Scy	cfile=$1
84275970Scy	set x "$@" "$1"
85275970Scy	shift
86275970Scy	;;
87275970Scy      *)
88275970Scy	set x "$@" "$1"
89275970Scy	shift
90275970Scy	;;
91275970Scy    esac
92275970Scy  fi
93275970Scy  shift
94275970Scydone
95275970Scy
96275970Scyif test -z "$ofile" || test -z "$cfile"; then
97275970Scy  # If no `-o' option was seen then we might have been invoked from a
98275970Scy  # pattern rule where we don't need one.  That is ok -- this is a
99275970Scy  # normal compilation that the losing compiler can handle.  If no
100275970Scy  # `.c' file was seen then we are probably linking.  That is also
101275970Scy  # ok.
102275970Scy  exec "$@"
103275970Scyfi
104275970Scy
105275970Scy# Name of file we expect compiler to create.
106275970Scycofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
107275970Scy
108275970Scy# Create the lock directory.
109275970Scy# Note: use `[/\\:.-]' here to ensure that we don't use the same name
110275970Scy# that we are using for the .o file.  Also, base the name on the expected
111275970Scy# object file name, since that is what matters with a parallel build.
112275970Scylockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
113275970Scywhile true; do
114275970Scy  if mkdir "$lockdir" >/dev/null 2>&1; then
115275970Scy    break
116275970Scy  fi
117275970Scy  sleep 1
118275970Scydone
119275970Scy# FIXME: race condition here if user kills between mkdir and trap.
120275970Scytrap "rmdir '$lockdir'; exit 1" 1 2 15
121275970Scy
122275970Scy# Run the compile.
123275970Scy"$@"
124275970Scyret=$?
125275970Scy
126275970Scyif test -f "$cofile"; then
127275970Scy  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
128275970Scyelif test -f "${cofile}bj"; then
129275970Scy  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
130275970Scyfi
131275970Scy
132275970Scyrmdir "$lockdir"
133275970Scyexit $ret
134275970Scy
135275970Scy# Local Variables:
136275970Scy# mode: shell-script
137275970Scy# sh-indentation: 2
138275970Scy# eval: (add-hook 'write-file-hooks 'time-stamp)
139275970Scy# time-stamp-start: "scriptversion="
140275970Scy# time-stamp-format: "%:y-%02m-%02d.%02H"
141275970Scy# time-stamp-time-zone: "UTC"
142275970Scy# time-stamp-end: "; # UTC"
143275970Scy# End:
144