187aef7c3Smrg#! /bin/sh
287aef7c3Smrg# Wrapper for compilers which do not understand '-c -o'.
387aef7c3Smrg
4764c86d1Smrgscriptversion=2018-03-07.03; # UTC
587aef7c3Smrg
6764c86d1Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
787aef7c3Smrg# Written by Tom Tromey <tromey@cygnus.com>.
887aef7c3Smrg#
987aef7c3Smrg# This program is free software; you can redistribute it and/or modify
1087aef7c3Smrg# it under the terms of the GNU General Public License as published by
1187aef7c3Smrg# the Free Software Foundation; either version 2, or (at your option)
1287aef7c3Smrg# any later version.
1387aef7c3Smrg#
1487aef7c3Smrg# This program is distributed in the hope that it will be useful,
1587aef7c3Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1687aef7c3Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1787aef7c3Smrg# GNU General Public License for more details.
1887aef7c3Smrg#
1987aef7c3Smrg# You should have received a copy of the GNU General Public License
20764c86d1Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2187aef7c3Smrg
2287aef7c3Smrg# As a special exception to the GNU General Public License, if you
2387aef7c3Smrg# distribute this file as part of a program that contains a
2487aef7c3Smrg# configuration script generated by Autoconf, you may include it under
2587aef7c3Smrg# the same distribution terms that you use for the rest of that program.
2687aef7c3Smrg
2787aef7c3Smrg# This file is maintained in Automake, please report
2887aef7c3Smrg# bugs to <bug-automake@gnu.org> or send patches to
2987aef7c3Smrg# <automake-patches@gnu.org>.
3087aef7c3Smrg
3187aef7c3Smrgnl='
3287aef7c3Smrg'
3387aef7c3Smrg
3487aef7c3Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3587aef7c3Smrg# there to prevent tools from complaining about whitespace usage.
3687aef7c3SmrgIFS=" ""	$nl"
3787aef7c3Smrg
3887aef7c3Smrgfile_conv=
3987aef7c3Smrg
4087aef7c3Smrg# func_file_conv build_file lazy
4187aef7c3Smrg# Convert a $build file to $host form and store it in $file
4287aef7c3Smrg# Currently only supports Windows hosts. If the determined conversion
4387aef7c3Smrg# type is listed in (the comma separated) LAZY, no conversion will
4487aef7c3Smrg# take place.
4587aef7c3Smrgfunc_file_conv ()
4687aef7c3Smrg{
4787aef7c3Smrg  file=$1
4887aef7c3Smrg  case $file in
4987aef7c3Smrg    / | /[!/]*) # absolute file, and not a UNC file
5087aef7c3Smrg      if test -z "$file_conv"; then
5187aef7c3Smrg	# lazily determine how to convert abs files
5287aef7c3Smrg	case `uname -s` in
5387aef7c3Smrg	  MINGW*)
5487aef7c3Smrg	    file_conv=mingw
5587aef7c3Smrg	    ;;
56764c86d1Smrg	  CYGWIN* | MSYS*)
5787aef7c3Smrg	    file_conv=cygwin
5887aef7c3Smrg	    ;;
5987aef7c3Smrg	  *)
6087aef7c3Smrg	    file_conv=wine
6187aef7c3Smrg	    ;;
6287aef7c3Smrg	esac
6387aef7c3Smrg      fi
6487aef7c3Smrg      case $file_conv/,$2, in
6587aef7c3Smrg	*,$file_conv,*)
6687aef7c3Smrg	  ;;
6787aef7c3Smrg	mingw/*)
6887aef7c3Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6987aef7c3Smrg	  ;;
70764c86d1Smrg	cygwin/* | msys/*)
7187aef7c3Smrg	  file=`cygpath -m "$file" || echo "$file"`
7287aef7c3Smrg	  ;;
7387aef7c3Smrg	wine/*)
7487aef7c3Smrg	  file=`winepath -w "$file" || echo "$file"`
7587aef7c3Smrg	  ;;
7687aef7c3Smrg      esac
7787aef7c3Smrg      ;;
7887aef7c3Smrg  esac
7987aef7c3Smrg}
8087aef7c3Smrg
8187aef7c3Smrg# func_cl_dashL linkdir
8287aef7c3Smrg# Make cl look for libraries in LINKDIR
8387aef7c3Smrgfunc_cl_dashL ()
8487aef7c3Smrg{
8587aef7c3Smrg  func_file_conv "$1"
8687aef7c3Smrg  if test -z "$lib_path"; then
8787aef7c3Smrg    lib_path=$file
8887aef7c3Smrg  else
8987aef7c3Smrg    lib_path="$lib_path;$file"
9087aef7c3Smrg  fi
9187aef7c3Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9287aef7c3Smrg}
9387aef7c3Smrg
9487aef7c3Smrg# func_cl_dashl library
9587aef7c3Smrg# Do a library search-path lookup for cl
9687aef7c3Smrgfunc_cl_dashl ()
9787aef7c3Smrg{
9887aef7c3Smrg  lib=$1
9987aef7c3Smrg  found=no
10087aef7c3Smrg  save_IFS=$IFS
10187aef7c3Smrg  IFS=';'
10287aef7c3Smrg  for dir in $lib_path $LIB
10387aef7c3Smrg  do
10487aef7c3Smrg    IFS=$save_IFS
10587aef7c3Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10687aef7c3Smrg      found=yes
10787aef7c3Smrg      lib=$dir/$lib.dll.lib
10887aef7c3Smrg      break
10987aef7c3Smrg    fi
11087aef7c3Smrg    if test -f "$dir/$lib.lib"; then
11187aef7c3Smrg      found=yes
11287aef7c3Smrg      lib=$dir/$lib.lib
11387aef7c3Smrg      break
11487aef7c3Smrg    fi
11587aef7c3Smrg    if test -f "$dir/lib$lib.a"; then
11687aef7c3Smrg      found=yes
11787aef7c3Smrg      lib=$dir/lib$lib.a
11887aef7c3Smrg      break
11987aef7c3Smrg    fi
12087aef7c3Smrg  done
12187aef7c3Smrg  IFS=$save_IFS
12287aef7c3Smrg
12387aef7c3Smrg  if test "$found" != yes; then
12487aef7c3Smrg    lib=$lib.lib
12587aef7c3Smrg  fi
12687aef7c3Smrg}
12787aef7c3Smrg
12887aef7c3Smrg# func_cl_wrapper cl arg...
12987aef7c3Smrg# Adjust compile command to suit cl
13087aef7c3Smrgfunc_cl_wrapper ()
13187aef7c3Smrg{
13287aef7c3Smrg  # Assume a capable shell
13387aef7c3Smrg  lib_path=
13487aef7c3Smrg  shared=:
13587aef7c3Smrg  linker_opts=
13687aef7c3Smrg  for arg
13787aef7c3Smrg  do
13887aef7c3Smrg    if test -n "$eat"; then
13987aef7c3Smrg      eat=
14087aef7c3Smrg    else
14187aef7c3Smrg      case $1 in
14287aef7c3Smrg	-o)
14387aef7c3Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14487aef7c3Smrg	  eat=1
14587aef7c3Smrg	  case $2 in
14687aef7c3Smrg	    *.o | *.[oO][bB][jJ])
14787aef7c3Smrg	      func_file_conv "$2"
14887aef7c3Smrg	      set x "$@" -Fo"$file"
14987aef7c3Smrg	      shift
15087aef7c3Smrg	      ;;
15187aef7c3Smrg	    *)
15287aef7c3Smrg	      func_file_conv "$2"
15387aef7c3Smrg	      set x "$@" -Fe"$file"
15487aef7c3Smrg	      shift
15587aef7c3Smrg	      ;;
15687aef7c3Smrg	  esac
15787aef7c3Smrg	  ;;
15887aef7c3Smrg	-I)
15987aef7c3Smrg	  eat=1
16087aef7c3Smrg	  func_file_conv "$2" mingw
16187aef7c3Smrg	  set x "$@" -I"$file"
16287aef7c3Smrg	  shift
16387aef7c3Smrg	  ;;
16487aef7c3Smrg	-I*)
16587aef7c3Smrg	  func_file_conv "${1#-I}" mingw
16687aef7c3Smrg	  set x "$@" -I"$file"
16787aef7c3Smrg	  shift
16887aef7c3Smrg	  ;;
16987aef7c3Smrg	-l)
17087aef7c3Smrg	  eat=1
17187aef7c3Smrg	  func_cl_dashl "$2"
17287aef7c3Smrg	  set x "$@" "$lib"
17387aef7c3Smrg	  shift
17487aef7c3Smrg	  ;;
17587aef7c3Smrg	-l*)
17687aef7c3Smrg	  func_cl_dashl "${1#-l}"
17787aef7c3Smrg	  set x "$@" "$lib"
17887aef7c3Smrg	  shift
17987aef7c3Smrg	  ;;
18087aef7c3Smrg	-L)
18187aef7c3Smrg	  eat=1
18287aef7c3Smrg	  func_cl_dashL "$2"
18387aef7c3Smrg	  ;;
18487aef7c3Smrg	-L*)
18587aef7c3Smrg	  func_cl_dashL "${1#-L}"
18687aef7c3Smrg	  ;;
18787aef7c3Smrg	-static)
18887aef7c3Smrg	  shared=false
18987aef7c3Smrg	  ;;
19087aef7c3Smrg	-Wl,*)
19187aef7c3Smrg	  arg=${1#-Wl,}
19287aef7c3Smrg	  save_ifs="$IFS"; IFS=','
19387aef7c3Smrg	  for flag in $arg; do
19487aef7c3Smrg	    IFS="$save_ifs"
19587aef7c3Smrg	    linker_opts="$linker_opts $flag"
19687aef7c3Smrg	  done
19787aef7c3Smrg	  IFS="$save_ifs"
19887aef7c3Smrg	  ;;
19987aef7c3Smrg	-Xlinker)
20087aef7c3Smrg	  eat=1
20187aef7c3Smrg	  linker_opts="$linker_opts $2"
20287aef7c3Smrg	  ;;
20387aef7c3Smrg	-*)
20487aef7c3Smrg	  set x "$@" "$1"
20587aef7c3Smrg	  shift
20687aef7c3Smrg	  ;;
20787aef7c3Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20887aef7c3Smrg	  func_file_conv "$1"
20987aef7c3Smrg	  set x "$@" -Tp"$file"
21087aef7c3Smrg	  shift
21187aef7c3Smrg	  ;;
21287aef7c3Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21387aef7c3Smrg	  func_file_conv "$1" mingw
21487aef7c3Smrg	  set x "$@" "$file"
21587aef7c3Smrg	  shift
21687aef7c3Smrg	  ;;
21787aef7c3Smrg	*)
21887aef7c3Smrg	  set x "$@" "$1"
21987aef7c3Smrg	  shift
22087aef7c3Smrg	  ;;
22187aef7c3Smrg      esac
22287aef7c3Smrg    fi
22387aef7c3Smrg    shift
22487aef7c3Smrg  done
22587aef7c3Smrg  if test -n "$linker_opts"; then
22687aef7c3Smrg    linker_opts="-link$linker_opts"
22787aef7c3Smrg  fi
22887aef7c3Smrg  exec "$@" $linker_opts
22987aef7c3Smrg  exit 1
23087aef7c3Smrg}
23187aef7c3Smrg
23287aef7c3Smrgeat=
23387aef7c3Smrg
23487aef7c3Smrgcase $1 in
23587aef7c3Smrg  '')
23687aef7c3Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23787aef7c3Smrg     exit 1;
23887aef7c3Smrg     ;;
23987aef7c3Smrg  -h | --h*)
24087aef7c3Smrg    cat <<\EOF
24187aef7c3SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24287aef7c3Smrg
24387aef7c3SmrgWrapper for compilers which do not understand '-c -o'.
24487aef7c3SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24587aef7c3Smrgarguments, and rename the output as expected.
24687aef7c3Smrg
24787aef7c3SmrgIf you are trying to build a whole package this is not the
24887aef7c3Smrgright script to run: please start by reading the file 'INSTALL'.
24987aef7c3Smrg
25087aef7c3SmrgReport bugs to <bug-automake@gnu.org>.
25187aef7c3SmrgEOF
25287aef7c3Smrg    exit $?
25387aef7c3Smrg    ;;
25487aef7c3Smrg  -v | --v*)
25587aef7c3Smrg    echo "compile $scriptversion"
25687aef7c3Smrg    exit $?
25787aef7c3Smrg    ;;
258764c86d1Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259764c86d1Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26087aef7c3Smrg    func_cl_wrapper "$@"      # Doesn't return...
26187aef7c3Smrg    ;;
26287aef7c3Smrgesac
26387aef7c3Smrg
26487aef7c3Smrgofile=
26587aef7c3Smrgcfile=
26687aef7c3Smrg
26787aef7c3Smrgfor arg
26887aef7c3Smrgdo
26987aef7c3Smrg  if test -n "$eat"; then
27087aef7c3Smrg    eat=
27187aef7c3Smrg  else
27287aef7c3Smrg    case $1 in
27387aef7c3Smrg      -o)
27487aef7c3Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27587aef7c3Smrg	# So we strip '-o arg' only if arg is an object.
27687aef7c3Smrg	eat=1
27787aef7c3Smrg	case $2 in
27887aef7c3Smrg	  *.o | *.obj)
27987aef7c3Smrg	    ofile=$2
28087aef7c3Smrg	    ;;
28187aef7c3Smrg	  *)
28287aef7c3Smrg	    set x "$@" -o "$2"
28387aef7c3Smrg	    shift
28487aef7c3Smrg	    ;;
28587aef7c3Smrg	esac
28687aef7c3Smrg	;;
28787aef7c3Smrg      *.c)
28887aef7c3Smrg	cfile=$1
28987aef7c3Smrg	set x "$@" "$1"
29087aef7c3Smrg	shift
29187aef7c3Smrg	;;
29287aef7c3Smrg      *)
29387aef7c3Smrg	set x "$@" "$1"
29487aef7c3Smrg	shift
29587aef7c3Smrg	;;
29687aef7c3Smrg    esac
29787aef7c3Smrg  fi
29887aef7c3Smrg  shift
29987aef7c3Smrgdone
30087aef7c3Smrg
30187aef7c3Smrgif test -z "$ofile" || test -z "$cfile"; then
30287aef7c3Smrg  # If no '-o' option was seen then we might have been invoked from a
30387aef7c3Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30487aef7c3Smrg  # normal compilation that the losing compiler can handle.  If no
30587aef7c3Smrg  # '.c' file was seen then we are probably linking.  That is also
30687aef7c3Smrg  # ok.
30787aef7c3Smrg  exec "$@"
30887aef7c3Smrgfi
30987aef7c3Smrg
31087aef7c3Smrg# Name of file we expect compiler to create.
31187aef7c3Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31287aef7c3Smrg
31387aef7c3Smrg# Create the lock directory.
31487aef7c3Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31587aef7c3Smrg# that we are using for the .o file.  Also, base the name on the expected
31687aef7c3Smrg# object file name, since that is what matters with a parallel build.
31787aef7c3Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31887aef7c3Smrgwhile true; do
31987aef7c3Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32087aef7c3Smrg    break
32187aef7c3Smrg  fi
32287aef7c3Smrg  sleep 1
32387aef7c3Smrgdone
32487aef7c3Smrg# FIXME: race condition here if user kills between mkdir and trap.
32587aef7c3Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32687aef7c3Smrg
32787aef7c3Smrg# Run the compile.
32887aef7c3Smrg"$@"
32987aef7c3Smrgret=$?
33087aef7c3Smrg
33187aef7c3Smrgif test -f "$cofile"; then
33287aef7c3Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33387aef7c3Smrgelif test -f "${cofile}bj"; then
33487aef7c3Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33587aef7c3Smrgfi
33687aef7c3Smrg
33787aef7c3Smrgrmdir "$lockdir"
33887aef7c3Smrgexit $ret
33987aef7c3Smrg
34087aef7c3Smrg# Local Variables:
34187aef7c3Smrg# mode: shell-script
34287aef7c3Smrg# sh-indentation: 2
343764c86d1Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
34487aef7c3Smrg# time-stamp-start: "scriptversion="
34587aef7c3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346764c86d1Smrg# time-stamp-time-zone: "UTC0"
34787aef7c3Smrg# time-stamp-end: "; # UTC"
34887aef7c3Smrg# End:
349