1a4f78defSmrg#! /bin/sh
2a4f78defSmrg# Wrapper for compilers which do not understand '-c -o'.
3a4f78defSmrg
4fb570538Smrgscriptversion=2018-03-07.03; # UTC
5a4f78defSmrg
6fb570538Smrg# Copyright (C) 1999-2018 Free Software Foundation, Inc.
7a4f78defSmrg# Written by Tom Tromey <tromey@cygnus.com>.
8a4f78defSmrg#
9a4f78defSmrg# This program is free software; you can redistribute it and/or modify
10a4f78defSmrg# it under the terms of the GNU General Public License as published by
11a4f78defSmrg# the Free Software Foundation; either version 2, or (at your option)
12a4f78defSmrg# any later version.
13a4f78defSmrg#
14a4f78defSmrg# This program is distributed in the hope that it will be useful,
15a4f78defSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
16a4f78defSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17a4f78defSmrg# GNU General Public License for more details.
18a4f78defSmrg#
19a4f78defSmrg# You should have received a copy of the GNU General Public License
20fb570538Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21a4f78defSmrg
22a4f78defSmrg# As a special exception to the GNU General Public License, if you
23a4f78defSmrg# distribute this file as part of a program that contains a
24a4f78defSmrg# configuration script generated by Autoconf, you may include it under
25a4f78defSmrg# the same distribution terms that you use for the rest of that program.
26a4f78defSmrg
27a4f78defSmrg# This file is maintained in Automake, please report
28a4f78defSmrg# bugs to <bug-automake@gnu.org> or send patches to
29a4f78defSmrg# <automake-patches@gnu.org>.
30a4f78defSmrg
31a4f78defSmrgnl='
32a4f78defSmrg'
33a4f78defSmrg
34a4f78defSmrg# We need space, tab and new line, in precisely that order.  Quoting is
35a4f78defSmrg# there to prevent tools from complaining about whitespace usage.
36a4f78defSmrgIFS=" ""	$nl"
37a4f78defSmrg
38a4f78defSmrgfile_conv=
39a4f78defSmrg
40a4f78defSmrg# func_file_conv build_file lazy
41a4f78defSmrg# Convert a $build file to $host form and store it in $file
42a4f78defSmrg# Currently only supports Windows hosts. If the determined conversion
43a4f78defSmrg# type is listed in (the comma separated) LAZY, no conversion will
44a4f78defSmrg# take place.
45a4f78defSmrgfunc_file_conv ()
46a4f78defSmrg{
47a4f78defSmrg  file=$1
48a4f78defSmrg  case $file in
49a4f78defSmrg    / | /[!/]*) # absolute file, and not a UNC file
50a4f78defSmrg      if test -z "$file_conv"; then
51a4f78defSmrg	# lazily determine how to convert abs files
52a4f78defSmrg	case `uname -s` in
53a4f78defSmrg	  MINGW*)
54a4f78defSmrg	    file_conv=mingw
55a4f78defSmrg	    ;;
56a4f78defSmrg	  CYGWIN*)
57a4f78defSmrg	    file_conv=cygwin
58a4f78defSmrg	    ;;
59a4f78defSmrg	  *)
60a4f78defSmrg	    file_conv=wine
61a4f78defSmrg	    ;;
62a4f78defSmrg	esac
63a4f78defSmrg      fi
64a4f78defSmrg      case $file_conv/,$2, in
65a4f78defSmrg	*,$file_conv,*)
66a4f78defSmrg	  ;;
67a4f78defSmrg	mingw/*)
68a4f78defSmrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69a4f78defSmrg	  ;;
70a4f78defSmrg	cygwin/*)
71a4f78defSmrg	  file=`cygpath -m "$file" || echo "$file"`
72a4f78defSmrg	  ;;
73a4f78defSmrg	wine/*)
74a4f78defSmrg	  file=`winepath -w "$file" || echo "$file"`
75a4f78defSmrg	  ;;
76a4f78defSmrg      esac
77a4f78defSmrg      ;;
78a4f78defSmrg  esac
79a4f78defSmrg}
80a4f78defSmrg
81a4f78defSmrg# func_cl_dashL linkdir
82a4f78defSmrg# Make cl look for libraries in LINKDIR
83a4f78defSmrgfunc_cl_dashL ()
84a4f78defSmrg{
85a4f78defSmrg  func_file_conv "$1"
86a4f78defSmrg  if test -z "$lib_path"; then
87a4f78defSmrg    lib_path=$file
88a4f78defSmrg  else
89a4f78defSmrg    lib_path="$lib_path;$file"
90a4f78defSmrg  fi
91a4f78defSmrg  linker_opts="$linker_opts -LIBPATH:$file"
92a4f78defSmrg}
93a4f78defSmrg
94a4f78defSmrg# func_cl_dashl library
95a4f78defSmrg# Do a library search-path lookup for cl
96a4f78defSmrgfunc_cl_dashl ()
97a4f78defSmrg{
98a4f78defSmrg  lib=$1
99a4f78defSmrg  found=no
100a4f78defSmrg  save_IFS=$IFS
101a4f78defSmrg  IFS=';'
102a4f78defSmrg  for dir in $lib_path $LIB
103a4f78defSmrg  do
104a4f78defSmrg    IFS=$save_IFS
105a4f78defSmrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106a4f78defSmrg      found=yes
107a4f78defSmrg      lib=$dir/$lib.dll.lib
108a4f78defSmrg      break
109a4f78defSmrg    fi
110a4f78defSmrg    if test -f "$dir/$lib.lib"; then
111a4f78defSmrg      found=yes
112a4f78defSmrg      lib=$dir/$lib.lib
113a4f78defSmrg      break
114a4f78defSmrg    fi
115a4f78defSmrg    if test -f "$dir/lib$lib.a"; then
116a4f78defSmrg      found=yes
117a4f78defSmrg      lib=$dir/lib$lib.a
118a4f78defSmrg      break
119a4f78defSmrg    fi
120a4f78defSmrg  done
121a4f78defSmrg  IFS=$save_IFS
122a4f78defSmrg
123a4f78defSmrg  if test "$found" != yes; then
124a4f78defSmrg    lib=$lib.lib
125a4f78defSmrg  fi
126a4f78defSmrg}
127a4f78defSmrg
128a4f78defSmrg# func_cl_wrapper cl arg...
129a4f78defSmrg# Adjust compile command to suit cl
130a4f78defSmrgfunc_cl_wrapper ()
131a4f78defSmrg{
132a4f78defSmrg  # Assume a capable shell
133a4f78defSmrg  lib_path=
134a4f78defSmrg  shared=:
135a4f78defSmrg  linker_opts=
136a4f78defSmrg  for arg
137a4f78defSmrg  do
138a4f78defSmrg    if test -n "$eat"; then
139a4f78defSmrg      eat=
140a4f78defSmrg    else
141a4f78defSmrg      case $1 in
142a4f78defSmrg	-o)
143a4f78defSmrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144a4f78defSmrg	  eat=1
145a4f78defSmrg	  case $2 in
146a4f78defSmrg	    *.o | *.[oO][bB][jJ])
147a4f78defSmrg	      func_file_conv "$2"
148a4f78defSmrg	      set x "$@" -Fo"$file"
149a4f78defSmrg	      shift
150a4f78defSmrg	      ;;
151a4f78defSmrg	    *)
152a4f78defSmrg	      func_file_conv "$2"
153a4f78defSmrg	      set x "$@" -Fe"$file"
154a4f78defSmrg	      shift
155a4f78defSmrg	      ;;
156a4f78defSmrg	  esac
157a4f78defSmrg	  ;;
158a4f78defSmrg	-I)
159a4f78defSmrg	  eat=1
160a4f78defSmrg	  func_file_conv "$2" mingw
161a4f78defSmrg	  set x "$@" -I"$file"
162a4f78defSmrg	  shift
163a4f78defSmrg	  ;;
164a4f78defSmrg	-I*)
165a4f78defSmrg	  func_file_conv "${1#-I}" mingw
166a4f78defSmrg	  set x "$@" -I"$file"
167a4f78defSmrg	  shift
168a4f78defSmrg	  ;;
169a4f78defSmrg	-l)
170a4f78defSmrg	  eat=1
171a4f78defSmrg	  func_cl_dashl "$2"
172a4f78defSmrg	  set x "$@" "$lib"
173a4f78defSmrg	  shift
174a4f78defSmrg	  ;;
175a4f78defSmrg	-l*)
176a4f78defSmrg	  func_cl_dashl "${1#-l}"
177a4f78defSmrg	  set x "$@" "$lib"
178a4f78defSmrg	  shift
179a4f78defSmrg	  ;;
180a4f78defSmrg	-L)
181a4f78defSmrg	  eat=1
182a4f78defSmrg	  func_cl_dashL "$2"
183a4f78defSmrg	  ;;
184a4f78defSmrg	-L*)
185a4f78defSmrg	  func_cl_dashL "${1#-L}"
186a4f78defSmrg	  ;;
187a4f78defSmrg	-static)
188a4f78defSmrg	  shared=false
189a4f78defSmrg	  ;;
190a4f78defSmrg	-Wl,*)
191a4f78defSmrg	  arg=${1#-Wl,}
192a4f78defSmrg	  save_ifs="$IFS"; IFS=','
193a4f78defSmrg	  for flag in $arg; do
194a4f78defSmrg	    IFS="$save_ifs"
195a4f78defSmrg	    linker_opts="$linker_opts $flag"
196a4f78defSmrg	  done
197a4f78defSmrg	  IFS="$save_ifs"
198a4f78defSmrg	  ;;
199a4f78defSmrg	-Xlinker)
200a4f78defSmrg	  eat=1
201a4f78defSmrg	  linker_opts="$linker_opts $2"
202a4f78defSmrg	  ;;
203a4f78defSmrg	-*)
204a4f78defSmrg	  set x "$@" "$1"
205a4f78defSmrg	  shift
206a4f78defSmrg	  ;;
207a4f78defSmrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208a4f78defSmrg	  func_file_conv "$1"
209a4f78defSmrg	  set x "$@" -Tp"$file"
210a4f78defSmrg	  shift
211a4f78defSmrg	  ;;
212a4f78defSmrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213a4f78defSmrg	  func_file_conv "$1" mingw
214a4f78defSmrg	  set x "$@" "$file"
215a4f78defSmrg	  shift
216a4f78defSmrg	  ;;
217a4f78defSmrg	*)
218a4f78defSmrg	  set x "$@" "$1"
219a4f78defSmrg	  shift
220a4f78defSmrg	  ;;
221a4f78defSmrg      esac
222a4f78defSmrg    fi
223a4f78defSmrg    shift
224a4f78defSmrg  done
225a4f78defSmrg  if test -n "$linker_opts"; then
226a4f78defSmrg    linker_opts="-link$linker_opts"
227a4f78defSmrg  fi
228a4f78defSmrg  exec "$@" $linker_opts
229a4f78defSmrg  exit 1
230a4f78defSmrg}
231a4f78defSmrg
232a4f78defSmrgeat=
233a4f78defSmrg
234a4f78defSmrgcase $1 in
235a4f78defSmrg  '')
236a4f78defSmrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237a4f78defSmrg     exit 1;
238a4f78defSmrg     ;;
239a4f78defSmrg  -h | --h*)
240a4f78defSmrg    cat <<\EOF
241a4f78defSmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
242a4f78defSmrg
243a4f78defSmrgWrapper for compilers which do not understand '-c -o'.
244a4f78defSmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245a4f78defSmrgarguments, and rename the output as expected.
246a4f78defSmrg
247a4f78defSmrgIf you are trying to build a whole package this is not the
248a4f78defSmrgright script to run: please start by reading the file 'INSTALL'.
249a4f78defSmrg
250a4f78defSmrgReport bugs to <bug-automake@gnu.org>.
251a4f78defSmrgEOF
252a4f78defSmrg    exit $?
253a4f78defSmrg    ;;
254a4f78defSmrg  -v | --v*)
255a4f78defSmrg    echo "compile $scriptversion"
256a4f78defSmrg    exit $?
257a4f78defSmrg    ;;
258fb570538Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259fb570538Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260a4f78defSmrg    func_cl_wrapper "$@"      # Doesn't return...
261a4f78defSmrg    ;;
262a4f78defSmrgesac
263a4f78defSmrg
264a4f78defSmrgofile=
265a4f78defSmrgcfile=
266a4f78defSmrg
267a4f78defSmrgfor arg
268a4f78defSmrgdo
269a4f78defSmrg  if test -n "$eat"; then
270a4f78defSmrg    eat=
271a4f78defSmrg  else
272a4f78defSmrg    case $1 in
273a4f78defSmrg      -o)
274a4f78defSmrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275a4f78defSmrg	# So we strip '-o arg' only if arg is an object.
276a4f78defSmrg	eat=1
277a4f78defSmrg	case $2 in
278a4f78defSmrg	  *.o | *.obj)
279a4f78defSmrg	    ofile=$2
280a4f78defSmrg	    ;;
281a4f78defSmrg	  *)
282a4f78defSmrg	    set x "$@" -o "$2"
283a4f78defSmrg	    shift
284a4f78defSmrg	    ;;
285a4f78defSmrg	esac
286a4f78defSmrg	;;
287a4f78defSmrg      *.c)
288a4f78defSmrg	cfile=$1
289a4f78defSmrg	set x "$@" "$1"
290a4f78defSmrg	shift
291a4f78defSmrg	;;
292a4f78defSmrg      *)
293a4f78defSmrg	set x "$@" "$1"
294a4f78defSmrg	shift
295a4f78defSmrg	;;
296a4f78defSmrg    esac
297a4f78defSmrg  fi
298a4f78defSmrg  shift
299a4f78defSmrgdone
300a4f78defSmrg
301a4f78defSmrgif test -z "$ofile" || test -z "$cfile"; then
302a4f78defSmrg  # If no '-o' option was seen then we might have been invoked from a
303a4f78defSmrg  # pattern rule where we don't need one.  That is ok -- this is a
304a4f78defSmrg  # normal compilation that the losing compiler can handle.  If no
305a4f78defSmrg  # '.c' file was seen then we are probably linking.  That is also
306a4f78defSmrg  # ok.
307a4f78defSmrg  exec "$@"
308a4f78defSmrgfi
309a4f78defSmrg
310a4f78defSmrg# Name of file we expect compiler to create.
311a4f78defSmrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312a4f78defSmrg
313a4f78defSmrg# Create the lock directory.
314a4f78defSmrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315a4f78defSmrg# that we are using for the .o file.  Also, base the name on the expected
316a4f78defSmrg# object file name, since that is what matters with a parallel build.
317a4f78defSmrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318a4f78defSmrgwhile true; do
319a4f78defSmrg  if mkdir "$lockdir" >/dev/null 2>&1; then
320a4f78defSmrg    break
321a4f78defSmrg  fi
322a4f78defSmrg  sleep 1
323a4f78defSmrgdone
324a4f78defSmrg# FIXME: race condition here if user kills between mkdir and trap.
325a4f78defSmrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
326a4f78defSmrg
327a4f78defSmrg# Run the compile.
328a4f78defSmrg"$@"
329a4f78defSmrgret=$?
330a4f78defSmrg
331a4f78defSmrgif test -f "$cofile"; then
332a4f78defSmrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333a4f78defSmrgelif test -f "${cofile}bj"; then
334a4f78defSmrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335a4f78defSmrgfi
336a4f78defSmrg
337a4f78defSmrgrmdir "$lockdir"
338a4f78defSmrgexit $ret
339a4f78defSmrg
340a4f78defSmrg# Local Variables:
341a4f78defSmrg# mode: shell-script
342a4f78defSmrg# sh-indentation: 2
343fb570538Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
344a4f78defSmrg# time-stamp-start: "scriptversion="
345a4f78defSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346fb570538Smrg# time-stamp-time-zone: "UTC0"
347a4f78defSmrg# time-stamp-end: "; # UTC"
348a4f78defSmrg# End:
349