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