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