cannam@125: #! /bin/sh
cannam@125: # Wrapper for compilers which do not understand '-c -o'.
cannam@125: 
cannam@125: scriptversion=2012-10-14.11; # UTC
cannam@125: 
cannam@125: # Copyright (C) 1999-2014 Free Software Foundation, Inc.
cannam@125: # Written by Tom Tromey <tromey@cygnus.com>.
cannam@125: #
cannam@125: # This program is free software; you can redistribute it and/or modify
cannam@125: # it under the terms of the GNU General Public License as published by
cannam@125: # the Free Software Foundation; either version 2, or (at your option)
cannam@125: # any later version.
cannam@125: #
cannam@125: # This program is distributed in the hope that it will be useful,
cannam@125: # but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@125: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cannam@125: # GNU General Public License for more details.
cannam@125: #
cannam@125: # You should have received a copy of the GNU General Public License
cannam@125: # along with this program.  If not, see <http://www.gnu.org/licenses/>.
cannam@125: 
cannam@125: # As a special exception to the GNU General Public License, if you
cannam@125: # distribute this file as part of a program that contains a
cannam@125: # configuration script generated by Autoconf, you may include it under
cannam@125: # the same distribution terms that you use for the rest of that program.
cannam@125: 
cannam@125: # This file is maintained in Automake, please report
cannam@125: # bugs to <bug-automake@gnu.org> or send patches to
cannam@125: # <automake-patches@gnu.org>.
cannam@125: 
cannam@125: nl='
cannam@125: '
cannam@125: 
cannam@125: # We need space, tab and new line, in precisely that order.  Quoting is
cannam@125: # there to prevent tools from complaining about whitespace usage.
cannam@125: IFS=" ""	$nl"
cannam@125: 
cannam@125: file_conv=
cannam@125: 
cannam@125: # func_file_conv build_file lazy
cannam@125: # Convert a $build file to $host form and store it in $file
cannam@125: # Currently only supports Windows hosts. If the determined conversion
cannam@125: # type is listed in (the comma separated) LAZY, no conversion will
cannam@125: # take place.
cannam@125: func_file_conv ()
cannam@125: {
cannam@125:   file=$1
cannam@125:   case $file in
cannam@125:     / | /[!/]*) # absolute file, and not a UNC file
cannam@125:       if test -z "$file_conv"; then
cannam@125: 	# lazily determine how to convert abs files
cannam@125: 	case `uname -s` in
cannam@125: 	  MINGW*)
cannam@125: 	    file_conv=mingw
cannam@125: 	    ;;
cannam@125: 	  CYGWIN*)
cannam@125: 	    file_conv=cygwin
cannam@125: 	    ;;
cannam@125: 	  *)
cannam@125: 	    file_conv=wine
cannam@125: 	    ;;
cannam@125: 	esac
cannam@125:       fi
cannam@125:       case $file_conv/,$2, in
cannam@125: 	*,$file_conv,*)
cannam@125: 	  ;;
cannam@125: 	mingw/*)
cannam@125: 	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
cannam@125: 	  ;;
cannam@125: 	cygwin/*)
cannam@125: 	  file=`cygpath -m "$file" || echo "$file"`
cannam@125: 	  ;;
cannam@125: 	wine/*)
cannam@125: 	  file=`winepath -w "$file" || echo "$file"`
cannam@125: 	  ;;
cannam@125:       esac
cannam@125:       ;;
cannam@125:   esac
cannam@125: }
cannam@125: 
cannam@125: # func_cl_dashL linkdir
cannam@125: # Make cl look for libraries in LINKDIR
cannam@125: func_cl_dashL ()
cannam@125: {
cannam@125:   func_file_conv "$1"
cannam@125:   if test -z "$lib_path"; then
cannam@125:     lib_path=$file
cannam@125:   else
cannam@125:     lib_path="$lib_path;$file"
cannam@125:   fi
cannam@125:   linker_opts="$linker_opts -LIBPATH:$file"
cannam@125: }
cannam@125: 
cannam@125: # func_cl_dashl library
cannam@125: # Do a library search-path lookup for cl
cannam@125: func_cl_dashl ()
cannam@125: {
cannam@125:   lib=$1
cannam@125:   found=no
cannam@125:   save_IFS=$IFS
cannam@125:   IFS=';'
cannam@125:   for dir in $lib_path $LIB
cannam@125:   do
cannam@125:     IFS=$save_IFS
cannam@125:     if $shared && test -f "$dir/$lib.dll.lib"; then
cannam@125:       found=yes
cannam@125:       lib=$dir/$lib.dll.lib
cannam@125:       break
cannam@125:     fi
cannam@125:     if test -f "$dir/$lib.lib"; then
cannam@125:       found=yes
cannam@125:       lib=$dir/$lib.lib
cannam@125:       break
cannam@125:     fi
cannam@125:     if test -f "$dir/lib$lib.a"; then
cannam@125:       found=yes
cannam@125:       lib=$dir/lib$lib.a
cannam@125:       break
cannam@125:     fi
cannam@125:   done
cannam@125:   IFS=$save_IFS
cannam@125: 
cannam@125:   if test "$found" != yes; then
cannam@125:     lib=$lib.lib
cannam@125:   fi
cannam@125: }
cannam@125: 
cannam@125: # func_cl_wrapper cl arg...
cannam@125: # Adjust compile command to suit cl
cannam@125: func_cl_wrapper ()
cannam@125: {
cannam@125:   # Assume a capable shell
cannam@125:   lib_path=
cannam@125:   shared=:
cannam@125:   linker_opts=
cannam@125:   for arg
cannam@125:   do
cannam@125:     if test -n "$eat"; then
cannam@125:       eat=
cannam@125:     else
cannam@125:       case $1 in
cannam@125: 	-o)
cannam@125: 	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
cannam@125: 	  eat=1
cannam@125: 	  case $2 in
cannam@125: 	    *.o | *.[oO][bB][jJ])
cannam@125: 	      func_file_conv "$2"
cannam@125: 	      set x "$@" -Fo"$file"
cannam@125: 	      shift
cannam@125: 	      ;;
cannam@125: 	    *)
cannam@125: 	      func_file_conv "$2"
cannam@125: 	      set x "$@" -Fe"$file"
cannam@125: 	      shift
cannam@125: 	      ;;
cannam@125: 	  esac
cannam@125: 	  ;;
cannam@125: 	-I)
cannam@125: 	  eat=1
cannam@125: 	  func_file_conv "$2" mingw
cannam@125: 	  set x "$@" -I"$file"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	-I*)
cannam@125: 	  func_file_conv "${1#-I}" mingw
cannam@125: 	  set x "$@" -I"$file"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	-l)
cannam@125: 	  eat=1
cannam@125: 	  func_cl_dashl "$2"
cannam@125: 	  set x "$@" "$lib"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	-l*)
cannam@125: 	  func_cl_dashl "${1#-l}"
cannam@125: 	  set x "$@" "$lib"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	-L)
cannam@125: 	  eat=1
cannam@125: 	  func_cl_dashL "$2"
cannam@125: 	  ;;
cannam@125: 	-L*)
cannam@125: 	  func_cl_dashL "${1#-L}"
cannam@125: 	  ;;
cannam@125: 	-static)
cannam@125: 	  shared=false
cannam@125: 	  ;;
cannam@125: 	-Wl,*)
cannam@125: 	  arg=${1#-Wl,}
cannam@125: 	  save_ifs="$IFS"; IFS=','
cannam@125: 	  for flag in $arg; do
cannam@125: 	    IFS="$save_ifs"
cannam@125: 	    linker_opts="$linker_opts $flag"
cannam@125: 	  done
cannam@125: 	  IFS="$save_ifs"
cannam@125: 	  ;;
cannam@125: 	-Xlinker)
cannam@125: 	  eat=1
cannam@125: 	  linker_opts="$linker_opts $2"
cannam@125: 	  ;;
cannam@125: 	-*)
cannam@125: 	  set x "$@" "$1"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
cannam@125: 	  func_file_conv "$1"
cannam@125: 	  set x "$@" -Tp"$file"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
cannam@125: 	  func_file_conv "$1" mingw
cannam@125: 	  set x "$@" "$file"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125: 	*)
cannam@125: 	  set x "$@" "$1"
cannam@125: 	  shift
cannam@125: 	  ;;
cannam@125:       esac
cannam@125:     fi
cannam@125:     shift
cannam@125:   done
cannam@125:   if test -n "$linker_opts"; then
cannam@125:     linker_opts="-link$linker_opts"
cannam@125:   fi
cannam@125:   exec "$@" $linker_opts
cannam@125:   exit 1
cannam@125: }
cannam@125: 
cannam@125: eat=
cannam@125: 
cannam@125: case $1 in
cannam@125:   '')
cannam@125:      echo "$0: No command.  Try '$0 --help' for more information." 1>&2
cannam@125:      exit 1;
cannam@125:      ;;
cannam@125:   -h | --h*)
cannam@125:     cat <<\EOF
cannam@125: Usage: compile [--help] [--version] PROGRAM [ARGS]
cannam@125: 
cannam@125: Wrapper for compilers which do not understand '-c -o'.
cannam@125: Remove '-o dest.o' from ARGS, run PROGRAM with the remaining
cannam@125: arguments, and rename the output as expected.
cannam@125: 
cannam@125: If you are trying to build a whole package this is not the
cannam@125: right script to run: please start by reading the file 'INSTALL'.
cannam@125: 
cannam@125: Report bugs to <bug-automake@gnu.org>.
cannam@125: EOF
cannam@125:     exit $?
cannam@125:     ;;
cannam@125:   -v | --v*)
cannam@125:     echo "compile $scriptversion"
cannam@125:     exit $?
cannam@125:     ;;
cannam@125:   cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
cannam@125:     func_cl_wrapper "$@"      # Doesn't return...
cannam@125:     ;;
cannam@125: esac
cannam@125: 
cannam@125: ofile=
cannam@125: cfile=
cannam@125: 
cannam@125: for arg
cannam@125: do
cannam@125:   if test -n "$eat"; then
cannam@125:     eat=
cannam@125:   else
cannam@125:     case $1 in
cannam@125:       -o)
cannam@125: 	# configure might choose to run compile as 'compile cc -o foo foo.c'.
cannam@125: 	# So we strip '-o arg' only if arg is an object.
cannam@125: 	eat=1
cannam@125: 	case $2 in
cannam@125: 	  *.o | *.obj)
cannam@125: 	    ofile=$2
cannam@125: 	    ;;
cannam@125: 	  *)
cannam@125: 	    set x "$@" -o "$2"
cannam@125: 	    shift
cannam@125: 	    ;;
cannam@125: 	esac
cannam@125: 	;;
cannam@125:       *.c)
cannam@125: 	cfile=$1
cannam@125: 	set x "$@" "$1"
cannam@125: 	shift
cannam@125: 	;;
cannam@125:       *)
cannam@125: 	set x "$@" "$1"
cannam@125: 	shift
cannam@125: 	;;
cannam@125:     esac
cannam@125:   fi
cannam@125:   shift
cannam@125: done
cannam@125: 
cannam@125: if test -z "$ofile" || test -z "$cfile"; then
cannam@125:   # If no '-o' option was seen then we might have been invoked from a
cannam@125:   # pattern rule where we don't need one.  That is ok -- this is a
cannam@125:   # normal compilation that the losing compiler can handle.  If no
cannam@125:   # '.c' file was seen then we are probably linking.  That is also
cannam@125:   # ok.
cannam@125:   exec "$@"
cannam@125: fi
cannam@125: 
cannam@125: # Name of file we expect compiler to create.
cannam@125: cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
cannam@125: 
cannam@125: # Create the lock directory.
cannam@125: # Note: use '[/\\:.-]' here to ensure that we don't use the same name
cannam@125: # that we are using for the .o file.  Also, base the name on the expected
cannam@125: # object file name, since that is what matters with a parallel build.
cannam@125: lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
cannam@125: while true; do
cannam@125:   if mkdir "$lockdir" >/dev/null 2>&1; then
cannam@125:     break
cannam@125:   fi
cannam@125:   sleep 1
cannam@125: done
cannam@125: # FIXME: race condition here if user kills between mkdir and trap.
cannam@125: trap "rmdir '$lockdir'; exit 1" 1 2 15
cannam@125: 
cannam@125: # Run the compile.
cannam@125: "$@"
cannam@125: ret=$?
cannam@125: 
cannam@125: if test -f "$cofile"; then
cannam@125:   test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
cannam@125: elif test -f "${cofile}bj"; then
cannam@125:   test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
cannam@125: fi
cannam@125: 
cannam@125: rmdir "$lockdir"
cannam@125: exit $ret
cannam@125: 
cannam@125: # Local Variables:
cannam@125: # mode: shell-script
cannam@125: # sh-indentation: 2
cannam@125: # eval: (add-hook 'write-file-hooks 'time-stamp)
cannam@125: # time-stamp-start: "scriptversion="
cannam@125: # time-stamp-format: "%:y-%02m-%02d.%02H"
cannam@125: # time-stamp-time-zone: "UTC"
cannam@125: # time-stamp-end: "; # UTC"
cannam@125: # End: