1186690Sobrien#! /bin/sh
2268515Sdelphij# Wrapper for compilers which do not understand '-c -o'.
3186690Sobrien
4268515Sdelphijscriptversion=2012-10-14.11; # UTC
5186690Sobrien
6284778Sdelphij# Copyright (C) 1999-2014 Free Software Foundation, Inc.
7186690Sobrien# Written by Tom Tromey <tromey@cygnus.com>.
8186690Sobrien#
9186690Sobrien# This program is free software; you can redistribute it and/or modify
10186690Sobrien# it under the terms of the GNU General Public License as published by
11186690Sobrien# the Free Software Foundation; either version 2, or (at your option)
12186690Sobrien# any later version.
13186690Sobrien#
14186690Sobrien# This program is distributed in the hope that it will be useful,
15186690Sobrien# but WITHOUT ANY WARRANTY; without even the implied warranty of
16186690Sobrien# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17186690Sobrien# GNU General Public License for more details.
18186690Sobrien#
19186690Sobrien# You should have received a copy of the GNU General Public License
20234449Sobrien# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21186690Sobrien
22186690Sobrien# As a special exception to the GNU General Public License, if you
23186690Sobrien# distribute this file as part of a program that contains a
24186690Sobrien# configuration script generated by Autoconf, you may include it under
25186690Sobrien# the same distribution terms that you use for the rest of that program.
26186690Sobrien
27186690Sobrien# This file is maintained in Automake, please report
28186690Sobrien# bugs to <bug-automake@gnu.org> or send patches to
29186690Sobrien# <automake-patches@gnu.org>.
30186690Sobrien
31268515Sdelphijnl='
32268515Sdelphij'
33268515Sdelphij
34268515Sdelphij# We need space, tab and new line, in precisely that order.  Quoting is
35268515Sdelphij# there to prevent tools from complaining about whitespace usage.
36268515SdelphijIFS=" ""	$nl"
37268515Sdelphij
38268515Sdelphijfile_conv=
39268515Sdelphij
40268515Sdelphij# func_file_conv build_file lazy
41268515Sdelphij# Convert a $build file to $host form and store it in $file
42268515Sdelphij# Currently only supports Windows hosts. If the determined conversion
43268515Sdelphij# type is listed in (the comma separated) LAZY, no conversion will
44268515Sdelphij# take place.
45268515Sdelphijfunc_file_conv ()
46268515Sdelphij{
47268515Sdelphij  file=$1
48268515Sdelphij  case $file in
49268515Sdelphij    / | /[!/]*) # absolute file, and not a UNC file
50268515Sdelphij      if test -z "$file_conv"; then
51268515Sdelphij	# lazily determine how to convert abs files
52268515Sdelphij	case `uname -s` in
53268515Sdelphij	  MINGW*)
54268515Sdelphij	    file_conv=mingw
55268515Sdelphij	    ;;
56268515Sdelphij	  CYGWIN*)
57268515Sdelphij	    file_conv=cygwin
58268515Sdelphij	    ;;
59268515Sdelphij	  *)
60268515Sdelphij	    file_conv=wine
61268515Sdelphij	    ;;
62268515Sdelphij	esac
63268515Sdelphij      fi
64268515Sdelphij      case $file_conv/,$2, in
65268515Sdelphij	*,$file_conv,*)
66268515Sdelphij	  ;;
67268515Sdelphij	mingw/*)
68268515Sdelphij	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69268515Sdelphij	  ;;
70268515Sdelphij	cygwin/*)
71268515Sdelphij	  file=`cygpath -m "$file" || echo "$file"`
72268515Sdelphij	  ;;
73268515Sdelphij	wine/*)
74268515Sdelphij	  file=`winepath -w "$file" || echo "$file"`
75268515Sdelphij	  ;;
76268515Sdelphij      esac
77268515Sdelphij      ;;
78268515Sdelphij  esac
79268515Sdelphij}
80268515Sdelphij
81268515Sdelphij# func_cl_dashL linkdir
82268515Sdelphij# Make cl look for libraries in LINKDIR
83268515Sdelphijfunc_cl_dashL ()
84268515Sdelphij{
85268515Sdelphij  func_file_conv "$1"
86268515Sdelphij  if test -z "$lib_path"; then
87268515Sdelphij    lib_path=$file
88268515Sdelphij  else
89268515Sdelphij    lib_path="$lib_path;$file"
90268515Sdelphij  fi
91268515Sdelphij  linker_opts="$linker_opts -LIBPATH:$file"
92268515Sdelphij}
93268515Sdelphij
94268515Sdelphij# func_cl_dashl library
95268515Sdelphij# Do a library search-path lookup for cl
96268515Sdelphijfunc_cl_dashl ()
97268515Sdelphij{
98268515Sdelphij  lib=$1
99268515Sdelphij  found=no
100268515Sdelphij  save_IFS=$IFS
101268515Sdelphij  IFS=';'
102268515Sdelphij  for dir in $lib_path $LIB
103268515Sdelphij  do
104268515Sdelphij    IFS=$save_IFS
105268515Sdelphij    if $shared && test -f "$dir/$lib.dll.lib"; then
106268515Sdelphij      found=yes
107268515Sdelphij      lib=$dir/$lib.dll.lib
108268515Sdelphij      break
109268515Sdelphij    fi
110268515Sdelphij    if test -f "$dir/$lib.lib"; then
111268515Sdelphij      found=yes
112268515Sdelphij      lib=$dir/$lib.lib
113268515Sdelphij      break
114268515Sdelphij    fi
115268515Sdelphij    if test -f "$dir/lib$lib.a"; then
116268515Sdelphij      found=yes
117268515Sdelphij      lib=$dir/lib$lib.a
118268515Sdelphij      break
119268515Sdelphij    fi
120268515Sdelphij  done
121268515Sdelphij  IFS=$save_IFS
122268515Sdelphij
123268515Sdelphij  if test "$found" != yes; then
124268515Sdelphij    lib=$lib.lib
125268515Sdelphij  fi
126268515Sdelphij}
127268515Sdelphij
128268515Sdelphij# func_cl_wrapper cl arg...
129268515Sdelphij# Adjust compile command to suit cl
130268515Sdelphijfunc_cl_wrapper ()
131268515Sdelphij{
132268515Sdelphij  # Assume a capable shell
133268515Sdelphij  lib_path=
134268515Sdelphij  shared=:
135268515Sdelphij  linker_opts=
136268515Sdelphij  for arg
137268515Sdelphij  do
138268515Sdelphij    if test -n "$eat"; then
139268515Sdelphij      eat=
140268515Sdelphij    else
141268515Sdelphij      case $1 in
142268515Sdelphij	-o)
143268515Sdelphij	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144268515Sdelphij	  eat=1
145268515Sdelphij	  case $2 in
146268515Sdelphij	    *.o | *.[oO][bB][jJ])
147268515Sdelphij	      func_file_conv "$2"
148268515Sdelphij	      set x "$@" -Fo"$file"
149268515Sdelphij	      shift
150268515Sdelphij	      ;;
151268515Sdelphij	    *)
152268515Sdelphij	      func_file_conv "$2"
153268515Sdelphij	      set x "$@" -Fe"$file"
154268515Sdelphij	      shift
155268515Sdelphij	      ;;
156268515Sdelphij	  esac
157268515Sdelphij	  ;;
158268515Sdelphij	-I)
159268515Sdelphij	  eat=1
160268515Sdelphij	  func_file_conv "$2" mingw
161268515Sdelphij	  set x "$@" -I"$file"
162268515Sdelphij	  shift
163268515Sdelphij	  ;;
164268515Sdelphij	-I*)
165268515Sdelphij	  func_file_conv "${1#-I}" mingw
166268515Sdelphij	  set x "$@" -I"$file"
167268515Sdelphij	  shift
168268515Sdelphij	  ;;
169268515Sdelphij	-l)
170268515Sdelphij	  eat=1
171268515Sdelphij	  func_cl_dashl "$2"
172268515Sdelphij	  set x "$@" "$lib"
173268515Sdelphij	  shift
174268515Sdelphij	  ;;
175268515Sdelphij	-l*)
176268515Sdelphij	  func_cl_dashl "${1#-l}"
177268515Sdelphij	  set x "$@" "$lib"
178268515Sdelphij	  shift
179268515Sdelphij	  ;;
180268515Sdelphij	-L)
181268515Sdelphij	  eat=1
182268515Sdelphij	  func_cl_dashL "$2"
183268515Sdelphij	  ;;
184268515Sdelphij	-L*)
185268515Sdelphij	  func_cl_dashL "${1#-L}"
186268515Sdelphij	  ;;
187268515Sdelphij	-static)
188268515Sdelphij	  shared=false
189268515Sdelphij	  ;;
190268515Sdelphij	-Wl,*)
191268515Sdelphij	  arg=${1#-Wl,}
192268515Sdelphij	  save_ifs="$IFS"; IFS=','
193268515Sdelphij	  for flag in $arg; do
194268515Sdelphij	    IFS="$save_ifs"
195268515Sdelphij	    linker_opts="$linker_opts $flag"
196268515Sdelphij	  done
197268515Sdelphij	  IFS="$save_ifs"
198268515Sdelphij	  ;;
199268515Sdelphij	-Xlinker)
200268515Sdelphij	  eat=1
201268515Sdelphij	  linker_opts="$linker_opts $2"
202268515Sdelphij	  ;;
203268515Sdelphij	-*)
204268515Sdelphij	  set x "$@" "$1"
205268515Sdelphij	  shift
206268515Sdelphij	  ;;
207268515Sdelphij	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208268515Sdelphij	  func_file_conv "$1"
209268515Sdelphij	  set x "$@" -Tp"$file"
210268515Sdelphij	  shift
211268515Sdelphij	  ;;
212268515Sdelphij	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213268515Sdelphij	  func_file_conv "$1" mingw
214268515Sdelphij	  set x "$@" "$file"
215268515Sdelphij	  shift
216268515Sdelphij	  ;;
217268515Sdelphij	*)
218268515Sdelphij	  set x "$@" "$1"
219268515Sdelphij	  shift
220268515Sdelphij	  ;;
221268515Sdelphij      esac
222268515Sdelphij    fi
223268515Sdelphij    shift
224268515Sdelphij  done
225268515Sdelphij  if test -n "$linker_opts"; then
226268515Sdelphij    linker_opts="-link$linker_opts"
227268515Sdelphij  fi
228268515Sdelphij  exec "$@" $linker_opts
229268515Sdelphij  exit 1
230268515Sdelphij}
231268515Sdelphij
232268515Sdelphijeat=
233268515Sdelphij
234186690Sobriencase $1 in
235186690Sobrien  '')
236268515Sdelphij     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237186690Sobrien     exit 1;
238186690Sobrien     ;;
239186690Sobrien  -h | --h*)
240186690Sobrien    cat <<\EOF
241186690SobrienUsage: compile [--help] [--version] PROGRAM [ARGS]
242186690Sobrien
243268515SdelphijWrapper for compilers which do not understand '-c -o'.
244268515SdelphijRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245186690Sobrienarguments, and rename the output as expected.
246186690Sobrien
247186690SobrienIf you are trying to build a whole package this is not the
248268515Sdelphijright script to run: please start by reading the file 'INSTALL'.
249186690Sobrien
250186690SobrienReport bugs to <bug-automake@gnu.org>.
251186690SobrienEOF
252186690Sobrien    exit $?
253186690Sobrien    ;;
254186690Sobrien  -v | --v*)
255186690Sobrien    echo "compile $scriptversion"
256186690Sobrien    exit $?
257186690Sobrien    ;;
258268515Sdelphij  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
259268515Sdelphij    func_cl_wrapper "$@"      # Doesn't return...
260268515Sdelphij    ;;
261186690Sobrienesac
262186690Sobrien
263186690Sobrienofile=
264186690Sobriencfile=
265186690Sobrien
266186690Sobrienfor arg
267186690Sobriendo
268186690Sobrien  if test -n "$eat"; then
269186690Sobrien    eat=
270186690Sobrien  else
271186690Sobrien    case $1 in
272186690Sobrien      -o)
273268515Sdelphij	# configure might choose to run compile as 'compile cc -o foo foo.c'.
274268515Sdelphij	# So we strip '-o arg' only if arg is an object.
275186690Sobrien	eat=1
276186690Sobrien	case $2 in
277186690Sobrien	  *.o | *.obj)
278186690Sobrien	    ofile=$2
279186690Sobrien	    ;;
280186690Sobrien	  *)
281186690Sobrien	    set x "$@" -o "$2"
282186690Sobrien	    shift
283186690Sobrien	    ;;
284186690Sobrien	esac
285186690Sobrien	;;
286186690Sobrien      *.c)
287186690Sobrien	cfile=$1
288186690Sobrien	set x "$@" "$1"
289186690Sobrien	shift
290186690Sobrien	;;
291186690Sobrien      *)
292186690Sobrien	set x "$@" "$1"
293186690Sobrien	shift
294186690Sobrien	;;
295186690Sobrien    esac
296186690Sobrien  fi
297186690Sobrien  shift
298186690Sobriendone
299186690Sobrien
300186690Sobrienif test -z "$ofile" || test -z "$cfile"; then
301268515Sdelphij  # If no '-o' option was seen then we might have been invoked from a
302186690Sobrien  # pattern rule where we don't need one.  That is ok -- this is a
303186690Sobrien  # normal compilation that the losing compiler can handle.  If no
304268515Sdelphij  # '.c' file was seen then we are probably linking.  That is also
305186690Sobrien  # ok.
306186690Sobrien  exec "$@"
307186690Sobrienfi
308186690Sobrien
309186690Sobrien# Name of file we expect compiler to create.
310234449Sobriencofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
311186690Sobrien
312186690Sobrien# Create the lock directory.
313268515Sdelphij# Note: use '[/\\:.-]' here to ensure that we don't use the same name
314186690Sobrien# that we are using for the .o file.  Also, base the name on the expected
315186690Sobrien# object file name, since that is what matters with a parallel build.
316234449Sobrienlockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
317186690Sobrienwhile true; do
318186690Sobrien  if mkdir "$lockdir" >/dev/null 2>&1; then
319186690Sobrien    break
320186690Sobrien  fi
321186690Sobrien  sleep 1
322186690Sobriendone
323186690Sobrien# FIXME: race condition here if user kills between mkdir and trap.
324186690Sobrientrap "rmdir '$lockdir'; exit 1" 1 2 15
325186690Sobrien
326186690Sobrien# Run the compile.
327186690Sobrien"$@"
328186690Sobrienret=$?
329186690Sobrien
330186690Sobrienif test -f "$cofile"; then
331234449Sobrien  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
332186690Sobrienelif test -f "${cofile}bj"; then
333234449Sobrien  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
334186690Sobrienfi
335186690Sobrien
336186690Sobrienrmdir "$lockdir"
337186690Sobrienexit $ret
338186690Sobrien
339186690Sobrien# Local Variables:
340186690Sobrien# mode: shell-script
341186690Sobrien# sh-indentation: 2
342186690Sobrien# eval: (add-hook 'write-file-hooks 'time-stamp)
343186690Sobrien# time-stamp-start: "scriptversion="
344186690Sobrien# time-stamp-format: "%:y-%02m-%02d.%02H"
345234449Sobrien# time-stamp-time-zone: "UTC"
346234449Sobrien# time-stamp-end: "; # UTC"
347186690Sobrien# End:
348