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