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