166ab3337Smrg#! /bin/sh
266ab3337Smrg# Wrapper for compilers which do not understand '-c -o'.
366ab3337Smrg
466ab3337Smrgscriptversion=2018-03-07.03; # UTC
566ab3337Smrg
666ab3337Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
766ab3337Smrg# Written by Tom Tromey <tromey@cygnus.com>.
866ab3337Smrg#
966ab3337Smrg# This program is free software; you can redistribute it and/or modify
1066ab3337Smrg# it under the terms of the GNU General Public License as published by
1166ab3337Smrg# the Free Software Foundation; either version 2, or (at your option)
1266ab3337Smrg# any later version.
1366ab3337Smrg#
1466ab3337Smrg# This program is distributed in the hope that it will be useful,
1566ab3337Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1666ab3337Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1766ab3337Smrg# GNU General Public License for more details.
1866ab3337Smrg#
1966ab3337Smrg# You should have received a copy of the GNU General Public License
2066ab3337Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2166ab3337Smrg
2266ab3337Smrg# As a special exception to the GNU General Public License, if you
2366ab3337Smrg# distribute this file as part of a program that contains a
2466ab3337Smrg# configuration script generated by Autoconf, you may include it under
2566ab3337Smrg# the same distribution terms that you use for the rest of that program.
2666ab3337Smrg
2766ab3337Smrg# This file is maintained in Automake, please report
2866ab3337Smrg# bugs to <bug-automake@gnu.org> or send patches to
2966ab3337Smrg# <automake-patches@gnu.org>.
3066ab3337Smrg
3166ab3337Smrgnl='
3266ab3337Smrg'
3366ab3337Smrg
3466ab3337Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3566ab3337Smrg# there to prevent tools from complaining about whitespace usage.
3666ab3337SmrgIFS=" ""	$nl"
3766ab3337Smrg
3866ab3337Smrgfile_conv=
3966ab3337Smrg
4066ab3337Smrg# func_file_conv build_file lazy
4166ab3337Smrg# Convert a $build file to $host form and store it in $file
4266ab3337Smrg# Currently only supports Windows hosts. If the determined conversion
4366ab3337Smrg# type is listed in (the comma separated) LAZY, no conversion will
4466ab3337Smrg# take place.
4566ab3337Smrgfunc_file_conv ()
4666ab3337Smrg{
4766ab3337Smrg  file=$1
4866ab3337Smrg  case $file in
4966ab3337Smrg    / | /[!/]*) # absolute file, and not a UNC file
5066ab3337Smrg      if test -z "$file_conv"; then
5166ab3337Smrg	# lazily determine how to convert abs files
5266ab3337Smrg	case `uname -s` in
5366ab3337Smrg	  MINGW*)
5466ab3337Smrg	    file_conv=mingw
5566ab3337Smrg	    ;;
5666ab3337Smrg	  CYGWIN* | MSYS*)
5766ab3337Smrg	    file_conv=cygwin
5866ab3337Smrg	    ;;
5966ab3337Smrg	  *)
6066ab3337Smrg	    file_conv=wine
6166ab3337Smrg	    ;;
6266ab3337Smrg	esac
6366ab3337Smrg      fi
6466ab3337Smrg      case $file_conv/,$2, in
6566ab3337Smrg	*,$file_conv,*)
6666ab3337Smrg	  ;;
6766ab3337Smrg	mingw/*)
6866ab3337Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6966ab3337Smrg	  ;;
7066ab3337Smrg	cygwin/* | msys/*)
7166ab3337Smrg	  file=`cygpath -m "$file" || echo "$file"`
7266ab3337Smrg	  ;;
7366ab3337Smrg	wine/*)
7466ab3337Smrg	  file=`winepath -w "$file" || echo "$file"`
7566ab3337Smrg	  ;;
7666ab3337Smrg      esac
7766ab3337Smrg      ;;
7866ab3337Smrg  esac
7966ab3337Smrg}
8066ab3337Smrg
8166ab3337Smrg# func_cl_dashL linkdir
8266ab3337Smrg# Make cl look for libraries in LINKDIR
8366ab3337Smrgfunc_cl_dashL ()
8466ab3337Smrg{
8566ab3337Smrg  func_file_conv "$1"
8666ab3337Smrg  if test -z "$lib_path"; then
8766ab3337Smrg    lib_path=$file
8866ab3337Smrg  else
8966ab3337Smrg    lib_path="$lib_path;$file"
9066ab3337Smrg  fi
9166ab3337Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9266ab3337Smrg}
9366ab3337Smrg
9466ab3337Smrg# func_cl_dashl library
9566ab3337Smrg# Do a library search-path lookup for cl
9666ab3337Smrgfunc_cl_dashl ()
9766ab3337Smrg{
9866ab3337Smrg  lib=$1
9966ab3337Smrg  found=no
10066ab3337Smrg  save_IFS=$IFS
10166ab3337Smrg  IFS=';'
10266ab3337Smrg  for dir in $lib_path $LIB
10366ab3337Smrg  do
10466ab3337Smrg    IFS=$save_IFS
10566ab3337Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10666ab3337Smrg      found=yes
10766ab3337Smrg      lib=$dir/$lib.dll.lib
10866ab3337Smrg      break
10966ab3337Smrg    fi
11066ab3337Smrg    if test -f "$dir/$lib.lib"; then
11166ab3337Smrg      found=yes
11266ab3337Smrg      lib=$dir/$lib.lib
11366ab3337Smrg      break
11466ab3337Smrg    fi
11566ab3337Smrg    if test -f "$dir/lib$lib.a"; then
11666ab3337Smrg      found=yes
11766ab3337Smrg      lib=$dir/lib$lib.a
11866ab3337Smrg      break
11966ab3337Smrg    fi
12066ab3337Smrg  done
12166ab3337Smrg  IFS=$save_IFS
12266ab3337Smrg
12366ab3337Smrg  if test "$found" != yes; then
12466ab3337Smrg    lib=$lib.lib
12566ab3337Smrg  fi
12666ab3337Smrg}
12766ab3337Smrg
12866ab3337Smrg# func_cl_wrapper cl arg...
12966ab3337Smrg# Adjust compile command to suit cl
13066ab3337Smrgfunc_cl_wrapper ()
13166ab3337Smrg{
13266ab3337Smrg  # Assume a capable shell
13366ab3337Smrg  lib_path=
13466ab3337Smrg  shared=:
13566ab3337Smrg  linker_opts=
13666ab3337Smrg  for arg
13766ab3337Smrg  do
13866ab3337Smrg    if test -n "$eat"; then
13966ab3337Smrg      eat=
14066ab3337Smrg    else
14166ab3337Smrg      case $1 in
14266ab3337Smrg	-o)
14366ab3337Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14466ab3337Smrg	  eat=1
14566ab3337Smrg	  case $2 in
14666ab3337Smrg	    *.o | *.[oO][bB][jJ])
14766ab3337Smrg	      func_file_conv "$2"
14866ab3337Smrg	      set x "$@" -Fo"$file"
14966ab3337Smrg	      shift
15066ab3337Smrg	      ;;
15166ab3337Smrg	    *)
15266ab3337Smrg	      func_file_conv "$2"
15366ab3337Smrg	      set x "$@" -Fe"$file"
15466ab3337Smrg	      shift
15566ab3337Smrg	      ;;
15666ab3337Smrg	  esac
15766ab3337Smrg	  ;;
15866ab3337Smrg	-I)
15966ab3337Smrg	  eat=1
16066ab3337Smrg	  func_file_conv "$2" mingw
16166ab3337Smrg	  set x "$@" -I"$file"
16266ab3337Smrg	  shift
16366ab3337Smrg	  ;;
16466ab3337Smrg	-I*)
16566ab3337Smrg	  func_file_conv "${1#-I}" mingw
16666ab3337Smrg	  set x "$@" -I"$file"
16766ab3337Smrg	  shift
16866ab3337Smrg	  ;;
16966ab3337Smrg	-l)
17066ab3337Smrg	  eat=1
17166ab3337Smrg	  func_cl_dashl "$2"
17266ab3337Smrg	  set x "$@" "$lib"
17366ab3337Smrg	  shift
17466ab3337Smrg	  ;;
17566ab3337Smrg	-l*)
17666ab3337Smrg	  func_cl_dashl "${1#-l}"
17766ab3337Smrg	  set x "$@" "$lib"
17866ab3337Smrg	  shift
17966ab3337Smrg	  ;;
18066ab3337Smrg	-L)
18166ab3337Smrg	  eat=1
18266ab3337Smrg	  func_cl_dashL "$2"
18366ab3337Smrg	  ;;
18466ab3337Smrg	-L*)
18566ab3337Smrg	  func_cl_dashL "${1#-L}"
18666ab3337Smrg	  ;;
18766ab3337Smrg	-static)
18866ab3337Smrg	  shared=false
18966ab3337Smrg	  ;;
19066ab3337Smrg	-Wl,*)
19166ab3337Smrg	  arg=${1#-Wl,}
19266ab3337Smrg	  save_ifs="$IFS"; IFS=','
19366ab3337Smrg	  for flag in $arg; do
19466ab3337Smrg	    IFS="$save_ifs"
19566ab3337Smrg	    linker_opts="$linker_opts $flag"
19666ab3337Smrg	  done
19766ab3337Smrg	  IFS="$save_ifs"
19866ab3337Smrg	  ;;
19966ab3337Smrg	-Xlinker)
20066ab3337Smrg	  eat=1
20166ab3337Smrg	  linker_opts="$linker_opts $2"
20266ab3337Smrg	  ;;
20366ab3337Smrg	-*)
20466ab3337Smrg	  set x "$@" "$1"
20566ab3337Smrg	  shift
20666ab3337Smrg	  ;;
20766ab3337Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20866ab3337Smrg	  func_file_conv "$1"
20966ab3337Smrg	  set x "$@" -Tp"$file"
21066ab3337Smrg	  shift
21166ab3337Smrg	  ;;
21266ab3337Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21366ab3337Smrg	  func_file_conv "$1" mingw
21466ab3337Smrg	  set x "$@" "$file"
21566ab3337Smrg	  shift
21666ab3337Smrg	  ;;
21766ab3337Smrg	*)
21866ab3337Smrg	  set x "$@" "$1"
21966ab3337Smrg	  shift
22066ab3337Smrg	  ;;
22166ab3337Smrg      esac
22266ab3337Smrg    fi
22366ab3337Smrg    shift
22466ab3337Smrg  done
22566ab3337Smrg  if test -n "$linker_opts"; then
22666ab3337Smrg    linker_opts="-link$linker_opts"
22766ab3337Smrg  fi
22866ab3337Smrg  exec "$@" $linker_opts
22966ab3337Smrg  exit 1
23066ab3337Smrg}
23166ab3337Smrg
23266ab3337Smrgeat=
23366ab3337Smrg
23466ab3337Smrgcase $1 in
23566ab3337Smrg  '')
23666ab3337Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23766ab3337Smrg     exit 1;
23866ab3337Smrg     ;;
23966ab3337Smrg  -h | --h*)
24066ab3337Smrg    cat <<\EOF
24166ab3337SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24266ab3337Smrg
24366ab3337SmrgWrapper for compilers which do not understand '-c -o'.
24466ab3337SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24566ab3337Smrgarguments, and rename the output as expected.
24666ab3337Smrg
24766ab3337SmrgIf you are trying to build a whole package this is not the
24866ab3337Smrgright script to run: please start by reading the file 'INSTALL'.
24966ab3337Smrg
25066ab3337SmrgReport bugs to <bug-automake@gnu.org>.
25166ab3337SmrgEOF
25266ab3337Smrg    exit $?
25366ab3337Smrg    ;;
25466ab3337Smrg  -v | --v*)
25566ab3337Smrg    echo "compile $scriptversion"
25666ab3337Smrg    exit $?
25766ab3337Smrg    ;;
25866ab3337Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25966ab3337Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26066ab3337Smrg    func_cl_wrapper "$@"      # Doesn't return...
26166ab3337Smrg    ;;
26266ab3337Smrgesac
26366ab3337Smrg
26466ab3337Smrgofile=
26566ab3337Smrgcfile=
26666ab3337Smrg
26766ab3337Smrgfor arg
26866ab3337Smrgdo
26966ab3337Smrg  if test -n "$eat"; then
27066ab3337Smrg    eat=
27166ab3337Smrg  else
27266ab3337Smrg    case $1 in
27366ab3337Smrg      -o)
27466ab3337Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27566ab3337Smrg	# So we strip '-o arg' only if arg is an object.
27666ab3337Smrg	eat=1
27766ab3337Smrg	case $2 in
27866ab3337Smrg	  *.o | *.obj)
27966ab3337Smrg	    ofile=$2
28066ab3337Smrg	    ;;
28166ab3337Smrg	  *)
28266ab3337Smrg	    set x "$@" -o "$2"
28366ab3337Smrg	    shift
28466ab3337Smrg	    ;;
28566ab3337Smrg	esac
28666ab3337Smrg	;;
28766ab3337Smrg      *.c)
28866ab3337Smrg	cfile=$1
28966ab3337Smrg	set x "$@" "$1"
29066ab3337Smrg	shift
29166ab3337Smrg	;;
29266ab3337Smrg      *)
29366ab3337Smrg	set x "$@" "$1"
29466ab3337Smrg	shift
29566ab3337Smrg	;;
29666ab3337Smrg    esac
29766ab3337Smrg  fi
29866ab3337Smrg  shift
29966ab3337Smrgdone
30066ab3337Smrg
30166ab3337Smrgif test -z "$ofile" || test -z "$cfile"; then
30266ab3337Smrg  # If no '-o' option was seen then we might have been invoked from a
30366ab3337Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30466ab3337Smrg  # normal compilation that the losing compiler can handle.  If no
30566ab3337Smrg  # '.c' file was seen then we are probably linking.  That is also
30666ab3337Smrg  # ok.
30766ab3337Smrg  exec "$@"
30866ab3337Smrgfi
30966ab3337Smrg
31066ab3337Smrg# Name of file we expect compiler to create.
31166ab3337Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31266ab3337Smrg
31366ab3337Smrg# Create the lock directory.
31466ab3337Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31566ab3337Smrg# that we are using for the .o file.  Also, base the name on the expected
31666ab3337Smrg# object file name, since that is what matters with a parallel build.
31766ab3337Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31866ab3337Smrgwhile true; do
31966ab3337Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32066ab3337Smrg    break
32166ab3337Smrg  fi
32266ab3337Smrg  sleep 1
32366ab3337Smrgdone
32466ab3337Smrg# FIXME: race condition here if user kills between mkdir and trap.
32566ab3337Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32666ab3337Smrg
32766ab3337Smrg# Run the compile.
32866ab3337Smrg"$@"
32966ab3337Smrgret=$?
33066ab3337Smrg
33166ab3337Smrgif test -f "$cofile"; then
33266ab3337Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33366ab3337Smrgelif test -f "${cofile}bj"; then
33466ab3337Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33566ab3337Smrgfi
33666ab3337Smrg
33766ab3337Smrgrmdir "$lockdir"
33866ab3337Smrgexit $ret
33966ab3337Smrg
34066ab3337Smrg# Local Variables:
34166ab3337Smrg# mode: shell-script
34266ab3337Smrg# sh-indentation: 2
34366ab3337Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
34466ab3337Smrg# time-stamp-start: "scriptversion="
34566ab3337Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34666ab3337Smrg# time-stamp-time-zone: "UTC0"
34766ab3337Smrg# time-stamp-end: "; # UTC"
34866ab3337Smrg# End:
349