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