17c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#! /bin/sh
27c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
37c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Wrapper for compilers which do not understand `-c -o'.
47c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
57c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Copyright 1999, 2000 Free Software Foundation, Inc.
67c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Written by Tom Tromey <tromey@cygnus.com>.
77c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#
87c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# This program is free software; you can redistribute it and/or modify
97c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# it under the terms of the GNU General Public License as published by
107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# the Free Software Foundation; either version 2, or (at your option)
117c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# any later version.
127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#
137c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# This program is distributed in the hope that it will be useful,
147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# but WITHOUT ANY WARRANTY; without even the implied warranty of
157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# GNU General Public License for more details.
177c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#
187c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# You should have received a copy of the GNU General Public License
197c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# along with this program; if not, write to the Free Software
207c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
217c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
227c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# As a special exception to the GNU General Public License, if you
237c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# distribute this file as part of a program that contains a
247c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# configuration script generated by Autoconf, you may include it under
257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# the same distribution terms that you use for the rest of that program.
267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Usage:
287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# compile PROGRAM [ARGS]...
297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# `-o FOO.o' is removed from the args passed to the actual compile.
307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetprog=$1
327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetshift
337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetofile=
357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetcfile=
367c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetargs=
377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetwhile test $# -gt 0; do
387c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   case "$1" in
397c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    -o)
407c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       # configure might choose to run compile as `compile cc -o foo foo.c'.
417c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       # So we do something ugly here.
427c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       ofile=$2
437c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       shift
447c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       case "$ofile" in
457c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	*.o | *.obj)
467c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	   ;;
477c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	*)
487c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	   args="$args -o $ofile"
497c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	   ofile=
507c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet	   ;;
517c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       esac
527c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       ;;
537c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    *.c)
547c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       cfile=$1
557c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       args="$args $1"
567c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       ;;
577c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    *)
587c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       args="$args $1"
597c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet       ;;
607c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   esac
617c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   shift
627c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetdone
637c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
647c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetif test -z "$ofile" || test -z "$cfile"; then
657c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   # If no `-o' option was seen then we might have been invoked from a
667c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   # pattern rule where we don't need one.  That is ok -- this is a
677c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   # normal compilation that the losing compiler can handle.  If no
687c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   # `.c' file was seen then we are probably linking.  That is also
697c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   # ok.
707c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   exec "$prog" $args
717c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetfi
727c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
737c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Name of file we expect compiler to create.
747c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetcofile=`echo $cfile | sed -e 's|^.*/||' -e 's/\.c$/.o/'`
757c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
767c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Create the lock directory.
777c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Note: use `[/.-]' here to ensure that we don't use the same name
787c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# that we are using for the .o file.  Also, base the name on the expected
797c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# object file name, since that is what matters with a parallel build.
807c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetlockdir=`echo $cofile | sed -e 's|[/.-]|_|g'`.d
817c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetwhile true; do
827c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   if mkdir $lockdir > /dev/null 2>&1; then
837c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet      break
847c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   fi
857c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   sleep 1
867c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetdone
877c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# FIXME: race condition here if user kills between mkdir and trap.
887c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichettrap "rmdir $lockdir; exit 1" 1 2 15
897c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
907c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet# Run the compile.
917c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet"$prog" $args
927c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetstatus=$?
937c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
947c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetif test -f "$cofile"; then
957c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet   mv "$cofile" "$ofile"
967c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetfi
977c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
987c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetrmdir $lockdir
997c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetexit $status
100