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