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