184bf8334Smrg#! /bin/sh
284bf8334Smrg# Wrapper for compilers which do not understand '-c -o'.
384bf8334Smrg
4370b807fSmrgscriptversion=2018-03-07.03; # UTC
584bf8334Smrg
6370b807fSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
784bf8334Smrg# Written by Tom Tromey <tromey@cygnus.com>.
884bf8334Smrg#
984bf8334Smrg# This program is free software; you can redistribute it and/or modify
1084bf8334Smrg# it under the terms of the GNU General Public License as published by
1184bf8334Smrg# the Free Software Foundation; either version 2, or (at your option)
1284bf8334Smrg# any later version.
1384bf8334Smrg#
1484bf8334Smrg# This program is distributed in the hope that it will be useful,
1584bf8334Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1684bf8334Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1784bf8334Smrg# GNU General Public License for more details.
1884bf8334Smrg#
1984bf8334Smrg# You should have received a copy of the GNU General Public License
20370b807fSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2184bf8334Smrg
2284bf8334Smrg# As a special exception to the GNU General Public License, if you
2384bf8334Smrg# distribute this file as part of a program that contains a
2484bf8334Smrg# configuration script generated by Autoconf, you may include it under
2584bf8334Smrg# the same distribution terms that you use for the rest of that program.
2684bf8334Smrg
2784bf8334Smrg# This file is maintained in Automake, please report
2884bf8334Smrg# bugs to <bug-automake@gnu.org> or send patches to
2984bf8334Smrg# <automake-patches@gnu.org>.
3084bf8334Smrg
3184bf8334Smrgnl='
3284bf8334Smrg'
3384bf8334Smrg
3484bf8334Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3584bf8334Smrg# there to prevent tools from complaining about whitespace usage.
3684bf8334SmrgIFS=" ""	$nl"
3784bf8334Smrg
3884bf8334Smrgfile_conv=
3984bf8334Smrg
4084bf8334Smrg# func_file_conv build_file lazy
4184bf8334Smrg# Convert a $build file to $host form and store it in $file
4284bf8334Smrg# Currently only supports Windows hosts. If the determined conversion
4384bf8334Smrg# type is listed in (the comma separated) LAZY, no conversion will
4484bf8334Smrg# take place.
4584bf8334Smrgfunc_file_conv ()
4684bf8334Smrg{
4784bf8334Smrg  file=$1
4884bf8334Smrg  case $file in
4984bf8334Smrg    / | /[!/]*) # absolute file, and not a UNC file
5084bf8334Smrg      if test -z "$file_conv"; then
5184bf8334Smrg	# lazily determine how to convert abs files
5284bf8334Smrg	case `uname -s` in
5384bf8334Smrg	  MINGW*)
5484bf8334Smrg	    file_conv=mingw
5584bf8334Smrg	    ;;
56370b807fSmrg	  CYGWIN* | MSYS*)
5784bf8334Smrg	    file_conv=cygwin
5884bf8334Smrg	    ;;
5984bf8334Smrg	  *)
6084bf8334Smrg	    file_conv=wine
6184bf8334Smrg	    ;;
6284bf8334Smrg	esac
6384bf8334Smrg      fi
6484bf8334Smrg      case $file_conv/,$2, in
6584bf8334Smrg	*,$file_conv,*)
6684bf8334Smrg	  ;;
6784bf8334Smrg	mingw/*)
6884bf8334Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6984bf8334Smrg	  ;;
70370b807fSmrg	cygwin/* | msys/*)
7184bf8334Smrg	  file=`cygpath -m "$file" || echo "$file"`
7284bf8334Smrg	  ;;
7384bf8334Smrg	wine/*)
7484bf8334Smrg	  file=`winepath -w "$file" || echo "$file"`
7584bf8334Smrg	  ;;
7684bf8334Smrg      esac
7784bf8334Smrg      ;;
7884bf8334Smrg  esac
7984bf8334Smrg}
8084bf8334Smrg
8184bf8334Smrg# func_cl_dashL linkdir
8284bf8334Smrg# Make cl look for libraries in LINKDIR
8384bf8334Smrgfunc_cl_dashL ()
8484bf8334Smrg{
8584bf8334Smrg  func_file_conv "$1"
8684bf8334Smrg  if test -z "$lib_path"; then
8784bf8334Smrg    lib_path=$file
8884bf8334Smrg  else
8984bf8334Smrg    lib_path="$lib_path;$file"
9084bf8334Smrg  fi
9184bf8334Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9284bf8334Smrg}
9384bf8334Smrg
9484bf8334Smrg# func_cl_dashl library
9584bf8334Smrg# Do a library search-path lookup for cl
9684bf8334Smrgfunc_cl_dashl ()
9784bf8334Smrg{
9884bf8334Smrg  lib=$1
9984bf8334Smrg  found=no
10084bf8334Smrg  save_IFS=$IFS
10184bf8334Smrg  IFS=';'
10284bf8334Smrg  for dir in $lib_path $LIB
10384bf8334Smrg  do
10484bf8334Smrg    IFS=$save_IFS
10584bf8334Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10684bf8334Smrg      found=yes
10784bf8334Smrg      lib=$dir/$lib.dll.lib
10884bf8334Smrg      break
10984bf8334Smrg    fi
11084bf8334Smrg    if test -f "$dir/$lib.lib"; then
11184bf8334Smrg      found=yes
11284bf8334Smrg      lib=$dir/$lib.lib
11384bf8334Smrg      break
11484bf8334Smrg    fi
11584bf8334Smrg    if test -f "$dir/lib$lib.a"; then
11684bf8334Smrg      found=yes
11784bf8334Smrg      lib=$dir/lib$lib.a
11884bf8334Smrg      break
11984bf8334Smrg    fi
12084bf8334Smrg  done
12184bf8334Smrg  IFS=$save_IFS
12284bf8334Smrg
12384bf8334Smrg  if test "$found" != yes; then
12484bf8334Smrg    lib=$lib.lib
12584bf8334Smrg  fi
12684bf8334Smrg}
12784bf8334Smrg
12884bf8334Smrg# func_cl_wrapper cl arg...
12984bf8334Smrg# Adjust compile command to suit cl
13084bf8334Smrgfunc_cl_wrapper ()
13184bf8334Smrg{
13284bf8334Smrg  # Assume a capable shell
13384bf8334Smrg  lib_path=
13484bf8334Smrg  shared=:
13584bf8334Smrg  linker_opts=
13684bf8334Smrg  for arg
13784bf8334Smrg  do
13884bf8334Smrg    if test -n "$eat"; then
13984bf8334Smrg      eat=
14084bf8334Smrg    else
14184bf8334Smrg      case $1 in
14284bf8334Smrg	-o)
14384bf8334Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14484bf8334Smrg	  eat=1
14584bf8334Smrg	  case $2 in
14684bf8334Smrg	    *.o | *.[oO][bB][jJ])
14784bf8334Smrg	      func_file_conv "$2"
14884bf8334Smrg	      set x "$@" -Fo"$file"
14984bf8334Smrg	      shift
15084bf8334Smrg	      ;;
15184bf8334Smrg	    *)
15284bf8334Smrg	      func_file_conv "$2"
15384bf8334Smrg	      set x "$@" -Fe"$file"
15484bf8334Smrg	      shift
15584bf8334Smrg	      ;;
15684bf8334Smrg	  esac
15784bf8334Smrg	  ;;
15884bf8334Smrg	-I)
15984bf8334Smrg	  eat=1
16084bf8334Smrg	  func_file_conv "$2" mingw
16184bf8334Smrg	  set x "$@" -I"$file"
16284bf8334Smrg	  shift
16384bf8334Smrg	  ;;
16484bf8334Smrg	-I*)
16584bf8334Smrg	  func_file_conv "${1#-I}" mingw
16684bf8334Smrg	  set x "$@" -I"$file"
16784bf8334Smrg	  shift
16884bf8334Smrg	  ;;
16984bf8334Smrg	-l)
17084bf8334Smrg	  eat=1
17184bf8334Smrg	  func_cl_dashl "$2"
17284bf8334Smrg	  set x "$@" "$lib"
17384bf8334Smrg	  shift
17484bf8334Smrg	  ;;
17584bf8334Smrg	-l*)
17684bf8334Smrg	  func_cl_dashl "${1#-l}"
17784bf8334Smrg	  set x "$@" "$lib"
17884bf8334Smrg	  shift
17984bf8334Smrg	  ;;
18084bf8334Smrg	-L)
18184bf8334Smrg	  eat=1
18284bf8334Smrg	  func_cl_dashL "$2"
18384bf8334Smrg	  ;;
18484bf8334Smrg	-L*)
18584bf8334Smrg	  func_cl_dashL "${1#-L}"
18684bf8334Smrg	  ;;
18784bf8334Smrg	-static)
18884bf8334Smrg	  shared=false
18984bf8334Smrg	  ;;
19084bf8334Smrg	-Wl,*)
19184bf8334Smrg	  arg=${1#-Wl,}
19284bf8334Smrg	  save_ifs="$IFS"; IFS=','
19384bf8334Smrg	  for flag in $arg; do
19484bf8334Smrg	    IFS="$save_ifs"
19584bf8334Smrg	    linker_opts="$linker_opts $flag"
19684bf8334Smrg	  done
19784bf8334Smrg	  IFS="$save_ifs"
19884bf8334Smrg	  ;;
19984bf8334Smrg	-Xlinker)
20084bf8334Smrg	  eat=1
20184bf8334Smrg	  linker_opts="$linker_opts $2"
20284bf8334Smrg	  ;;
20384bf8334Smrg	-*)
20484bf8334Smrg	  set x "$@" "$1"
20584bf8334Smrg	  shift
20684bf8334Smrg	  ;;
20784bf8334Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20884bf8334Smrg	  func_file_conv "$1"
20984bf8334Smrg	  set x "$@" -Tp"$file"
21084bf8334Smrg	  shift
21184bf8334Smrg	  ;;
21284bf8334Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21384bf8334Smrg	  func_file_conv "$1" mingw
21484bf8334Smrg	  set x "$@" "$file"
21584bf8334Smrg	  shift
21684bf8334Smrg	  ;;
21784bf8334Smrg	*)
21884bf8334Smrg	  set x "$@" "$1"
21984bf8334Smrg	  shift
22084bf8334Smrg	  ;;
22184bf8334Smrg      esac
22284bf8334Smrg    fi
22384bf8334Smrg    shift
22484bf8334Smrg  done
22584bf8334Smrg  if test -n "$linker_opts"; then
22684bf8334Smrg    linker_opts="-link$linker_opts"
22784bf8334Smrg  fi
22884bf8334Smrg  exec "$@" $linker_opts
22984bf8334Smrg  exit 1
23084bf8334Smrg}
23184bf8334Smrg
23284bf8334Smrgeat=
23384bf8334Smrg
23484bf8334Smrgcase $1 in
23584bf8334Smrg  '')
23684bf8334Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23784bf8334Smrg     exit 1;
23884bf8334Smrg     ;;
23984bf8334Smrg  -h | --h*)
24084bf8334Smrg    cat <<\EOF
24184bf8334SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24284bf8334Smrg
24384bf8334SmrgWrapper for compilers which do not understand '-c -o'.
24484bf8334SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24584bf8334Smrgarguments, and rename the output as expected.
24684bf8334Smrg
24784bf8334SmrgIf you are trying to build a whole package this is not the
24884bf8334Smrgright script to run: please start by reading the file 'INSTALL'.
24984bf8334Smrg
25084bf8334SmrgReport bugs to <bug-automake@gnu.org>.
25184bf8334SmrgEOF
25284bf8334Smrg    exit $?
25384bf8334Smrg    ;;
25484bf8334Smrg  -v | --v*)
25584bf8334Smrg    echo "compile $scriptversion"
25684bf8334Smrg    exit $?
25784bf8334Smrg    ;;
258370b807fSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259370b807fSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26084bf8334Smrg    func_cl_wrapper "$@"      # Doesn't return...
26184bf8334Smrg    ;;
26284bf8334Smrgesac
26384bf8334Smrg
26484bf8334Smrgofile=
26584bf8334Smrgcfile=
26684bf8334Smrg
26784bf8334Smrgfor arg
26884bf8334Smrgdo
26984bf8334Smrg  if test -n "$eat"; then
27084bf8334Smrg    eat=
27184bf8334Smrg  else
27284bf8334Smrg    case $1 in
27384bf8334Smrg      -o)
27484bf8334Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27584bf8334Smrg	# So we strip '-o arg' only if arg is an object.
27684bf8334Smrg	eat=1
27784bf8334Smrg	case $2 in
27884bf8334Smrg	  *.o | *.obj)
27984bf8334Smrg	    ofile=$2
28084bf8334Smrg	    ;;
28184bf8334Smrg	  *)
28284bf8334Smrg	    set x "$@" -o "$2"
28384bf8334Smrg	    shift
28484bf8334Smrg	    ;;
28584bf8334Smrg	esac
28684bf8334Smrg	;;
28784bf8334Smrg      *.c)
28884bf8334Smrg	cfile=$1
28984bf8334Smrg	set x "$@" "$1"
29084bf8334Smrg	shift
29184bf8334Smrg	;;
29284bf8334Smrg      *)
29384bf8334Smrg	set x "$@" "$1"
29484bf8334Smrg	shift
29584bf8334Smrg	;;
29684bf8334Smrg    esac
29784bf8334Smrg  fi
29884bf8334Smrg  shift
29984bf8334Smrgdone
30084bf8334Smrg
30184bf8334Smrgif test -z "$ofile" || test -z "$cfile"; then
30284bf8334Smrg  # If no '-o' option was seen then we might have been invoked from a
30384bf8334Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30484bf8334Smrg  # normal compilation that the losing compiler can handle.  If no
30584bf8334Smrg  # '.c' file was seen then we are probably linking.  That is also
30684bf8334Smrg  # ok.
30784bf8334Smrg  exec "$@"
30884bf8334Smrgfi
30984bf8334Smrg
31084bf8334Smrg# Name of file we expect compiler to create.
31184bf8334Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31284bf8334Smrg
31384bf8334Smrg# Create the lock directory.
31484bf8334Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31584bf8334Smrg# that we are using for the .o file.  Also, base the name on the expected
31684bf8334Smrg# object file name, since that is what matters with a parallel build.
31784bf8334Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31884bf8334Smrgwhile true; do
31984bf8334Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32084bf8334Smrg    break
32184bf8334Smrg  fi
32284bf8334Smrg  sleep 1
32384bf8334Smrgdone
32484bf8334Smrg# FIXME: race condition here if user kills between mkdir and trap.
32584bf8334Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32684bf8334Smrg
32784bf8334Smrg# Run the compile.
32884bf8334Smrg"$@"
32984bf8334Smrgret=$?
33084bf8334Smrg
33184bf8334Smrgif test -f "$cofile"; then
33284bf8334Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33384bf8334Smrgelif test -f "${cofile}bj"; then
33484bf8334Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33584bf8334Smrgfi
33684bf8334Smrg
33784bf8334Smrgrmdir "$lockdir"
33884bf8334Smrgexit $ret
33984bf8334Smrg
34084bf8334Smrg# Local Variables:
34184bf8334Smrg# mode: shell-script
34284bf8334Smrg# sh-indentation: 2
343370b807fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34484bf8334Smrg# time-stamp-start: "scriptversion="
34584bf8334Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346370b807fSmrg# time-stamp-time-zone: "UTC0"
34784bf8334Smrg# time-stamp-end: "; # UTC"
34884bf8334Smrg# End:
349