16af7124fSmrg#! /bin/sh
26af7124fSmrg# Wrapper for compilers which do not understand '-c -o'.
36af7124fSmrg
400ca1914Smrgscriptversion=2024-06-19.01; # UTC
56af7124fSmrg
600ca1914Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
76af7124fSmrg# Written by Tom Tromey <tromey@cygnus.com>.
86af7124fSmrg#
96af7124fSmrg# This program is free software; you can redistribute it and/or modify
106af7124fSmrg# it under the terms of the GNU General Public License as published by
116af7124fSmrg# the Free Software Foundation; either version 2, or (at your option)
126af7124fSmrg# any later version.
136af7124fSmrg#
146af7124fSmrg# This program is distributed in the hope that it will be useful,
156af7124fSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
166af7124fSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
176af7124fSmrg# GNU General Public License for more details.
186af7124fSmrg#
196af7124fSmrg# You should have received a copy of the GNU General Public License
20da1f2d5dSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
216af7124fSmrg
226af7124fSmrg# As a special exception to the GNU General Public License, if you
236af7124fSmrg# distribute this file as part of a program that contains a
246af7124fSmrg# configuration script generated by Autoconf, you may include it under
256af7124fSmrg# the same distribution terms that you use for the rest of that program.
266af7124fSmrg
276af7124fSmrg# This file is maintained in Automake, please report
286af7124fSmrg# bugs to <bug-automake@gnu.org> or send patches to
296af7124fSmrg# <automake-patches@gnu.org>.
306af7124fSmrg
316af7124fSmrgnl='
326af7124fSmrg'
336af7124fSmrg
346af7124fSmrg# We need space, tab and new line, in precisely that order.  Quoting is
356af7124fSmrg# there to prevent tools from complaining about whitespace usage.
366af7124fSmrgIFS=" ""	$nl"
376af7124fSmrg
386af7124fSmrgfile_conv=
396af7124fSmrg
406af7124fSmrg# func_file_conv build_file lazy
416af7124fSmrg# Convert a $build file to $host form and store it in $file
426af7124fSmrg# Currently only supports Windows hosts. If the determined conversion
436af7124fSmrg# type is listed in (the comma separated) LAZY, no conversion will
446af7124fSmrg# take place.
456af7124fSmrgfunc_file_conv ()
466af7124fSmrg{
476af7124fSmrg  file=$1
486af7124fSmrg  case $file in
496af7124fSmrg    / | /[!/]*) # absolute file, and not a UNC file
506af7124fSmrg      if test -z "$file_conv"; then
516af7124fSmrg	# lazily determine how to convert abs files
526af7124fSmrg	case `uname -s` in
536af7124fSmrg	  MINGW*)
546af7124fSmrg	    file_conv=mingw
556af7124fSmrg	    ;;
56da1f2d5dSmrg	  CYGWIN* | MSYS*)
576af7124fSmrg	    file_conv=cygwin
586af7124fSmrg	    ;;
596af7124fSmrg	  *)
606af7124fSmrg	    file_conv=wine
616af7124fSmrg	    ;;
626af7124fSmrg	esac
636af7124fSmrg      fi
646af7124fSmrg      case $file_conv/,$2, in
656af7124fSmrg	*,$file_conv,*)
666af7124fSmrg	  ;;
676af7124fSmrg	mingw/*)
686af7124fSmrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
696af7124fSmrg	  ;;
70da1f2d5dSmrg	cygwin/* | msys/*)
716af7124fSmrg	  file=`cygpath -m "$file" || echo "$file"`
726af7124fSmrg	  ;;
736af7124fSmrg	wine/*)
746af7124fSmrg	  file=`winepath -w "$file" || echo "$file"`
756af7124fSmrg	  ;;
766af7124fSmrg      esac
776af7124fSmrg      ;;
786af7124fSmrg  esac
796af7124fSmrg}
806af7124fSmrg
816af7124fSmrg# func_cl_dashL linkdir
826af7124fSmrg# Make cl look for libraries in LINKDIR
836af7124fSmrgfunc_cl_dashL ()
846af7124fSmrg{
856af7124fSmrg  func_file_conv "$1"
866af7124fSmrg  if test -z "$lib_path"; then
876af7124fSmrg    lib_path=$file
886af7124fSmrg  else
896af7124fSmrg    lib_path="$lib_path;$file"
906af7124fSmrg  fi
916af7124fSmrg  linker_opts="$linker_opts -LIBPATH:$file"
926af7124fSmrg}
936af7124fSmrg
946af7124fSmrg# func_cl_dashl library
956af7124fSmrg# Do a library search-path lookup for cl
966af7124fSmrgfunc_cl_dashl ()
976af7124fSmrg{
986af7124fSmrg  lib=$1
996af7124fSmrg  found=no
1006af7124fSmrg  save_IFS=$IFS
1016af7124fSmrg  IFS=';'
1026af7124fSmrg  for dir in $lib_path $LIB
1036af7124fSmrg  do
1046af7124fSmrg    IFS=$save_IFS
1056af7124fSmrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1066af7124fSmrg      found=yes
1076af7124fSmrg      lib=$dir/$lib.dll.lib
1086af7124fSmrg      break
1096af7124fSmrg    fi
1106af7124fSmrg    if test -f "$dir/$lib.lib"; then
1116af7124fSmrg      found=yes
1126af7124fSmrg      lib=$dir/$lib.lib
1136af7124fSmrg      break
1146af7124fSmrg    fi
1156af7124fSmrg    if test -f "$dir/lib$lib.a"; then
1166af7124fSmrg      found=yes
1176af7124fSmrg      lib=$dir/lib$lib.a
1186af7124fSmrg      break
1196af7124fSmrg    fi
1206af7124fSmrg  done
1216af7124fSmrg  IFS=$save_IFS
1226af7124fSmrg
1236af7124fSmrg  if test "$found" != yes; then
1246af7124fSmrg    lib=$lib.lib
1256af7124fSmrg  fi
1266af7124fSmrg}
1276af7124fSmrg
1286af7124fSmrg# func_cl_wrapper cl arg...
1296af7124fSmrg# Adjust compile command to suit cl
1306af7124fSmrgfunc_cl_wrapper ()
1316af7124fSmrg{
1326af7124fSmrg  # Assume a capable shell
1336af7124fSmrg  lib_path=
1346af7124fSmrg  shared=:
1356af7124fSmrg  linker_opts=
1366af7124fSmrg  for arg
1376af7124fSmrg  do
1386af7124fSmrg    if test -n "$eat"; then
1396af7124fSmrg      eat=
1406af7124fSmrg    else
1416af7124fSmrg      case $1 in
1426af7124fSmrg	-o)
1436af7124fSmrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1446af7124fSmrg	  eat=1
1456af7124fSmrg	  case $2 in
14600ca1914Smrg	    *.o | *.lo | *.[oO][bB][jJ])
1476af7124fSmrg	      func_file_conv "$2"
1486af7124fSmrg	      set x "$@" -Fo"$file"
1496af7124fSmrg	      shift
1506af7124fSmrg	      ;;
1516af7124fSmrg	    *)
1526af7124fSmrg	      func_file_conv "$2"
1536af7124fSmrg	      set x "$@" -Fe"$file"
1546af7124fSmrg	      shift
1556af7124fSmrg	      ;;
1566af7124fSmrg	  esac
1576af7124fSmrg	  ;;
1586af7124fSmrg	-I)
1596af7124fSmrg	  eat=1
1606af7124fSmrg	  func_file_conv "$2" mingw
1616af7124fSmrg	  set x "$@" -I"$file"
1626af7124fSmrg	  shift
1636af7124fSmrg	  ;;
1646af7124fSmrg	-I*)
1656af7124fSmrg	  func_file_conv "${1#-I}" mingw
1666af7124fSmrg	  set x "$@" -I"$file"
1676af7124fSmrg	  shift
1686af7124fSmrg	  ;;
1696af7124fSmrg	-l)
1706af7124fSmrg	  eat=1
1716af7124fSmrg	  func_cl_dashl "$2"
1726af7124fSmrg	  set x "$@" "$lib"
1736af7124fSmrg	  shift
1746af7124fSmrg	  ;;
1756af7124fSmrg	-l*)
1766af7124fSmrg	  func_cl_dashl "${1#-l}"
1776af7124fSmrg	  set x "$@" "$lib"
1786af7124fSmrg	  shift
1796af7124fSmrg	  ;;
1806af7124fSmrg	-L)
1816af7124fSmrg	  eat=1
1826af7124fSmrg	  func_cl_dashL "$2"
1836af7124fSmrg	  ;;
1846af7124fSmrg	-L*)
1856af7124fSmrg	  func_cl_dashL "${1#-L}"
1866af7124fSmrg	  ;;
1876af7124fSmrg	-static)
1886af7124fSmrg	  shared=false
1896af7124fSmrg	  ;;
1906af7124fSmrg	-Wl,*)
1916af7124fSmrg	  arg=${1#-Wl,}
1926af7124fSmrg	  save_ifs="$IFS"; IFS=','
1936af7124fSmrg	  for flag in $arg; do
1946af7124fSmrg	    IFS="$save_ifs"
1956af7124fSmrg	    linker_opts="$linker_opts $flag"
1966af7124fSmrg	  done
1976af7124fSmrg	  IFS="$save_ifs"
1986af7124fSmrg	  ;;
1996af7124fSmrg	-Xlinker)
2006af7124fSmrg	  eat=1
2016af7124fSmrg	  linker_opts="$linker_opts $2"
2026af7124fSmrg	  ;;
2036af7124fSmrg	-*)
2046af7124fSmrg	  set x "$@" "$1"
2056af7124fSmrg	  shift
2066af7124fSmrg	  ;;
2076af7124fSmrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2086af7124fSmrg	  func_file_conv "$1"
2096af7124fSmrg	  set x "$@" -Tp"$file"
2106af7124fSmrg	  shift
2116af7124fSmrg	  ;;
2126af7124fSmrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2136af7124fSmrg	  func_file_conv "$1" mingw
2146af7124fSmrg	  set x "$@" "$file"
2156af7124fSmrg	  shift
2166af7124fSmrg	  ;;
2176af7124fSmrg	*)
2186af7124fSmrg	  set x "$@" "$1"
2196af7124fSmrg	  shift
2206af7124fSmrg	  ;;
2216af7124fSmrg      esac
2226af7124fSmrg    fi
2236af7124fSmrg    shift
2246af7124fSmrg  done
2256af7124fSmrg  if test -n "$linker_opts"; then
2266af7124fSmrg    linker_opts="-link$linker_opts"
2276af7124fSmrg  fi
2286af7124fSmrg  exec "$@" $linker_opts
2296af7124fSmrg  exit 1
2306af7124fSmrg}
2316af7124fSmrg
2326af7124fSmrgeat=
2336af7124fSmrg
2346af7124fSmrgcase $1 in
2356af7124fSmrg  '')
2366af7124fSmrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2376af7124fSmrg     exit 1;
2386af7124fSmrg     ;;
2396af7124fSmrg  -h | --h*)
2406af7124fSmrg    cat <<\EOF
2416af7124fSmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2426af7124fSmrg
2436af7124fSmrgWrapper for compilers which do not understand '-c -o'.
2446af7124fSmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2456af7124fSmrgarguments, and rename the output as expected.
2466af7124fSmrg
2476af7124fSmrgIf you are trying to build a whole package this is not the
2486af7124fSmrgright script to run: please start by reading the file 'INSTALL'.
2496af7124fSmrg
2506af7124fSmrgReport bugs to <bug-automake@gnu.org>.
25100ca1914SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
25200ca1914SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
2536af7124fSmrgEOF
2546af7124fSmrg    exit $?
2556af7124fSmrg    ;;
2566af7124fSmrg  -v | --v*)
25700ca1914Smrg    echo "compile (GNU Automake) $scriptversion"
2586af7124fSmrg    exit $?
2596af7124fSmrg    ;;
260da1f2d5dSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
26100ca1914Smrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262da1f2d5dSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2636af7124fSmrg    func_cl_wrapper "$@"      # Doesn't return...
2646af7124fSmrg    ;;
2656af7124fSmrgesac
2666af7124fSmrg
2676af7124fSmrgofile=
2686af7124fSmrgcfile=
2696af7124fSmrg
2706af7124fSmrgfor arg
2716af7124fSmrgdo
2726af7124fSmrg  if test -n "$eat"; then
2736af7124fSmrg    eat=
2746af7124fSmrg  else
2756af7124fSmrg    case $1 in
2766af7124fSmrg      -o)
2776af7124fSmrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2786af7124fSmrg	# So we strip '-o arg' only if arg is an object.
2796af7124fSmrg	eat=1
2806af7124fSmrg	case $2 in
2816af7124fSmrg	  *.o | *.obj)
2826af7124fSmrg	    ofile=$2
2836af7124fSmrg	    ;;
2846af7124fSmrg	  *)
2856af7124fSmrg	    set x "$@" -o "$2"
2866af7124fSmrg	    shift
2876af7124fSmrg	    ;;
2886af7124fSmrg	esac
2896af7124fSmrg	;;
2906af7124fSmrg      *.c)
2916af7124fSmrg	cfile=$1
2926af7124fSmrg	set x "$@" "$1"
2936af7124fSmrg	shift
2946af7124fSmrg	;;
2956af7124fSmrg      *)
2966af7124fSmrg	set x "$@" "$1"
2976af7124fSmrg	shift
2986af7124fSmrg	;;
2996af7124fSmrg    esac
3006af7124fSmrg  fi
3016af7124fSmrg  shift
3026af7124fSmrgdone
3036af7124fSmrg
3046af7124fSmrgif test -z "$ofile" || test -z "$cfile"; then
3056af7124fSmrg  # If no '-o' option was seen then we might have been invoked from a
3066af7124fSmrg  # pattern rule where we don't need one.  That is ok -- this is a
3076af7124fSmrg  # normal compilation that the losing compiler can handle.  If no
3086af7124fSmrg  # '.c' file was seen then we are probably linking.  That is also
3096af7124fSmrg  # ok.
3106af7124fSmrg  exec "$@"
3116af7124fSmrgfi
3126af7124fSmrg
3136af7124fSmrg# Name of file we expect compiler to create.
3146af7124fSmrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3156af7124fSmrg
3166af7124fSmrg# Create the lock directory.
3176af7124fSmrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3186af7124fSmrg# that we are using for the .o file.  Also, base the name on the expected
3196af7124fSmrg# object file name, since that is what matters with a parallel build.
3206af7124fSmrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3216af7124fSmrgwhile true; do
3226af7124fSmrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3236af7124fSmrg    break
3246af7124fSmrg  fi
3256af7124fSmrg  sleep 1
3266af7124fSmrgdone
3276af7124fSmrg# FIXME: race condition here if user kills between mkdir and trap.
3286af7124fSmrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3296af7124fSmrg
3306af7124fSmrg# Run the compile.
3316af7124fSmrg"$@"
3326af7124fSmrgret=$?
3336af7124fSmrg
3346af7124fSmrgif test -f "$cofile"; then
3356af7124fSmrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3366af7124fSmrgelif test -f "${cofile}bj"; then
3376af7124fSmrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3386af7124fSmrgfi
3396af7124fSmrg
3406af7124fSmrgrmdir "$lockdir"
3416af7124fSmrgexit $ret
3426af7124fSmrg
3436af7124fSmrg# Local Variables:
3446af7124fSmrg# mode: shell-script
3456af7124fSmrg# sh-indentation: 2
346da1f2d5dSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
3476af7124fSmrg# time-stamp-start: "scriptversion="
3486af7124fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349da1f2d5dSmrg# time-stamp-time-zone: "UTC0"
3506af7124fSmrg# time-stamp-end: "; # UTC"
3516af7124fSmrg# End:
352