11016ad83Smrg#! /bin/sh
21016ad83Smrg# Wrapper for compilers which do not understand '-c -o'.
31016ad83Smrg
4fe12f63cSmrgscriptversion=2018-03-07.03; # UTC
51016ad83Smrg
68ffb90f1Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
71016ad83Smrg# Written by Tom Tromey <tromey@cygnus.com>.
81016ad83Smrg#
91016ad83Smrg# This program is free software; you can redistribute it and/or modify
101016ad83Smrg# it under the terms of the GNU General Public License as published by
111016ad83Smrg# the Free Software Foundation; either version 2, or (at your option)
121016ad83Smrg# any later version.
131016ad83Smrg#
141016ad83Smrg# This program is distributed in the hope that it will be useful,
151016ad83Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
161016ad83Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171016ad83Smrg# GNU General Public License for more details.
181016ad83Smrg#
191016ad83Smrg# You should have received a copy of the GNU General Public License
20fe12f63cSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
211016ad83Smrg
221016ad83Smrg# As a special exception to the GNU General Public License, if you
231016ad83Smrg# distribute this file as part of a program that contains a
241016ad83Smrg# configuration script generated by Autoconf, you may include it under
251016ad83Smrg# the same distribution terms that you use for the rest of that program.
261016ad83Smrg
271016ad83Smrg# This file is maintained in Automake, please report
281016ad83Smrg# bugs to <bug-automake@gnu.org> or send patches to
291016ad83Smrg# <automake-patches@gnu.org>.
301016ad83Smrg
311016ad83Smrgnl='
321016ad83Smrg'
331016ad83Smrg
341016ad83Smrg# We need space, tab and new line, in precisely that order.  Quoting is
351016ad83Smrg# there to prevent tools from complaining about whitespace usage.
361016ad83SmrgIFS=" ""	$nl"
371016ad83Smrg
381016ad83Smrgfile_conv=
391016ad83Smrg
401016ad83Smrg# func_file_conv build_file lazy
411016ad83Smrg# Convert a $build file to $host form and store it in $file
421016ad83Smrg# Currently only supports Windows hosts. If the determined conversion
431016ad83Smrg# type is listed in (the comma separated) LAZY, no conversion will
441016ad83Smrg# take place.
451016ad83Smrgfunc_file_conv ()
461016ad83Smrg{
471016ad83Smrg  file=$1
481016ad83Smrg  case $file in
491016ad83Smrg    / | /[!/]*) # absolute file, and not a UNC file
501016ad83Smrg      if test -z "$file_conv"; then
511016ad83Smrg	# lazily determine how to convert abs files
521016ad83Smrg	case `uname -s` in
531016ad83Smrg	  MINGW*)
541016ad83Smrg	    file_conv=mingw
551016ad83Smrg	    ;;
568ffb90f1Smrg	  CYGWIN* | MSYS*)
571016ad83Smrg	    file_conv=cygwin
581016ad83Smrg	    ;;
591016ad83Smrg	  *)
601016ad83Smrg	    file_conv=wine
611016ad83Smrg	    ;;
621016ad83Smrg	esac
631016ad83Smrg      fi
641016ad83Smrg      case $file_conv/,$2, in
651016ad83Smrg	*,$file_conv,*)
661016ad83Smrg	  ;;
671016ad83Smrg	mingw/*)
681016ad83Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
691016ad83Smrg	  ;;
708ffb90f1Smrg	cygwin/* | msys/*)
711016ad83Smrg	  file=`cygpath -m "$file" || echo "$file"`
721016ad83Smrg	  ;;
731016ad83Smrg	wine/*)
741016ad83Smrg	  file=`winepath -w "$file" || echo "$file"`
751016ad83Smrg	  ;;
761016ad83Smrg      esac
771016ad83Smrg      ;;
781016ad83Smrg  esac
791016ad83Smrg}
801016ad83Smrg
811016ad83Smrg# func_cl_dashL linkdir
821016ad83Smrg# Make cl look for libraries in LINKDIR
831016ad83Smrgfunc_cl_dashL ()
841016ad83Smrg{
851016ad83Smrg  func_file_conv "$1"
861016ad83Smrg  if test -z "$lib_path"; then
871016ad83Smrg    lib_path=$file
881016ad83Smrg  else
891016ad83Smrg    lib_path="$lib_path;$file"
901016ad83Smrg  fi
911016ad83Smrg  linker_opts="$linker_opts -LIBPATH:$file"
921016ad83Smrg}
931016ad83Smrg
941016ad83Smrg# func_cl_dashl library
951016ad83Smrg# Do a library search-path lookup for cl
961016ad83Smrgfunc_cl_dashl ()
971016ad83Smrg{
981016ad83Smrg  lib=$1
991016ad83Smrg  found=no
1001016ad83Smrg  save_IFS=$IFS
1011016ad83Smrg  IFS=';'
1021016ad83Smrg  for dir in $lib_path $LIB
1031016ad83Smrg  do
1041016ad83Smrg    IFS=$save_IFS
1051016ad83Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1061016ad83Smrg      found=yes
1071016ad83Smrg      lib=$dir/$lib.dll.lib
1081016ad83Smrg      break
1091016ad83Smrg    fi
1101016ad83Smrg    if test -f "$dir/$lib.lib"; then
1111016ad83Smrg      found=yes
1121016ad83Smrg      lib=$dir/$lib.lib
1131016ad83Smrg      break
1141016ad83Smrg    fi
1151016ad83Smrg    if test -f "$dir/lib$lib.a"; then
1161016ad83Smrg      found=yes
1171016ad83Smrg      lib=$dir/lib$lib.a
1181016ad83Smrg      break
1191016ad83Smrg    fi
1201016ad83Smrg  done
1211016ad83Smrg  IFS=$save_IFS
1221016ad83Smrg
1231016ad83Smrg  if test "$found" != yes; then
1241016ad83Smrg    lib=$lib.lib
1251016ad83Smrg  fi
1261016ad83Smrg}
1271016ad83Smrg
1281016ad83Smrg# func_cl_wrapper cl arg...
1291016ad83Smrg# Adjust compile command to suit cl
1301016ad83Smrgfunc_cl_wrapper ()
1311016ad83Smrg{
1321016ad83Smrg  # Assume a capable shell
1331016ad83Smrg  lib_path=
1341016ad83Smrg  shared=:
1351016ad83Smrg  linker_opts=
1361016ad83Smrg  for arg
1371016ad83Smrg  do
1381016ad83Smrg    if test -n "$eat"; then
1391016ad83Smrg      eat=
1401016ad83Smrg    else
1411016ad83Smrg      case $1 in
1421016ad83Smrg	-o)
1431016ad83Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1441016ad83Smrg	  eat=1
1451016ad83Smrg	  case $2 in
1461016ad83Smrg	    *.o | *.[oO][bB][jJ])
1471016ad83Smrg	      func_file_conv "$2"
1481016ad83Smrg	      set x "$@" -Fo"$file"
1491016ad83Smrg	      shift
1501016ad83Smrg	      ;;
1511016ad83Smrg	    *)
1521016ad83Smrg	      func_file_conv "$2"
1531016ad83Smrg	      set x "$@" -Fe"$file"
1541016ad83Smrg	      shift
1551016ad83Smrg	      ;;
1561016ad83Smrg	  esac
1571016ad83Smrg	  ;;
1581016ad83Smrg	-I)
1591016ad83Smrg	  eat=1
1601016ad83Smrg	  func_file_conv "$2" mingw
1611016ad83Smrg	  set x "$@" -I"$file"
1621016ad83Smrg	  shift
1631016ad83Smrg	  ;;
1641016ad83Smrg	-I*)
1651016ad83Smrg	  func_file_conv "${1#-I}" mingw
1661016ad83Smrg	  set x "$@" -I"$file"
1671016ad83Smrg	  shift
1681016ad83Smrg	  ;;
1691016ad83Smrg	-l)
1701016ad83Smrg	  eat=1
1711016ad83Smrg	  func_cl_dashl "$2"
1721016ad83Smrg	  set x "$@" "$lib"
1731016ad83Smrg	  shift
1741016ad83Smrg	  ;;
1751016ad83Smrg	-l*)
1761016ad83Smrg	  func_cl_dashl "${1#-l}"
1771016ad83Smrg	  set x "$@" "$lib"
1781016ad83Smrg	  shift
1791016ad83Smrg	  ;;
1801016ad83Smrg	-L)
1811016ad83Smrg	  eat=1
1821016ad83Smrg	  func_cl_dashL "$2"
1831016ad83Smrg	  ;;
1841016ad83Smrg	-L*)
1851016ad83Smrg	  func_cl_dashL "${1#-L}"
1861016ad83Smrg	  ;;
1871016ad83Smrg	-static)
1881016ad83Smrg	  shared=false
1891016ad83Smrg	  ;;
1901016ad83Smrg	-Wl,*)
1911016ad83Smrg	  arg=${1#-Wl,}
1921016ad83Smrg	  save_ifs="$IFS"; IFS=','
1931016ad83Smrg	  for flag in $arg; do
1941016ad83Smrg	    IFS="$save_ifs"
1951016ad83Smrg	    linker_opts="$linker_opts $flag"
1961016ad83Smrg	  done
1971016ad83Smrg	  IFS="$save_ifs"
1981016ad83Smrg	  ;;
1991016ad83Smrg	-Xlinker)
2001016ad83Smrg	  eat=1
2011016ad83Smrg	  linker_opts="$linker_opts $2"
2021016ad83Smrg	  ;;
2031016ad83Smrg	-*)
2041016ad83Smrg	  set x "$@" "$1"
2051016ad83Smrg	  shift
2061016ad83Smrg	  ;;
2071016ad83Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2081016ad83Smrg	  func_file_conv "$1"
2091016ad83Smrg	  set x "$@" -Tp"$file"
2101016ad83Smrg	  shift
2111016ad83Smrg	  ;;
2121016ad83Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2131016ad83Smrg	  func_file_conv "$1" mingw
2141016ad83Smrg	  set x "$@" "$file"
2151016ad83Smrg	  shift
2161016ad83Smrg	  ;;
2171016ad83Smrg	*)
2181016ad83Smrg	  set x "$@" "$1"
2191016ad83Smrg	  shift
2201016ad83Smrg	  ;;
2211016ad83Smrg      esac
2221016ad83Smrg    fi
2231016ad83Smrg    shift
2241016ad83Smrg  done
2251016ad83Smrg  if test -n "$linker_opts"; then
2261016ad83Smrg    linker_opts="-link$linker_opts"
2271016ad83Smrg  fi
2281016ad83Smrg  exec "$@" $linker_opts
2291016ad83Smrg  exit 1
2301016ad83Smrg}
2311016ad83Smrg
2321016ad83Smrgeat=
2331016ad83Smrg
2341016ad83Smrgcase $1 in
2351016ad83Smrg  '')
2361016ad83Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2371016ad83Smrg     exit 1;
2381016ad83Smrg     ;;
2391016ad83Smrg  -h | --h*)
2401016ad83Smrg    cat <<\EOF
2411016ad83SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2421016ad83Smrg
2431016ad83SmrgWrapper for compilers which do not understand '-c -o'.
2441016ad83SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2451016ad83Smrgarguments, and rename the output as expected.
2461016ad83Smrg
2471016ad83SmrgIf you are trying to build a whole package this is not the
2481016ad83Smrgright script to run: please start by reading the file 'INSTALL'.
2491016ad83Smrg
2501016ad83SmrgReport bugs to <bug-automake@gnu.org>.
2511016ad83SmrgEOF
2521016ad83Smrg    exit $?
2531016ad83Smrg    ;;
2541016ad83Smrg  -v | --v*)
2551016ad83Smrg    echo "compile $scriptversion"
2561016ad83Smrg    exit $?
2571016ad83Smrg    ;;
258245c37e9Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259245c37e9Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2601016ad83Smrg    func_cl_wrapper "$@"      # Doesn't return...
2611016ad83Smrg    ;;
2621016ad83Smrgesac
2631016ad83Smrg
2641016ad83Smrgofile=
2651016ad83Smrgcfile=
2661016ad83Smrg
2671016ad83Smrgfor arg
2681016ad83Smrgdo
2691016ad83Smrg  if test -n "$eat"; then
2701016ad83Smrg    eat=
2711016ad83Smrg  else
2721016ad83Smrg    case $1 in
2731016ad83Smrg      -o)
2741016ad83Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2751016ad83Smrg	# So we strip '-o arg' only if arg is an object.
2761016ad83Smrg	eat=1
2771016ad83Smrg	case $2 in
2781016ad83Smrg	  *.o | *.obj)
2791016ad83Smrg	    ofile=$2
2801016ad83Smrg	    ;;
2811016ad83Smrg	  *)
2821016ad83Smrg	    set x "$@" -o "$2"
2831016ad83Smrg	    shift
2841016ad83Smrg	    ;;
2851016ad83Smrg	esac
2861016ad83Smrg	;;
2871016ad83Smrg      *.c)
2881016ad83Smrg	cfile=$1
2891016ad83Smrg	set x "$@" "$1"
2901016ad83Smrg	shift
2911016ad83Smrg	;;
2921016ad83Smrg      *)
2931016ad83Smrg	set x "$@" "$1"
2941016ad83Smrg	shift
2951016ad83Smrg	;;
2961016ad83Smrg    esac
2971016ad83Smrg  fi
2981016ad83Smrg  shift
2991016ad83Smrgdone
3001016ad83Smrg
3011016ad83Smrgif test -z "$ofile" || test -z "$cfile"; then
3021016ad83Smrg  # If no '-o' option was seen then we might have been invoked from a
3031016ad83Smrg  # pattern rule where we don't need one.  That is ok -- this is a
3041016ad83Smrg  # normal compilation that the losing compiler can handle.  If no
3051016ad83Smrg  # '.c' file was seen then we are probably linking.  That is also
3061016ad83Smrg  # ok.
3071016ad83Smrg  exec "$@"
3081016ad83Smrgfi
3091016ad83Smrg
3101016ad83Smrg# Name of file we expect compiler to create.
3111016ad83Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3121016ad83Smrg
3131016ad83Smrg# Create the lock directory.
3141016ad83Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3151016ad83Smrg# that we are using for the .o file.  Also, base the name on the expected
3161016ad83Smrg# object file name, since that is what matters with a parallel build.
3171016ad83Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3181016ad83Smrgwhile true; do
3191016ad83Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3201016ad83Smrg    break
3211016ad83Smrg  fi
3221016ad83Smrg  sleep 1
3231016ad83Smrgdone
3241016ad83Smrg# FIXME: race condition here if user kills between mkdir and trap.
3251016ad83Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3261016ad83Smrg
3271016ad83Smrg# Run the compile.
3281016ad83Smrg"$@"
3291016ad83Smrgret=$?
3301016ad83Smrg
3311016ad83Smrgif test -f "$cofile"; then
3321016ad83Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3331016ad83Smrgelif test -f "${cofile}bj"; then
3341016ad83Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3351016ad83Smrgfi
3361016ad83Smrg
3371016ad83Smrgrmdir "$lockdir"
3381016ad83Smrgexit $ret
3391016ad83Smrg
3401016ad83Smrg# Local Variables:
3411016ad83Smrg# mode: shell-script
3421016ad83Smrg# sh-indentation: 2
343fe12f63cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
3441016ad83Smrg# time-stamp-start: "scriptversion="
3451016ad83Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346245c37e9Smrg# time-stamp-time-zone: "UTC0"
3471016ad83Smrg# time-stamp-end: "; # UTC"
3481016ad83Smrg# End:
349