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