compile revision 2163f7f8
10c91c449Smrg#! /bin/sh
20c91c449Smrg# Wrapper for compilers which do not understand '-c -o'.
30c91c449Smrg
42163f7f8Smrgscriptversion=2018-03-07.03; # UTC
50c91c449Smrg
62163f7f8Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
70c91c449Smrg# Written by Tom Tromey <tromey@cygnus.com>.
80c91c449Smrg#
90c91c449Smrg# This program is free software; you can redistribute it and/or modify
100c91c449Smrg# it under the terms of the GNU General Public License as published by
110c91c449Smrg# the Free Software Foundation; either version 2, or (at your option)
120c91c449Smrg# any later version.
130c91c449Smrg#
140c91c449Smrg# This program is distributed in the hope that it will be useful,
150c91c449Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
160c91c449Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
170c91c449Smrg# GNU General Public License for more details.
180c91c449Smrg#
190c91c449Smrg# You should have received a copy of the GNU General Public License
202163f7f8Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
210c91c449Smrg
220c91c449Smrg# As a special exception to the GNU General Public License, if you
230c91c449Smrg# distribute this file as part of a program that contains a
240c91c449Smrg# configuration script generated by Autoconf, you may include it under
250c91c449Smrg# the same distribution terms that you use for the rest of that program.
260c91c449Smrg
270c91c449Smrg# This file is maintained in Automake, please report
280c91c449Smrg# bugs to <bug-automake@gnu.org> or send patches to
290c91c449Smrg# <automake-patches@gnu.org>.
300c91c449Smrg
310c91c449Smrgnl='
320c91c449Smrg'
330c91c449Smrg
340c91c449Smrg# We need space, tab and new line, in precisely that order.  Quoting is
350c91c449Smrg# there to prevent tools from complaining about whitespace usage.
360c91c449SmrgIFS=" ""	$nl"
370c91c449Smrg
380c91c449Smrgfile_conv=
390c91c449Smrg
400c91c449Smrg# func_file_conv build_file lazy
410c91c449Smrg# Convert a $build file to $host form and store it in $file
420c91c449Smrg# Currently only supports Windows hosts. If the determined conversion
430c91c449Smrg# type is listed in (the comma separated) LAZY, no conversion will
440c91c449Smrg# take place.
450c91c449Smrgfunc_file_conv ()
460c91c449Smrg{
470c91c449Smrg  file=$1
480c91c449Smrg  case $file in
490c91c449Smrg    / | /[!/]*) # absolute file, and not a UNC file
500c91c449Smrg      if test -z "$file_conv"; then
510c91c449Smrg	# lazily determine how to convert abs files
520c91c449Smrg	case `uname -s` in
530c91c449Smrg	  MINGW*)
540c91c449Smrg	    file_conv=mingw
550c91c449Smrg	    ;;
562163f7f8Smrg	  CYGWIN* | MSYS*)
570c91c449Smrg	    file_conv=cygwin
580c91c449Smrg	    ;;
590c91c449Smrg	  *)
600c91c449Smrg	    file_conv=wine
610c91c449Smrg	    ;;
620c91c449Smrg	esac
630c91c449Smrg      fi
640c91c449Smrg      case $file_conv/,$2, in
650c91c449Smrg	*,$file_conv,*)
660c91c449Smrg	  ;;
670c91c449Smrg	mingw/*)
680c91c449Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
690c91c449Smrg	  ;;
702163f7f8Smrg	cygwin/* | msys/*)
710c91c449Smrg	  file=`cygpath -m "$file" || echo "$file"`
720c91c449Smrg	  ;;
730c91c449Smrg	wine/*)
740c91c449Smrg	  file=`winepath -w "$file" || echo "$file"`
750c91c449Smrg	  ;;
760c91c449Smrg      esac
770c91c449Smrg      ;;
780c91c449Smrg  esac
790c91c449Smrg}
800c91c449Smrg
810c91c449Smrg# func_cl_dashL linkdir
820c91c449Smrg# Make cl look for libraries in LINKDIR
830c91c449Smrgfunc_cl_dashL ()
840c91c449Smrg{
850c91c449Smrg  func_file_conv "$1"
860c91c449Smrg  if test -z "$lib_path"; then
870c91c449Smrg    lib_path=$file
880c91c449Smrg  else
890c91c449Smrg    lib_path="$lib_path;$file"
900c91c449Smrg  fi
910c91c449Smrg  linker_opts="$linker_opts -LIBPATH:$file"
920c91c449Smrg}
930c91c449Smrg
940c91c449Smrg# func_cl_dashl library
950c91c449Smrg# Do a library search-path lookup for cl
960c91c449Smrgfunc_cl_dashl ()
970c91c449Smrg{
980c91c449Smrg  lib=$1
990c91c449Smrg  found=no
1000c91c449Smrg  save_IFS=$IFS
1010c91c449Smrg  IFS=';'
1020c91c449Smrg  for dir in $lib_path $LIB
1030c91c449Smrg  do
1040c91c449Smrg    IFS=$save_IFS
1050c91c449Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1060c91c449Smrg      found=yes
1070c91c449Smrg      lib=$dir/$lib.dll.lib
1080c91c449Smrg      break
1090c91c449Smrg    fi
1100c91c449Smrg    if test -f "$dir/$lib.lib"; then
1110c91c449Smrg      found=yes
1120c91c449Smrg      lib=$dir/$lib.lib
1130c91c449Smrg      break
1140c91c449Smrg    fi
1150c91c449Smrg    if test -f "$dir/lib$lib.a"; then
1160c91c449Smrg      found=yes
1170c91c449Smrg      lib=$dir/lib$lib.a
1180c91c449Smrg      break
1190c91c449Smrg    fi
1200c91c449Smrg  done
1210c91c449Smrg  IFS=$save_IFS
1220c91c449Smrg
1230c91c449Smrg  if test "$found" != yes; then
1240c91c449Smrg    lib=$lib.lib
1250c91c449Smrg  fi
1260c91c449Smrg}
1270c91c449Smrg
1280c91c449Smrg# func_cl_wrapper cl arg...
1290c91c449Smrg# Adjust compile command to suit cl
1300c91c449Smrgfunc_cl_wrapper ()
1310c91c449Smrg{
1320c91c449Smrg  # Assume a capable shell
1330c91c449Smrg  lib_path=
1340c91c449Smrg  shared=:
1350c91c449Smrg  linker_opts=
1360c91c449Smrg  for arg
1370c91c449Smrg  do
1380c91c449Smrg    if test -n "$eat"; then
1390c91c449Smrg      eat=
1400c91c449Smrg    else
1410c91c449Smrg      case $1 in
1420c91c449Smrg	-o)
1430c91c449Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1440c91c449Smrg	  eat=1
1450c91c449Smrg	  case $2 in
1460c91c449Smrg	    *.o | *.[oO][bB][jJ])
1470c91c449Smrg	      func_file_conv "$2"
1480c91c449Smrg	      set x "$@" -Fo"$file"
1490c91c449Smrg	      shift
1500c91c449Smrg	      ;;
1510c91c449Smrg	    *)
1520c91c449Smrg	      func_file_conv "$2"
1530c91c449Smrg	      set x "$@" -Fe"$file"
1540c91c449Smrg	      shift
1550c91c449Smrg	      ;;
1560c91c449Smrg	  esac
1570c91c449Smrg	  ;;
1580c91c449Smrg	-I)
1590c91c449Smrg	  eat=1
1600c91c449Smrg	  func_file_conv "$2" mingw
1610c91c449Smrg	  set x "$@" -I"$file"
1620c91c449Smrg	  shift
1630c91c449Smrg	  ;;
1640c91c449Smrg	-I*)
1650c91c449Smrg	  func_file_conv "${1#-I}" mingw
1660c91c449Smrg	  set x "$@" -I"$file"
1670c91c449Smrg	  shift
1680c91c449Smrg	  ;;
1690c91c449Smrg	-l)
1700c91c449Smrg	  eat=1
1710c91c449Smrg	  func_cl_dashl "$2"
1720c91c449Smrg	  set x "$@" "$lib"
1730c91c449Smrg	  shift
1740c91c449Smrg	  ;;
1750c91c449Smrg	-l*)
1760c91c449Smrg	  func_cl_dashl "${1#-l}"
1770c91c449Smrg	  set x "$@" "$lib"
1780c91c449Smrg	  shift
1790c91c449Smrg	  ;;
1800c91c449Smrg	-L)
1810c91c449Smrg	  eat=1
1820c91c449Smrg	  func_cl_dashL "$2"
1830c91c449Smrg	  ;;
1840c91c449Smrg	-L*)
1850c91c449Smrg	  func_cl_dashL "${1#-L}"
1860c91c449Smrg	  ;;
1870c91c449Smrg	-static)
1880c91c449Smrg	  shared=false
1890c91c449Smrg	  ;;
1900c91c449Smrg	-Wl,*)
1910c91c449Smrg	  arg=${1#-Wl,}
1920c91c449Smrg	  save_ifs="$IFS"; IFS=','
1930c91c449Smrg	  for flag in $arg; do
1940c91c449Smrg	    IFS="$save_ifs"
1950c91c449Smrg	    linker_opts="$linker_opts $flag"
1960c91c449Smrg	  done
1970c91c449Smrg	  IFS="$save_ifs"
1980c91c449Smrg	  ;;
1990c91c449Smrg	-Xlinker)
2000c91c449Smrg	  eat=1
2010c91c449Smrg	  linker_opts="$linker_opts $2"
2020c91c449Smrg	  ;;
2030c91c449Smrg	-*)
2040c91c449Smrg	  set x "$@" "$1"
2050c91c449Smrg	  shift
2060c91c449Smrg	  ;;
2070c91c449Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2080c91c449Smrg	  func_file_conv "$1"
2090c91c449Smrg	  set x "$@" -Tp"$file"
2100c91c449Smrg	  shift
2110c91c449Smrg	  ;;
2120c91c449Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2130c91c449Smrg	  func_file_conv "$1" mingw
2140c91c449Smrg	  set x "$@" "$file"
2150c91c449Smrg	  shift
2160c91c449Smrg	  ;;
2170c91c449Smrg	*)
2180c91c449Smrg	  set x "$@" "$1"
2190c91c449Smrg	  shift
2200c91c449Smrg	  ;;
2210c91c449Smrg      esac
2220c91c449Smrg    fi
2230c91c449Smrg    shift
2240c91c449Smrg  done
2250c91c449Smrg  if test -n "$linker_opts"; then
2260c91c449Smrg    linker_opts="-link$linker_opts"
2270c91c449Smrg  fi
2280c91c449Smrg  exec "$@" $linker_opts
2290c91c449Smrg  exit 1
2300c91c449Smrg}
2310c91c449Smrg
2320c91c449Smrgeat=
2330c91c449Smrg
2340c91c449Smrgcase $1 in
2350c91c449Smrg  '')
2360c91c449Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2370c91c449Smrg     exit 1;
2380c91c449Smrg     ;;
2390c91c449Smrg  -h | --h*)
2400c91c449Smrg    cat <<\EOF
2410c91c449SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2420c91c449Smrg
2430c91c449SmrgWrapper for compilers which do not understand '-c -o'.
2440c91c449SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2450c91c449Smrgarguments, and rename the output as expected.
2460c91c449Smrg
2470c91c449SmrgIf you are trying to build a whole package this is not the
2480c91c449Smrgright script to run: please start by reading the file 'INSTALL'.
2490c91c449Smrg
2500c91c449SmrgReport bugs to <bug-automake@gnu.org>.
2510c91c449SmrgEOF
2520c91c449Smrg    exit $?
2530c91c449Smrg    ;;
2540c91c449Smrg  -v | --v*)
2550c91c449Smrg    echo "compile $scriptversion"
2560c91c449Smrg    exit $?
2570c91c449Smrg    ;;
2582163f7f8Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
2592163f7f8Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2600c91c449Smrg    func_cl_wrapper "$@"      # Doesn't return...
2610c91c449Smrg    ;;
2620c91c449Smrgesac
2630c91c449Smrg
2640c91c449Smrgofile=
2650c91c449Smrgcfile=
2660c91c449Smrg
2670c91c449Smrgfor arg
2680c91c449Smrgdo
2690c91c449Smrg  if test -n "$eat"; then
2700c91c449Smrg    eat=
2710c91c449Smrg  else
2720c91c449Smrg    case $1 in
2730c91c449Smrg      -o)
2740c91c449Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2750c91c449Smrg	# So we strip '-o arg' only if arg is an object.
2760c91c449Smrg	eat=1
2770c91c449Smrg	case $2 in
2780c91c449Smrg	  *.o | *.obj)
2790c91c449Smrg	    ofile=$2
2800c91c449Smrg	    ;;
2810c91c449Smrg	  *)
2820c91c449Smrg	    set x "$@" -o "$2"
2830c91c449Smrg	    shift
2840c91c449Smrg	    ;;
2850c91c449Smrg	esac
2860c91c449Smrg	;;
2870c91c449Smrg      *.c)
2880c91c449Smrg	cfile=$1
2890c91c449Smrg	set x "$@" "$1"
2900c91c449Smrg	shift
2910c91c449Smrg	;;
2920c91c449Smrg      *)
2930c91c449Smrg	set x "$@" "$1"
2940c91c449Smrg	shift
2950c91c449Smrg	;;
2960c91c449Smrg    esac
2970c91c449Smrg  fi
2980c91c449Smrg  shift
2990c91c449Smrgdone
3000c91c449Smrg
3010c91c449Smrgif test -z "$ofile" || test -z "$cfile"; then
3020c91c449Smrg  # If no '-o' option was seen then we might have been invoked from a
3030c91c449Smrg  # pattern rule where we don't need one.  That is ok -- this is a
3040c91c449Smrg  # normal compilation that the losing compiler can handle.  If no
3050c91c449Smrg  # '.c' file was seen then we are probably linking.  That is also
3060c91c449Smrg  # ok.
3070c91c449Smrg  exec "$@"
3080c91c449Smrgfi
3090c91c449Smrg
3100c91c449Smrg# Name of file we expect compiler to create.
3110c91c449Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3120c91c449Smrg
3130c91c449Smrg# Create the lock directory.
3140c91c449Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3150c91c449Smrg# that we are using for the .o file.  Also, base the name on the expected
3160c91c449Smrg# object file name, since that is what matters with a parallel build.
3170c91c449Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3180c91c449Smrgwhile true; do
3190c91c449Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3200c91c449Smrg    break
3210c91c449Smrg  fi
3220c91c449Smrg  sleep 1
3230c91c449Smrgdone
3240c91c449Smrg# FIXME: race condition here if user kills between mkdir and trap.
3250c91c449Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3260c91c449Smrg
3270c91c449Smrg# Run the compile.
3280c91c449Smrg"$@"
3290c91c449Smrgret=$?
3300c91c449Smrg
3310c91c449Smrgif test -f "$cofile"; then
3320c91c449Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3330c91c449Smrgelif test -f "${cofile}bj"; then
3340c91c449Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3350c91c449Smrgfi
3360c91c449Smrg
3370c91c449Smrgrmdir "$lockdir"
3380c91c449Smrgexit $ret
3390c91c449Smrg
3400c91c449Smrg# Local Variables:
3410c91c449Smrg# mode: shell-script
3420c91c449Smrg# sh-indentation: 2
3432163f7f8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
3440c91c449Smrg# time-stamp-start: "scriptversion="
3450c91c449Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3462163f7f8Smrg# time-stamp-time-zone: "UTC0"
3470c91c449Smrg# time-stamp-end: "; # UTC"
3480c91c449Smrg# End:
349