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