1132451Sroberto#! /bin/sh
2132451Sroberto# Wrapper for compilers which do not understand `-c -o'.
3132451Sroberto
4182007Srobertoscriptversion=2005-05-14.22
5182007Sroberto
6182007Sroberto# Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
7132451Sroberto# Written by Tom Tromey <tromey@cygnus.com>.
8132451Sroberto#
9132451Sroberto# This program is free software; you can redistribute it and/or modify
10132451Sroberto# it under the terms of the GNU General Public License as published by
11132451Sroberto# the Free Software Foundation; either version 2, or (at your option)
12132451Sroberto# any later version.
13132451Sroberto#
14132451Sroberto# This program is distributed in the hope that it will be useful,
15132451Sroberto# but WITHOUT ANY WARRANTY; without even the implied warranty of
16132451Sroberto# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17132451Sroberto# GNU General Public License for more details.
18132451Sroberto#
19132451Sroberto# You should have received a copy of the GNU General Public License
20132451Sroberto# along with this program; if not, write to the Free Software
21182007Sroberto# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22132451Sroberto
23132451Sroberto# As a special exception to the GNU General Public License, if you
24132451Sroberto# distribute this file as part of a program that contains a
25132451Sroberto# configuration script generated by Autoconf, you may include it under
26132451Sroberto# the same distribution terms that you use for the rest of that program.
27132451Sroberto
28182007Sroberto# This file is maintained in Automake, please report
29182007Sroberto# bugs to <bug-automake@gnu.org> or send patches to
30182007Sroberto# <automake-patches@gnu.org>.
31132451Sroberto
32182007Srobertocase $1 in
33182007Sroberto  '')
34182007Sroberto     echo "$0: No command.  Try \`$0 --help' for more information." 1>&2
35182007Sroberto     exit 1;
36182007Sroberto     ;;
37182007Sroberto  -h | --h*)
38182007Sroberto    cat <<\EOF
39182007SrobertoUsage: compile [--help] [--version] PROGRAM [ARGS]
40132451Sroberto
41182007SrobertoWrapper for compilers which do not understand `-c -o'.
42182007SrobertoRemove `-o dest.o' from ARGS, run PROGRAM with the remaining
43182007Srobertoarguments, and rename the output as expected.
44182007Sroberto
45182007SrobertoIf you are trying to build a whole package this is not the
46182007Srobertoright script to run: please start by reading the file `INSTALL'.
47182007Sroberto
48182007SrobertoReport bugs to <bug-automake@gnu.org>.
49182007SrobertoEOF
50182007Sroberto    exit $?
51182007Sroberto    ;;
52182007Sroberto  -v | --v*)
53182007Sroberto    echo "compile $scriptversion"
54182007Sroberto    exit $?
55182007Sroberto    ;;
56182007Srobertoesac
57182007Sroberto
58132451Srobertoofile=
59132451Srobertocfile=
60182007Srobertoeat=
61182007Sroberto
62182007Srobertofor arg
63182007Srobertodo
64182007Sroberto  if test -n "$eat"; then
65182007Sroberto    eat=
66182007Sroberto  else
67182007Sroberto    case $1 in
68182007Sroberto      -o)
69182007Sroberto	# configure might choose to run compile as `compile cc -o foo foo.c'.
70182007Sroberto	# So we strip `-o arg' only if arg is an object.
71182007Sroberto	eat=1
72182007Sroberto	case $2 in
73182007Sroberto	  *.o | *.obj)
74182007Sroberto	    ofile=$2
75182007Sroberto	    ;;
76182007Sroberto	  *)
77182007Sroberto	    set x "$@" -o "$2"
78182007Sroberto	    shift
79182007Sroberto	    ;;
80182007Sroberto	esac
81182007Sroberto	;;
82182007Sroberto      *.c)
83182007Sroberto	cfile=$1
84182007Sroberto	set x "$@" "$1"
85182007Sroberto	shift
86182007Sroberto	;;
87182007Sroberto      *)
88182007Sroberto	set x "$@" "$1"
89182007Sroberto	shift
90182007Sroberto	;;
91182007Sroberto    esac
92182007Sroberto  fi
93182007Sroberto  shift
94132451Srobertodone
95132451Sroberto
96132451Srobertoif test -z "$ofile" || test -z "$cfile"; then
97182007Sroberto  # If no `-o' option was seen then we might have been invoked from a
98182007Sroberto  # pattern rule where we don't need one.  That is ok -- this is a
99182007Sroberto  # normal compilation that the losing compiler can handle.  If no
100182007Sroberto  # `.c' file was seen then we are probably linking.  That is also
101182007Sroberto  # ok.
102182007Sroberto  exec "$@"
103132451Srobertofi
104132451Sroberto
105132451Sroberto# Name of file we expect compiler to create.
106182007Srobertocofile=`echo "$cfile" | sed -e 's|^.*/||' -e 's/\.c$/.o/'`
107132451Sroberto
108132451Sroberto# Create the lock directory.
109132451Sroberto# Note: use `[/.-]' here to ensure that we don't use the same name
110132451Sroberto# that we are using for the .o file.  Also, base the name on the expected
111132451Sroberto# object file name, since that is what matters with a parallel build.
112182007Srobertolockdir=`echo "$cofile" | sed -e 's|[/.-]|_|g'`.d
113132451Srobertowhile true; do
114182007Sroberto  if mkdir "$lockdir" >/dev/null 2>&1; then
115182007Sroberto    break
116182007Sroberto  fi
117182007Sroberto  sleep 1
118132451Srobertodone
119132451Sroberto# FIXME: race condition here if user kills between mkdir and trap.
120182007Srobertotrap "rmdir '$lockdir'; exit 1" 1 2 15
121132451Sroberto
122132451Sroberto# Run the compile.
123182007Sroberto"$@"
124182007Srobertoret=$?
125132451Sroberto
126132451Srobertoif test -f "$cofile"; then
127182007Sroberto  mv "$cofile" "$ofile"
128182007Srobertoelif test -f "${cofile}bj"; then
129182007Sroberto  mv "${cofile}bj" "$ofile"
130132451Srobertofi
131132451Sroberto
132182007Srobertormdir "$lockdir"
133182007Srobertoexit $ret
134182007Sroberto
135182007Sroberto# Local Variables:
136182007Sroberto# mode: shell-script
137182007Sroberto# sh-indentation: 2
138182007Sroberto# eval: (add-hook 'write-file-hooks 'time-stamp)
139182007Sroberto# time-stamp-start: "scriptversion="
140182007Sroberto# time-stamp-format: "%:y-%02m-%02d.%02H"
141182007Sroberto# time-stamp-end: "$"
142182007Sroberto# End:
143