18a0d9095Smrg#! /bin/sh
28a0d9095Smrg# Wrapper for compilers which do not understand '-c -o'.
38a0d9095Smrg
447a4502cSmrgscriptversion=2024-06-19.01; # UTC
58a0d9095Smrg
647a4502cSmrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
78a0d9095Smrg# Written by Tom Tromey <tromey@cygnus.com>.
88a0d9095Smrg#
98a0d9095Smrg# This program is free software; you can redistribute it and/or modify
108a0d9095Smrg# it under the terms of the GNU General Public License as published by
118a0d9095Smrg# the Free Software Foundation; either version 2, or (at your option)
128a0d9095Smrg# any later version.
138a0d9095Smrg#
148a0d9095Smrg# This program is distributed in the hope that it will be useful,
158a0d9095Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
168a0d9095Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
178a0d9095Smrg# GNU General Public License for more details.
188a0d9095Smrg#
198a0d9095Smrg# You should have received a copy of the GNU General Public License
20e45ace2bSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
218a0d9095Smrg
228a0d9095Smrg# As a special exception to the GNU General Public License, if you
238a0d9095Smrg# distribute this file as part of a program that contains a
248a0d9095Smrg# configuration script generated by Autoconf, you may include it under
258a0d9095Smrg# the same distribution terms that you use for the rest of that program.
268a0d9095Smrg
278a0d9095Smrg# This file is maintained in Automake, please report
288a0d9095Smrg# bugs to <bug-automake@gnu.org> or send patches to
298a0d9095Smrg# <automake-patches@gnu.org>.
308a0d9095Smrg
318a0d9095Smrgnl='
328a0d9095Smrg'
338a0d9095Smrg
348a0d9095Smrg# We need space, tab and new line, in precisely that order.  Quoting is
358a0d9095Smrg# there to prevent tools from complaining about whitespace usage.
368a0d9095SmrgIFS=" ""	$nl"
378a0d9095Smrg
388a0d9095Smrgfile_conv=
398a0d9095Smrg
408a0d9095Smrg# func_file_conv build_file lazy
418a0d9095Smrg# Convert a $build file to $host form and store it in $file
428a0d9095Smrg# Currently only supports Windows hosts. If the determined conversion
438a0d9095Smrg# type is listed in (the comma separated) LAZY, no conversion will
448a0d9095Smrg# take place.
458a0d9095Smrgfunc_file_conv ()
468a0d9095Smrg{
478a0d9095Smrg  file=$1
488a0d9095Smrg  case $file in
498a0d9095Smrg    / | /[!/]*) # absolute file, and not a UNC file
508a0d9095Smrg      if test -z "$file_conv"; then
518a0d9095Smrg	# lazily determine how to convert abs files
528a0d9095Smrg	case `uname -s` in
538a0d9095Smrg	  MINGW*)
548a0d9095Smrg	    file_conv=mingw
558a0d9095Smrg	    ;;
56e45ace2bSmrg	  CYGWIN* | MSYS*)
578a0d9095Smrg	    file_conv=cygwin
588a0d9095Smrg	    ;;
598a0d9095Smrg	  *)
608a0d9095Smrg	    file_conv=wine
618a0d9095Smrg	    ;;
628a0d9095Smrg	esac
638a0d9095Smrg      fi
648a0d9095Smrg      case $file_conv/,$2, in
658a0d9095Smrg	*,$file_conv,*)
668a0d9095Smrg	  ;;
678a0d9095Smrg	mingw/*)
688a0d9095Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
698a0d9095Smrg	  ;;
70e45ace2bSmrg	cygwin/* | msys/*)
718a0d9095Smrg	  file=`cygpath -m "$file" || echo "$file"`
728a0d9095Smrg	  ;;
738a0d9095Smrg	wine/*)
748a0d9095Smrg	  file=`winepath -w "$file" || echo "$file"`
758a0d9095Smrg	  ;;
768a0d9095Smrg      esac
778a0d9095Smrg      ;;
788a0d9095Smrg  esac
798a0d9095Smrg}
808a0d9095Smrg
818a0d9095Smrg# func_cl_dashL linkdir
828a0d9095Smrg# Make cl look for libraries in LINKDIR
838a0d9095Smrgfunc_cl_dashL ()
848a0d9095Smrg{
858a0d9095Smrg  func_file_conv "$1"
868a0d9095Smrg  if test -z "$lib_path"; then
878a0d9095Smrg    lib_path=$file
888a0d9095Smrg  else
898a0d9095Smrg    lib_path="$lib_path;$file"
908a0d9095Smrg  fi
918a0d9095Smrg  linker_opts="$linker_opts -LIBPATH:$file"
928a0d9095Smrg}
938a0d9095Smrg
948a0d9095Smrg# func_cl_dashl library
958a0d9095Smrg# Do a library search-path lookup for cl
968a0d9095Smrgfunc_cl_dashl ()
978a0d9095Smrg{
988a0d9095Smrg  lib=$1
998a0d9095Smrg  found=no
1008a0d9095Smrg  save_IFS=$IFS
1018a0d9095Smrg  IFS=';'
1028a0d9095Smrg  for dir in $lib_path $LIB
1038a0d9095Smrg  do
1048a0d9095Smrg    IFS=$save_IFS
1058a0d9095Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
1068a0d9095Smrg      found=yes
1078a0d9095Smrg      lib=$dir/$lib.dll.lib
1088a0d9095Smrg      break
1098a0d9095Smrg    fi
1108a0d9095Smrg    if test -f "$dir/$lib.lib"; then
1118a0d9095Smrg      found=yes
1128a0d9095Smrg      lib=$dir/$lib.lib
1138a0d9095Smrg      break
1148a0d9095Smrg    fi
1158a0d9095Smrg    if test -f "$dir/lib$lib.a"; then
1168a0d9095Smrg      found=yes
1178a0d9095Smrg      lib=$dir/lib$lib.a
1188a0d9095Smrg      break
1198a0d9095Smrg    fi
1208a0d9095Smrg  done
1218a0d9095Smrg  IFS=$save_IFS
1228a0d9095Smrg
1238a0d9095Smrg  if test "$found" != yes; then
1248a0d9095Smrg    lib=$lib.lib
1258a0d9095Smrg  fi
1268a0d9095Smrg}
1278a0d9095Smrg
1288a0d9095Smrg# func_cl_wrapper cl arg...
1298a0d9095Smrg# Adjust compile command to suit cl
1308a0d9095Smrgfunc_cl_wrapper ()
1318a0d9095Smrg{
1328a0d9095Smrg  # Assume a capable shell
1338a0d9095Smrg  lib_path=
1348a0d9095Smrg  shared=:
1358a0d9095Smrg  linker_opts=
1368a0d9095Smrg  for arg
1378a0d9095Smrg  do
1388a0d9095Smrg    if test -n "$eat"; then
1398a0d9095Smrg      eat=
1408a0d9095Smrg    else
1418a0d9095Smrg      case $1 in
1428a0d9095Smrg	-o)
1438a0d9095Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
1448a0d9095Smrg	  eat=1
1458a0d9095Smrg	  case $2 in
14647a4502cSmrg	    *.o | *.lo | *.[oO][bB][jJ])
1478a0d9095Smrg	      func_file_conv "$2"
1488a0d9095Smrg	      set x "$@" -Fo"$file"
1498a0d9095Smrg	      shift
1508a0d9095Smrg	      ;;
1518a0d9095Smrg	    *)
1528a0d9095Smrg	      func_file_conv "$2"
1538a0d9095Smrg	      set x "$@" -Fe"$file"
1548a0d9095Smrg	      shift
1558a0d9095Smrg	      ;;
1568a0d9095Smrg	  esac
1578a0d9095Smrg	  ;;
1588a0d9095Smrg	-I)
1598a0d9095Smrg	  eat=1
1608a0d9095Smrg	  func_file_conv "$2" mingw
1618a0d9095Smrg	  set x "$@" -I"$file"
1628a0d9095Smrg	  shift
1638a0d9095Smrg	  ;;
1648a0d9095Smrg	-I*)
1658a0d9095Smrg	  func_file_conv "${1#-I}" mingw
1668a0d9095Smrg	  set x "$@" -I"$file"
1678a0d9095Smrg	  shift
1688a0d9095Smrg	  ;;
1698a0d9095Smrg	-l)
1708a0d9095Smrg	  eat=1
1718a0d9095Smrg	  func_cl_dashl "$2"
1728a0d9095Smrg	  set x "$@" "$lib"
1738a0d9095Smrg	  shift
1748a0d9095Smrg	  ;;
1758a0d9095Smrg	-l*)
1768a0d9095Smrg	  func_cl_dashl "${1#-l}"
1778a0d9095Smrg	  set x "$@" "$lib"
1788a0d9095Smrg	  shift
1798a0d9095Smrg	  ;;
1808a0d9095Smrg	-L)
1818a0d9095Smrg	  eat=1
1828a0d9095Smrg	  func_cl_dashL "$2"
1838a0d9095Smrg	  ;;
1848a0d9095Smrg	-L*)
1858a0d9095Smrg	  func_cl_dashL "${1#-L}"
1868a0d9095Smrg	  ;;
1878a0d9095Smrg	-static)
1888a0d9095Smrg	  shared=false
1898a0d9095Smrg	  ;;
1908a0d9095Smrg	-Wl,*)
1918a0d9095Smrg	  arg=${1#-Wl,}
1928a0d9095Smrg	  save_ifs="$IFS"; IFS=','
1938a0d9095Smrg	  for flag in $arg; do
1948a0d9095Smrg	    IFS="$save_ifs"
1958a0d9095Smrg	    linker_opts="$linker_opts $flag"
1968a0d9095Smrg	  done
1978a0d9095Smrg	  IFS="$save_ifs"
1988a0d9095Smrg	  ;;
1998a0d9095Smrg	-Xlinker)
2008a0d9095Smrg	  eat=1
2018a0d9095Smrg	  linker_opts="$linker_opts $2"
2028a0d9095Smrg	  ;;
2038a0d9095Smrg	-*)
2048a0d9095Smrg	  set x "$@" "$1"
2058a0d9095Smrg	  shift
2068a0d9095Smrg	  ;;
2078a0d9095Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
2088a0d9095Smrg	  func_file_conv "$1"
2098a0d9095Smrg	  set x "$@" -Tp"$file"
2108a0d9095Smrg	  shift
2118a0d9095Smrg	  ;;
2128a0d9095Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
2138a0d9095Smrg	  func_file_conv "$1" mingw
2148a0d9095Smrg	  set x "$@" "$file"
2158a0d9095Smrg	  shift
2168a0d9095Smrg	  ;;
2178a0d9095Smrg	*)
2188a0d9095Smrg	  set x "$@" "$1"
2198a0d9095Smrg	  shift
2208a0d9095Smrg	  ;;
2218a0d9095Smrg      esac
2228a0d9095Smrg    fi
2238a0d9095Smrg    shift
2248a0d9095Smrg  done
2258a0d9095Smrg  if test -n "$linker_opts"; then
2268a0d9095Smrg    linker_opts="-link$linker_opts"
2278a0d9095Smrg  fi
2288a0d9095Smrg  exec "$@" $linker_opts
2298a0d9095Smrg  exit 1
2308a0d9095Smrg}
2318a0d9095Smrg
2328a0d9095Smrgeat=
2338a0d9095Smrg
2348a0d9095Smrgcase $1 in
2358a0d9095Smrg  '')
2368a0d9095Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2378a0d9095Smrg     exit 1;
2388a0d9095Smrg     ;;
2398a0d9095Smrg  -h | --h*)
2408a0d9095Smrg    cat <<\EOF
2418a0d9095SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2428a0d9095Smrg
2438a0d9095SmrgWrapper for compilers which do not understand '-c -o'.
2448a0d9095SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2458a0d9095Smrgarguments, and rename the output as expected.
2468a0d9095Smrg
2478a0d9095SmrgIf you are trying to build a whole package this is not the
2488a0d9095Smrgright script to run: please start by reading the file 'INSTALL'.
2498a0d9095Smrg
2508a0d9095SmrgReport bugs to <bug-automake@gnu.org>.
25147a4502cSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
25247a4502cSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
2538a0d9095SmrgEOF
2548a0d9095Smrg    exit $?
2558a0d9095Smrg    ;;
2568a0d9095Smrg  -v | --v*)
25747a4502cSmrg    echo "compile (GNU Automake) $scriptversion"
2588a0d9095Smrg    exit $?
2598a0d9095Smrg    ;;
260e45ace2bSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
26147a4502cSmrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262e45ace2bSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
2638a0d9095Smrg    func_cl_wrapper "$@"      # Doesn't return...
2648a0d9095Smrg    ;;
2658a0d9095Smrgesac
2668a0d9095Smrg
2678a0d9095Smrgofile=
2688a0d9095Smrgcfile=
2698a0d9095Smrg
2708a0d9095Smrgfor arg
2718a0d9095Smrgdo
2728a0d9095Smrg  if test -n "$eat"; then
2738a0d9095Smrg    eat=
2748a0d9095Smrg  else
2758a0d9095Smrg    case $1 in
2768a0d9095Smrg      -o)
2778a0d9095Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
2788a0d9095Smrg	# So we strip '-o arg' only if arg is an object.
2798a0d9095Smrg	eat=1
2808a0d9095Smrg	case $2 in
2818a0d9095Smrg	  *.o | *.obj)
2828a0d9095Smrg	    ofile=$2
2838a0d9095Smrg	    ;;
2848a0d9095Smrg	  *)
2858a0d9095Smrg	    set x "$@" -o "$2"
2868a0d9095Smrg	    shift
2878a0d9095Smrg	    ;;
2888a0d9095Smrg	esac
2898a0d9095Smrg	;;
2908a0d9095Smrg      *.c)
2918a0d9095Smrg	cfile=$1
2928a0d9095Smrg	set x "$@" "$1"
2938a0d9095Smrg	shift
2948a0d9095Smrg	;;
2958a0d9095Smrg      *)
2968a0d9095Smrg	set x "$@" "$1"
2978a0d9095Smrg	shift
2988a0d9095Smrg	;;
2998a0d9095Smrg    esac
3008a0d9095Smrg  fi
3018a0d9095Smrg  shift
3028a0d9095Smrgdone
3038a0d9095Smrg
3048a0d9095Smrgif test -z "$ofile" || test -z "$cfile"; then
3058a0d9095Smrg  # If no '-o' option was seen then we might have been invoked from a
3068a0d9095Smrg  # pattern rule where we don't need one.  That is ok -- this is a
3078a0d9095Smrg  # normal compilation that the losing compiler can handle.  If no
3088a0d9095Smrg  # '.c' file was seen then we are probably linking.  That is also
3098a0d9095Smrg  # ok.
3108a0d9095Smrg  exec "$@"
3118a0d9095Smrgfi
3128a0d9095Smrg
3138a0d9095Smrg# Name of file we expect compiler to create.
3148a0d9095Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3158a0d9095Smrg
3168a0d9095Smrg# Create the lock directory.
3178a0d9095Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3188a0d9095Smrg# that we are using for the .o file.  Also, base the name on the expected
3198a0d9095Smrg# object file name, since that is what matters with a parallel build.
3208a0d9095Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3218a0d9095Smrgwhile true; do
3228a0d9095Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3238a0d9095Smrg    break
3248a0d9095Smrg  fi
3258a0d9095Smrg  sleep 1
3268a0d9095Smrgdone
3278a0d9095Smrg# FIXME: race condition here if user kills between mkdir and trap.
3288a0d9095Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3298a0d9095Smrg
3308a0d9095Smrg# Run the compile.
3318a0d9095Smrg"$@"
3328a0d9095Smrgret=$?
3338a0d9095Smrg
3348a0d9095Smrgif test -f "$cofile"; then
3358a0d9095Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3368a0d9095Smrgelif test -f "${cofile}bj"; then
3378a0d9095Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3388a0d9095Smrgfi
3398a0d9095Smrg
3408a0d9095Smrgrmdir "$lockdir"
3418a0d9095Smrgexit $ret
3428a0d9095Smrg
3438a0d9095Smrg# Local Variables:
3448a0d9095Smrg# mode: shell-script
3458a0d9095Smrg# sh-indentation: 2
346e45ace2bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
3478a0d9095Smrg# time-stamp-start: "scriptversion="
3488a0d9095Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349e45ace2bSmrg# time-stamp-time-zone: "UTC0"
3508a0d9095Smrg# time-stamp-end: "; # UTC"
3518a0d9095Smrg# End:
352