133c89af1Smrg#! /bin/sh
2ad47b356Smrg# Wrapper for compilers which do not understand '-c -o'.
333c89af1Smrg
4352bf44eSmrgscriptversion=2018-03-07.03; # UTC
533c89af1Smrg
6352bf44eSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
733c89af1Smrg# Written by Tom Tromey <tromey@cygnus.com>.
833c89af1Smrg#
933c89af1Smrg# This program is free software; you can redistribute it and/or modify
1033c89af1Smrg# it under the terms of the GNU General Public License as published by
1133c89af1Smrg# the Free Software Foundation; either version 2, or (at your option)
1233c89af1Smrg# any later version.
1333c89af1Smrg#
1433c89af1Smrg# This program is distributed in the hope that it will be useful,
1533c89af1Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1633c89af1Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1733c89af1Smrg# GNU General Public License for more details.
1833c89af1Smrg#
1933c89af1Smrg# You should have received a copy of the GNU General Public License
20352bf44eSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2133c89af1Smrg
2233c89af1Smrg# As a special exception to the GNU General Public License, if you
2333c89af1Smrg# distribute this file as part of a program that contains a
2433c89af1Smrg# configuration script generated by Autoconf, you may include it under
2533c89af1Smrg# the same distribution terms that you use for the rest of that program.
2633c89af1Smrg
2733c89af1Smrg# This file is maintained in Automake, please report
2833c89af1Smrg# bugs to <bug-automake@gnu.org> or send patches to
2933c89af1Smrg# <automake-patches@gnu.org>.
3033c89af1Smrg
31ad47b356Smrgnl='
32ad47b356Smrg'
33ad47b356Smrg
34ad47b356Smrg# We need space, tab and new line, in precisely that order.  Quoting is
35ad47b356Smrg# there to prevent tools from complaining about whitespace usage.
36ad47b356SmrgIFS=" ""	$nl"
37ad47b356Smrg
38ad47b356Smrgfile_conv=
39ad47b356Smrg
40ad47b356Smrg# func_file_conv build_file lazy
41ad47b356Smrg# Convert a $build file to $host form and store it in $file
42ad47b356Smrg# Currently only supports Windows hosts. If the determined conversion
43ad47b356Smrg# type is listed in (the comma separated) LAZY, no conversion will
44ad47b356Smrg# take place.
45ad47b356Smrgfunc_file_conv ()
46ad47b356Smrg{
47ad47b356Smrg  file=$1
48ad47b356Smrg  case $file in
49ad47b356Smrg    / | /[!/]*) # absolute file, and not a UNC file
50ad47b356Smrg      if test -z "$file_conv"; then
51ad47b356Smrg	# lazily determine how to convert abs files
52ad47b356Smrg	case `uname -s` in
53ad47b356Smrg	  MINGW*)
54ad47b356Smrg	    file_conv=mingw
55ad47b356Smrg	    ;;
56352bf44eSmrg	  CYGWIN* | MSYS*)
57ad47b356Smrg	    file_conv=cygwin
58ad47b356Smrg	    ;;
59ad47b356Smrg	  *)
60ad47b356Smrg	    file_conv=wine
61ad47b356Smrg	    ;;
62ad47b356Smrg	esac
63ad47b356Smrg      fi
64ad47b356Smrg      case $file_conv/,$2, in
65ad47b356Smrg	*,$file_conv,*)
66ad47b356Smrg	  ;;
67ad47b356Smrg	mingw/*)
68ad47b356Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69ad47b356Smrg	  ;;
70352bf44eSmrg	cygwin/* | msys/*)
71ad47b356Smrg	  file=`cygpath -m "$file" || echo "$file"`
72ad47b356Smrg	  ;;
73ad47b356Smrg	wine/*)
74ad47b356Smrg	  file=`winepath -w "$file" || echo "$file"`
75ad47b356Smrg	  ;;
76ad47b356Smrg      esac
77ad47b356Smrg      ;;
78ad47b356Smrg  esac
79ad47b356Smrg}
80ad47b356Smrg
81ad47b356Smrg# func_cl_dashL linkdir
82ad47b356Smrg# Make cl look for libraries in LINKDIR
83ad47b356Smrgfunc_cl_dashL ()
84ad47b356Smrg{
85ad47b356Smrg  func_file_conv "$1"
86ad47b356Smrg  if test -z "$lib_path"; then
87ad47b356Smrg    lib_path=$file
88ad47b356Smrg  else
89ad47b356Smrg    lib_path="$lib_path;$file"
90ad47b356Smrg  fi
91ad47b356Smrg  linker_opts="$linker_opts -LIBPATH:$file"
92ad47b356Smrg}
93ad47b356Smrg
94ad47b356Smrg# func_cl_dashl library
95ad47b356Smrg# Do a library search-path lookup for cl
96ad47b356Smrgfunc_cl_dashl ()
97ad47b356Smrg{
98ad47b356Smrg  lib=$1
99ad47b356Smrg  found=no
100ad47b356Smrg  save_IFS=$IFS
101ad47b356Smrg  IFS=';'
102ad47b356Smrg  for dir in $lib_path $LIB
103ad47b356Smrg  do
104ad47b356Smrg    IFS=$save_IFS
105ad47b356Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106ad47b356Smrg      found=yes
107ad47b356Smrg      lib=$dir/$lib.dll.lib
108ad47b356Smrg      break
109ad47b356Smrg    fi
110ad47b356Smrg    if test -f "$dir/$lib.lib"; then
111ad47b356Smrg      found=yes
112ad47b356Smrg      lib=$dir/$lib.lib
113ad47b356Smrg      break
114ad47b356Smrg    fi
115ad47b356Smrg    if test -f "$dir/lib$lib.a"; then
116ad47b356Smrg      found=yes
117ad47b356Smrg      lib=$dir/lib$lib.a
118ad47b356Smrg      break
119ad47b356Smrg    fi
120ad47b356Smrg  done
121ad47b356Smrg  IFS=$save_IFS
122ad47b356Smrg
123ad47b356Smrg  if test "$found" != yes; then
124ad47b356Smrg    lib=$lib.lib
125ad47b356Smrg  fi
126ad47b356Smrg}
127ad47b356Smrg
128ad47b356Smrg# func_cl_wrapper cl arg...
129ad47b356Smrg# Adjust compile command to suit cl
130ad47b356Smrgfunc_cl_wrapper ()
131ad47b356Smrg{
132ad47b356Smrg  # Assume a capable shell
133ad47b356Smrg  lib_path=
134ad47b356Smrg  shared=:
135ad47b356Smrg  linker_opts=
136ad47b356Smrg  for arg
137ad47b356Smrg  do
138ad47b356Smrg    if test -n "$eat"; then
139ad47b356Smrg      eat=
140ad47b356Smrg    else
141ad47b356Smrg      case $1 in
142ad47b356Smrg	-o)
143ad47b356Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144ad47b356Smrg	  eat=1
145ad47b356Smrg	  case $2 in
146ad47b356Smrg	    *.o | *.[oO][bB][jJ])
147ad47b356Smrg	      func_file_conv "$2"
148ad47b356Smrg	      set x "$@" -Fo"$file"
149ad47b356Smrg	      shift
150ad47b356Smrg	      ;;
151ad47b356Smrg	    *)
152ad47b356Smrg	      func_file_conv "$2"
153ad47b356Smrg	      set x "$@" -Fe"$file"
154ad47b356Smrg	      shift
155ad47b356Smrg	      ;;
156ad47b356Smrg	  esac
157ad47b356Smrg	  ;;
158ad47b356Smrg	-I)
159ad47b356Smrg	  eat=1
160ad47b356Smrg	  func_file_conv "$2" mingw
161ad47b356Smrg	  set x "$@" -I"$file"
162ad47b356Smrg	  shift
163ad47b356Smrg	  ;;
164ad47b356Smrg	-I*)
165ad47b356Smrg	  func_file_conv "${1#-I}" mingw
166ad47b356Smrg	  set x "$@" -I"$file"
167ad47b356Smrg	  shift
168ad47b356Smrg	  ;;
169ad47b356Smrg	-l)
170ad47b356Smrg	  eat=1
171ad47b356Smrg	  func_cl_dashl "$2"
172ad47b356Smrg	  set x "$@" "$lib"
173ad47b356Smrg	  shift
174ad47b356Smrg	  ;;
175ad47b356Smrg	-l*)
176ad47b356Smrg	  func_cl_dashl "${1#-l}"
177ad47b356Smrg	  set x "$@" "$lib"
178ad47b356Smrg	  shift
179ad47b356Smrg	  ;;
180ad47b356Smrg	-L)
181ad47b356Smrg	  eat=1
182ad47b356Smrg	  func_cl_dashL "$2"
183ad47b356Smrg	  ;;
184ad47b356Smrg	-L*)
185ad47b356Smrg	  func_cl_dashL "${1#-L}"
186ad47b356Smrg	  ;;
187ad47b356Smrg	-static)
188ad47b356Smrg	  shared=false
189ad47b356Smrg	  ;;
190ad47b356Smrg	-Wl,*)
191ad47b356Smrg	  arg=${1#-Wl,}
192ad47b356Smrg	  save_ifs="$IFS"; IFS=','
193ad47b356Smrg	  for flag in $arg; do
194ad47b356Smrg	    IFS="$save_ifs"
195ad47b356Smrg	    linker_opts="$linker_opts $flag"
196ad47b356Smrg	  done
197ad47b356Smrg	  IFS="$save_ifs"
198ad47b356Smrg	  ;;
199ad47b356Smrg	-Xlinker)
200ad47b356Smrg	  eat=1
201ad47b356Smrg	  linker_opts="$linker_opts $2"
202ad47b356Smrg	  ;;
203ad47b356Smrg	-*)
204ad47b356Smrg	  set x "$@" "$1"
205ad47b356Smrg	  shift
206ad47b356Smrg	  ;;
207ad47b356Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208ad47b356Smrg	  func_file_conv "$1"
209ad47b356Smrg	  set x "$@" -Tp"$file"
210ad47b356Smrg	  shift
211ad47b356Smrg	  ;;
212ad47b356Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213ad47b356Smrg	  func_file_conv "$1" mingw
214ad47b356Smrg	  set x "$@" "$file"
215ad47b356Smrg	  shift
216ad47b356Smrg	  ;;
217ad47b356Smrg	*)
218ad47b356Smrg	  set x "$@" "$1"
219ad47b356Smrg	  shift
220ad47b356Smrg	  ;;
221ad47b356Smrg      esac
222ad47b356Smrg    fi
223ad47b356Smrg    shift
224ad47b356Smrg  done
225ad47b356Smrg  if test -n "$linker_opts"; then
226ad47b356Smrg    linker_opts="-link$linker_opts"
227ad47b356Smrg  fi
228ad47b356Smrg  exec "$@" $linker_opts
229ad47b356Smrg  exit 1
230ad47b356Smrg}
231ad47b356Smrg
232ad47b356Smrgeat=
233ad47b356Smrg
23433c89af1Smrgcase $1 in
23533c89af1Smrg  '')
236ad47b356Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23733c89af1Smrg     exit 1;
23833c89af1Smrg     ;;
23933c89af1Smrg  -h | --h*)
24033c89af1Smrg    cat <<\EOF
24133c89af1SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24233c89af1Smrg
243ad47b356SmrgWrapper for compilers which do not understand '-c -o'.
244ad47b356SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24533c89af1Smrgarguments, and rename the output as expected.
24633c89af1Smrg
24733c89af1SmrgIf you are trying to build a whole package this is not the
248ad47b356Smrgright script to run: please start by reading the file 'INSTALL'.
24933c89af1Smrg
25033c89af1SmrgReport bugs to <bug-automake@gnu.org>.
25133c89af1SmrgEOF
25233c89af1Smrg    exit $?
25333c89af1Smrg    ;;
25433c89af1Smrg  -v | --v*)
25533c89af1Smrg    echo "compile $scriptversion"
25633c89af1Smrg    exit $?
25733c89af1Smrg    ;;
258352bf44eSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259352bf44eSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260ad47b356Smrg    func_cl_wrapper "$@"      # Doesn't return...
261ad47b356Smrg    ;;
26233c89af1Smrgesac
26333c89af1Smrg
26433c89af1Smrgofile=
26533c89af1Smrgcfile=
26633c89af1Smrg
26733c89af1Smrgfor arg
26833c89af1Smrgdo
26933c89af1Smrg  if test -n "$eat"; then
27033c89af1Smrg    eat=
27133c89af1Smrg  else
27233c89af1Smrg    case $1 in
27333c89af1Smrg      -o)
274ad47b356Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275ad47b356Smrg	# So we strip '-o arg' only if arg is an object.
27633c89af1Smrg	eat=1
27733c89af1Smrg	case $2 in
27833c89af1Smrg	  *.o | *.obj)
27933c89af1Smrg	    ofile=$2
28033c89af1Smrg	    ;;
28133c89af1Smrg	  *)
28233c89af1Smrg	    set x "$@" -o "$2"
28333c89af1Smrg	    shift
28433c89af1Smrg	    ;;
28533c89af1Smrg	esac
28633c89af1Smrg	;;
28733c89af1Smrg      *.c)
28833c89af1Smrg	cfile=$1
28933c89af1Smrg	set x "$@" "$1"
29033c89af1Smrg	shift
29133c89af1Smrg	;;
29233c89af1Smrg      *)
29333c89af1Smrg	set x "$@" "$1"
29433c89af1Smrg	shift
29533c89af1Smrg	;;
29633c89af1Smrg    esac
29733c89af1Smrg  fi
29833c89af1Smrg  shift
29933c89af1Smrgdone
30033c89af1Smrg
30133c89af1Smrgif test -z "$ofile" || test -z "$cfile"; then
302ad47b356Smrg  # If no '-o' option was seen then we might have been invoked from a
30333c89af1Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30433c89af1Smrg  # normal compilation that the losing compiler can handle.  If no
305ad47b356Smrg  # '.c' file was seen then we are probably linking.  That is also
30633c89af1Smrg  # ok.
30733c89af1Smrg  exec "$@"
30833c89af1Smrgfi
30933c89af1Smrg
31033c89af1Smrg# Name of file we expect compiler to create.
311ad47b356Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31233c89af1Smrg
31333c89af1Smrg# Create the lock directory.
314ad47b356Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31533c89af1Smrg# that we are using for the .o file.  Also, base the name on the expected
31633c89af1Smrg# object file name, since that is what matters with a parallel build.
317ad47b356Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
31833c89af1Smrgwhile true; do
31933c89af1Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32033c89af1Smrg    break
32133c89af1Smrg  fi
32233c89af1Smrg  sleep 1
32333c89af1Smrgdone
32433c89af1Smrg# FIXME: race condition here if user kills between mkdir and trap.
32533c89af1Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32633c89af1Smrg
32733c89af1Smrg# Run the compile.
32833c89af1Smrg"$@"
32933c89af1Smrgret=$?
33033c89af1Smrg
33133c89af1Smrgif test -f "$cofile"; then
332ad47b356Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33333c89af1Smrgelif test -f "${cofile}bj"; then
334ad47b356Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33533c89af1Smrgfi
33633c89af1Smrg
33733c89af1Smrgrmdir "$lockdir"
33833c89af1Smrgexit $ret
33933c89af1Smrg
34033c89af1Smrg# Local Variables:
34133c89af1Smrg# mode: shell-script
34233c89af1Smrg# sh-indentation: 2
343352bf44eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34433c89af1Smrg# time-stamp-start: "scriptversion="
34533c89af1Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346352bf44eSmrg# time-stamp-time-zone: "UTC0"
347ad47b356Smrg# time-stamp-end: "; # UTC"
34833c89af1Smrg# End:
349