1174832Sdes#! /bin/sh
2255376Sdes# Wrapper for compilers which do not understand '-c -o'.
3174832Sdes
4348980Sdesscriptversion=2018-03-07.03; # UTC
5174832Sdes
6348980Sdes# Copyright (C) 1999-2018 Free Software Foundation, Inc.
7174832Sdes# Written by Tom Tromey <tromey@cygnus.com>.
8174832Sdes#
9174832Sdes# This program is free software; you can redistribute it and/or modify
10174832Sdes# it under the terms of the GNU General Public License as published by
11174832Sdes# the Free Software Foundation; either version 2, or (at your option)
12174832Sdes# any later version.
13174832Sdes#
14174832Sdes# This program is distributed in the hope that it will be useful,
15174832Sdes# but WITHOUT ANY WARRANTY; without even the implied warranty of
16174832Sdes# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17174832Sdes# GNU General Public License for more details.
18174832Sdes#
19174832Sdes# You should have received a copy of the GNU General Public License
20348980Sdes# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21174832Sdes
22174832Sdes# As a special exception to the GNU General Public License, if you
23174832Sdes# distribute this file as part of a program that contains a
24174832Sdes# configuration script generated by Autoconf, you may include it under
25174832Sdes# the same distribution terms that you use for the rest of that program.
26174832Sdes
27174832Sdes# This file is maintained in Automake, please report
28174832Sdes# bugs to <bug-automake@gnu.org> or send patches to
29174832Sdes# <automake-patches@gnu.org>.
30174832Sdes
31255376Sdesnl='
32255376Sdes'
33255376Sdes
34255376Sdes# We need space, tab and new line, in precisely that order.  Quoting is
35255376Sdes# there to prevent tools from complaining about whitespace usage.
36255376SdesIFS=" ""	$nl"
37255376Sdes
38255376Sdesfile_conv=
39255376Sdes
40255376Sdes# func_file_conv build_file lazy
41255376Sdes# Convert a $build file to $host form and store it in $file
42255376Sdes# Currently only supports Windows hosts. If the determined conversion
43255376Sdes# type is listed in (the comma separated) LAZY, no conversion will
44255376Sdes# take place.
45255376Sdesfunc_file_conv ()
46255376Sdes{
47255376Sdes  file=$1
48255376Sdes  case $file in
49255376Sdes    / | /[!/]*) # absolute file, and not a UNC file
50255376Sdes      if test -z "$file_conv"; then
51255376Sdes	# lazily determine how to convert abs files
52255376Sdes	case `uname -s` in
53255376Sdes	  MINGW*)
54255376Sdes	    file_conv=mingw
55255376Sdes	    ;;
56255376Sdes	  CYGWIN*)
57255376Sdes	    file_conv=cygwin
58255376Sdes	    ;;
59255376Sdes	  *)
60255376Sdes	    file_conv=wine
61255376Sdes	    ;;
62255376Sdes	esac
63255376Sdes      fi
64255376Sdes      case $file_conv/,$2, in
65255376Sdes	*,$file_conv,*)
66255376Sdes	  ;;
67255376Sdes	mingw/*)
68255376Sdes	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69255376Sdes	  ;;
70255376Sdes	cygwin/*)
71255376Sdes	  file=`cygpath -m "$file" || echo "$file"`
72255376Sdes	  ;;
73255376Sdes	wine/*)
74255376Sdes	  file=`winepath -w "$file" || echo "$file"`
75255376Sdes	  ;;
76255376Sdes      esac
77255376Sdes      ;;
78255376Sdes  esac
79255376Sdes}
80255376Sdes
81255376Sdes# func_cl_dashL linkdir
82255376Sdes# Make cl look for libraries in LINKDIR
83255376Sdesfunc_cl_dashL ()
84255376Sdes{
85255376Sdes  func_file_conv "$1"
86255376Sdes  if test -z "$lib_path"; then
87255376Sdes    lib_path=$file
88255376Sdes  else
89255376Sdes    lib_path="$lib_path;$file"
90255376Sdes  fi
91255376Sdes  linker_opts="$linker_opts -LIBPATH:$file"
92255376Sdes}
93255376Sdes
94255376Sdes# func_cl_dashl library
95255376Sdes# Do a library search-path lookup for cl
96255376Sdesfunc_cl_dashl ()
97255376Sdes{
98255376Sdes  lib=$1
99255376Sdes  found=no
100255376Sdes  save_IFS=$IFS
101255376Sdes  IFS=';'
102255376Sdes  for dir in $lib_path $LIB
103255376Sdes  do
104255376Sdes    IFS=$save_IFS
105255376Sdes    if $shared && test -f "$dir/$lib.dll.lib"; then
106255376Sdes      found=yes
107255376Sdes      lib=$dir/$lib.dll.lib
108255376Sdes      break
109255376Sdes    fi
110255376Sdes    if test -f "$dir/$lib.lib"; then
111255376Sdes      found=yes
112255376Sdes      lib=$dir/$lib.lib
113255376Sdes      break
114255376Sdes    fi
115255376Sdes    if test -f "$dir/lib$lib.a"; then
116255376Sdes      found=yes
117255376Sdes      lib=$dir/lib$lib.a
118255376Sdes      break
119255376Sdes    fi
120255376Sdes  done
121255376Sdes  IFS=$save_IFS
122255376Sdes
123255376Sdes  if test "$found" != yes; then
124255376Sdes    lib=$lib.lib
125255376Sdes  fi
126255376Sdes}
127255376Sdes
128255376Sdes# func_cl_wrapper cl arg...
129255376Sdes# Adjust compile command to suit cl
130255376Sdesfunc_cl_wrapper ()
131255376Sdes{
132255376Sdes  # Assume a capable shell
133255376Sdes  lib_path=
134255376Sdes  shared=:
135255376Sdes  linker_opts=
136255376Sdes  for arg
137255376Sdes  do
138255376Sdes    if test -n "$eat"; then
139255376Sdes      eat=
140255376Sdes    else
141255376Sdes      case $1 in
142255376Sdes	-o)
143255376Sdes	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144255376Sdes	  eat=1
145255376Sdes	  case $2 in
146255376Sdes	    *.o | *.[oO][bB][jJ])
147255376Sdes	      func_file_conv "$2"
148255376Sdes	      set x "$@" -Fo"$file"
149255376Sdes	      shift
150255376Sdes	      ;;
151255376Sdes	    *)
152255376Sdes	      func_file_conv "$2"
153255376Sdes	      set x "$@" -Fe"$file"
154255376Sdes	      shift
155255376Sdes	      ;;
156255376Sdes	  esac
157255376Sdes	  ;;
158255376Sdes	-I)
159255376Sdes	  eat=1
160255376Sdes	  func_file_conv "$2" mingw
161255376Sdes	  set x "$@" -I"$file"
162255376Sdes	  shift
163255376Sdes	  ;;
164255376Sdes	-I*)
165255376Sdes	  func_file_conv "${1#-I}" mingw
166255376Sdes	  set x "$@" -I"$file"
167255376Sdes	  shift
168255376Sdes	  ;;
169255376Sdes	-l)
170255376Sdes	  eat=1
171255376Sdes	  func_cl_dashl "$2"
172255376Sdes	  set x "$@" "$lib"
173255376Sdes	  shift
174255376Sdes	  ;;
175255376Sdes	-l*)
176255376Sdes	  func_cl_dashl "${1#-l}"
177255376Sdes	  set x "$@" "$lib"
178255376Sdes	  shift
179255376Sdes	  ;;
180255376Sdes	-L)
181255376Sdes	  eat=1
182255376Sdes	  func_cl_dashL "$2"
183255376Sdes	  ;;
184255376Sdes	-L*)
185255376Sdes	  func_cl_dashL "${1#-L}"
186255376Sdes	  ;;
187255376Sdes	-static)
188255376Sdes	  shared=false
189255376Sdes	  ;;
190255376Sdes	-Wl,*)
191255376Sdes	  arg=${1#-Wl,}
192255376Sdes	  save_ifs="$IFS"; IFS=','
193255376Sdes	  for flag in $arg; do
194255376Sdes	    IFS="$save_ifs"
195255376Sdes	    linker_opts="$linker_opts $flag"
196255376Sdes	  done
197255376Sdes	  IFS="$save_ifs"
198255376Sdes	  ;;
199255376Sdes	-Xlinker)
200255376Sdes	  eat=1
201255376Sdes	  linker_opts="$linker_opts $2"
202255376Sdes	  ;;
203255376Sdes	-*)
204255376Sdes	  set x "$@" "$1"
205255376Sdes	  shift
206255376Sdes	  ;;
207255376Sdes	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208255376Sdes	  func_file_conv "$1"
209255376Sdes	  set x "$@" -Tp"$file"
210255376Sdes	  shift
211255376Sdes	  ;;
212255376Sdes	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213255376Sdes	  func_file_conv "$1" mingw
214255376Sdes	  set x "$@" "$file"
215255376Sdes	  shift
216255376Sdes	  ;;
217255376Sdes	*)
218255376Sdes	  set x "$@" "$1"
219255376Sdes	  shift
220255376Sdes	  ;;
221255376Sdes      esac
222255376Sdes    fi
223255376Sdes    shift
224255376Sdes  done
225255376Sdes  if test -n "$linker_opts"; then
226255376Sdes    linker_opts="-link$linker_opts"
227255376Sdes  fi
228255376Sdes  exec "$@" $linker_opts
229255376Sdes  exit 1
230255376Sdes}
231255376Sdes
232255376Sdeseat=
233255376Sdes
234174832Sdescase $1 in
235174832Sdes  '')
236255376Sdes     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237174832Sdes     exit 1;
238174832Sdes     ;;
239174832Sdes  -h | --h*)
240174832Sdes    cat <<\EOF
241174832SdesUsage: compile [--help] [--version] PROGRAM [ARGS]
242174832Sdes
243255376SdesWrapper for compilers which do not understand '-c -o'.
244255376SdesRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245174832Sdesarguments, and rename the output as expected.
246174832Sdes
247174832SdesIf you are trying to build a whole package this is not the
248255376Sdesright script to run: please start by reading the file 'INSTALL'.
249174832Sdes
250174832SdesReport bugs to <bug-automake@gnu.org>.
251174832SdesEOF
252174832Sdes    exit $?
253174832Sdes    ;;
254174832Sdes  -v | --v*)
255174832Sdes    echo "compile $scriptversion"
256174832Sdes    exit $?
257174832Sdes    ;;
258348980Sdes  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259348980Sdes  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260255376Sdes    func_cl_wrapper "$@"      # Doesn't return...
261255376Sdes    ;;
262174832Sdesesac
263174832Sdes
264174832Sdesofile=
265174832Sdescfile=
266174832Sdes
267174832Sdesfor arg
268174832Sdesdo
269174832Sdes  if test -n "$eat"; then
270174832Sdes    eat=
271174832Sdes  else
272174832Sdes    case $1 in
273174832Sdes      -o)
274255376Sdes	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275255376Sdes	# So we strip '-o arg' only if arg is an object.
276174832Sdes	eat=1
277174832Sdes	case $2 in
278174832Sdes	  *.o | *.obj)
279174832Sdes	    ofile=$2
280174832Sdes	    ;;
281174832Sdes	  *)
282174832Sdes	    set x "$@" -o "$2"
283174832Sdes	    shift
284174832Sdes	    ;;
285174832Sdes	esac
286174832Sdes	;;
287174832Sdes      *.c)
288174832Sdes	cfile=$1
289174832Sdes	set x "$@" "$1"
290174832Sdes	shift
291174832Sdes	;;
292174832Sdes      *)
293174832Sdes	set x "$@" "$1"
294174832Sdes	shift
295174832Sdes	;;
296174832Sdes    esac
297174832Sdes  fi
298174832Sdes  shift
299174832Sdesdone
300174832Sdes
301174832Sdesif test -z "$ofile" || test -z "$cfile"; then
302255376Sdes  # If no '-o' option was seen then we might have been invoked from a
303174832Sdes  # pattern rule where we don't need one.  That is ok -- this is a
304174832Sdes  # normal compilation that the losing compiler can handle.  If no
305255376Sdes  # '.c' file was seen then we are probably linking.  That is also
306174832Sdes  # ok.
307174832Sdes  exec "$@"
308174832Sdesfi
309174832Sdes
310174832Sdes# Name of file we expect compiler to create.
311255376Sdescofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312174832Sdes
313174832Sdes# Create the lock directory.
314255376Sdes# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315174832Sdes# that we are using for the .o file.  Also, base the name on the expected
316174832Sdes# object file name, since that is what matters with a parallel build.
317255376Sdeslockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318174832Sdeswhile true; do
319174832Sdes  if mkdir "$lockdir" >/dev/null 2>&1; then
320174832Sdes    break
321174832Sdes  fi
322174832Sdes  sleep 1
323174832Sdesdone
324174832Sdes# FIXME: race condition here if user kills between mkdir and trap.
325174832Sdestrap "rmdir '$lockdir'; exit 1" 1 2 15
326174832Sdes
327174832Sdes# Run the compile.
328174832Sdes"$@"
329174832Sdesret=$?
330174832Sdes
331174832Sdesif test -f "$cofile"; then
332255376Sdes  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333174832Sdeselif test -f "${cofile}bj"; then
334255376Sdes  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335174832Sdesfi
336174832Sdes
337174832Sdesrmdir "$lockdir"
338174832Sdesexit $ret
339174832Sdes
340174832Sdes# Local Variables:
341174832Sdes# mode: shell-script
342174832Sdes# sh-indentation: 2
343348980Sdes# eval: (add-hook 'before-save-hook 'time-stamp)
344174832Sdes# time-stamp-start: "scriptversion="
345174832Sdes# time-stamp-format: "%:y-%02m-%02d.%02H"
346348980Sdes# time-stamp-time-zone: "UTC0"
347255376Sdes# time-stamp-end: "; # UTC"
348174832Sdes# End:
349