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