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