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