1219820Sjeff#! /bin/sh
2219820Sjeff# Wrapper for compilers which do not understand `-c -o'.
3219820Sjeff
4219820Sjeffscriptversion=2005-05-14.22
5219820Sjeff
6219820Sjeff# Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
7219820Sjeff# Written by Tom Tromey <tromey@cygnus.com>.
8219820Sjeff#
9219820Sjeff# This program is free software; you can redistribute it and/or modify
10219820Sjeff# it under the terms of the GNU General Public License as published by
11219820Sjeff# the Free Software Foundation; either version 2, or (at your option)
12219820Sjeff# any later version.
13219820Sjeff#
14219820Sjeff# This program is distributed in the hope that it will be useful,
15219820Sjeff# but WITHOUT ANY WARRANTY; without even the implied warranty of
16219820Sjeff# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17219820Sjeff# GNU General Public License for more details.
18219820Sjeff#
19219820Sjeff# You should have received a copy of the GNU General Public License
20219820Sjeff# along with this program; if not, write to the Free Software
21219820Sjeff# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22219820Sjeff
23219820Sjeff# As a special exception to the GNU General Public License, if you
24219820Sjeff# distribute this file as part of a program that contains a
25219820Sjeff# configuration script generated by Autoconf, you may include it under
26219820Sjeff# the same distribution terms that you use for the rest of that program.
27219820Sjeff
28219820Sjeff# This file is maintained in Automake, please report
29219820Sjeff# bugs to <bug-automake@gnu.org> or send patches to
30219820Sjeff# <automake-patches@gnu.org>.
31219820Sjeff
32219820Sjeffcase $1 in
33219820Sjeff  '')
34219820Sjeff     echo "$0: No command.  Try \`$0 --help' for more information." 1>&2
35219820Sjeff     exit 1;
36219820Sjeff     ;;
37219820Sjeff  -h | --h*)
38219820Sjeff    cat <<\EOF
39219820SjeffUsage: compile [--help] [--version] PROGRAM [ARGS]
40219820Sjeff
41219820SjeffWrapper for compilers which do not understand `-c -o'.
42219820SjeffRemove `-o dest.o' from ARGS, run PROGRAM with the remaining
43219820Sjeffarguments, and rename the output as expected.
44219820Sjeff
45219820SjeffIf you are trying to build a whole package this is not the
46219820Sjeffright script to run: please start by reading the file `INSTALL'.
47219820Sjeff
48219820SjeffReport bugs to <bug-automake@gnu.org>.
49219820SjeffEOF
50219820Sjeff    exit $?
51219820Sjeff    ;;
52219820Sjeff  -v | --v*)
53219820Sjeff    echo "compile $scriptversion"
54219820Sjeff    exit $?
55219820Sjeff    ;;
56219820Sjeffesac
57219820Sjeff
58219820Sjeffofile=
59219820Sjeffcfile=
60219820Sjeffeat=
61219820Sjeff
62219820Sjefffor arg
63219820Sjeffdo
64219820Sjeff  if test -n "$eat"; then
65219820Sjeff    eat=
66219820Sjeff  else
67219820Sjeff    case $1 in
68219820Sjeff      -o)
69219820Sjeff	# configure might choose to run compile as `compile cc -o foo foo.c'.
70219820Sjeff	# So we strip `-o arg' only if arg is an object.
71219820Sjeff	eat=1
72219820Sjeff	case $2 in
73219820Sjeff	  *.o | *.obj)
74219820Sjeff	    ofile=$2
75219820Sjeff	    ;;
76219820Sjeff	  *)
77219820Sjeff	    set x "$@" -o "$2"
78219820Sjeff	    shift
79219820Sjeff	    ;;
80219820Sjeff	esac
81219820Sjeff	;;
82219820Sjeff      *.c)
83219820Sjeff	cfile=$1
84219820Sjeff	set x "$@" "$1"
85219820Sjeff	shift
86219820Sjeff	;;
87219820Sjeff      *)
88219820Sjeff	set x "$@" "$1"
89219820Sjeff	shift
90219820Sjeff	;;
91219820Sjeff    esac
92219820Sjeff  fi
93219820Sjeff  shift
94219820Sjeffdone
95219820Sjeff
96219820Sjeffif test -z "$ofile" || test -z "$cfile"; then
97219820Sjeff  # If no `-o' option was seen then we might have been invoked from a
98219820Sjeff  # pattern rule where we don't need one.  That is ok -- this is a
99219820Sjeff  # normal compilation that the losing compiler can handle.  If no
100219820Sjeff  # `.c' file was seen then we are probably linking.  That is also
101219820Sjeff  # ok.
102219820Sjeff  exec "$@"
103219820Sjefffi
104219820Sjeff
105219820Sjeff# Name of file we expect compiler to create.
106219820Sjeffcofile=`echo "$cfile" | sed -e 's|^.*/||' -e 's/\.c$/.o/'`
107219820Sjeff
108219820Sjeff# Create the lock directory.
109219820Sjeff# Note: use `[/.-]' here to ensure that we don't use the same name
110219820Sjeff# that we are using for the .o file.  Also, base the name on the expected
111219820Sjeff# object file name, since that is what matters with a parallel build.
112219820Sjefflockdir=`echo "$cofile" | sed -e 's|[/.-]|_|g'`.d
113219820Sjeffwhile true; do
114219820Sjeff  if mkdir "$lockdir" >/dev/null 2>&1; then
115219820Sjeff    break
116219820Sjeff  fi
117219820Sjeff  sleep 1
118219820Sjeffdone
119219820Sjeff# FIXME: race condition here if user kills between mkdir and trap.
120219820Sjefftrap "rmdir '$lockdir'; exit 1" 1 2 15
121219820Sjeff
122219820Sjeff# Run the compile.
123219820Sjeff"$@"
124219820Sjeffret=$?
125219820Sjeff
126219820Sjeffif test -f "$cofile"; then
127219820Sjeff  mv "$cofile" "$ofile"
128219820Sjeffelif test -f "${cofile}bj"; then
129219820Sjeff  mv "${cofile}bj" "$ofile"
130219820Sjefffi
131219820Sjeff
132219820Sjeffrmdir "$lockdir"
133219820Sjeffexit $ret
134219820Sjeff
135219820Sjeff# Local Variables:
136219820Sjeff# mode: shell-script
137219820Sjeff# sh-indentation: 2
138219820Sjeff# eval: (add-hook 'write-file-hooks 'time-stamp)
139219820Sjeff# time-stamp-start: "scriptversion="
140219820Sjeff# time-stamp-format: "%:y-%02m-%02d.%02H"
141219820Sjeff# time-stamp-end: "$"
142219820Sjeff# End:
143