1275970Scy#! /bin/sh
2316722Sdelphij# Wrapper for compilers which do not understand '-c -o'.
3275970Scy
4316722Sdelphijscriptversion=2012-10-14.11; # UTC
5275970Scy
6316722Sdelphij# Copyright (C) 1999-2014 Free Software Foundation, Inc.
7275970Scy# Written by Tom Tromey <tromey@cygnus.com>.
8275970Scy#
9275970Scy# This program is free software; you can redistribute it and/or modify
10275970Scy# it under the terms of the GNU General Public License as published by
11275970Scy# the Free Software Foundation; either version 2, or (at your option)
12275970Scy# any later version.
13275970Scy#
14275970Scy# This program is distributed in the hope that it will be useful,
15275970Scy# but WITHOUT ANY WARRANTY; without even the implied warranty of
16275970Scy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17275970Scy# GNU General Public License for more details.
18275970Scy#
19275970Scy# You should have received a copy of the GNU General Public License
20275970Scy# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21275970Scy
22275970Scy# As a special exception to the GNU General Public License, if you
23275970Scy# distribute this file as part of a program that contains a
24275970Scy# configuration script generated by Autoconf, you may include it under
25275970Scy# the same distribution terms that you use for the rest of that program.
26275970Scy
27275970Scy# This file is maintained in Automake, please report
28275970Scy# bugs to <bug-automake@gnu.org> or send patches to
29275970Scy# <automake-patches@gnu.org>.
30275970Scy
31316722Sdelphijnl='
32316722Sdelphij'
33316722Sdelphij
34316722Sdelphij# We need space, tab and new line, in precisely that order.  Quoting is
35316722Sdelphij# there to prevent tools from complaining about whitespace usage.
36316722SdelphijIFS=" ""	$nl"
37316722Sdelphij
38316722Sdelphijfile_conv=
39316722Sdelphij
40316722Sdelphij# func_file_conv build_file lazy
41316722Sdelphij# Convert a $build file to $host form and store it in $file
42316722Sdelphij# Currently only supports Windows hosts. If the determined conversion
43316722Sdelphij# type is listed in (the comma separated) LAZY, no conversion will
44316722Sdelphij# take place.
45316722Sdelphijfunc_file_conv ()
46316722Sdelphij{
47316722Sdelphij  file=$1
48316722Sdelphij  case $file in
49316722Sdelphij    / | /[!/]*) # absolute file, and not a UNC file
50316722Sdelphij      if test -z "$file_conv"; then
51316722Sdelphij	# lazily determine how to convert abs files
52316722Sdelphij	case `uname -s` in
53316722Sdelphij	  MINGW*)
54316722Sdelphij	    file_conv=mingw
55316722Sdelphij	    ;;
56316722Sdelphij	  CYGWIN*)
57316722Sdelphij	    file_conv=cygwin
58316722Sdelphij	    ;;
59316722Sdelphij	  *)
60316722Sdelphij	    file_conv=wine
61316722Sdelphij	    ;;
62316722Sdelphij	esac
63316722Sdelphij      fi
64316722Sdelphij      case $file_conv/,$2, in
65316722Sdelphij	*,$file_conv,*)
66316722Sdelphij	  ;;
67316722Sdelphij	mingw/*)
68316722Sdelphij	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69316722Sdelphij	  ;;
70316722Sdelphij	cygwin/*)
71316722Sdelphij	  file=`cygpath -m "$file" || echo "$file"`
72316722Sdelphij	  ;;
73316722Sdelphij	wine/*)
74316722Sdelphij	  file=`winepath -w "$file" || echo "$file"`
75316722Sdelphij	  ;;
76316722Sdelphij      esac
77316722Sdelphij      ;;
78316722Sdelphij  esac
79316722Sdelphij}
80316722Sdelphij
81316722Sdelphij# func_cl_dashL linkdir
82316722Sdelphij# Make cl look for libraries in LINKDIR
83316722Sdelphijfunc_cl_dashL ()
84316722Sdelphij{
85316722Sdelphij  func_file_conv "$1"
86316722Sdelphij  if test -z "$lib_path"; then
87316722Sdelphij    lib_path=$file
88316722Sdelphij  else
89316722Sdelphij    lib_path="$lib_path;$file"
90316722Sdelphij  fi
91316722Sdelphij  linker_opts="$linker_opts -LIBPATH:$file"
92316722Sdelphij}
93316722Sdelphij
94316722Sdelphij# func_cl_dashl library
95316722Sdelphij# Do a library search-path lookup for cl
96316722Sdelphijfunc_cl_dashl ()
97316722Sdelphij{
98316722Sdelphij  lib=$1
99316722Sdelphij  found=no
100316722Sdelphij  save_IFS=$IFS
101316722Sdelphij  IFS=';'
102316722Sdelphij  for dir in $lib_path $LIB
103316722Sdelphij  do
104316722Sdelphij    IFS=$save_IFS
105316722Sdelphij    if $shared && test -f "$dir/$lib.dll.lib"; then
106316722Sdelphij      found=yes
107316722Sdelphij      lib=$dir/$lib.dll.lib
108316722Sdelphij      break
109316722Sdelphij    fi
110316722Sdelphij    if test -f "$dir/$lib.lib"; then
111316722Sdelphij      found=yes
112316722Sdelphij      lib=$dir/$lib.lib
113316722Sdelphij      break
114316722Sdelphij    fi
115316722Sdelphij    if test -f "$dir/lib$lib.a"; then
116316722Sdelphij      found=yes
117316722Sdelphij      lib=$dir/lib$lib.a
118316722Sdelphij      break
119316722Sdelphij    fi
120316722Sdelphij  done
121316722Sdelphij  IFS=$save_IFS
122316722Sdelphij
123316722Sdelphij  if test "$found" != yes; then
124316722Sdelphij    lib=$lib.lib
125316722Sdelphij  fi
126316722Sdelphij}
127316722Sdelphij
128316722Sdelphij# func_cl_wrapper cl arg...
129316722Sdelphij# Adjust compile command to suit cl
130316722Sdelphijfunc_cl_wrapper ()
131316722Sdelphij{
132316722Sdelphij  # Assume a capable shell
133316722Sdelphij  lib_path=
134316722Sdelphij  shared=:
135316722Sdelphij  linker_opts=
136316722Sdelphij  for arg
137316722Sdelphij  do
138316722Sdelphij    if test -n "$eat"; then
139316722Sdelphij      eat=
140316722Sdelphij    else
141316722Sdelphij      case $1 in
142316722Sdelphij	-o)
143316722Sdelphij	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144316722Sdelphij	  eat=1
145316722Sdelphij	  case $2 in
146316722Sdelphij	    *.o | *.[oO][bB][jJ])
147316722Sdelphij	      func_file_conv "$2"
148316722Sdelphij	      set x "$@" -Fo"$file"
149316722Sdelphij	      shift
150316722Sdelphij	      ;;
151316722Sdelphij	    *)
152316722Sdelphij	      func_file_conv "$2"
153316722Sdelphij	      set x "$@" -Fe"$file"
154316722Sdelphij	      shift
155316722Sdelphij	      ;;
156316722Sdelphij	  esac
157316722Sdelphij	  ;;
158316722Sdelphij	-I)
159316722Sdelphij	  eat=1
160316722Sdelphij	  func_file_conv "$2" mingw
161316722Sdelphij	  set x "$@" -I"$file"
162316722Sdelphij	  shift
163316722Sdelphij	  ;;
164316722Sdelphij	-I*)
165316722Sdelphij	  func_file_conv "${1#-I}" mingw
166316722Sdelphij	  set x "$@" -I"$file"
167316722Sdelphij	  shift
168316722Sdelphij	  ;;
169316722Sdelphij	-l)
170316722Sdelphij	  eat=1
171316722Sdelphij	  func_cl_dashl "$2"
172316722Sdelphij	  set x "$@" "$lib"
173316722Sdelphij	  shift
174316722Sdelphij	  ;;
175316722Sdelphij	-l*)
176316722Sdelphij	  func_cl_dashl "${1#-l}"
177316722Sdelphij	  set x "$@" "$lib"
178316722Sdelphij	  shift
179316722Sdelphij	  ;;
180316722Sdelphij	-L)
181316722Sdelphij	  eat=1
182316722Sdelphij	  func_cl_dashL "$2"
183316722Sdelphij	  ;;
184316722Sdelphij	-L*)
185316722Sdelphij	  func_cl_dashL "${1#-L}"
186316722Sdelphij	  ;;
187316722Sdelphij	-static)
188316722Sdelphij	  shared=false
189316722Sdelphij	  ;;
190316722Sdelphij	-Wl,*)
191316722Sdelphij	  arg=${1#-Wl,}
192316722Sdelphij	  save_ifs="$IFS"; IFS=','
193316722Sdelphij	  for flag in $arg; do
194316722Sdelphij	    IFS="$save_ifs"
195316722Sdelphij	    linker_opts="$linker_opts $flag"
196316722Sdelphij	  done
197316722Sdelphij	  IFS="$save_ifs"
198316722Sdelphij	  ;;
199316722Sdelphij	-Xlinker)
200316722Sdelphij	  eat=1
201316722Sdelphij	  linker_opts="$linker_opts $2"
202316722Sdelphij	  ;;
203316722Sdelphij	-*)
204316722Sdelphij	  set x "$@" "$1"
205316722Sdelphij	  shift
206316722Sdelphij	  ;;
207316722Sdelphij	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208316722Sdelphij	  func_file_conv "$1"
209316722Sdelphij	  set x "$@" -Tp"$file"
210316722Sdelphij	  shift
211316722Sdelphij	  ;;
212316722Sdelphij	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213316722Sdelphij	  func_file_conv "$1" mingw
214316722Sdelphij	  set x "$@" "$file"
215316722Sdelphij	  shift
216316722Sdelphij	  ;;
217316722Sdelphij	*)
218316722Sdelphij	  set x "$@" "$1"
219316722Sdelphij	  shift
220316722Sdelphij	  ;;
221316722Sdelphij      esac
222316722Sdelphij    fi
223316722Sdelphij    shift
224316722Sdelphij  done
225316722Sdelphij  if test -n "$linker_opts"; then
226316722Sdelphij    linker_opts="-link$linker_opts"
227316722Sdelphij  fi
228316722Sdelphij  exec "$@" $linker_opts
229316722Sdelphij  exit 1
230316722Sdelphij}
231316722Sdelphij
232316722Sdelphijeat=
233316722Sdelphij
234275970Scycase $1 in
235275970Scy  '')
236316722Sdelphij     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237275970Scy     exit 1;
238275970Scy     ;;
239275970Scy  -h | --h*)
240275970Scy    cat <<\EOF
241275970ScyUsage: compile [--help] [--version] PROGRAM [ARGS]
242275970Scy
243316722SdelphijWrapper for compilers which do not understand '-c -o'.
244316722SdelphijRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245275970Scyarguments, and rename the output as expected.
246275970Scy
247275970ScyIf you are trying to build a whole package this is not the
248316722Sdelphijright script to run: please start by reading the file 'INSTALL'.
249275970Scy
250275970ScyReport bugs to <bug-automake@gnu.org>.
251275970ScyEOF
252275970Scy    exit $?
253275970Scy    ;;
254275970Scy  -v | --v*)
255275970Scy    echo "compile $scriptversion"
256275970Scy    exit $?
257275970Scy    ;;
258316722Sdelphij  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
259316722Sdelphij    func_cl_wrapper "$@"      # Doesn't return...
260316722Sdelphij    ;;
261275970Scyesac
262275970Scy
263275970Scyofile=
264275970Scycfile=
265275970Scy
266275970Scyfor arg
267275970Scydo
268275970Scy  if test -n "$eat"; then
269275970Scy    eat=
270275970Scy  else
271275970Scy    case $1 in
272275970Scy      -o)
273316722Sdelphij	# configure might choose to run compile as 'compile cc -o foo foo.c'.
274316722Sdelphij	# So we strip '-o arg' only if arg is an object.
275275970Scy	eat=1
276275970Scy	case $2 in
277275970Scy	  *.o | *.obj)
278275970Scy	    ofile=$2
279275970Scy	    ;;
280275970Scy	  *)
281275970Scy	    set x "$@" -o "$2"
282275970Scy	    shift
283275970Scy	    ;;
284275970Scy	esac
285275970Scy	;;
286275970Scy      *.c)
287275970Scy	cfile=$1
288275970Scy	set x "$@" "$1"
289275970Scy	shift
290275970Scy	;;
291275970Scy      *)
292275970Scy	set x "$@" "$1"
293275970Scy	shift
294275970Scy	;;
295275970Scy    esac
296275970Scy  fi
297275970Scy  shift
298275970Scydone
299275970Scy
300275970Scyif test -z "$ofile" || test -z "$cfile"; then
301316722Sdelphij  # If no '-o' option was seen then we might have been invoked from a
302275970Scy  # pattern rule where we don't need one.  That is ok -- this is a
303275970Scy  # normal compilation that the losing compiler can handle.  If no
304316722Sdelphij  # '.c' file was seen then we are probably linking.  That is also
305275970Scy  # ok.
306275970Scy  exec "$@"
307275970Scyfi
308275970Scy
309275970Scy# Name of file we expect compiler to create.
310275970Scycofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
311275970Scy
312275970Scy# Create the lock directory.
313316722Sdelphij# Note: use '[/\\:.-]' here to ensure that we don't use the same name
314275970Scy# that we are using for the .o file.  Also, base the name on the expected
315275970Scy# object file name, since that is what matters with a parallel build.
316275970Scylockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
317275970Scywhile true; do
318275970Scy  if mkdir "$lockdir" >/dev/null 2>&1; then
319275970Scy    break
320275970Scy  fi
321275970Scy  sleep 1
322275970Scydone
323275970Scy# FIXME: race condition here if user kills between mkdir and trap.
324275970Scytrap "rmdir '$lockdir'; exit 1" 1 2 15
325275970Scy
326275970Scy# Run the compile.
327275970Scy"$@"
328275970Scyret=$?
329275970Scy
330275970Scyif test -f "$cofile"; then
331275970Scy  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
332275970Scyelif test -f "${cofile}bj"; then
333275970Scy  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
334275970Scyfi
335275970Scy
336275970Scyrmdir "$lockdir"
337275970Scyexit $ret
338275970Scy
339275970Scy# Local Variables:
340275970Scy# mode: shell-script
341275970Scy# sh-indentation: 2
342275970Scy# eval: (add-hook 'write-file-hooks 'time-stamp)
343275970Scy# time-stamp-start: "scriptversion="
344275970Scy# time-stamp-format: "%:y-%02m-%02d.%02H"
345275970Scy# time-stamp-time-zone: "UTC"
346275970Scy# time-stamp-end: "; # UTC"
347275970Scy# End:
348