136ffeb23Smrg#! /bin/sh
236ffeb23Smrg# Wrapper for compilers which do not understand '-c -o'.
336ffeb23Smrg
48ae5c7d9Smrgscriptversion=2018-03-07.03; # UTC
536ffeb23Smrg
68ae5c7d9Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
736ffeb23Smrg# Written by Tom Tromey <tromey@cygnus.com>.
836ffeb23Smrg#
936ffeb23Smrg# This program is free software; you can redistribute it and/or modify
1036ffeb23Smrg# it under the terms of the GNU General Public License as published by
1136ffeb23Smrg# the Free Software Foundation; either version 2, or (at your option)
1236ffeb23Smrg# any later version.
1336ffeb23Smrg#
1436ffeb23Smrg# This program is distributed in the hope that it will be useful,
1536ffeb23Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1636ffeb23Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1736ffeb23Smrg# GNU General Public License for more details.
1836ffeb23Smrg#
1936ffeb23Smrg# You should have received a copy of the GNU General Public License
208ae5c7d9Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2136ffeb23Smrg
2236ffeb23Smrg# As a special exception to the GNU General Public License, if you
2336ffeb23Smrg# distribute this file as part of a program that contains a
2436ffeb23Smrg# configuration script generated by Autoconf, you may include it under
2536ffeb23Smrg# the same distribution terms that you use for the rest of that program.
2636ffeb23Smrg
2736ffeb23Smrg# This file is maintained in Automake, please report
2836ffeb23Smrg# bugs to <bug-automake@gnu.org> or send patches to
2936ffeb23Smrg# <automake-patches@gnu.org>.
3036ffeb23Smrg
3136ffeb23Smrgnl='
3236ffeb23Smrg'
3336ffeb23Smrg
3436ffeb23Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3536ffeb23Smrg# there to prevent tools from complaining about whitespace usage.
3636ffeb23SmrgIFS=" ""	$nl"
3736ffeb23Smrg
3836ffeb23Smrgfile_conv=
3936ffeb23Smrg
4036ffeb23Smrg# func_file_conv build_file lazy
4136ffeb23Smrg# Convert a $build file to $host form and store it in $file
4236ffeb23Smrg# Currently only supports Windows hosts. If the determined conversion
4336ffeb23Smrg# type is listed in (the comma separated) LAZY, no conversion will
4436ffeb23Smrg# take place.
4536ffeb23Smrgfunc_file_conv ()
4636ffeb23Smrg{
4736ffeb23Smrg  file=$1
4836ffeb23Smrg  case $file in
4936ffeb23Smrg    / | /[!/]*) # absolute file, and not a UNC file
5036ffeb23Smrg      if test -z "$file_conv"; then
5136ffeb23Smrg	# lazily determine how to convert abs files
5236ffeb23Smrg	case `uname -s` in
5336ffeb23Smrg	  MINGW*)
5436ffeb23Smrg	    file_conv=mingw
5536ffeb23Smrg	    ;;
568ae5c7d9Smrg	  CYGWIN* | MSYS*)
5736ffeb23Smrg	    file_conv=cygwin
5836ffeb23Smrg	    ;;
5936ffeb23Smrg	  *)
6036ffeb23Smrg	    file_conv=wine
6136ffeb23Smrg	    ;;
6236ffeb23Smrg	esac
6336ffeb23Smrg      fi
6436ffeb23Smrg      case $file_conv/,$2, in
6536ffeb23Smrg	*,$file_conv,*)
6636ffeb23Smrg	  ;;
6736ffeb23Smrg	mingw/*)
6836ffeb23Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6936ffeb23Smrg	  ;;
708ae5c7d9Smrg	cygwin/* | msys/*)
7136ffeb23Smrg	  file=`cygpath -m "$file" || echo "$file"`
7236ffeb23Smrg	  ;;
7336ffeb23Smrg	wine/*)
7436ffeb23Smrg	  file=`winepath -w "$file" || echo "$file"`
7536ffeb23Smrg	  ;;
7636ffeb23Smrg      esac
7736ffeb23Smrg      ;;
7836ffeb23Smrg  esac
7936ffeb23Smrg}
8036ffeb23Smrg
8136ffeb23Smrg# func_cl_dashL linkdir
8236ffeb23Smrg# Make cl look for libraries in LINKDIR
8336ffeb23Smrgfunc_cl_dashL ()
8436ffeb23Smrg{
8536ffeb23Smrg  func_file_conv "$1"
8636ffeb23Smrg  if test -z "$lib_path"; then
8736ffeb23Smrg    lib_path=$file
8836ffeb23Smrg  else
8936ffeb23Smrg    lib_path="$lib_path;$file"
9036ffeb23Smrg  fi
9136ffeb23Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9236ffeb23Smrg}
9336ffeb23Smrg
9436ffeb23Smrg# func_cl_dashl library
9536ffeb23Smrg# Do a library search-path lookup for cl
9636ffeb23Smrgfunc_cl_dashl ()
9736ffeb23Smrg{
9836ffeb23Smrg  lib=$1
9936ffeb23Smrg  found=no
10036ffeb23Smrg  save_IFS=$IFS
10136ffeb23Smrg  IFS=';'
10236ffeb23Smrg  for dir in $lib_path $LIB
10336ffeb23Smrg  do
10436ffeb23Smrg    IFS=$save_IFS
10536ffeb23Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10636ffeb23Smrg      found=yes
10736ffeb23Smrg      lib=$dir/$lib.dll.lib
10836ffeb23Smrg      break
10936ffeb23Smrg    fi
11036ffeb23Smrg    if test -f "$dir/$lib.lib"; then
11136ffeb23Smrg      found=yes
11236ffeb23Smrg      lib=$dir/$lib.lib
11336ffeb23Smrg      break
11436ffeb23Smrg    fi
11536ffeb23Smrg    if test -f "$dir/lib$lib.a"; then
11636ffeb23Smrg      found=yes
11736ffeb23Smrg      lib=$dir/lib$lib.a
11836ffeb23Smrg      break
11936ffeb23Smrg    fi
12036ffeb23Smrg  done
12136ffeb23Smrg  IFS=$save_IFS
12236ffeb23Smrg
12336ffeb23Smrg  if test "$found" != yes; then
12436ffeb23Smrg    lib=$lib.lib
12536ffeb23Smrg  fi
12636ffeb23Smrg}
12736ffeb23Smrg
12836ffeb23Smrg# func_cl_wrapper cl arg...
12936ffeb23Smrg# Adjust compile command to suit cl
13036ffeb23Smrgfunc_cl_wrapper ()
13136ffeb23Smrg{
13236ffeb23Smrg  # Assume a capable shell
13336ffeb23Smrg  lib_path=
13436ffeb23Smrg  shared=:
13536ffeb23Smrg  linker_opts=
13636ffeb23Smrg  for arg
13736ffeb23Smrg  do
13836ffeb23Smrg    if test -n "$eat"; then
13936ffeb23Smrg      eat=
14036ffeb23Smrg    else
14136ffeb23Smrg      case $1 in
14236ffeb23Smrg	-o)
14336ffeb23Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14436ffeb23Smrg	  eat=1
14536ffeb23Smrg	  case $2 in
14636ffeb23Smrg	    *.o | *.[oO][bB][jJ])
14736ffeb23Smrg	      func_file_conv "$2"
14836ffeb23Smrg	      set x "$@" -Fo"$file"
14936ffeb23Smrg	      shift
15036ffeb23Smrg	      ;;
15136ffeb23Smrg	    *)
15236ffeb23Smrg	      func_file_conv "$2"
15336ffeb23Smrg	      set x "$@" -Fe"$file"
15436ffeb23Smrg	      shift
15536ffeb23Smrg	      ;;
15636ffeb23Smrg	  esac
15736ffeb23Smrg	  ;;
15836ffeb23Smrg	-I)
15936ffeb23Smrg	  eat=1
16036ffeb23Smrg	  func_file_conv "$2" mingw
16136ffeb23Smrg	  set x "$@" -I"$file"
16236ffeb23Smrg	  shift
16336ffeb23Smrg	  ;;
16436ffeb23Smrg	-I*)
16536ffeb23Smrg	  func_file_conv "${1#-I}" mingw
16636ffeb23Smrg	  set x "$@" -I"$file"
16736ffeb23Smrg	  shift
16836ffeb23Smrg	  ;;
16936ffeb23Smrg	-l)
17036ffeb23Smrg	  eat=1
17136ffeb23Smrg	  func_cl_dashl "$2"
17236ffeb23Smrg	  set x "$@" "$lib"
17336ffeb23Smrg	  shift
17436ffeb23Smrg	  ;;
17536ffeb23Smrg	-l*)
17636ffeb23Smrg	  func_cl_dashl "${1#-l}"
17736ffeb23Smrg	  set x "$@" "$lib"
17836ffeb23Smrg	  shift
17936ffeb23Smrg	  ;;
18036ffeb23Smrg	-L)
18136ffeb23Smrg	  eat=1
18236ffeb23Smrg	  func_cl_dashL "$2"
18336ffeb23Smrg	  ;;
18436ffeb23Smrg	-L*)
18536ffeb23Smrg	  func_cl_dashL "${1#-L}"
18636ffeb23Smrg	  ;;
18736ffeb23Smrg	-static)
18836ffeb23Smrg	  shared=false
18936ffeb23Smrg	  ;;
19036ffeb23Smrg	-Wl,*)
19136ffeb23Smrg	  arg=${1#-Wl,}
19236ffeb23Smrg	  save_ifs="$IFS"; IFS=','
19336ffeb23Smrg	  for flag in $arg; do
19436ffeb23Smrg	    IFS="$save_ifs"
19536ffeb23Smrg	    linker_opts="$linker_opts $flag"
19636ffeb23Smrg	  done
19736ffeb23Smrg	  IFS="$save_ifs"
19836ffeb23Smrg	  ;;
19936ffeb23Smrg	-Xlinker)
20036ffeb23Smrg	  eat=1
20136ffeb23Smrg	  linker_opts="$linker_opts $2"
20236ffeb23Smrg	  ;;
20336ffeb23Smrg	-*)
20436ffeb23Smrg	  set x "$@" "$1"
20536ffeb23Smrg	  shift
20636ffeb23Smrg	  ;;
20736ffeb23Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20836ffeb23Smrg	  func_file_conv "$1"
20936ffeb23Smrg	  set x "$@" -Tp"$file"
21036ffeb23Smrg	  shift
21136ffeb23Smrg	  ;;
21236ffeb23Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21336ffeb23Smrg	  func_file_conv "$1" mingw
21436ffeb23Smrg	  set x "$@" "$file"
21536ffeb23Smrg	  shift
21636ffeb23Smrg	  ;;
21736ffeb23Smrg	*)
21836ffeb23Smrg	  set x "$@" "$1"
21936ffeb23Smrg	  shift
22036ffeb23Smrg	  ;;
22136ffeb23Smrg      esac
22236ffeb23Smrg    fi
22336ffeb23Smrg    shift
22436ffeb23Smrg  done
22536ffeb23Smrg  if test -n "$linker_opts"; then
22636ffeb23Smrg    linker_opts="-link$linker_opts"
22736ffeb23Smrg  fi
22836ffeb23Smrg  exec "$@" $linker_opts
22936ffeb23Smrg  exit 1
23036ffeb23Smrg}
23136ffeb23Smrg
23236ffeb23Smrgeat=
23336ffeb23Smrg
23436ffeb23Smrgcase $1 in
23536ffeb23Smrg  '')
23636ffeb23Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23736ffeb23Smrg     exit 1;
23836ffeb23Smrg     ;;
23936ffeb23Smrg  -h | --h*)
24036ffeb23Smrg    cat <<\EOF
24136ffeb23SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24236ffeb23Smrg
24336ffeb23SmrgWrapper for compilers which do not understand '-c -o'.
24436ffeb23SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24536ffeb23Smrgarguments, and rename the output as expected.
24636ffeb23Smrg
24736ffeb23SmrgIf you are trying to build a whole package this is not the
24836ffeb23Smrgright script to run: please start by reading the file 'INSTALL'.
24936ffeb23Smrg
25036ffeb23SmrgReport bugs to <bug-automake@gnu.org>.
25136ffeb23SmrgEOF
25236ffeb23Smrg    exit $?
25336ffeb23Smrg    ;;
25436ffeb23Smrg  -v | --v*)
25536ffeb23Smrg    echo "compile $scriptversion"
25636ffeb23Smrg    exit $?
25736ffeb23Smrg    ;;
2588ae5c7d9Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
2598ae5c7d9Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26036ffeb23Smrg    func_cl_wrapper "$@"      # Doesn't return...
26136ffeb23Smrg    ;;
26236ffeb23Smrgesac
26336ffeb23Smrg
26436ffeb23Smrgofile=
26536ffeb23Smrgcfile=
26636ffeb23Smrg
26736ffeb23Smrgfor arg
26836ffeb23Smrgdo
26936ffeb23Smrg  if test -n "$eat"; then
27036ffeb23Smrg    eat=
27136ffeb23Smrg  else
27236ffeb23Smrg    case $1 in
27336ffeb23Smrg      -o)
27436ffeb23Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27536ffeb23Smrg	# So we strip '-o arg' only if arg is an object.
27636ffeb23Smrg	eat=1
27736ffeb23Smrg	case $2 in
27836ffeb23Smrg	  *.o | *.obj)
27936ffeb23Smrg	    ofile=$2
28036ffeb23Smrg	    ;;
28136ffeb23Smrg	  *)
28236ffeb23Smrg	    set x "$@" -o "$2"
28336ffeb23Smrg	    shift
28436ffeb23Smrg	    ;;
28536ffeb23Smrg	esac
28636ffeb23Smrg	;;
28736ffeb23Smrg      *.c)
28836ffeb23Smrg	cfile=$1
28936ffeb23Smrg	set x "$@" "$1"
29036ffeb23Smrg	shift
29136ffeb23Smrg	;;
29236ffeb23Smrg      *)
29336ffeb23Smrg	set x "$@" "$1"
29436ffeb23Smrg	shift
29536ffeb23Smrg	;;
29636ffeb23Smrg    esac
29736ffeb23Smrg  fi
29836ffeb23Smrg  shift
29936ffeb23Smrgdone
30036ffeb23Smrg
30136ffeb23Smrgif test -z "$ofile" || test -z "$cfile"; then
30236ffeb23Smrg  # If no '-o' option was seen then we might have been invoked from a
30336ffeb23Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30436ffeb23Smrg  # normal compilation that the losing compiler can handle.  If no
30536ffeb23Smrg  # '.c' file was seen then we are probably linking.  That is also
30636ffeb23Smrg  # ok.
30736ffeb23Smrg  exec "$@"
30836ffeb23Smrgfi
30936ffeb23Smrg
31036ffeb23Smrg# Name of file we expect compiler to create.
31136ffeb23Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31236ffeb23Smrg
31336ffeb23Smrg# Create the lock directory.
31436ffeb23Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31536ffeb23Smrg# that we are using for the .o file.  Also, base the name on the expected
31636ffeb23Smrg# object file name, since that is what matters with a parallel build.
31736ffeb23Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31836ffeb23Smrgwhile true; do
31936ffeb23Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32036ffeb23Smrg    break
32136ffeb23Smrg  fi
32236ffeb23Smrg  sleep 1
32336ffeb23Smrgdone
32436ffeb23Smrg# FIXME: race condition here if user kills between mkdir and trap.
32536ffeb23Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32636ffeb23Smrg
32736ffeb23Smrg# Run the compile.
32836ffeb23Smrg"$@"
32936ffeb23Smrgret=$?
33036ffeb23Smrg
33136ffeb23Smrgif test -f "$cofile"; then
33236ffeb23Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33336ffeb23Smrgelif test -f "${cofile}bj"; then
33436ffeb23Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33536ffeb23Smrgfi
33636ffeb23Smrg
33736ffeb23Smrgrmdir "$lockdir"
33836ffeb23Smrgexit $ret
33936ffeb23Smrg
34036ffeb23Smrg# Local Variables:
34136ffeb23Smrg# mode: shell-script
34236ffeb23Smrg# sh-indentation: 2
3438ae5c7d9Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
34436ffeb23Smrg# time-stamp-start: "scriptversion="
34536ffeb23Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3468ae5c7d9Smrg# time-stamp-time-zone: "UTC0"
34736ffeb23Smrg# time-stamp-end: "; # UTC"
34836ffeb23Smrg# End:
349