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