1235456Sgnn#! /bin/sh
2235368Sgnn# Wrapper for compilers which do not understand '-c -o'.
3235368Sgnn
4235368Sgnnscriptversion=2012-10-14.11; # UTC
5235368Sgnn
6235368Sgnn# Copyright (C) 1999-2014 Free Software Foundation, Inc.
7235368Sgnn# Written by Tom Tromey <tromey@cygnus.com>.
8235368Sgnn#
9235368Sgnn# This program is free software; you can redistribute it and/or modify
10235368Sgnn# it under the terms of the GNU General Public License as published by
11235368Sgnn# the Free Software Foundation; either version 2, or (at your option)
12235368Sgnn# any later version.
13235368Sgnn#
14235368Sgnn# This program is distributed in the hope that it will be useful,
15235368Sgnn# but WITHOUT ANY WARRANTY; without even the implied warranty of
16235368Sgnn# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17235368Sgnn# GNU General Public License for more details.
18235368Sgnn#
19235368Sgnn# You should have received a copy of the GNU General Public License
20235368Sgnn# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21235368Sgnn
22235368Sgnn# As a special exception to the GNU General Public License, if you
23235368Sgnn# distribute this file as part of a program that contains a
24235368Sgnn# configuration script generated by Autoconf, you may include it under
25235368Sgnn# the same distribution terms that you use for the rest of that program.
26235368Sgnn
27235368Sgnn# This file is maintained in Automake, please report
28235368Sgnn# bugs to <bug-automake@gnu.org> or send patches to
29235368Sgnn# <automake-patches@gnu.org>.
30235368Sgnn
31235368Sgnnnl='
32235368Sgnn'
33235368Sgnn
34235368Sgnn# We need space, tab and new line, in precisely that order.  Quoting is
35235368Sgnn# there to prevent tools from complaining about whitespace usage.
36235368SgnnIFS=" ""	$nl"
37235368Sgnn
38235368Sgnnfile_conv=
39235368Sgnn
40235368Sgnn# func_file_conv build_file lazy
41235368Sgnn# Convert a $build file to $host form and store it in $file
42235368Sgnn# Currently only supports Windows hosts. If the determined conversion
43235368Sgnn# type is listed in (the comma separated) LAZY, no conversion will
44235368Sgnn# take place.
45235368Sgnnfunc_file_conv ()
46235368Sgnn{
47235368Sgnn  file=$1
48235368Sgnn  case $file in
49235368Sgnn    / | /[!/]*) # absolute file, and not a UNC file
50235368Sgnn      if test -z "$file_conv"; then
51235368Sgnn	# lazily determine how to convert abs files
52235368Sgnn	case `uname -s` in
53235368Sgnn	  MINGW*)
54235368Sgnn	    file_conv=mingw
55235368Sgnn	    ;;
56235368Sgnn	  CYGWIN*)
57235368Sgnn	    file_conv=cygwin
58235368Sgnn	    ;;
59235368Sgnn	  *)
60235368Sgnn	    file_conv=wine
61235368Sgnn	    ;;
62235368Sgnn	esac
63235368Sgnn      fi
64235368Sgnn      case $file_conv/,$2, in
65235368Sgnn	*,$file_conv,*)
66235368Sgnn	  ;;
67235368Sgnn	mingw/*)
68235368Sgnn	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69235368Sgnn	  ;;
70235368Sgnn	cygwin/*)
71235368Sgnn	  file=`cygpath -m "$file" || echo "$file"`
72235368Sgnn	  ;;
73235368Sgnn	wine/*)
74235368Sgnn	  file=`winepath -w "$file" || echo "$file"`
75235368Sgnn	  ;;
76235368Sgnn      esac
77235368Sgnn      ;;
78235368Sgnn  esac
79235368Sgnn}
80235368Sgnn
81235368Sgnn# func_cl_dashL linkdir
82235368Sgnn# Make cl look for libraries in LINKDIR
83235368Sgnnfunc_cl_dashL ()
84235368Sgnn{
85235368Sgnn  func_file_conv "$1"
86235368Sgnn  if test -z "$lib_path"; then
87235368Sgnn    lib_path=$file
88235368Sgnn  else
89235368Sgnn    lib_path="$lib_path;$file"
90235368Sgnn  fi
91235368Sgnn  linker_opts="$linker_opts -LIBPATH:$file"
92235368Sgnn}
93235368Sgnn
94235368Sgnn# func_cl_dashl library
95235368Sgnn# Do a library search-path lookup for cl
96235368Sgnnfunc_cl_dashl ()
97235368Sgnn{
98235368Sgnn  lib=$1
99235368Sgnn  found=no
100235368Sgnn  save_IFS=$IFS
101235368Sgnn  IFS=';'
102235368Sgnn  for dir in $lib_path $LIB
103235368Sgnn  do
104235368Sgnn    IFS=$save_IFS
105235368Sgnn    if $shared && test -f "$dir/$lib.dll.lib"; then
106235368Sgnn      found=yes
107235368Sgnn      lib=$dir/$lib.dll.lib
108235368Sgnn      break
109235368Sgnn    fi
110235368Sgnn    if test -f "$dir/$lib.lib"; then
111235368Sgnn      found=yes
112235368Sgnn      lib=$dir/$lib.lib
113235368Sgnn      break
114235368Sgnn    fi
115235368Sgnn    if test -f "$dir/lib$lib.a"; then
116235368Sgnn      found=yes
117235368Sgnn      lib=$dir/lib$lib.a
118235368Sgnn      break
119235368Sgnn    fi
120235368Sgnn  done
121235368Sgnn  IFS=$save_IFS
122235368Sgnn
123235368Sgnn  if test "$found" != yes; then
124235368Sgnn    lib=$lib.lib
125235368Sgnn  fi
126235368Sgnn}
127235368Sgnn
128235368Sgnn# func_cl_wrapper cl arg...
129235368Sgnn# Adjust compile command to suit cl
130235368Sgnnfunc_cl_wrapper ()
131235368Sgnn{
132235368Sgnn  # Assume a capable shell
133235368Sgnn  lib_path=
134235368Sgnn  shared=:
135235368Sgnn  linker_opts=
136235368Sgnn  for arg
137235368Sgnn  do
138235368Sgnn    if test -n "$eat"; then
139235368Sgnn      eat=
140235368Sgnn    else
141235368Sgnn      case $1 in
142235368Sgnn	-o)
143235368Sgnn	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144235368Sgnn	  eat=1
145235368Sgnn	  case $2 in
146235368Sgnn	    *.o | *.[oO][bB][jJ])
147235368Sgnn	      func_file_conv "$2"
148235368Sgnn	      set x "$@" -Fo"$file"
149235368Sgnn	      shift
150235368Sgnn	      ;;
151235368Sgnn	    *)
152235368Sgnn	      func_file_conv "$2"
153235368Sgnn	      set x "$@" -Fe"$file"
154235368Sgnn	      shift
155235368Sgnn	      ;;
156235368Sgnn	  esac
157235368Sgnn	  ;;
158235368Sgnn	-I)
159235368Sgnn	  eat=1
160235368Sgnn	  func_file_conv "$2" mingw
161235368Sgnn	  set x "$@" -I"$file"
162235368Sgnn	  shift
163235368Sgnn	  ;;
164235368Sgnn	-I*)
165235368Sgnn	  func_file_conv "${1#-I}" mingw
166235368Sgnn	  set x "$@" -I"$file"
167235368Sgnn	  shift
168235368Sgnn	  ;;
169235368Sgnn	-l)
170235368Sgnn	  eat=1
171235368Sgnn	  func_cl_dashl "$2"
172235368Sgnn	  set x "$@" "$lib"
173235368Sgnn	  shift
174235368Sgnn	  ;;
175235368Sgnn	-l*)
176235368Sgnn	  func_cl_dashl "${1#-l}"
177235368Sgnn	  set x "$@" "$lib"
178235368Sgnn	  shift
179235368Sgnn	  ;;
180235368Sgnn	-L)
181235368Sgnn	  eat=1
182235368Sgnn	  func_cl_dashL "$2"
183235368Sgnn	  ;;
184235368Sgnn	-L*)
185235368Sgnn	  func_cl_dashL "${1#-L}"
186235368Sgnn	  ;;
187235368Sgnn	-static)
188235368Sgnn	  shared=false
189235368Sgnn	  ;;
190235368Sgnn	-Wl,*)
191235368Sgnn	  arg=${1#-Wl,}
192235368Sgnn	  save_ifs="$IFS"; IFS=','
193235368Sgnn	  for flag in $arg; do
194235368Sgnn	    IFS="$save_ifs"
195235368Sgnn	    linker_opts="$linker_opts $flag"
196235368Sgnn	  done
197235368Sgnn	  IFS="$save_ifs"
198235368Sgnn	  ;;
199235368Sgnn	-Xlinker)
200235368Sgnn	  eat=1
201235368Sgnn	  linker_opts="$linker_opts $2"
202235368Sgnn	  ;;
203235368Sgnn	-*)
204235368Sgnn	  set x "$@" "$1"
205235368Sgnn	  shift
206235368Sgnn	  ;;
207235368Sgnn	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208235368Sgnn	  func_file_conv "$1"
209235368Sgnn	  set x "$@" -Tp"$file"
210235368Sgnn	  shift
211235368Sgnn	  ;;
212235368Sgnn	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213235368Sgnn	  func_file_conv "$1" mingw
214235368Sgnn	  set x "$@" "$file"
215235368Sgnn	  shift
216235368Sgnn	  ;;
217235368Sgnn	*)
218235368Sgnn	  set x "$@" "$1"
219235368Sgnn	  shift
220235368Sgnn	  ;;
221235368Sgnn      esac
222235368Sgnn    fi
223235368Sgnn    shift
224235368Sgnn  done
225235368Sgnn  if test -n "$linker_opts"; then
226235368Sgnn    linker_opts="-link$linker_opts"
227235368Sgnn  fi
228235368Sgnn  exec "$@" $linker_opts
229235368Sgnn  exit 1
230235368Sgnn}
231235368Sgnn
232235368Sgnneat=
233235368Sgnn
234case $1 in
235  '')
236     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237     exit 1;
238     ;;
239  -h | --h*)
240    cat <<\EOF
241Usage: compile [--help] [--version] PROGRAM [ARGS]
242
243Wrapper for compilers which do not understand '-c -o'.
244Remove '-o dest.o' from ARGS, run PROGRAM with the remaining
245arguments, and rename the output as expected.
246
247If you are trying to build a whole package this is not the
248right script to run: please start by reading the file 'INSTALL'.
249
250Report bugs to <bug-automake@gnu.org>.
251EOF
252    exit $?
253    ;;
254  -v | --v*)
255    echo "compile $scriptversion"
256    exit $?
257    ;;
258  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
259    func_cl_wrapper "$@"      # Doesn't return...
260    ;;
261esac
262
263ofile=
264cfile=
265
266for arg
267do
268  if test -n "$eat"; then
269    eat=
270  else
271    case $1 in
272      -o)
273	# configure might choose to run compile as 'compile cc -o foo foo.c'.
274	# So we strip '-o arg' only if arg is an object.
275	eat=1
276	case $2 in
277	  *.o | *.obj)
278	    ofile=$2
279	    ;;
280	  *)
281	    set x "$@" -o "$2"
282	    shift
283	    ;;
284	esac
285	;;
286      *.c)
287	cfile=$1
288	set x "$@" "$1"
289	shift
290	;;
291      *)
292	set x "$@" "$1"
293	shift
294	;;
295    esac
296  fi
297  shift
298done
299
300if test -z "$ofile" || test -z "$cfile"; then
301  # If no '-o' option was seen then we might have been invoked from a
302  # pattern rule where we don't need one.  That is ok -- this is a
303  # normal compilation that the losing compiler can handle.  If no
304  # '.c' file was seen then we are probably linking.  That is also
305  # ok.
306  exec "$@"
307fi
308
309# Name of file we expect compiler to create.
310cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
311
312# Create the lock directory.
313# Note: use '[/\\:.-]' here to ensure that we don't use the same name
314# that we are using for the .o file.  Also, base the name on the expected
315# object file name, since that is what matters with a parallel build.
316lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
317while true; do
318  if mkdir "$lockdir" >/dev/null 2>&1; then
319    break
320  fi
321  sleep 1
322done
323# FIXME: race condition here if user kills between mkdir and trap.
324trap "rmdir '$lockdir'; exit 1" 1 2 15
325
326# Run the compile.
327"$@"
328ret=$?
329
330if test -f "$cofile"; then
331  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
332elif test -f "${cofile}bj"; then
333  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
334fi
335
336rmdir "$lockdir"
337exit $ret
338
339# Local Variables:
340# mode: shell-script
341# sh-indentation: 2
342# eval: (add-hook 'write-file-hooks 'time-stamp)
343# time-stamp-start: "scriptversion="
344# time-stamp-format: "%:y-%02m-%02d.%02H"
345# time-stamp-time-zone: "UTC"
346# time-stamp-end: "; # UTC"
347# End:
348