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