170728a38Smrg#! /bin/sh
270728a38Smrg# Wrapper for compilers which do not understand '-c -o'.
370728a38Smrg
454cef2ddSmrgscriptversion=2018-03-07.03; # UTC
570728a38Smrg
654cef2ddSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
770728a38Smrg# Written by Tom Tromey <tromey@cygnus.com>.
870728a38Smrg#
970728a38Smrg# This program is free software; you can redistribute it and/or modify
1070728a38Smrg# it under the terms of the GNU General Public License as published by
1170728a38Smrg# the Free Software Foundation; either version 2, or (at your option)
1270728a38Smrg# any later version.
1370728a38Smrg#
1470728a38Smrg# This program is distributed in the hope that it will be useful,
1570728a38Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1670728a38Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1770728a38Smrg# GNU General Public License for more details.
1870728a38Smrg#
1970728a38Smrg# You should have received a copy of the GNU General Public License
2054cef2ddSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2170728a38Smrg
2270728a38Smrg# As a special exception to the GNU General Public License, if you
2370728a38Smrg# distribute this file as part of a program that contains a
2470728a38Smrg# configuration script generated by Autoconf, you may include it under
2570728a38Smrg# the same distribution terms that you use for the rest of that program.
2670728a38Smrg
2770728a38Smrg# This file is maintained in Automake, please report
2870728a38Smrg# bugs to <bug-automake@gnu.org> or send patches to
2970728a38Smrg# <automake-patches@gnu.org>.
3070728a38Smrg
3170728a38Smrgnl='
3270728a38Smrg'
3370728a38Smrg
3470728a38Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3570728a38Smrg# there to prevent tools from complaining about whitespace usage.
3670728a38SmrgIFS=" ""	$nl"
3770728a38Smrg
3870728a38Smrgfile_conv=
3970728a38Smrg
4070728a38Smrg# func_file_conv build_file lazy
4170728a38Smrg# Convert a $build file to $host form and store it in $file
4270728a38Smrg# Currently only supports Windows hosts. If the determined conversion
4370728a38Smrg# type is listed in (the comma separated) LAZY, no conversion will
4470728a38Smrg# take place.
4570728a38Smrgfunc_file_conv ()
4670728a38Smrg{
4770728a38Smrg  file=$1
4870728a38Smrg  case $file in
4970728a38Smrg    / | /[!/]*) # absolute file, and not a UNC file
5070728a38Smrg      if test -z "$file_conv"; then
5170728a38Smrg	# lazily determine how to convert abs files
5270728a38Smrg	case `uname -s` in
5370728a38Smrg	  MINGW*)
5470728a38Smrg	    file_conv=mingw
5570728a38Smrg	    ;;
5654cef2ddSmrg	  CYGWIN* | MSYS*)
5770728a38Smrg	    file_conv=cygwin
5870728a38Smrg	    ;;
5970728a38Smrg	  *)
6070728a38Smrg	    file_conv=wine
6170728a38Smrg	    ;;
6270728a38Smrg	esac
6370728a38Smrg      fi
6470728a38Smrg      case $file_conv/,$2, in
6570728a38Smrg	*,$file_conv,*)
6670728a38Smrg	  ;;
6770728a38Smrg	mingw/*)
6870728a38Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6970728a38Smrg	  ;;
7054cef2ddSmrg	cygwin/* | msys/*)
7170728a38Smrg	  file=`cygpath -m "$file" || echo "$file"`
7270728a38Smrg	  ;;
7370728a38Smrg	wine/*)
7470728a38Smrg	  file=`winepath -w "$file" || echo "$file"`
7570728a38Smrg	  ;;
7670728a38Smrg      esac
7770728a38Smrg      ;;
7870728a38Smrg  esac
7970728a38Smrg}
8070728a38Smrg
8170728a38Smrg# func_cl_dashL linkdir
8270728a38Smrg# Make cl look for libraries in LINKDIR
8370728a38Smrgfunc_cl_dashL ()
8470728a38Smrg{
8570728a38Smrg  func_file_conv "$1"
8670728a38Smrg  if test -z "$lib_path"; then
8770728a38Smrg    lib_path=$file
8870728a38Smrg  else
8970728a38Smrg    lib_path="$lib_path;$file"
9070728a38Smrg  fi
9170728a38Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9270728a38Smrg}
9370728a38Smrg
9470728a38Smrg# func_cl_dashl library
9570728a38Smrg# Do a library search-path lookup for cl
9670728a38Smrgfunc_cl_dashl ()
9770728a38Smrg{
9870728a38Smrg  lib=$1
9970728a38Smrg  found=no
10070728a38Smrg  save_IFS=$IFS
10170728a38Smrg  IFS=';'
10270728a38Smrg  for dir in $lib_path $LIB
10370728a38Smrg  do
10470728a38Smrg    IFS=$save_IFS
10570728a38Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10670728a38Smrg      found=yes
10770728a38Smrg      lib=$dir/$lib.dll.lib
10870728a38Smrg      break
10970728a38Smrg    fi
11070728a38Smrg    if test -f "$dir/$lib.lib"; then
11170728a38Smrg      found=yes
11270728a38Smrg      lib=$dir/$lib.lib
11370728a38Smrg      break
11470728a38Smrg    fi
11570728a38Smrg    if test -f "$dir/lib$lib.a"; then
11670728a38Smrg      found=yes
11770728a38Smrg      lib=$dir/lib$lib.a
11870728a38Smrg      break
11970728a38Smrg    fi
12070728a38Smrg  done
12170728a38Smrg  IFS=$save_IFS
12270728a38Smrg
12370728a38Smrg  if test "$found" != yes; then
12470728a38Smrg    lib=$lib.lib
12570728a38Smrg  fi
12670728a38Smrg}
12770728a38Smrg
12870728a38Smrg# func_cl_wrapper cl arg...
12970728a38Smrg# Adjust compile command to suit cl
13070728a38Smrgfunc_cl_wrapper ()
13170728a38Smrg{
13270728a38Smrg  # Assume a capable shell
13370728a38Smrg  lib_path=
13470728a38Smrg  shared=:
13570728a38Smrg  linker_opts=
13670728a38Smrg  for arg
13770728a38Smrg  do
13870728a38Smrg    if test -n "$eat"; then
13970728a38Smrg      eat=
14070728a38Smrg    else
14170728a38Smrg      case $1 in
14270728a38Smrg	-o)
14370728a38Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14470728a38Smrg	  eat=1
14570728a38Smrg	  case $2 in
14670728a38Smrg	    *.o | *.[oO][bB][jJ])
14770728a38Smrg	      func_file_conv "$2"
14870728a38Smrg	      set x "$@" -Fo"$file"
14970728a38Smrg	      shift
15070728a38Smrg	      ;;
15170728a38Smrg	    *)
15270728a38Smrg	      func_file_conv "$2"
15370728a38Smrg	      set x "$@" -Fe"$file"
15470728a38Smrg	      shift
15570728a38Smrg	      ;;
15670728a38Smrg	  esac
15770728a38Smrg	  ;;
15870728a38Smrg	-I)
15970728a38Smrg	  eat=1
16070728a38Smrg	  func_file_conv "$2" mingw
16170728a38Smrg	  set x "$@" -I"$file"
16270728a38Smrg	  shift
16370728a38Smrg	  ;;
16470728a38Smrg	-I*)
16570728a38Smrg	  func_file_conv "${1#-I}" mingw
16670728a38Smrg	  set x "$@" -I"$file"
16770728a38Smrg	  shift
16870728a38Smrg	  ;;
16970728a38Smrg	-l)
17070728a38Smrg	  eat=1
17170728a38Smrg	  func_cl_dashl "$2"
17270728a38Smrg	  set x "$@" "$lib"
17370728a38Smrg	  shift
17470728a38Smrg	  ;;
17570728a38Smrg	-l*)
17670728a38Smrg	  func_cl_dashl "${1#-l}"
17770728a38Smrg	  set x "$@" "$lib"
17870728a38Smrg	  shift
17970728a38Smrg	  ;;
18070728a38Smrg	-L)
18170728a38Smrg	  eat=1
18270728a38Smrg	  func_cl_dashL "$2"
18370728a38Smrg	  ;;
18470728a38Smrg	-L*)
18570728a38Smrg	  func_cl_dashL "${1#-L}"
18670728a38Smrg	  ;;
18770728a38Smrg	-static)
18870728a38Smrg	  shared=false
18970728a38Smrg	  ;;
19070728a38Smrg	-Wl,*)
19170728a38Smrg	  arg=${1#-Wl,}
19270728a38Smrg	  save_ifs="$IFS"; IFS=','
19370728a38Smrg	  for flag in $arg; do
19470728a38Smrg	    IFS="$save_ifs"
19570728a38Smrg	    linker_opts="$linker_opts $flag"
19670728a38Smrg	  done
19770728a38Smrg	  IFS="$save_ifs"
19870728a38Smrg	  ;;
19970728a38Smrg	-Xlinker)
20070728a38Smrg	  eat=1
20170728a38Smrg	  linker_opts="$linker_opts $2"
20270728a38Smrg	  ;;
20370728a38Smrg	-*)
20470728a38Smrg	  set x "$@" "$1"
20570728a38Smrg	  shift
20670728a38Smrg	  ;;
20770728a38Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20870728a38Smrg	  func_file_conv "$1"
20970728a38Smrg	  set x "$@" -Tp"$file"
21070728a38Smrg	  shift
21170728a38Smrg	  ;;
21270728a38Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21370728a38Smrg	  func_file_conv "$1" mingw
21470728a38Smrg	  set x "$@" "$file"
21570728a38Smrg	  shift
21670728a38Smrg	  ;;
21770728a38Smrg	*)
21870728a38Smrg	  set x "$@" "$1"
21970728a38Smrg	  shift
22070728a38Smrg	  ;;
22170728a38Smrg      esac
22270728a38Smrg    fi
22370728a38Smrg    shift
22470728a38Smrg  done
22570728a38Smrg  if test -n "$linker_opts"; then
22670728a38Smrg    linker_opts="-link$linker_opts"
22770728a38Smrg  fi
22870728a38Smrg  exec "$@" $linker_opts
22970728a38Smrg  exit 1
23070728a38Smrg}
23170728a38Smrg
23270728a38Smrgeat=
23370728a38Smrg
23470728a38Smrgcase $1 in
23570728a38Smrg  '')
23670728a38Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23770728a38Smrg     exit 1;
23870728a38Smrg     ;;
23970728a38Smrg  -h | --h*)
24070728a38Smrg    cat <<\EOF
24170728a38SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24270728a38Smrg
24370728a38SmrgWrapper for compilers which do not understand '-c -o'.
24470728a38SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24570728a38Smrgarguments, and rename the output as expected.
24670728a38Smrg
24770728a38SmrgIf you are trying to build a whole package this is not the
24870728a38Smrgright script to run: please start by reading the file 'INSTALL'.
24970728a38Smrg
25070728a38SmrgReport bugs to <bug-automake@gnu.org>.
25170728a38SmrgEOF
25270728a38Smrg    exit $?
25370728a38Smrg    ;;
25470728a38Smrg  -v | --v*)
25570728a38Smrg    echo "compile $scriptversion"
25670728a38Smrg    exit $?
25770728a38Smrg    ;;
25854cef2ddSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25954cef2ddSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26070728a38Smrg    func_cl_wrapper "$@"      # Doesn't return...
26170728a38Smrg    ;;
26270728a38Smrgesac
26370728a38Smrg
26470728a38Smrgofile=
26570728a38Smrgcfile=
26670728a38Smrg
26770728a38Smrgfor arg
26870728a38Smrgdo
26970728a38Smrg  if test -n "$eat"; then
27070728a38Smrg    eat=
27170728a38Smrg  else
27270728a38Smrg    case $1 in
27370728a38Smrg      -o)
27470728a38Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27570728a38Smrg	# So we strip '-o arg' only if arg is an object.
27670728a38Smrg	eat=1
27770728a38Smrg	case $2 in
27870728a38Smrg	  *.o | *.obj)
27970728a38Smrg	    ofile=$2
28070728a38Smrg	    ;;
28170728a38Smrg	  *)
28270728a38Smrg	    set x "$@" -o "$2"
28370728a38Smrg	    shift
28470728a38Smrg	    ;;
28570728a38Smrg	esac
28670728a38Smrg	;;
28770728a38Smrg      *.c)
28870728a38Smrg	cfile=$1
28970728a38Smrg	set x "$@" "$1"
29070728a38Smrg	shift
29170728a38Smrg	;;
29270728a38Smrg      *)
29370728a38Smrg	set x "$@" "$1"
29470728a38Smrg	shift
29570728a38Smrg	;;
29670728a38Smrg    esac
29770728a38Smrg  fi
29870728a38Smrg  shift
29970728a38Smrgdone
30070728a38Smrg
30170728a38Smrgif test -z "$ofile" || test -z "$cfile"; then
30270728a38Smrg  # If no '-o' option was seen then we might have been invoked from a
30370728a38Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30470728a38Smrg  # normal compilation that the losing compiler can handle.  If no
30570728a38Smrg  # '.c' file was seen then we are probably linking.  That is also
30670728a38Smrg  # ok.
30770728a38Smrg  exec "$@"
30870728a38Smrgfi
30970728a38Smrg
31070728a38Smrg# Name of file we expect compiler to create.
31170728a38Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31270728a38Smrg
31370728a38Smrg# Create the lock directory.
31470728a38Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31570728a38Smrg# that we are using for the .o file.  Also, base the name on the expected
31670728a38Smrg# object file name, since that is what matters with a parallel build.
31770728a38Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31870728a38Smrgwhile true; do
31970728a38Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32070728a38Smrg    break
32170728a38Smrg  fi
32270728a38Smrg  sleep 1
32370728a38Smrgdone
32470728a38Smrg# FIXME: race condition here if user kills between mkdir and trap.
32570728a38Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32670728a38Smrg
32770728a38Smrg# Run the compile.
32870728a38Smrg"$@"
32970728a38Smrgret=$?
33070728a38Smrg
33170728a38Smrgif test -f "$cofile"; then
33270728a38Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33370728a38Smrgelif test -f "${cofile}bj"; then
33470728a38Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33570728a38Smrgfi
33670728a38Smrg
33770728a38Smrgrmdir "$lockdir"
33870728a38Smrgexit $ret
33970728a38Smrg
34070728a38Smrg# Local Variables:
34170728a38Smrg# mode: shell-script
34270728a38Smrg# sh-indentation: 2
34354cef2ddSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34470728a38Smrg# time-stamp-start: "scriptversion="
34570728a38Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34654cef2ddSmrg# time-stamp-time-zone: "UTC0"
34770728a38Smrg# time-stamp-end: "; # UTC"
34870728a38Smrg# End:
349