116e5c272Smrg#! /bin/sh
216e5c272Smrg# Wrapper for compilers which do not understand '-c -o'.
316e5c272Smrg
416e5c272Smrgscriptversion=2018-03-07.03; # UTC
516e5c272Smrg
616e5c272Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
716e5c272Smrg# Written by Tom Tromey <tromey@cygnus.com>.
816e5c272Smrg#
916e5c272Smrg# This program is free software; you can redistribute it and/or modify
1016e5c272Smrg# it under the terms of the GNU General Public License as published by
1116e5c272Smrg# the Free Software Foundation; either version 2, or (at your option)
1216e5c272Smrg# any later version.
1316e5c272Smrg#
1416e5c272Smrg# This program is distributed in the hope that it will be useful,
1516e5c272Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1616e5c272Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1716e5c272Smrg# GNU General Public License for more details.
1816e5c272Smrg#
1916e5c272Smrg# You should have received a copy of the GNU General Public License
2016e5c272Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2116e5c272Smrg
2216e5c272Smrg# As a special exception to the GNU General Public License, if you
2316e5c272Smrg# distribute this file as part of a program that contains a
2416e5c272Smrg# configuration script generated by Autoconf, you may include it under
2516e5c272Smrg# the same distribution terms that you use for the rest of that program.
2616e5c272Smrg
2716e5c272Smrg# This file is maintained in Automake, please report
2816e5c272Smrg# bugs to <bug-automake@gnu.org> or send patches to
2916e5c272Smrg# <automake-patches@gnu.org>.
3016e5c272Smrg
3116e5c272Smrgnl='
3216e5c272Smrg'
3316e5c272Smrg
3416e5c272Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3516e5c272Smrg# there to prevent tools from complaining about whitespace usage.
3616e5c272SmrgIFS=" ""	$nl"
3716e5c272Smrg
3816e5c272Smrgfile_conv=
3916e5c272Smrg
4016e5c272Smrg# func_file_conv build_file lazy
4116e5c272Smrg# Convert a $build file to $host form and store it in $file
4216e5c272Smrg# Currently only supports Windows hosts. If the determined conversion
4316e5c272Smrg# type is listed in (the comma separated) LAZY, no conversion will
4416e5c272Smrg# take place.
4516e5c272Smrgfunc_file_conv ()
4616e5c272Smrg{
4716e5c272Smrg  file=$1
4816e5c272Smrg  case $file in
4916e5c272Smrg    / | /[!/]*) # absolute file, and not a UNC file
5016e5c272Smrg      if test -z "$file_conv"; then
5116e5c272Smrg	# lazily determine how to convert abs files
5216e5c272Smrg	case `uname -s` in
5316e5c272Smrg	  MINGW*)
5416e5c272Smrg	    file_conv=mingw
5516e5c272Smrg	    ;;
5616e5c272Smrg	  CYGWIN* | MSYS*)
5716e5c272Smrg	    file_conv=cygwin
5816e5c272Smrg	    ;;
5916e5c272Smrg	  *)
6016e5c272Smrg	    file_conv=wine
6116e5c272Smrg	    ;;
6216e5c272Smrg	esac
6316e5c272Smrg      fi
6416e5c272Smrg      case $file_conv/,$2, in
6516e5c272Smrg	*,$file_conv,*)
6616e5c272Smrg	  ;;
6716e5c272Smrg	mingw/*)
6816e5c272Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6916e5c272Smrg	  ;;
7016e5c272Smrg	cygwin/* | msys/*)
7116e5c272Smrg	  file=`cygpath -m "$file" || echo "$file"`
7216e5c272Smrg	  ;;
7316e5c272Smrg	wine/*)
7416e5c272Smrg	  file=`winepath -w "$file" || echo "$file"`
7516e5c272Smrg	  ;;
7616e5c272Smrg      esac
7716e5c272Smrg      ;;
7816e5c272Smrg  esac
7916e5c272Smrg}
8016e5c272Smrg
8116e5c272Smrg# func_cl_dashL linkdir
8216e5c272Smrg# Make cl look for libraries in LINKDIR
8316e5c272Smrgfunc_cl_dashL ()
8416e5c272Smrg{
8516e5c272Smrg  func_file_conv "$1"
8616e5c272Smrg  if test -z "$lib_path"; then
8716e5c272Smrg    lib_path=$file
8816e5c272Smrg  else
8916e5c272Smrg    lib_path="$lib_path;$file"
9016e5c272Smrg  fi
9116e5c272Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9216e5c272Smrg}
9316e5c272Smrg
9416e5c272Smrg# func_cl_dashl library
9516e5c272Smrg# Do a library search-path lookup for cl
9616e5c272Smrgfunc_cl_dashl ()
9716e5c272Smrg{
9816e5c272Smrg  lib=$1
9916e5c272Smrg  found=no
10016e5c272Smrg  save_IFS=$IFS
10116e5c272Smrg  IFS=';'
10216e5c272Smrg  for dir in $lib_path $LIB
10316e5c272Smrg  do
10416e5c272Smrg    IFS=$save_IFS
10516e5c272Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10616e5c272Smrg      found=yes
10716e5c272Smrg      lib=$dir/$lib.dll.lib
10816e5c272Smrg      break
10916e5c272Smrg    fi
11016e5c272Smrg    if test -f "$dir/$lib.lib"; then
11116e5c272Smrg      found=yes
11216e5c272Smrg      lib=$dir/$lib.lib
11316e5c272Smrg      break
11416e5c272Smrg    fi
11516e5c272Smrg    if test -f "$dir/lib$lib.a"; then
11616e5c272Smrg      found=yes
11716e5c272Smrg      lib=$dir/lib$lib.a
11816e5c272Smrg      break
11916e5c272Smrg    fi
12016e5c272Smrg  done
12116e5c272Smrg  IFS=$save_IFS
12216e5c272Smrg
12316e5c272Smrg  if test "$found" != yes; then
12416e5c272Smrg    lib=$lib.lib
12516e5c272Smrg  fi
12616e5c272Smrg}
12716e5c272Smrg
12816e5c272Smrg# func_cl_wrapper cl arg...
12916e5c272Smrg# Adjust compile command to suit cl
13016e5c272Smrgfunc_cl_wrapper ()
13116e5c272Smrg{
13216e5c272Smrg  # Assume a capable shell
13316e5c272Smrg  lib_path=
13416e5c272Smrg  shared=:
13516e5c272Smrg  linker_opts=
13616e5c272Smrg  for arg
13716e5c272Smrg  do
13816e5c272Smrg    if test -n "$eat"; then
13916e5c272Smrg      eat=
14016e5c272Smrg    else
14116e5c272Smrg      case $1 in
14216e5c272Smrg	-o)
14316e5c272Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14416e5c272Smrg	  eat=1
14516e5c272Smrg	  case $2 in
14616e5c272Smrg	    *.o | *.[oO][bB][jJ])
14716e5c272Smrg	      func_file_conv "$2"
14816e5c272Smrg	      set x "$@" -Fo"$file"
14916e5c272Smrg	      shift
15016e5c272Smrg	      ;;
15116e5c272Smrg	    *)
15216e5c272Smrg	      func_file_conv "$2"
15316e5c272Smrg	      set x "$@" -Fe"$file"
15416e5c272Smrg	      shift
15516e5c272Smrg	      ;;
15616e5c272Smrg	  esac
15716e5c272Smrg	  ;;
15816e5c272Smrg	-I)
15916e5c272Smrg	  eat=1
16016e5c272Smrg	  func_file_conv "$2" mingw
16116e5c272Smrg	  set x "$@" -I"$file"
16216e5c272Smrg	  shift
16316e5c272Smrg	  ;;
16416e5c272Smrg	-I*)
16516e5c272Smrg	  func_file_conv "${1#-I}" mingw
16616e5c272Smrg	  set x "$@" -I"$file"
16716e5c272Smrg	  shift
16816e5c272Smrg	  ;;
16916e5c272Smrg	-l)
17016e5c272Smrg	  eat=1
17116e5c272Smrg	  func_cl_dashl "$2"
17216e5c272Smrg	  set x "$@" "$lib"
17316e5c272Smrg	  shift
17416e5c272Smrg	  ;;
17516e5c272Smrg	-l*)
17616e5c272Smrg	  func_cl_dashl "${1#-l}"
17716e5c272Smrg	  set x "$@" "$lib"
17816e5c272Smrg	  shift
17916e5c272Smrg	  ;;
18016e5c272Smrg	-L)
18116e5c272Smrg	  eat=1
18216e5c272Smrg	  func_cl_dashL "$2"
18316e5c272Smrg	  ;;
18416e5c272Smrg	-L*)
18516e5c272Smrg	  func_cl_dashL "${1#-L}"
18616e5c272Smrg	  ;;
18716e5c272Smrg	-static)
18816e5c272Smrg	  shared=false
18916e5c272Smrg	  ;;
19016e5c272Smrg	-Wl,*)
19116e5c272Smrg	  arg=${1#-Wl,}
19216e5c272Smrg	  save_ifs="$IFS"; IFS=','
19316e5c272Smrg	  for flag in $arg; do
19416e5c272Smrg	    IFS="$save_ifs"
19516e5c272Smrg	    linker_opts="$linker_opts $flag"
19616e5c272Smrg	  done
19716e5c272Smrg	  IFS="$save_ifs"
19816e5c272Smrg	  ;;
19916e5c272Smrg	-Xlinker)
20016e5c272Smrg	  eat=1
20116e5c272Smrg	  linker_opts="$linker_opts $2"
20216e5c272Smrg	  ;;
20316e5c272Smrg	-*)
20416e5c272Smrg	  set x "$@" "$1"
20516e5c272Smrg	  shift
20616e5c272Smrg	  ;;
20716e5c272Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20816e5c272Smrg	  func_file_conv "$1"
20916e5c272Smrg	  set x "$@" -Tp"$file"
21016e5c272Smrg	  shift
21116e5c272Smrg	  ;;
21216e5c272Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21316e5c272Smrg	  func_file_conv "$1" mingw
21416e5c272Smrg	  set x "$@" "$file"
21516e5c272Smrg	  shift
21616e5c272Smrg	  ;;
21716e5c272Smrg	*)
21816e5c272Smrg	  set x "$@" "$1"
21916e5c272Smrg	  shift
22016e5c272Smrg	  ;;
22116e5c272Smrg      esac
22216e5c272Smrg    fi
22316e5c272Smrg    shift
22416e5c272Smrg  done
22516e5c272Smrg  if test -n "$linker_opts"; then
22616e5c272Smrg    linker_opts="-link$linker_opts"
22716e5c272Smrg  fi
22816e5c272Smrg  exec "$@" $linker_opts
22916e5c272Smrg  exit 1
23016e5c272Smrg}
23116e5c272Smrg
23216e5c272Smrgeat=
23316e5c272Smrg
23416e5c272Smrgcase $1 in
23516e5c272Smrg  '')
23616e5c272Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23716e5c272Smrg     exit 1;
23816e5c272Smrg     ;;
23916e5c272Smrg  -h | --h*)
24016e5c272Smrg    cat <<\EOF
24116e5c272SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24216e5c272Smrg
24316e5c272SmrgWrapper for compilers which do not understand '-c -o'.
24416e5c272SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24516e5c272Smrgarguments, and rename the output as expected.
24616e5c272Smrg
24716e5c272SmrgIf you are trying to build a whole package this is not the
24816e5c272Smrgright script to run: please start by reading the file 'INSTALL'.
24916e5c272Smrg
25016e5c272SmrgReport bugs to <bug-automake@gnu.org>.
25116e5c272SmrgEOF
25216e5c272Smrg    exit $?
25316e5c272Smrg    ;;
25416e5c272Smrg  -v | --v*)
25516e5c272Smrg    echo "compile $scriptversion"
25616e5c272Smrg    exit $?
25716e5c272Smrg    ;;
25816e5c272Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25916e5c272Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26016e5c272Smrg    func_cl_wrapper "$@"      # Doesn't return...
26116e5c272Smrg    ;;
26216e5c272Smrgesac
26316e5c272Smrg
26416e5c272Smrgofile=
26516e5c272Smrgcfile=
26616e5c272Smrg
26716e5c272Smrgfor arg
26816e5c272Smrgdo
26916e5c272Smrg  if test -n "$eat"; then
27016e5c272Smrg    eat=
27116e5c272Smrg  else
27216e5c272Smrg    case $1 in
27316e5c272Smrg      -o)
27416e5c272Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27516e5c272Smrg	# So we strip '-o arg' only if arg is an object.
27616e5c272Smrg	eat=1
27716e5c272Smrg	case $2 in
27816e5c272Smrg	  *.o | *.obj)
27916e5c272Smrg	    ofile=$2
28016e5c272Smrg	    ;;
28116e5c272Smrg	  *)
28216e5c272Smrg	    set x "$@" -o "$2"
28316e5c272Smrg	    shift
28416e5c272Smrg	    ;;
28516e5c272Smrg	esac
28616e5c272Smrg	;;
28716e5c272Smrg      *.c)
28816e5c272Smrg	cfile=$1
28916e5c272Smrg	set x "$@" "$1"
29016e5c272Smrg	shift
29116e5c272Smrg	;;
29216e5c272Smrg      *)
29316e5c272Smrg	set x "$@" "$1"
29416e5c272Smrg	shift
29516e5c272Smrg	;;
29616e5c272Smrg    esac
29716e5c272Smrg  fi
29816e5c272Smrg  shift
29916e5c272Smrgdone
30016e5c272Smrg
30116e5c272Smrgif test -z "$ofile" || test -z "$cfile"; then
30216e5c272Smrg  # If no '-o' option was seen then we might have been invoked from a
30316e5c272Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30416e5c272Smrg  # normal compilation that the losing compiler can handle.  If no
30516e5c272Smrg  # '.c' file was seen then we are probably linking.  That is also
30616e5c272Smrg  # ok.
30716e5c272Smrg  exec "$@"
30816e5c272Smrgfi
30916e5c272Smrg
31016e5c272Smrg# Name of file we expect compiler to create.
31116e5c272Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31216e5c272Smrg
31316e5c272Smrg# Create the lock directory.
31416e5c272Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31516e5c272Smrg# that we are using for the .o file.  Also, base the name on the expected
31616e5c272Smrg# object file name, since that is what matters with a parallel build.
31716e5c272Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31816e5c272Smrgwhile true; do
31916e5c272Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32016e5c272Smrg    break
32116e5c272Smrg  fi
32216e5c272Smrg  sleep 1
32316e5c272Smrgdone
32416e5c272Smrg# FIXME: race condition here if user kills between mkdir and trap.
32516e5c272Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32616e5c272Smrg
32716e5c272Smrg# Run the compile.
32816e5c272Smrg"$@"
32916e5c272Smrgret=$?
33016e5c272Smrg
33116e5c272Smrgif test -f "$cofile"; then
33216e5c272Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33316e5c272Smrgelif test -f "${cofile}bj"; then
33416e5c272Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33516e5c272Smrgfi
33616e5c272Smrg
33716e5c272Smrgrmdir "$lockdir"
33816e5c272Smrgexit $ret
33916e5c272Smrg
34016e5c272Smrg# Local Variables:
34116e5c272Smrg# mode: shell-script
34216e5c272Smrg# sh-indentation: 2
34316e5c272Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
34416e5c272Smrg# time-stamp-start: "scriptversion="
34516e5c272Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34616e5c272Smrg# time-stamp-time-zone: "UTC0"
34716e5c272Smrg# time-stamp-end: "; # UTC"
34816e5c272Smrg# End:
349