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