19d794632Smrg#! /bin/sh
29d794632Smrg# Wrapper for compilers which do not understand '-c -o'.
39d794632Smrg
4c048b52eSmrgscriptversion=2018-03-07.03; # UTC
59d794632Smrg
6c048b52eSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
79d794632Smrg# Written by Tom Tromey <tromey@cygnus.com>.
89d794632Smrg#
99d794632Smrg# This program is free software; you can redistribute it and/or modify
109d794632Smrg# it under the terms of the GNU General Public License as published by
119d794632Smrg# the Free Software Foundation; either version 2, or (at your option)
129d794632Smrg# any later version.
139d794632Smrg#
149d794632Smrg# This program is distributed in the hope that it will be useful,
159d794632Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
169d794632Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
179d794632Smrg# GNU General Public License for more details.
189d794632Smrg#
199d794632Smrg# You should have received a copy of the GNU General Public License
20c048b52eSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
219d794632Smrg
229d794632Smrg# As a special exception to the GNU General Public License, if you
239d794632Smrg# distribute this file as part of a program that contains a
249d794632Smrg# configuration script generated by Autoconf, you may include it under
259d794632Smrg# the same distribution terms that you use for the rest of that program.
269d794632Smrg
279d794632Smrg# This file is maintained in Automake, please report
289d794632Smrg# bugs to <bug-automake@gnu.org> or send patches to
299d794632Smrg# <automake-patches@gnu.org>.
309d794632Smrg
319d794632Smrgnl='
329d794632Smrg'
339d794632Smrg
349d794632Smrg# We need space, tab and new line, in precisely that order.  Quoting is
359d794632Smrg# there to prevent tools from complaining about whitespace usage.
369d794632SmrgIFS=" ""	$nl"
379d794632Smrg
389d794632Smrgfile_conv=
399d794632Smrg
409d794632Smrg# func_file_conv build_file lazy
419d794632Smrg# Convert a $build file to $host form and store it in $file
429d794632Smrg# Currently only supports Windows hosts. If the determined conversion
439d794632Smrg# type is listed in (the comma separated) LAZY, no conversion will
449d794632Smrg# take place.
459d794632Smrgfunc_file_conv ()
469d794632Smrg{
479d794632Smrg  file=$1
489d794632Smrg  case $file in
499d794632Smrg    / | /[!/]*) # absolute file, and not a UNC file
509d794632Smrg      if test -z "$file_conv"; then
519d794632Smrg	# lazily determine how to convert abs files
529d794632Smrg	case `uname -s` in
539d794632Smrg	  MINGW*)
549d794632Smrg	    file_conv=mingw
559d794632Smrg	    ;;
56c048b52eSmrg	  CYGWIN* | MSYS*)
579d794632Smrg	    file_conv=cygwin
589d794632Smrg	    ;;
599d794632Smrg	  *)
609d794632Smrg	    file_conv=wine
619d794632Smrg	    ;;
629d794632Smrg	esac
639d794632Smrg      fi
649d794632Smrg      case $file_conv/,$2, in
659d794632Smrg	*,$file_conv,*)
669d794632Smrg	  ;;
679d794632Smrg	mingw/*)
689d794632Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
699d794632Smrg	  ;;
70c048b52eSmrg	cygwin/* | msys/*)
719d794632Smrg	  file=`cygpath -m "$file" || echo "$file"`
729d794632Smrg	  ;;
739d794632Smrg	wine/*)
749d794632Smrg	  file=`winepath -w "$file" || echo "$file"`
759d794632Smrg	  ;;
769d794632Smrg      esac
779d794632Smrg      ;;
789d794632Smrg  esac
799d794632Smrg}
809d794632Smrg
819d794632Smrg# func_cl_dashL linkdir
829d794632Smrg# Make cl look for libraries in LINKDIR
839d794632Smrgfunc_cl_dashL ()
849d794632Smrg{
859d794632Smrg  func_file_conv "$1"
869d794632Smrg  if test -z "$lib_path"; then
879d794632Smrg    lib_path=$file
889d794632Smrg  else
899d794632Smrg    lib_path="$lib_path;$file"
909d794632Smrg  fi
919d794632Smrg  linker_opts="$linker_opts -LIBPATH:$file"
929d794632Smrg}
939d794632Smrg
949d794632Smrg# func_cl_dashl library
959d794632Smrg# Do a library search-path lookup for cl
969d794632Smrgfunc_cl_dashl ()
979d794632Smrg{
989d794632Smrg  lib=$1
999d794632Smrg  found=no
1009d794632Smrg  save_IFS=$IFS
1019d794632Smrg  IFS=';'
1029d794632Smrg  for dir in $lib_path $LIB
1039d794632Smrg  do
1049d794632Smrg    IFS=$save_IFS
1059d794632Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1069d794632Smrg      found=yes
1079d794632Smrg      lib=$dir/$lib.dll.lib
1089d794632Smrg      break
1099d794632Smrg    fi
1109d794632Smrg    if test -f "$dir/$lib.lib"; then
1119d794632Smrg      found=yes
1129d794632Smrg      lib=$dir/$lib.lib
1139d794632Smrg      break
1149d794632Smrg    fi
1159d794632Smrg    if test -f "$dir/lib$lib.a"; then
1169d794632Smrg      found=yes
1179d794632Smrg      lib=$dir/lib$lib.a
1189d794632Smrg      break
1199d794632Smrg    fi
1209d794632Smrg  done
1219d794632Smrg  IFS=$save_IFS
1229d794632Smrg
1239d794632Smrg  if test "$found" != yes; then
1249d794632Smrg    lib=$lib.lib
1259d794632Smrg  fi
1269d794632Smrg}
1279d794632Smrg
1289d794632Smrg# func_cl_wrapper cl arg...
1299d794632Smrg# Adjust compile command to suit cl
1309d794632Smrgfunc_cl_wrapper ()
1319d794632Smrg{
1329d794632Smrg  # Assume a capable shell
1339d794632Smrg  lib_path=
1349d794632Smrg  shared=:
1359d794632Smrg  linker_opts=
1369d794632Smrg  for arg
1379d794632Smrg  do
1389d794632Smrg    if test -n "$eat"; then
1399d794632Smrg      eat=
1409d794632Smrg    else
1419d794632Smrg      case $1 in
1429d794632Smrg	-o)
1439d794632Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1449d794632Smrg	  eat=1
1459d794632Smrg	  case $2 in
1469d794632Smrg	    *.o | *.[oO][bB][jJ])
1479d794632Smrg	      func_file_conv "$2"
1489d794632Smrg	      set x "$@" -Fo"$file"
1499d794632Smrg	      shift
1509d794632Smrg	      ;;
1519d794632Smrg	    *)
1529d794632Smrg	      func_file_conv "$2"
1539d794632Smrg	      set x "$@" -Fe"$file"
1549d794632Smrg	      shift
1559d794632Smrg	      ;;
1569d794632Smrg	  esac
1579d794632Smrg	  ;;
1589d794632Smrg	-I)
1599d794632Smrg	  eat=1
1609d794632Smrg	  func_file_conv "$2" mingw
1619d794632Smrg	  set x "$@" -I"$file"
1629d794632Smrg	  shift
1639d794632Smrg	  ;;
1649d794632Smrg	-I*)
1659d794632Smrg	  func_file_conv "${1#-I}" mingw
1669d794632Smrg	  set x "$@" -I"$file"
1679d794632Smrg	  shift
1689d794632Smrg	  ;;
1699d794632Smrg	-l)
1709d794632Smrg	  eat=1
1719d794632Smrg	  func_cl_dashl "$2"
1729d794632Smrg	  set x "$@" "$lib"
1739d794632Smrg	  shift
1749d794632Smrg	  ;;
1759d794632Smrg	-l*)
1769d794632Smrg	  func_cl_dashl "${1#-l}"
1779d794632Smrg	  set x "$@" "$lib"
1789d794632Smrg	  shift
1799d794632Smrg	  ;;
1809d794632Smrg	-L)
1819d794632Smrg	  eat=1
1829d794632Smrg	  func_cl_dashL "$2"
1839d794632Smrg	  ;;
1849d794632Smrg	-L*)
1859d794632Smrg	  func_cl_dashL "${1#-L}"
1869d794632Smrg	  ;;
1879d794632Smrg	-static)
1889d794632Smrg	  shared=false
1899d794632Smrg	  ;;
1909d794632Smrg	-Wl,*)
1919d794632Smrg	  arg=${1#-Wl,}
1929d794632Smrg	  save_ifs="$IFS"; IFS=','
1939d794632Smrg	  for flag in $arg; do
1949d794632Smrg	    IFS="$save_ifs"
1959d794632Smrg	    linker_opts="$linker_opts $flag"
1969d794632Smrg	  done
1979d794632Smrg	  IFS="$save_ifs"
1989d794632Smrg	  ;;
1999d794632Smrg	-Xlinker)
2009d794632Smrg	  eat=1
2019d794632Smrg	  linker_opts="$linker_opts $2"
2029d794632Smrg	  ;;
2039d794632Smrg	-*)
2049d794632Smrg	  set x "$@" "$1"
2059d794632Smrg	  shift
2069d794632Smrg	  ;;
2079d794632Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2089d794632Smrg	  func_file_conv "$1"
2099d794632Smrg	  set x "$@" -Tp"$file"
2109d794632Smrg	  shift
2119d794632Smrg	  ;;
2129d794632Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2139d794632Smrg	  func_file_conv "$1" mingw
2149d794632Smrg	  set x "$@" "$file"
2159d794632Smrg	  shift
2169d794632Smrg	  ;;
2179d794632Smrg	*)
2189d794632Smrg	  set x "$@" "$1"
2199d794632Smrg	  shift
2209d794632Smrg	  ;;
2219d794632Smrg      esac
2229d794632Smrg    fi
2239d794632Smrg    shift
2249d794632Smrg  done
2259d794632Smrg  if test -n "$linker_opts"; then
2269d794632Smrg    linker_opts="-link$linker_opts"
2279d794632Smrg  fi
2289d794632Smrg  exec "$@" $linker_opts
2299d794632Smrg  exit 1
2309d794632Smrg}
2319d794632Smrg
2329d794632Smrgeat=
2339d794632Smrg
2349d794632Smrgcase $1 in
2359d794632Smrg  '')
2369d794632Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2379d794632Smrg     exit 1;
2389d794632Smrg     ;;
2399d794632Smrg  -h | --h*)
2409d794632Smrg    cat <<\EOF
2419d794632SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2429d794632Smrg
2439d794632SmrgWrapper for compilers which do not understand '-c -o'.
2449d794632SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2459d794632Smrgarguments, and rename the output as expected.
2469d794632Smrg
2479d794632SmrgIf you are trying to build a whole package this is not the
2489d794632Smrgright script to run: please start by reading the file 'INSTALL'.
2499d794632Smrg
2509d794632SmrgReport bugs to <bug-automake@gnu.org>.
2519d794632SmrgEOF
2529d794632Smrg    exit $?
2539d794632Smrg    ;;
2549d794632Smrg  -v | --v*)
2559d794632Smrg    echo "compile $scriptversion"
2569d794632Smrg    exit $?
2579d794632Smrg    ;;
258c048b52eSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259c048b52eSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2609d794632Smrg    func_cl_wrapper "$@"      # Doesn't return...
2619d794632Smrg    ;;
2629d794632Smrgesac
2639d794632Smrg
2649d794632Smrgofile=
2659d794632Smrgcfile=
2669d794632Smrg
2679d794632Smrgfor arg
2689d794632Smrgdo
2699d794632Smrg  if test -n "$eat"; then
2709d794632Smrg    eat=
2719d794632Smrg  else
2729d794632Smrg    case $1 in
2739d794632Smrg      -o)
2749d794632Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2759d794632Smrg	# So we strip '-o arg' only if arg is an object.
2769d794632Smrg	eat=1
2779d794632Smrg	case $2 in
2789d794632Smrg	  *.o | *.obj)
2799d794632Smrg	    ofile=$2
2809d794632Smrg	    ;;
2819d794632Smrg	  *)
2829d794632Smrg	    set x "$@" -o "$2"
2839d794632Smrg	    shift
2849d794632Smrg	    ;;
2859d794632Smrg	esac
2869d794632Smrg	;;
2879d794632Smrg      *.c)
2889d794632Smrg	cfile=$1
2899d794632Smrg	set x "$@" "$1"
2909d794632Smrg	shift
2919d794632Smrg	;;
2929d794632Smrg      *)
2939d794632Smrg	set x "$@" "$1"
2949d794632Smrg	shift
2959d794632Smrg	;;
2969d794632Smrg    esac
2979d794632Smrg  fi
2989d794632Smrg  shift
2999d794632Smrgdone
3009d794632Smrg
3019d794632Smrgif test -z "$ofile" || test -z "$cfile"; then
3029d794632Smrg  # If no '-o' option was seen then we might have been invoked from a
3039d794632Smrg  # pattern rule where we don't need one.  That is ok -- this is a
3049d794632Smrg  # normal compilation that the losing compiler can handle.  If no
3059d794632Smrg  # '.c' file was seen then we are probably linking.  That is also
3069d794632Smrg  # ok.
3079d794632Smrg  exec "$@"
3089d794632Smrgfi
3099d794632Smrg
3109d794632Smrg# Name of file we expect compiler to create.
3119d794632Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3129d794632Smrg
3139d794632Smrg# Create the lock directory.
3149d794632Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3159d794632Smrg# that we are using for the .o file.  Also, base the name on the expected
3169d794632Smrg# object file name, since that is what matters with a parallel build.
3179d794632Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3189d794632Smrgwhile true; do
3199d794632Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3209d794632Smrg    break
3219d794632Smrg  fi
3229d794632Smrg  sleep 1
3239d794632Smrgdone
3249d794632Smrg# FIXME: race condition here if user kills between mkdir and trap.
3259d794632Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3269d794632Smrg
3279d794632Smrg# Run the compile.
3289d794632Smrg"$@"
3299d794632Smrgret=$?
3309d794632Smrg
3319d794632Smrgif test -f "$cofile"; then
3329d794632Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3339d794632Smrgelif test -f "${cofile}bj"; then
3349d794632Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3359d794632Smrgfi
3369d794632Smrg
3379d794632Smrgrmdir "$lockdir"
3389d794632Smrgexit $ret
3399d794632Smrg
3409d794632Smrg# Local Variables:
3419d794632Smrg# mode: shell-script
3429d794632Smrg# sh-indentation: 2
343c048b52eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
3449d794632Smrg# time-stamp-start: "scriptversion="
3459d794632Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346c048b52eSmrg# time-stamp-time-zone: "UTC0"
3479d794632Smrg# time-stamp-end: "; # UTC"
3489d794632Smrg# End:
349