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