11b2353dbSmrg#! /bin/sh
21b2353dbSmrg# Wrapper for compilers which do not understand '-c -o'.
31b2353dbSmrg
4889a2364Smrgscriptversion=2018-03-07.03; # UTC
51b2353dbSmrg
6889a2364Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
71b2353dbSmrg# Written by Tom Tromey <tromey@cygnus.com>.
81b2353dbSmrg#
91b2353dbSmrg# This program is free software; you can redistribute it and/or modify
101b2353dbSmrg# it under the terms of the GNU General Public License as published by
111b2353dbSmrg# the Free Software Foundation; either version 2, or (at your option)
121b2353dbSmrg# any later version.
131b2353dbSmrg#
141b2353dbSmrg# This program is distributed in the hope that it will be useful,
151b2353dbSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
161b2353dbSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171b2353dbSmrg# GNU General Public License for more details.
181b2353dbSmrg#
191b2353dbSmrg# You should have received a copy of the GNU General Public License
20889a2364Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
211b2353dbSmrg
221b2353dbSmrg# As a special exception to the GNU General Public License, if you
231b2353dbSmrg# distribute this file as part of a program that contains a
241b2353dbSmrg# configuration script generated by Autoconf, you may include it under
251b2353dbSmrg# the same distribution terms that you use for the rest of that program.
261b2353dbSmrg
271b2353dbSmrg# This file is maintained in Automake, please report
281b2353dbSmrg# bugs to <bug-automake@gnu.org> or send patches to
291b2353dbSmrg# <automake-patches@gnu.org>.
301b2353dbSmrg
311b2353dbSmrgnl='
321b2353dbSmrg'
331b2353dbSmrg
341b2353dbSmrg# We need space, tab and new line, in precisely that order.  Quoting is
351b2353dbSmrg# there to prevent tools from complaining about whitespace usage.
361b2353dbSmrgIFS=" ""	$nl"
371b2353dbSmrg
381b2353dbSmrgfile_conv=
391b2353dbSmrg
401b2353dbSmrg# func_file_conv build_file lazy
411b2353dbSmrg# Convert a $build file to $host form and store it in $file
421b2353dbSmrg# Currently only supports Windows hosts. If the determined conversion
431b2353dbSmrg# type is listed in (the comma separated) LAZY, no conversion will
441b2353dbSmrg# take place.
451b2353dbSmrgfunc_file_conv ()
461b2353dbSmrg{
471b2353dbSmrg  file=$1
481b2353dbSmrg  case $file in
491b2353dbSmrg    / | /[!/]*) # absolute file, and not a UNC file
501b2353dbSmrg      if test -z "$file_conv"; then
511b2353dbSmrg	# lazily determine how to convert abs files
521b2353dbSmrg	case `uname -s` in
531b2353dbSmrg	  MINGW*)
541b2353dbSmrg	    file_conv=mingw
551b2353dbSmrg	    ;;
56889a2364Smrg	  CYGWIN* | MSYS*)
571b2353dbSmrg	    file_conv=cygwin
581b2353dbSmrg	    ;;
591b2353dbSmrg	  *)
601b2353dbSmrg	    file_conv=wine
611b2353dbSmrg	    ;;
621b2353dbSmrg	esac
631b2353dbSmrg      fi
641b2353dbSmrg      case $file_conv/,$2, in
651b2353dbSmrg	*,$file_conv,*)
661b2353dbSmrg	  ;;
671b2353dbSmrg	mingw/*)
681b2353dbSmrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
691b2353dbSmrg	  ;;
70889a2364Smrg	cygwin/* | msys/*)
711b2353dbSmrg	  file=`cygpath -m "$file" || echo "$file"`
721b2353dbSmrg	  ;;
731b2353dbSmrg	wine/*)
741b2353dbSmrg	  file=`winepath -w "$file" || echo "$file"`
751b2353dbSmrg	  ;;
761b2353dbSmrg      esac
771b2353dbSmrg      ;;
781b2353dbSmrg  esac
791b2353dbSmrg}
801b2353dbSmrg
811b2353dbSmrg# func_cl_dashL linkdir
821b2353dbSmrg# Make cl look for libraries in LINKDIR
831b2353dbSmrgfunc_cl_dashL ()
841b2353dbSmrg{
851b2353dbSmrg  func_file_conv "$1"
861b2353dbSmrg  if test -z "$lib_path"; then
871b2353dbSmrg    lib_path=$file
881b2353dbSmrg  else
891b2353dbSmrg    lib_path="$lib_path;$file"
901b2353dbSmrg  fi
911b2353dbSmrg  linker_opts="$linker_opts -LIBPATH:$file"
921b2353dbSmrg}
931b2353dbSmrg
941b2353dbSmrg# func_cl_dashl library
951b2353dbSmrg# Do a library search-path lookup for cl
961b2353dbSmrgfunc_cl_dashl ()
971b2353dbSmrg{
981b2353dbSmrg  lib=$1
991b2353dbSmrg  found=no
1001b2353dbSmrg  save_IFS=$IFS
1011b2353dbSmrg  IFS=';'
1021b2353dbSmrg  for dir in $lib_path $LIB
1031b2353dbSmrg  do
1041b2353dbSmrg    IFS=$save_IFS
1051b2353dbSmrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1061b2353dbSmrg      found=yes
1071b2353dbSmrg      lib=$dir/$lib.dll.lib
1081b2353dbSmrg      break
1091b2353dbSmrg    fi
1101b2353dbSmrg    if test -f "$dir/$lib.lib"; then
1111b2353dbSmrg      found=yes
1121b2353dbSmrg      lib=$dir/$lib.lib
1131b2353dbSmrg      break
1141b2353dbSmrg    fi
1151b2353dbSmrg    if test -f "$dir/lib$lib.a"; then
1161b2353dbSmrg      found=yes
1171b2353dbSmrg      lib=$dir/lib$lib.a
1181b2353dbSmrg      break
1191b2353dbSmrg    fi
1201b2353dbSmrg  done
1211b2353dbSmrg  IFS=$save_IFS
1221b2353dbSmrg
1231b2353dbSmrg  if test "$found" != yes; then
1241b2353dbSmrg    lib=$lib.lib
1251b2353dbSmrg  fi
1261b2353dbSmrg}
1271b2353dbSmrg
1281b2353dbSmrg# func_cl_wrapper cl arg...
1291b2353dbSmrg# Adjust compile command to suit cl
1301b2353dbSmrgfunc_cl_wrapper ()
1311b2353dbSmrg{
1321b2353dbSmrg  # Assume a capable shell
1331b2353dbSmrg  lib_path=
1341b2353dbSmrg  shared=:
1351b2353dbSmrg  linker_opts=
1361b2353dbSmrg  for arg
1371b2353dbSmrg  do
1381b2353dbSmrg    if test -n "$eat"; then
1391b2353dbSmrg      eat=
1401b2353dbSmrg    else
1411b2353dbSmrg      case $1 in
1421b2353dbSmrg	-o)
1431b2353dbSmrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1441b2353dbSmrg	  eat=1
1451b2353dbSmrg	  case $2 in
1461b2353dbSmrg	    *.o | *.[oO][bB][jJ])
1471b2353dbSmrg	      func_file_conv "$2"
1481b2353dbSmrg	      set x "$@" -Fo"$file"
1491b2353dbSmrg	      shift
1501b2353dbSmrg	      ;;
1511b2353dbSmrg	    *)
1521b2353dbSmrg	      func_file_conv "$2"
1531b2353dbSmrg	      set x "$@" -Fe"$file"
1541b2353dbSmrg	      shift
1551b2353dbSmrg	      ;;
1561b2353dbSmrg	  esac
1571b2353dbSmrg	  ;;
1581b2353dbSmrg	-I)
1591b2353dbSmrg	  eat=1
1601b2353dbSmrg	  func_file_conv "$2" mingw
1611b2353dbSmrg	  set x "$@" -I"$file"
1621b2353dbSmrg	  shift
1631b2353dbSmrg	  ;;
1641b2353dbSmrg	-I*)
1651b2353dbSmrg	  func_file_conv "${1#-I}" mingw
1661b2353dbSmrg	  set x "$@" -I"$file"
1671b2353dbSmrg	  shift
1681b2353dbSmrg	  ;;
1691b2353dbSmrg	-l)
1701b2353dbSmrg	  eat=1
1711b2353dbSmrg	  func_cl_dashl "$2"
1721b2353dbSmrg	  set x "$@" "$lib"
1731b2353dbSmrg	  shift
1741b2353dbSmrg	  ;;
1751b2353dbSmrg	-l*)
1761b2353dbSmrg	  func_cl_dashl "${1#-l}"
1771b2353dbSmrg	  set x "$@" "$lib"
1781b2353dbSmrg	  shift
1791b2353dbSmrg	  ;;
1801b2353dbSmrg	-L)
1811b2353dbSmrg	  eat=1
1821b2353dbSmrg	  func_cl_dashL "$2"
1831b2353dbSmrg	  ;;
1841b2353dbSmrg	-L*)
1851b2353dbSmrg	  func_cl_dashL "${1#-L}"
1861b2353dbSmrg	  ;;
1871b2353dbSmrg	-static)
1881b2353dbSmrg	  shared=false
1891b2353dbSmrg	  ;;
1901b2353dbSmrg	-Wl,*)
1911b2353dbSmrg	  arg=${1#-Wl,}
1921b2353dbSmrg	  save_ifs="$IFS"; IFS=','
1931b2353dbSmrg	  for flag in $arg; do
1941b2353dbSmrg	    IFS="$save_ifs"
1951b2353dbSmrg	    linker_opts="$linker_opts $flag"
1961b2353dbSmrg	  done
1971b2353dbSmrg	  IFS="$save_ifs"
1981b2353dbSmrg	  ;;
1991b2353dbSmrg	-Xlinker)
2001b2353dbSmrg	  eat=1
2011b2353dbSmrg	  linker_opts="$linker_opts $2"
2021b2353dbSmrg	  ;;
2031b2353dbSmrg	-*)
2041b2353dbSmrg	  set x "$@" "$1"
2051b2353dbSmrg	  shift
2061b2353dbSmrg	  ;;
2071b2353dbSmrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2081b2353dbSmrg	  func_file_conv "$1"
2091b2353dbSmrg	  set x "$@" -Tp"$file"
2101b2353dbSmrg	  shift
2111b2353dbSmrg	  ;;
2121b2353dbSmrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2131b2353dbSmrg	  func_file_conv "$1" mingw
2141b2353dbSmrg	  set x "$@" "$file"
2151b2353dbSmrg	  shift
2161b2353dbSmrg	  ;;
2171b2353dbSmrg	*)
2181b2353dbSmrg	  set x "$@" "$1"
2191b2353dbSmrg	  shift
2201b2353dbSmrg	  ;;
2211b2353dbSmrg      esac
2221b2353dbSmrg    fi
2231b2353dbSmrg    shift
2241b2353dbSmrg  done
2251b2353dbSmrg  if test -n "$linker_opts"; then
2261b2353dbSmrg    linker_opts="-link$linker_opts"
2271b2353dbSmrg  fi
2281b2353dbSmrg  exec "$@" $linker_opts
2291b2353dbSmrg  exit 1
2301b2353dbSmrg}
2311b2353dbSmrg
2321b2353dbSmrgeat=
2331b2353dbSmrg
2341b2353dbSmrgcase $1 in
2351b2353dbSmrg  '')
2361b2353dbSmrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2371b2353dbSmrg     exit 1;
2381b2353dbSmrg     ;;
2391b2353dbSmrg  -h | --h*)
2401b2353dbSmrg    cat <<\EOF
2411b2353dbSmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2421b2353dbSmrg
2431b2353dbSmrgWrapper for compilers which do not understand '-c -o'.
2441b2353dbSmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2451b2353dbSmrgarguments, and rename the output as expected.
2461b2353dbSmrg
2471b2353dbSmrgIf you are trying to build a whole package this is not the
2481b2353dbSmrgright script to run: please start by reading the file 'INSTALL'.
2491b2353dbSmrg
2501b2353dbSmrgReport bugs to <bug-automake@gnu.org>.
2511b2353dbSmrgEOF
2521b2353dbSmrg    exit $?
2531b2353dbSmrg    ;;
2541b2353dbSmrg  -v | --v*)
2551b2353dbSmrg    echo "compile $scriptversion"
2561b2353dbSmrg    exit $?
2571b2353dbSmrg    ;;
258889a2364Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259889a2364Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2601b2353dbSmrg    func_cl_wrapper "$@"      # Doesn't return...
2611b2353dbSmrg    ;;
2621b2353dbSmrgesac
2631b2353dbSmrg
2641b2353dbSmrgofile=
2651b2353dbSmrgcfile=
2661b2353dbSmrg
2671b2353dbSmrgfor arg
2681b2353dbSmrgdo
2691b2353dbSmrg  if test -n "$eat"; then
2701b2353dbSmrg    eat=
2711b2353dbSmrg  else
2721b2353dbSmrg    case $1 in
2731b2353dbSmrg      -o)
2741b2353dbSmrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2751b2353dbSmrg	# So we strip '-o arg' only if arg is an object.
2761b2353dbSmrg	eat=1
2771b2353dbSmrg	case $2 in
2781b2353dbSmrg	  *.o | *.obj)
2791b2353dbSmrg	    ofile=$2
2801b2353dbSmrg	    ;;
2811b2353dbSmrg	  *)
2821b2353dbSmrg	    set x "$@" -o "$2"
2831b2353dbSmrg	    shift
2841b2353dbSmrg	    ;;
2851b2353dbSmrg	esac
2861b2353dbSmrg	;;
2871b2353dbSmrg      *.c)
2881b2353dbSmrg	cfile=$1
2891b2353dbSmrg	set x "$@" "$1"
2901b2353dbSmrg	shift
2911b2353dbSmrg	;;
2921b2353dbSmrg      *)
2931b2353dbSmrg	set x "$@" "$1"
2941b2353dbSmrg	shift
2951b2353dbSmrg	;;
2961b2353dbSmrg    esac
2971b2353dbSmrg  fi
2981b2353dbSmrg  shift
2991b2353dbSmrgdone
3001b2353dbSmrg
3011b2353dbSmrgif test -z "$ofile" || test -z "$cfile"; then
3021b2353dbSmrg  # If no '-o' option was seen then we might have been invoked from a
3031b2353dbSmrg  # pattern rule where we don't need one.  That is ok -- this is a
3041b2353dbSmrg  # normal compilation that the losing compiler can handle.  If no
3051b2353dbSmrg  # '.c' file was seen then we are probably linking.  That is also
3061b2353dbSmrg  # ok.
3071b2353dbSmrg  exec "$@"
3081b2353dbSmrgfi
3091b2353dbSmrg
3101b2353dbSmrg# Name of file we expect compiler to create.
3111b2353dbSmrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3121b2353dbSmrg
3131b2353dbSmrg# Create the lock directory.
3141b2353dbSmrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3151b2353dbSmrg# that we are using for the .o file.  Also, base the name on the expected
3161b2353dbSmrg# object file name, since that is what matters with a parallel build.
3171b2353dbSmrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3181b2353dbSmrgwhile true; do
3191b2353dbSmrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3201b2353dbSmrg    break
3211b2353dbSmrg  fi
3221b2353dbSmrg  sleep 1
3231b2353dbSmrgdone
3241b2353dbSmrg# FIXME: race condition here if user kills between mkdir and trap.
3251b2353dbSmrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3261b2353dbSmrg
3271b2353dbSmrg# Run the compile.
3281b2353dbSmrg"$@"
3291b2353dbSmrgret=$?
3301b2353dbSmrg
3311b2353dbSmrgif test -f "$cofile"; then
3321b2353dbSmrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3331b2353dbSmrgelif test -f "${cofile}bj"; then
3341b2353dbSmrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3351b2353dbSmrgfi
3361b2353dbSmrg
3371b2353dbSmrgrmdir "$lockdir"
3381b2353dbSmrgexit $ret
3391b2353dbSmrg
3401b2353dbSmrg# Local Variables:
3411b2353dbSmrg# mode: shell-script
3421b2353dbSmrg# sh-indentation: 2
343889a2364Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
3441b2353dbSmrg# time-stamp-start: "scriptversion="
3451b2353dbSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346889a2364Smrg# time-stamp-time-zone: "UTC0"
3471b2353dbSmrg# time-stamp-end: "; # UTC"
3481b2353dbSmrg# End:
349