115ffece8Smrg#! /bin/sh
215ffece8Smrg# Wrapper for compilers which do not understand '-c -o'.
315ffece8Smrg
415ffece8Smrgscriptversion=2018-03-07.03; # UTC
515ffece8Smrg
615ffece8Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
715ffece8Smrg# Written by Tom Tromey <tromey@cygnus.com>.
815ffece8Smrg#
915ffece8Smrg# This program is free software; you can redistribute it and/or modify
1015ffece8Smrg# it under the terms of the GNU General Public License as published by
1115ffece8Smrg# the Free Software Foundation; either version 2, or (at your option)
1215ffece8Smrg# any later version.
1315ffece8Smrg#
1415ffece8Smrg# This program is distributed in the hope that it will be useful,
1515ffece8Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1615ffece8Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1715ffece8Smrg# GNU General Public License for more details.
1815ffece8Smrg#
1915ffece8Smrg# You should have received a copy of the GNU General Public License
2015ffece8Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2115ffece8Smrg
2215ffece8Smrg# As a special exception to the GNU General Public License, if you
2315ffece8Smrg# distribute this file as part of a program that contains a
2415ffece8Smrg# configuration script generated by Autoconf, you may include it under
2515ffece8Smrg# the same distribution terms that you use for the rest of that program.
2615ffece8Smrg
2715ffece8Smrg# This file is maintained in Automake, please report
2815ffece8Smrg# bugs to <bug-automake@gnu.org> or send patches to
2915ffece8Smrg# <automake-patches@gnu.org>.
3015ffece8Smrg
3115ffece8Smrgnl='
3215ffece8Smrg'
3315ffece8Smrg
3415ffece8Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3515ffece8Smrg# there to prevent tools from complaining about whitespace usage.
3615ffece8SmrgIFS=" ""	$nl"
3715ffece8Smrg
3815ffece8Smrgfile_conv=
3915ffece8Smrg
4015ffece8Smrg# func_file_conv build_file lazy
4115ffece8Smrg# Convert a $build file to $host form and store it in $file
4215ffece8Smrg# Currently only supports Windows hosts. If the determined conversion
4315ffece8Smrg# type is listed in (the comma separated) LAZY, no conversion will
4415ffece8Smrg# take place.
4515ffece8Smrgfunc_file_conv ()
4615ffece8Smrg{
4715ffece8Smrg  file=$1
4815ffece8Smrg  case $file in
4915ffece8Smrg    / | /[!/]*) # absolute file, and not a UNC file
5015ffece8Smrg      if test -z "$file_conv"; then
5115ffece8Smrg	# lazily determine how to convert abs files
5215ffece8Smrg	case `uname -s` in
5315ffece8Smrg	  MINGW*)
5415ffece8Smrg	    file_conv=mingw
5515ffece8Smrg	    ;;
5615ffece8Smrg	  CYGWIN* | MSYS*)
5715ffece8Smrg	    file_conv=cygwin
5815ffece8Smrg	    ;;
5915ffece8Smrg	  *)
6015ffece8Smrg	    file_conv=wine
6115ffece8Smrg	    ;;
6215ffece8Smrg	esac
6315ffece8Smrg      fi
6415ffece8Smrg      case $file_conv/,$2, in
6515ffece8Smrg	*,$file_conv,*)
6615ffece8Smrg	  ;;
6715ffece8Smrg	mingw/*)
6815ffece8Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6915ffece8Smrg	  ;;
7015ffece8Smrg	cygwin/* | msys/*)
7115ffece8Smrg	  file=`cygpath -m "$file" || echo "$file"`
7215ffece8Smrg	  ;;
7315ffece8Smrg	wine/*)
7415ffece8Smrg	  file=`winepath -w "$file" || echo "$file"`
7515ffece8Smrg	  ;;
7615ffece8Smrg      esac
7715ffece8Smrg      ;;
7815ffece8Smrg  esac
7915ffece8Smrg}
8015ffece8Smrg
8115ffece8Smrg# func_cl_dashL linkdir
8215ffece8Smrg# Make cl look for libraries in LINKDIR
8315ffece8Smrgfunc_cl_dashL ()
8415ffece8Smrg{
8515ffece8Smrg  func_file_conv "$1"
8615ffece8Smrg  if test -z "$lib_path"; then
8715ffece8Smrg    lib_path=$file
8815ffece8Smrg  else
8915ffece8Smrg    lib_path="$lib_path;$file"
9015ffece8Smrg  fi
9115ffece8Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9215ffece8Smrg}
9315ffece8Smrg
9415ffece8Smrg# func_cl_dashl library
9515ffece8Smrg# Do a library search-path lookup for cl
9615ffece8Smrgfunc_cl_dashl ()
9715ffece8Smrg{
9815ffece8Smrg  lib=$1
9915ffece8Smrg  found=no
10015ffece8Smrg  save_IFS=$IFS
10115ffece8Smrg  IFS=';'
10215ffece8Smrg  for dir in $lib_path $LIB
10315ffece8Smrg  do
10415ffece8Smrg    IFS=$save_IFS
10515ffece8Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10615ffece8Smrg      found=yes
10715ffece8Smrg      lib=$dir/$lib.dll.lib
10815ffece8Smrg      break
10915ffece8Smrg    fi
11015ffece8Smrg    if test -f "$dir/$lib.lib"; then
11115ffece8Smrg      found=yes
11215ffece8Smrg      lib=$dir/$lib.lib
11315ffece8Smrg      break
11415ffece8Smrg    fi
11515ffece8Smrg    if test -f "$dir/lib$lib.a"; then
11615ffece8Smrg      found=yes
11715ffece8Smrg      lib=$dir/lib$lib.a
11815ffece8Smrg      break
11915ffece8Smrg    fi
12015ffece8Smrg  done
12115ffece8Smrg  IFS=$save_IFS
12215ffece8Smrg
12315ffece8Smrg  if test "$found" != yes; then
12415ffece8Smrg    lib=$lib.lib
12515ffece8Smrg  fi
12615ffece8Smrg}
12715ffece8Smrg
12815ffece8Smrg# func_cl_wrapper cl arg...
12915ffece8Smrg# Adjust compile command to suit cl
13015ffece8Smrgfunc_cl_wrapper ()
13115ffece8Smrg{
13215ffece8Smrg  # Assume a capable shell
13315ffece8Smrg  lib_path=
13415ffece8Smrg  shared=:
13515ffece8Smrg  linker_opts=
13615ffece8Smrg  for arg
13715ffece8Smrg  do
13815ffece8Smrg    if test -n "$eat"; then
13915ffece8Smrg      eat=
14015ffece8Smrg    else
14115ffece8Smrg      case $1 in
14215ffece8Smrg	-o)
14315ffece8Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14415ffece8Smrg	  eat=1
14515ffece8Smrg	  case $2 in
14615ffece8Smrg	    *.o | *.[oO][bB][jJ])
14715ffece8Smrg	      func_file_conv "$2"
14815ffece8Smrg	      set x "$@" -Fo"$file"
14915ffece8Smrg	      shift
15015ffece8Smrg	      ;;
15115ffece8Smrg	    *)
15215ffece8Smrg	      func_file_conv "$2"
15315ffece8Smrg	      set x "$@" -Fe"$file"
15415ffece8Smrg	      shift
15515ffece8Smrg	      ;;
15615ffece8Smrg	  esac
15715ffece8Smrg	  ;;
15815ffece8Smrg	-I)
15915ffece8Smrg	  eat=1
16015ffece8Smrg	  func_file_conv "$2" mingw
16115ffece8Smrg	  set x "$@" -I"$file"
16215ffece8Smrg	  shift
16315ffece8Smrg	  ;;
16415ffece8Smrg	-I*)
16515ffece8Smrg	  func_file_conv "${1#-I}" mingw
16615ffece8Smrg	  set x "$@" -I"$file"
16715ffece8Smrg	  shift
16815ffece8Smrg	  ;;
16915ffece8Smrg	-l)
17015ffece8Smrg	  eat=1
17115ffece8Smrg	  func_cl_dashl "$2"
17215ffece8Smrg	  set x "$@" "$lib"
17315ffece8Smrg	  shift
17415ffece8Smrg	  ;;
17515ffece8Smrg	-l*)
17615ffece8Smrg	  func_cl_dashl "${1#-l}"
17715ffece8Smrg	  set x "$@" "$lib"
17815ffece8Smrg	  shift
17915ffece8Smrg	  ;;
18015ffece8Smrg	-L)
18115ffece8Smrg	  eat=1
18215ffece8Smrg	  func_cl_dashL "$2"
18315ffece8Smrg	  ;;
18415ffece8Smrg	-L*)
18515ffece8Smrg	  func_cl_dashL "${1#-L}"
18615ffece8Smrg	  ;;
18715ffece8Smrg	-static)
18815ffece8Smrg	  shared=false
18915ffece8Smrg	  ;;
19015ffece8Smrg	-Wl,*)
19115ffece8Smrg	  arg=${1#-Wl,}
19215ffece8Smrg	  save_ifs="$IFS"; IFS=','
19315ffece8Smrg	  for flag in $arg; do
19415ffece8Smrg	    IFS="$save_ifs"
19515ffece8Smrg	    linker_opts="$linker_opts $flag"
19615ffece8Smrg	  done
19715ffece8Smrg	  IFS="$save_ifs"
19815ffece8Smrg	  ;;
19915ffece8Smrg	-Xlinker)
20015ffece8Smrg	  eat=1
20115ffece8Smrg	  linker_opts="$linker_opts $2"
20215ffece8Smrg	  ;;
20315ffece8Smrg	-*)
20415ffece8Smrg	  set x "$@" "$1"
20515ffece8Smrg	  shift
20615ffece8Smrg	  ;;
20715ffece8Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20815ffece8Smrg	  func_file_conv "$1"
20915ffece8Smrg	  set x "$@" -Tp"$file"
21015ffece8Smrg	  shift
21115ffece8Smrg	  ;;
21215ffece8Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21315ffece8Smrg	  func_file_conv "$1" mingw
21415ffece8Smrg	  set x "$@" "$file"
21515ffece8Smrg	  shift
21615ffece8Smrg	  ;;
21715ffece8Smrg	*)
21815ffece8Smrg	  set x "$@" "$1"
21915ffece8Smrg	  shift
22015ffece8Smrg	  ;;
22115ffece8Smrg      esac
22215ffece8Smrg    fi
22315ffece8Smrg    shift
22415ffece8Smrg  done
22515ffece8Smrg  if test -n "$linker_opts"; then
22615ffece8Smrg    linker_opts="-link$linker_opts"
22715ffece8Smrg  fi
22815ffece8Smrg  exec "$@" $linker_opts
22915ffece8Smrg  exit 1
23015ffece8Smrg}
23115ffece8Smrg
23215ffece8Smrgeat=
23315ffece8Smrg
23415ffece8Smrgcase $1 in
23515ffece8Smrg  '')
23615ffece8Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23715ffece8Smrg     exit 1;
23815ffece8Smrg     ;;
23915ffece8Smrg  -h | --h*)
24015ffece8Smrg    cat <<\EOF
24115ffece8SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24215ffece8Smrg
24315ffece8SmrgWrapper for compilers which do not understand '-c -o'.
24415ffece8SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24515ffece8Smrgarguments, and rename the output as expected.
24615ffece8Smrg
24715ffece8SmrgIf you are trying to build a whole package this is not the
24815ffece8Smrgright script to run: please start by reading the file 'INSTALL'.
24915ffece8Smrg
25015ffece8SmrgReport bugs to <bug-automake@gnu.org>.
25115ffece8SmrgEOF
25215ffece8Smrg    exit $?
25315ffece8Smrg    ;;
25415ffece8Smrg  -v | --v*)
25515ffece8Smrg    echo "compile $scriptversion"
25615ffece8Smrg    exit $?
25715ffece8Smrg    ;;
25815ffece8Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25915ffece8Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26015ffece8Smrg    func_cl_wrapper "$@"      # Doesn't return...
26115ffece8Smrg    ;;
26215ffece8Smrgesac
26315ffece8Smrg
26415ffece8Smrgofile=
26515ffece8Smrgcfile=
26615ffece8Smrg
26715ffece8Smrgfor arg
26815ffece8Smrgdo
26915ffece8Smrg  if test -n "$eat"; then
27015ffece8Smrg    eat=
27115ffece8Smrg  else
27215ffece8Smrg    case $1 in
27315ffece8Smrg      -o)
27415ffece8Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27515ffece8Smrg	# So we strip '-o arg' only if arg is an object.
27615ffece8Smrg	eat=1
27715ffece8Smrg	case $2 in
27815ffece8Smrg	  *.o | *.obj)
27915ffece8Smrg	    ofile=$2
28015ffece8Smrg	    ;;
28115ffece8Smrg	  *)
28215ffece8Smrg	    set x "$@" -o "$2"
28315ffece8Smrg	    shift
28415ffece8Smrg	    ;;
28515ffece8Smrg	esac
28615ffece8Smrg	;;
28715ffece8Smrg      *.c)
28815ffece8Smrg	cfile=$1
28915ffece8Smrg	set x "$@" "$1"
29015ffece8Smrg	shift
29115ffece8Smrg	;;
29215ffece8Smrg      *)
29315ffece8Smrg	set x "$@" "$1"
29415ffece8Smrg	shift
29515ffece8Smrg	;;
29615ffece8Smrg    esac
29715ffece8Smrg  fi
29815ffece8Smrg  shift
29915ffece8Smrgdone
30015ffece8Smrg
30115ffece8Smrgif test -z "$ofile" || test -z "$cfile"; then
30215ffece8Smrg  # If no '-o' option was seen then we might have been invoked from a
30315ffece8Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30415ffece8Smrg  # normal compilation that the losing compiler can handle.  If no
30515ffece8Smrg  # '.c' file was seen then we are probably linking.  That is also
30615ffece8Smrg  # ok.
30715ffece8Smrg  exec "$@"
30815ffece8Smrgfi
30915ffece8Smrg
31015ffece8Smrg# Name of file we expect compiler to create.
31115ffece8Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31215ffece8Smrg
31315ffece8Smrg# Create the lock directory.
31415ffece8Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31515ffece8Smrg# that we are using for the .o file.  Also, base the name on the expected
31615ffece8Smrg# object file name, since that is what matters with a parallel build.
31715ffece8Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31815ffece8Smrgwhile true; do
31915ffece8Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32015ffece8Smrg    break
32115ffece8Smrg  fi
32215ffece8Smrg  sleep 1
32315ffece8Smrgdone
32415ffece8Smrg# FIXME: race condition here if user kills between mkdir and trap.
32515ffece8Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32615ffece8Smrg
32715ffece8Smrg# Run the compile.
32815ffece8Smrg"$@"
32915ffece8Smrgret=$?
33015ffece8Smrg
33115ffece8Smrgif test -f "$cofile"; then
33215ffece8Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33315ffece8Smrgelif test -f "${cofile}bj"; then
33415ffece8Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33515ffece8Smrgfi
33615ffece8Smrg
33715ffece8Smrgrmdir "$lockdir"
33815ffece8Smrgexit $ret
33915ffece8Smrg
34015ffece8Smrg# Local Variables:
34115ffece8Smrg# mode: shell-script
34215ffece8Smrg# sh-indentation: 2
34315ffece8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
34415ffece8Smrg# time-stamp-start: "scriptversion="
34515ffece8Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34615ffece8Smrg# time-stamp-time-zone: "UTC0"
34715ffece8Smrg# time-stamp-end: "; # UTC"
34815ffece8Smrg# End:
349