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