133dddc75Smrg#! /bin/sh
233dddc75Smrg# Wrapper for compilers which do not understand '-c -o'.
333dddc75Smrg
47ed541caSmrgscriptversion=2018-03-07.03; # UTC
533dddc75Smrg
67ed541caSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
733dddc75Smrg# Written by Tom Tromey <tromey@cygnus.com>.
833dddc75Smrg#
933dddc75Smrg# This program is free software; you can redistribute it and/or modify
1033dddc75Smrg# it under the terms of the GNU General Public License as published by
1133dddc75Smrg# the Free Software Foundation; either version 2, or (at your option)
1233dddc75Smrg# any later version.
1333dddc75Smrg#
1433dddc75Smrg# This program is distributed in the hope that it will be useful,
1533dddc75Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1633dddc75Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1733dddc75Smrg# GNU General Public License for more details.
1833dddc75Smrg#
1933dddc75Smrg# You should have received a copy of the GNU General Public License
207ed541caSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2133dddc75Smrg
2233dddc75Smrg# As a special exception to the GNU General Public License, if you
2333dddc75Smrg# distribute this file as part of a program that contains a
2433dddc75Smrg# configuration script generated by Autoconf, you may include it under
2533dddc75Smrg# the same distribution terms that you use for the rest of that program.
2633dddc75Smrg
2733dddc75Smrg# This file is maintained in Automake, please report
2833dddc75Smrg# bugs to <bug-automake@gnu.org> or send patches to
2933dddc75Smrg# <automake-patches@gnu.org>.
3033dddc75Smrg
3133dddc75Smrgnl='
3233dddc75Smrg'
3333dddc75Smrg
3433dddc75Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3533dddc75Smrg# there to prevent tools from complaining about whitespace usage.
3633dddc75SmrgIFS=" ""	$nl"
3733dddc75Smrg
3833dddc75Smrgfile_conv=
3933dddc75Smrg
4033dddc75Smrg# func_file_conv build_file lazy
4133dddc75Smrg# Convert a $build file to $host form and store it in $file
4233dddc75Smrg# Currently only supports Windows hosts. If the determined conversion
4333dddc75Smrg# type is listed in (the comma separated) LAZY, no conversion will
4433dddc75Smrg# take place.
4533dddc75Smrgfunc_file_conv ()
4633dddc75Smrg{
4733dddc75Smrg  file=$1
4833dddc75Smrg  case $file in
4933dddc75Smrg    / | /[!/]*) # absolute file, and not a UNC file
5033dddc75Smrg      if test -z "$file_conv"; then
5133dddc75Smrg	# lazily determine how to convert abs files
5233dddc75Smrg	case `uname -s` in
5333dddc75Smrg	  MINGW*)
5433dddc75Smrg	    file_conv=mingw
5533dddc75Smrg	    ;;
567ed541caSmrg	  CYGWIN* | MSYS*)
5733dddc75Smrg	    file_conv=cygwin
5833dddc75Smrg	    ;;
5933dddc75Smrg	  *)
6033dddc75Smrg	    file_conv=wine
6133dddc75Smrg	    ;;
6233dddc75Smrg	esac
6333dddc75Smrg      fi
6433dddc75Smrg      case $file_conv/,$2, in
6533dddc75Smrg	*,$file_conv,*)
6633dddc75Smrg	  ;;
6733dddc75Smrg	mingw/*)
6833dddc75Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6933dddc75Smrg	  ;;
707ed541caSmrg	cygwin/* | msys/*)
7133dddc75Smrg	  file=`cygpath -m "$file" || echo "$file"`
7233dddc75Smrg	  ;;
7333dddc75Smrg	wine/*)
7433dddc75Smrg	  file=`winepath -w "$file" || echo "$file"`
7533dddc75Smrg	  ;;
7633dddc75Smrg      esac
7733dddc75Smrg      ;;
7833dddc75Smrg  esac
7933dddc75Smrg}
8033dddc75Smrg
8133dddc75Smrg# func_cl_dashL linkdir
8233dddc75Smrg# Make cl look for libraries in LINKDIR
8333dddc75Smrgfunc_cl_dashL ()
8433dddc75Smrg{
8533dddc75Smrg  func_file_conv "$1"
8633dddc75Smrg  if test -z "$lib_path"; then
8733dddc75Smrg    lib_path=$file
8833dddc75Smrg  else
8933dddc75Smrg    lib_path="$lib_path;$file"
9033dddc75Smrg  fi
9133dddc75Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9233dddc75Smrg}
9333dddc75Smrg
9433dddc75Smrg# func_cl_dashl library
9533dddc75Smrg# Do a library search-path lookup for cl
9633dddc75Smrgfunc_cl_dashl ()
9733dddc75Smrg{
9833dddc75Smrg  lib=$1
9933dddc75Smrg  found=no
10033dddc75Smrg  save_IFS=$IFS
10133dddc75Smrg  IFS=';'
10233dddc75Smrg  for dir in $lib_path $LIB
10333dddc75Smrg  do
10433dddc75Smrg    IFS=$save_IFS
10533dddc75Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10633dddc75Smrg      found=yes
10733dddc75Smrg      lib=$dir/$lib.dll.lib
10833dddc75Smrg      break
10933dddc75Smrg    fi
11033dddc75Smrg    if test -f "$dir/$lib.lib"; then
11133dddc75Smrg      found=yes
11233dddc75Smrg      lib=$dir/$lib.lib
11333dddc75Smrg      break
11433dddc75Smrg    fi
11533dddc75Smrg    if test -f "$dir/lib$lib.a"; then
11633dddc75Smrg      found=yes
11733dddc75Smrg      lib=$dir/lib$lib.a
11833dddc75Smrg      break
11933dddc75Smrg    fi
12033dddc75Smrg  done
12133dddc75Smrg  IFS=$save_IFS
12233dddc75Smrg
12333dddc75Smrg  if test "$found" != yes; then
12433dddc75Smrg    lib=$lib.lib
12533dddc75Smrg  fi
12633dddc75Smrg}
12733dddc75Smrg
12833dddc75Smrg# func_cl_wrapper cl arg...
12933dddc75Smrg# Adjust compile command to suit cl
13033dddc75Smrgfunc_cl_wrapper ()
13133dddc75Smrg{
13233dddc75Smrg  # Assume a capable shell
13333dddc75Smrg  lib_path=
13433dddc75Smrg  shared=:
13533dddc75Smrg  linker_opts=
13633dddc75Smrg  for arg
13733dddc75Smrg  do
13833dddc75Smrg    if test -n "$eat"; then
13933dddc75Smrg      eat=
14033dddc75Smrg    else
14133dddc75Smrg      case $1 in
14233dddc75Smrg	-o)
14333dddc75Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14433dddc75Smrg	  eat=1
14533dddc75Smrg	  case $2 in
14633dddc75Smrg	    *.o | *.[oO][bB][jJ])
14733dddc75Smrg	      func_file_conv "$2"
14833dddc75Smrg	      set x "$@" -Fo"$file"
14933dddc75Smrg	      shift
15033dddc75Smrg	      ;;
15133dddc75Smrg	    *)
15233dddc75Smrg	      func_file_conv "$2"
15333dddc75Smrg	      set x "$@" -Fe"$file"
15433dddc75Smrg	      shift
15533dddc75Smrg	      ;;
15633dddc75Smrg	  esac
15733dddc75Smrg	  ;;
15833dddc75Smrg	-I)
15933dddc75Smrg	  eat=1
16033dddc75Smrg	  func_file_conv "$2" mingw
16133dddc75Smrg	  set x "$@" -I"$file"
16233dddc75Smrg	  shift
16333dddc75Smrg	  ;;
16433dddc75Smrg	-I*)
16533dddc75Smrg	  func_file_conv "${1#-I}" mingw
16633dddc75Smrg	  set x "$@" -I"$file"
16733dddc75Smrg	  shift
16833dddc75Smrg	  ;;
16933dddc75Smrg	-l)
17033dddc75Smrg	  eat=1
17133dddc75Smrg	  func_cl_dashl "$2"
17233dddc75Smrg	  set x "$@" "$lib"
17333dddc75Smrg	  shift
17433dddc75Smrg	  ;;
17533dddc75Smrg	-l*)
17633dddc75Smrg	  func_cl_dashl "${1#-l}"
17733dddc75Smrg	  set x "$@" "$lib"
17833dddc75Smrg	  shift
17933dddc75Smrg	  ;;
18033dddc75Smrg	-L)
18133dddc75Smrg	  eat=1
18233dddc75Smrg	  func_cl_dashL "$2"
18333dddc75Smrg	  ;;
18433dddc75Smrg	-L*)
18533dddc75Smrg	  func_cl_dashL "${1#-L}"
18633dddc75Smrg	  ;;
18733dddc75Smrg	-static)
18833dddc75Smrg	  shared=false
18933dddc75Smrg	  ;;
19033dddc75Smrg	-Wl,*)
19133dddc75Smrg	  arg=${1#-Wl,}
19233dddc75Smrg	  save_ifs="$IFS"; IFS=','
19333dddc75Smrg	  for flag in $arg; do
19433dddc75Smrg	    IFS="$save_ifs"
19533dddc75Smrg	    linker_opts="$linker_opts $flag"
19633dddc75Smrg	  done
19733dddc75Smrg	  IFS="$save_ifs"
19833dddc75Smrg	  ;;
19933dddc75Smrg	-Xlinker)
20033dddc75Smrg	  eat=1
20133dddc75Smrg	  linker_opts="$linker_opts $2"
20233dddc75Smrg	  ;;
20333dddc75Smrg	-*)
20433dddc75Smrg	  set x "$@" "$1"
20533dddc75Smrg	  shift
20633dddc75Smrg	  ;;
20733dddc75Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20833dddc75Smrg	  func_file_conv "$1"
20933dddc75Smrg	  set x "$@" -Tp"$file"
21033dddc75Smrg	  shift
21133dddc75Smrg	  ;;
21233dddc75Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21333dddc75Smrg	  func_file_conv "$1" mingw
21433dddc75Smrg	  set x "$@" "$file"
21533dddc75Smrg	  shift
21633dddc75Smrg	  ;;
21733dddc75Smrg	*)
21833dddc75Smrg	  set x "$@" "$1"
21933dddc75Smrg	  shift
22033dddc75Smrg	  ;;
22133dddc75Smrg      esac
22233dddc75Smrg    fi
22333dddc75Smrg    shift
22433dddc75Smrg  done
22533dddc75Smrg  if test -n "$linker_opts"; then
22633dddc75Smrg    linker_opts="-link$linker_opts"
22733dddc75Smrg  fi
22833dddc75Smrg  exec "$@" $linker_opts
22933dddc75Smrg  exit 1
23033dddc75Smrg}
23133dddc75Smrg
23233dddc75Smrgeat=
23333dddc75Smrg
23433dddc75Smrgcase $1 in
23533dddc75Smrg  '')
23633dddc75Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23733dddc75Smrg     exit 1;
23833dddc75Smrg     ;;
23933dddc75Smrg  -h | --h*)
24033dddc75Smrg    cat <<\EOF
24133dddc75SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24233dddc75Smrg
24333dddc75SmrgWrapper for compilers which do not understand '-c -o'.
24433dddc75SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24533dddc75Smrgarguments, and rename the output as expected.
24633dddc75Smrg
24733dddc75SmrgIf you are trying to build a whole package this is not the
24833dddc75Smrgright script to run: please start by reading the file 'INSTALL'.
24933dddc75Smrg
25033dddc75SmrgReport bugs to <bug-automake@gnu.org>.
25133dddc75SmrgEOF
25233dddc75Smrg    exit $?
25333dddc75Smrg    ;;
25433dddc75Smrg  -v | --v*)
25533dddc75Smrg    echo "compile $scriptversion"
25633dddc75Smrg    exit $?
25733dddc75Smrg    ;;
2587ed541caSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
2597ed541caSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26033dddc75Smrg    func_cl_wrapper "$@"      # Doesn't return...
26133dddc75Smrg    ;;
26233dddc75Smrgesac
26333dddc75Smrg
26433dddc75Smrgofile=
26533dddc75Smrgcfile=
26633dddc75Smrg
26733dddc75Smrgfor arg
26833dddc75Smrgdo
26933dddc75Smrg  if test -n "$eat"; then
27033dddc75Smrg    eat=
27133dddc75Smrg  else
27233dddc75Smrg    case $1 in
27333dddc75Smrg      -o)
27433dddc75Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27533dddc75Smrg	# So we strip '-o arg' only if arg is an object.
27633dddc75Smrg	eat=1
27733dddc75Smrg	case $2 in
27833dddc75Smrg	  *.o | *.obj)
27933dddc75Smrg	    ofile=$2
28033dddc75Smrg	    ;;
28133dddc75Smrg	  *)
28233dddc75Smrg	    set x "$@" -o "$2"
28333dddc75Smrg	    shift
28433dddc75Smrg	    ;;
28533dddc75Smrg	esac
28633dddc75Smrg	;;
28733dddc75Smrg      *.c)
28833dddc75Smrg	cfile=$1
28933dddc75Smrg	set x "$@" "$1"
29033dddc75Smrg	shift
29133dddc75Smrg	;;
29233dddc75Smrg      *)
29333dddc75Smrg	set x "$@" "$1"
29433dddc75Smrg	shift
29533dddc75Smrg	;;
29633dddc75Smrg    esac
29733dddc75Smrg  fi
29833dddc75Smrg  shift
29933dddc75Smrgdone
30033dddc75Smrg
30133dddc75Smrgif test -z "$ofile" || test -z "$cfile"; then
30233dddc75Smrg  # If no '-o' option was seen then we might have been invoked from a
30333dddc75Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30433dddc75Smrg  # normal compilation that the losing compiler can handle.  If no
30533dddc75Smrg  # '.c' file was seen then we are probably linking.  That is also
30633dddc75Smrg  # ok.
30733dddc75Smrg  exec "$@"
30833dddc75Smrgfi
30933dddc75Smrg
31033dddc75Smrg# Name of file we expect compiler to create.
31133dddc75Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31233dddc75Smrg
31333dddc75Smrg# Create the lock directory.
31433dddc75Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31533dddc75Smrg# that we are using for the .o file.  Also, base the name on the expected
31633dddc75Smrg# object file name, since that is what matters with a parallel build.
31733dddc75Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31833dddc75Smrgwhile true; do
31933dddc75Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32033dddc75Smrg    break
32133dddc75Smrg  fi
32233dddc75Smrg  sleep 1
32333dddc75Smrgdone
32433dddc75Smrg# FIXME: race condition here if user kills between mkdir and trap.
32533dddc75Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32633dddc75Smrg
32733dddc75Smrg# Run the compile.
32833dddc75Smrg"$@"
32933dddc75Smrgret=$?
33033dddc75Smrg
33133dddc75Smrgif test -f "$cofile"; then
33233dddc75Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33333dddc75Smrgelif test -f "${cofile}bj"; then
33433dddc75Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33533dddc75Smrgfi
33633dddc75Smrg
33733dddc75Smrgrmdir "$lockdir"
33833dddc75Smrgexit $ret
33933dddc75Smrg
34033dddc75Smrg# Local Variables:
34133dddc75Smrg# mode: shell-script
34233dddc75Smrg# sh-indentation: 2
3437ed541caSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34433dddc75Smrg# time-stamp-start: "scriptversion="
34533dddc75Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3467ed541caSmrg# time-stamp-time-zone: "UTC0"
34733dddc75Smrg# time-stamp-end: "; # UTC"
34833dddc75Smrg# End:
349