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