184df3cf2Smrg#! /bin/sh
284df3cf2Smrg# Wrapper for compilers which do not understand '-c -o'.
384df3cf2Smrg
484df3cf2Smrgscriptversion=2012-10-14.11; # UTC
584df3cf2Smrg
684df3cf2Smrg# Copyright (C) 1999-2014 Free Software Foundation, Inc.
784df3cf2Smrg# Written by Tom Tromey <tromey@cygnus.com>.
884df3cf2Smrg#
984df3cf2Smrg# This program is free software; you can redistribute it and/or modify
1084df3cf2Smrg# it under the terms of the GNU General Public License as published by
1184df3cf2Smrg# the Free Software Foundation; either version 2, or (at your option)
1284df3cf2Smrg# any later version.
1384df3cf2Smrg#
1484df3cf2Smrg# This program is distributed in the hope that it will be useful,
1584df3cf2Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1684df3cf2Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1784df3cf2Smrg# GNU General Public License for more details.
1884df3cf2Smrg#
1984df3cf2Smrg# You should have received a copy of the GNU General Public License
2084df3cf2Smrg# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2184df3cf2Smrg
2284df3cf2Smrg# As a special exception to the GNU General Public License, if you
2384df3cf2Smrg# distribute this file as part of a program that contains a
2484df3cf2Smrg# configuration script generated by Autoconf, you may include it under
2584df3cf2Smrg# the same distribution terms that you use for the rest of that program.
2684df3cf2Smrg
2784df3cf2Smrg# This file is maintained in Automake, please report
2884df3cf2Smrg# bugs to <bug-automake@gnu.org> or send patches to
2984df3cf2Smrg# <automake-patches@gnu.org>.
3084df3cf2Smrg
3184df3cf2Smrgnl='
3284df3cf2Smrg'
3384df3cf2Smrg
3484df3cf2Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3584df3cf2Smrg# there to prevent tools from complaining about whitespace usage.
3684df3cf2SmrgIFS=" ""	$nl"
3784df3cf2Smrg
3884df3cf2Smrgfile_conv=
3984df3cf2Smrg
4084df3cf2Smrg# func_file_conv build_file lazy
4184df3cf2Smrg# Convert a $build file to $host form and store it in $file
4284df3cf2Smrg# Currently only supports Windows hosts. If the determined conversion
4384df3cf2Smrg# type is listed in (the comma separated) LAZY, no conversion will
4484df3cf2Smrg# take place.
4584df3cf2Smrgfunc_file_conv ()
4684df3cf2Smrg{
4784df3cf2Smrg  file=$1
4884df3cf2Smrg  case $file in
4984df3cf2Smrg    / | /[!/]*) # absolute file, and not a UNC file
5084df3cf2Smrg      if test -z "$file_conv"; then
5184df3cf2Smrg	# lazily determine how to convert abs files
5284df3cf2Smrg	case `uname -s` in
5384df3cf2Smrg	  MINGW*)
5484df3cf2Smrg	    file_conv=mingw
5584df3cf2Smrg	    ;;
5684df3cf2Smrg	  CYGWIN*)
5784df3cf2Smrg	    file_conv=cygwin
5884df3cf2Smrg	    ;;
5984df3cf2Smrg	  *)
6084df3cf2Smrg	    file_conv=wine
6184df3cf2Smrg	    ;;
6284df3cf2Smrg	esac
6384df3cf2Smrg      fi
6484df3cf2Smrg      case $file_conv/,$2, in
6584df3cf2Smrg	*,$file_conv,*)
6684df3cf2Smrg	  ;;
6784df3cf2Smrg	mingw/*)
6884df3cf2Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6984df3cf2Smrg	  ;;
7084df3cf2Smrg	cygwin/*)
7184df3cf2Smrg	  file=`cygpath -m "$file" || echo "$file"`
7284df3cf2Smrg	  ;;
7384df3cf2Smrg	wine/*)
7484df3cf2Smrg	  file=`winepath -w "$file" || echo "$file"`
7584df3cf2Smrg	  ;;
7684df3cf2Smrg      esac
7784df3cf2Smrg      ;;
7884df3cf2Smrg  esac
7984df3cf2Smrg}
8084df3cf2Smrg
8184df3cf2Smrg# func_cl_dashL linkdir
8284df3cf2Smrg# Make cl look for libraries in LINKDIR
8384df3cf2Smrgfunc_cl_dashL ()
8484df3cf2Smrg{
8584df3cf2Smrg  func_file_conv "$1"
8684df3cf2Smrg  if test -z "$lib_path"; then
8784df3cf2Smrg    lib_path=$file
8884df3cf2Smrg  else
8984df3cf2Smrg    lib_path="$lib_path;$file"
9084df3cf2Smrg  fi
9184df3cf2Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9284df3cf2Smrg}
9384df3cf2Smrg
9484df3cf2Smrg# func_cl_dashl library
9584df3cf2Smrg# Do a library search-path lookup for cl
9684df3cf2Smrgfunc_cl_dashl ()
9784df3cf2Smrg{
9884df3cf2Smrg  lib=$1
9984df3cf2Smrg  found=no
10084df3cf2Smrg  save_IFS=$IFS
10184df3cf2Smrg  IFS=';'
10284df3cf2Smrg  for dir in $lib_path $LIB
10384df3cf2Smrg  do
10484df3cf2Smrg    IFS=$save_IFS
10584df3cf2Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10684df3cf2Smrg      found=yes
10784df3cf2Smrg      lib=$dir/$lib.dll.lib
10884df3cf2Smrg      break
10984df3cf2Smrg    fi
11084df3cf2Smrg    if test -f "$dir/$lib.lib"; then
11184df3cf2Smrg      found=yes
11284df3cf2Smrg      lib=$dir/$lib.lib
11384df3cf2Smrg      break
11484df3cf2Smrg    fi
11584df3cf2Smrg    if test -f "$dir/lib$lib.a"; then
11684df3cf2Smrg      found=yes
11784df3cf2Smrg      lib=$dir/lib$lib.a
11884df3cf2Smrg      break
11984df3cf2Smrg    fi
12084df3cf2Smrg  done
12184df3cf2Smrg  IFS=$save_IFS
12284df3cf2Smrg
12384df3cf2Smrg  if test "$found" != yes; then
12484df3cf2Smrg    lib=$lib.lib
12584df3cf2Smrg  fi
12684df3cf2Smrg}
12784df3cf2Smrg
12884df3cf2Smrg# func_cl_wrapper cl arg...
12984df3cf2Smrg# Adjust compile command to suit cl
13084df3cf2Smrgfunc_cl_wrapper ()
13184df3cf2Smrg{
13284df3cf2Smrg  # Assume a capable shell
13384df3cf2Smrg  lib_path=
13484df3cf2Smrg  shared=:
13584df3cf2Smrg  linker_opts=
13684df3cf2Smrg  for arg
13784df3cf2Smrg  do
13884df3cf2Smrg    if test -n "$eat"; then
13984df3cf2Smrg      eat=
14084df3cf2Smrg    else
14184df3cf2Smrg      case $1 in
14284df3cf2Smrg	-o)
14384df3cf2Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14484df3cf2Smrg	  eat=1
14584df3cf2Smrg	  case $2 in
14684df3cf2Smrg	    *.o | *.[oO][bB][jJ])
14784df3cf2Smrg	      func_file_conv "$2"
14884df3cf2Smrg	      set x "$@" -Fo"$file"
14984df3cf2Smrg	      shift
15084df3cf2Smrg	      ;;
15184df3cf2Smrg	    *)
15284df3cf2Smrg	      func_file_conv "$2"
15384df3cf2Smrg	      set x "$@" -Fe"$file"
15484df3cf2Smrg	      shift
15584df3cf2Smrg	      ;;
15684df3cf2Smrg	  esac
15784df3cf2Smrg	  ;;
15884df3cf2Smrg	-I)
15984df3cf2Smrg	  eat=1
16084df3cf2Smrg	  func_file_conv "$2" mingw
16184df3cf2Smrg	  set x "$@" -I"$file"
16284df3cf2Smrg	  shift
16384df3cf2Smrg	  ;;
16484df3cf2Smrg	-I*)
16584df3cf2Smrg	  func_file_conv "${1#-I}" mingw
16684df3cf2Smrg	  set x "$@" -I"$file"
16784df3cf2Smrg	  shift
16884df3cf2Smrg	  ;;
16984df3cf2Smrg	-l)
17084df3cf2Smrg	  eat=1
17184df3cf2Smrg	  func_cl_dashl "$2"
17284df3cf2Smrg	  set x "$@" "$lib"
17384df3cf2Smrg	  shift
17484df3cf2Smrg	  ;;
17584df3cf2Smrg	-l*)
17684df3cf2Smrg	  func_cl_dashl "${1#-l}"
17784df3cf2Smrg	  set x "$@" "$lib"
17884df3cf2Smrg	  shift
17984df3cf2Smrg	  ;;
18084df3cf2Smrg	-L)
18184df3cf2Smrg	  eat=1
18284df3cf2Smrg	  func_cl_dashL "$2"
18384df3cf2Smrg	  ;;
18484df3cf2Smrg	-L*)
18584df3cf2Smrg	  func_cl_dashL "${1#-L}"
18684df3cf2Smrg	  ;;
18784df3cf2Smrg	-static)
18884df3cf2Smrg	  shared=false
18984df3cf2Smrg	  ;;
19084df3cf2Smrg	-Wl,*)
19184df3cf2Smrg	  arg=${1#-Wl,}
19284df3cf2Smrg	  save_ifs="$IFS"; IFS=','
19384df3cf2Smrg	  for flag in $arg; do
19484df3cf2Smrg	    IFS="$save_ifs"
19584df3cf2Smrg	    linker_opts="$linker_opts $flag"
19684df3cf2Smrg	  done
19784df3cf2Smrg	  IFS="$save_ifs"
19884df3cf2Smrg	  ;;
19984df3cf2Smrg	-Xlinker)
20084df3cf2Smrg	  eat=1
20184df3cf2Smrg	  linker_opts="$linker_opts $2"
20284df3cf2Smrg	  ;;
20384df3cf2Smrg	-*)
20484df3cf2Smrg	  set x "$@" "$1"
20584df3cf2Smrg	  shift
20684df3cf2Smrg	  ;;
20784df3cf2Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20884df3cf2Smrg	  func_file_conv "$1"
20984df3cf2Smrg	  set x "$@" -Tp"$file"
21084df3cf2Smrg	  shift
21184df3cf2Smrg	  ;;
21284df3cf2Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21384df3cf2Smrg	  func_file_conv "$1" mingw
21484df3cf2Smrg	  set x "$@" "$file"
21584df3cf2Smrg	  shift
21684df3cf2Smrg	  ;;
21784df3cf2Smrg	*)
21884df3cf2Smrg	  set x "$@" "$1"
21984df3cf2Smrg	  shift
22084df3cf2Smrg	  ;;
22184df3cf2Smrg      esac
22284df3cf2Smrg    fi
22384df3cf2Smrg    shift
22484df3cf2Smrg  done
22584df3cf2Smrg  if test -n "$linker_opts"; then
22684df3cf2Smrg    linker_opts="-link$linker_opts"
22784df3cf2Smrg  fi
22884df3cf2Smrg  exec "$@" $linker_opts
22984df3cf2Smrg  exit 1
23084df3cf2Smrg}
23184df3cf2Smrg
23284df3cf2Smrgeat=
23384df3cf2Smrg
23484df3cf2Smrgcase $1 in
23584df3cf2Smrg  '')
23684df3cf2Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23784df3cf2Smrg     exit 1;
23884df3cf2Smrg     ;;
23984df3cf2Smrg  -h | --h*)
24084df3cf2Smrg    cat <<\EOF
24184df3cf2SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24284df3cf2Smrg
24384df3cf2SmrgWrapper for compilers which do not understand '-c -o'.
24484df3cf2SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24584df3cf2Smrgarguments, and rename the output as expected.
24684df3cf2Smrg
24784df3cf2SmrgIf you are trying to build a whole package this is not the
24884df3cf2Smrgright script to run: please start by reading the file 'INSTALL'.
24984df3cf2Smrg
25084df3cf2SmrgReport bugs to <bug-automake@gnu.org>.
25184df3cf2SmrgEOF
25284df3cf2Smrg    exit $?
25384df3cf2Smrg    ;;
25484df3cf2Smrg  -v | --v*)
25584df3cf2Smrg    echo "compile $scriptversion"
25684df3cf2Smrg    exit $?
25784df3cf2Smrg    ;;
25884df3cf2Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
25984df3cf2Smrg    func_cl_wrapper "$@"      # Doesn't return...
26084df3cf2Smrg    ;;
26184df3cf2Smrgesac
26284df3cf2Smrg
26384df3cf2Smrgofile=
26484df3cf2Smrgcfile=
26584df3cf2Smrg
26684df3cf2Smrgfor arg
26784df3cf2Smrgdo
26884df3cf2Smrg  if test -n "$eat"; then
26984df3cf2Smrg    eat=
27084df3cf2Smrg  else
27184df3cf2Smrg    case $1 in
27284df3cf2Smrg      -o)
27384df3cf2Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27484df3cf2Smrg	# So we strip '-o arg' only if arg is an object.
27584df3cf2Smrg	eat=1
27684df3cf2Smrg	case $2 in
27784df3cf2Smrg	  *.o | *.obj)
27884df3cf2Smrg	    ofile=$2
27984df3cf2Smrg	    ;;
28084df3cf2Smrg	  *)
28184df3cf2Smrg	    set x "$@" -o "$2"
28284df3cf2Smrg	    shift
28384df3cf2Smrg	    ;;
28484df3cf2Smrg	esac
28584df3cf2Smrg	;;
28684df3cf2Smrg      *.c)
28784df3cf2Smrg	cfile=$1
28884df3cf2Smrg	set x "$@" "$1"
28984df3cf2Smrg	shift
29084df3cf2Smrg	;;
29184df3cf2Smrg      *)
29284df3cf2Smrg	set x "$@" "$1"
29384df3cf2Smrg	shift
29484df3cf2Smrg	;;
29584df3cf2Smrg    esac
29684df3cf2Smrg  fi
29784df3cf2Smrg  shift
29884df3cf2Smrgdone
29984df3cf2Smrg
30084df3cf2Smrgif test -z "$ofile" || test -z "$cfile"; then
30184df3cf2Smrg  # If no '-o' option was seen then we might have been invoked from a
30284df3cf2Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30384df3cf2Smrg  # normal compilation that the losing compiler can handle.  If no
30484df3cf2Smrg  # '.c' file was seen then we are probably linking.  That is also
30584df3cf2Smrg  # ok.
30684df3cf2Smrg  exec "$@"
30784df3cf2Smrgfi
30884df3cf2Smrg
30984df3cf2Smrg# Name of file we expect compiler to create.
31084df3cf2Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31184df3cf2Smrg
31284df3cf2Smrg# Create the lock directory.
31384df3cf2Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31484df3cf2Smrg# that we are using for the .o file.  Also, base the name on the expected
31584df3cf2Smrg# object file name, since that is what matters with a parallel build.
31684df3cf2Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31784df3cf2Smrgwhile true; do
31884df3cf2Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
31984df3cf2Smrg    break
32084df3cf2Smrg  fi
32184df3cf2Smrg  sleep 1
32284df3cf2Smrgdone
32384df3cf2Smrg# FIXME: race condition here if user kills between mkdir and trap.
32484df3cf2Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32584df3cf2Smrg
32684df3cf2Smrg# Run the compile.
32784df3cf2Smrg"$@"
32884df3cf2Smrgret=$?
32984df3cf2Smrg
33084df3cf2Smrgif test -f "$cofile"; then
33184df3cf2Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33284df3cf2Smrgelif test -f "${cofile}bj"; then
33384df3cf2Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33484df3cf2Smrgfi
33584df3cf2Smrg
33684df3cf2Smrgrmdir "$lockdir"
33784df3cf2Smrgexit $ret
33884df3cf2Smrg
33984df3cf2Smrg# Local Variables:
34084df3cf2Smrg# mode: shell-script
34184df3cf2Smrg# sh-indentation: 2
34284df3cf2Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
34384df3cf2Smrg# time-stamp-start: "scriptversion="
34484df3cf2Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34584df3cf2Smrg# time-stamp-time-zone: "UTC"
34684df3cf2Smrg# time-stamp-end: "; # UTC"
34784df3cf2Smrg# End:
348