14963b7b6Smrg#! /bin/sh
24963b7b6Smrg
34963b7b6Smrg# Wrapper for compilers which do not understand `-c -o'.
44963b7b6Smrg
54963b7b6Smrg# Copyright 1999, 2000 Free Software Foundation, Inc.
64963b7b6Smrg# Written by Tom Tromey <tromey@cygnus.com>.
74963b7b6Smrg#
84963b7b6Smrg# This program is free software; you can redistribute it and/or modify
94963b7b6Smrg# it under the terms of the GNU General Public License as published by
104963b7b6Smrg# the Free Software Foundation; either version 2, or (at your option)
114963b7b6Smrg# any later version.
124963b7b6Smrg#
134963b7b6Smrg# This program is distributed in the hope that it will be useful,
144963b7b6Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
154963b7b6Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
164963b7b6Smrg# GNU General Public License for more details.
174963b7b6Smrg#
184963b7b6Smrg# You should have received a copy of the GNU General Public License
194963b7b6Smrg# along with this program; if not, write to the Free Software
204963b7b6Smrg# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
214963b7b6Smrg
224963b7b6Smrg# As a special exception to the GNU General Public License, if you
234963b7b6Smrg# distribute this file as part of a program that contains a
244963b7b6Smrg# configuration script generated by Autoconf, you may include it under
254963b7b6Smrg# the same distribution terms that you use for the rest of that program.
264963b7b6Smrg
274963b7b6Smrg# Usage:
284963b7b6Smrg# compile PROGRAM [ARGS]...
294963b7b6Smrg# `-o FOO.o' is removed from the args passed to the actual compile.
304963b7b6Smrg
314963b7b6Smrgprog=$1
324963b7b6Smrgshift
334963b7b6Smrg
344963b7b6Smrgofile=
354963b7b6Smrgcfile=
364963b7b6Smrgargs=
374963b7b6Smrgwhile test $# -gt 0; do
384963b7b6Smrg   case "$1" in
394963b7b6Smrg    -o)
404963b7b6Smrg       # configure might choose to run compile as `compile cc -o foo foo.c'.
414963b7b6Smrg       # So we do something ugly here.
424963b7b6Smrg       ofile=$2
434963b7b6Smrg       shift
444963b7b6Smrg       case "$ofile" in
454963b7b6Smrg	*.o | *.obj)
464963b7b6Smrg	   ;;
474963b7b6Smrg	*)
484963b7b6Smrg	   args="$args -o $ofile"
494963b7b6Smrg	   ofile=
504963b7b6Smrg	   ;;
514963b7b6Smrg       esac
524963b7b6Smrg       ;;
534963b7b6Smrg    *.c)
544963b7b6Smrg       cfile=$1
554963b7b6Smrg       args="$args $1"
564963b7b6Smrg       ;;
574963b7b6Smrg    *)
584963b7b6Smrg       args="$args $1"
594963b7b6Smrg       ;;
604963b7b6Smrg   esac
614963b7b6Smrg   shift
624963b7b6Smrgdone
634963b7b6Smrg
644963b7b6Smrgif test -z "$ofile" || test -z "$cfile"; then
654963b7b6Smrg   # If no `-o' option was seen then we might have been invoked from a
664963b7b6Smrg   # pattern rule where we don't need one.  That is ok -- this is a
674963b7b6Smrg   # normal compilation that the losing compiler can handle.  If no
684963b7b6Smrg   # `.c' file was seen then we are probably linking.  That is also
694963b7b6Smrg   # ok.
704963b7b6Smrg   exec "$prog" $args
714963b7b6Smrgfi
724963b7b6Smrg
734963b7b6Smrg# Name of file we expect compiler to create.
744963b7b6Smrgcofile=`echo $cfile | sed -e 's|^.*/||' -e 's/\.c$/.o/'`
754963b7b6Smrg
764963b7b6Smrg# Create the lock directory.
774963b7b6Smrg# Note: use `[/.-]' here to ensure that we don't use the same name
784963b7b6Smrg# that we are using for the .o file.  Also, base the name on the expected
794963b7b6Smrg# object file name, since that is what matters with a parallel build.
804963b7b6Smrglockdir=`echo $cofile | sed -e 's|[/.-]|_|g'`.d
814963b7b6Smrgwhile true; do
824963b7b6Smrg   if mkdir $lockdir > /dev/null 2>&1; then
834963b7b6Smrg      break
844963b7b6Smrg   fi
854963b7b6Smrg   sleep 1
864963b7b6Smrgdone
874963b7b6Smrg# FIXME: race condition here if user kills between mkdir and trap.
884963b7b6Smrgtrap "rmdir $lockdir; exit 1" 1 2 15
894963b7b6Smrg
904963b7b6Smrg# Run the compile.
914963b7b6Smrg"$prog" $args
924963b7b6Smrgstatus=$?
934963b7b6Smrg
944963b7b6Smrgif test -f "$cofile"; then
954963b7b6Smrg   mv "$cofile" "$ofile"
964963b7b6Smrgfi
974963b7b6Smrg
984963b7b6Smrgrmdir $lockdir
994963b7b6Smrgexit $status
100