12b15cb3dSCy Schubert#! /bin/sh
2f0574f5cSXin LI# Wrapper for compilers which do not understand '-c -o'.
32b15cb3dSCy Schubert
4*2d4e511cSCy Schubertscriptversion=2012-10-14.11; # UTC
52b15cb3dSCy Schubert
6*2d4e511cSCy Schubert# Copyright (C) 1999-2014 Free Software Foundation, Inc.
72b15cb3dSCy Schubert# Written by Tom Tromey <tromey@cygnus.com>.
82b15cb3dSCy Schubert#
92b15cb3dSCy Schubert# This program is free software; you can redistribute it and/or modify
102b15cb3dSCy Schubert# it under the terms of the GNU General Public License as published by
112b15cb3dSCy Schubert# the Free Software Foundation; either version 2, or (at your option)
122b15cb3dSCy Schubert# any later version.
132b15cb3dSCy Schubert#
142b15cb3dSCy Schubert# This program is distributed in the hope that it will be useful,
152b15cb3dSCy Schubert# but WITHOUT ANY WARRANTY; without even the implied warranty of
162b15cb3dSCy Schubert# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
172b15cb3dSCy Schubert# GNU General Public License for more details.
182b15cb3dSCy Schubert#
192b15cb3dSCy Schubert# You should have received a copy of the GNU General Public License
202b15cb3dSCy Schubert# along with this program.  If not, see <http://www.gnu.org/licenses/>.
212b15cb3dSCy Schubert
222b15cb3dSCy Schubert# As a special exception to the GNU General Public License, if you
232b15cb3dSCy Schubert# distribute this file as part of a program that contains a
242b15cb3dSCy Schubert# configuration script generated by Autoconf, you may include it under
252b15cb3dSCy Schubert# the same distribution terms that you use for the rest of that program.
262b15cb3dSCy Schubert
272b15cb3dSCy Schubert# This file is maintained in Automake, please report
282b15cb3dSCy Schubert# bugs to <bug-automake@gnu.org> or send patches to
292b15cb3dSCy Schubert# <automake-patches@gnu.org>.
302b15cb3dSCy Schubert
31f0574f5cSXin LInl='
32f0574f5cSXin LI'
33f0574f5cSXin LI
34f0574f5cSXin LI# We need space, tab and new line, in precisely that order.  Quoting is
35f0574f5cSXin LI# there to prevent tools from complaining about whitespace usage.
36f0574f5cSXin LIIFS=" ""	$nl"
37f0574f5cSXin LI
38f0574f5cSXin LIfile_conv=
39f0574f5cSXin LI
40f0574f5cSXin LI# func_file_conv build_file lazy
41f0574f5cSXin LI# Convert a $build file to $host form and store it in $file
42f0574f5cSXin LI# Currently only supports Windows hosts. If the determined conversion
43f0574f5cSXin LI# type is listed in (the comma separated) LAZY, no conversion will
44f0574f5cSXin LI# take place.
45f0574f5cSXin LIfunc_file_conv ()
46f0574f5cSXin LI{
47f0574f5cSXin LI  file=$1
48f0574f5cSXin LI  case $file in
49f0574f5cSXin LI    / | /[!/]*) # absolute file, and not a UNC file
50f0574f5cSXin LI      if test -z "$file_conv"; then
51f0574f5cSXin LI	# lazily determine how to convert abs files
52f0574f5cSXin LI	case `uname -s` in
53f0574f5cSXin LI	  MINGW*)
54f0574f5cSXin LI	    file_conv=mingw
55f0574f5cSXin LI	    ;;
56f0574f5cSXin LI	  CYGWIN*)
57f0574f5cSXin LI	    file_conv=cygwin
58f0574f5cSXin LI	    ;;
59f0574f5cSXin LI	  *)
60f0574f5cSXin LI	    file_conv=wine
61f0574f5cSXin LI	    ;;
62f0574f5cSXin LI	esac
63f0574f5cSXin LI      fi
64f0574f5cSXin LI      case $file_conv/,$2, in
65f0574f5cSXin LI	*,$file_conv,*)
66f0574f5cSXin LI	  ;;
67f0574f5cSXin LI	mingw/*)
68f0574f5cSXin LI	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69f0574f5cSXin LI	  ;;
70f0574f5cSXin LI	cygwin/*)
71f0574f5cSXin LI	  file=`cygpath -m "$file" || echo "$file"`
72f0574f5cSXin LI	  ;;
73f0574f5cSXin LI	wine/*)
74f0574f5cSXin LI	  file=`winepath -w "$file" || echo "$file"`
75f0574f5cSXin LI	  ;;
76f0574f5cSXin LI      esac
77f0574f5cSXin LI      ;;
78f0574f5cSXin LI  esac
79f0574f5cSXin LI}
80f0574f5cSXin LI
81f0574f5cSXin LI# func_cl_dashL linkdir
82f0574f5cSXin LI# Make cl look for libraries in LINKDIR
83f0574f5cSXin LIfunc_cl_dashL ()
84f0574f5cSXin LI{
85f0574f5cSXin LI  func_file_conv "$1"
86f0574f5cSXin LI  if test -z "$lib_path"; then
87f0574f5cSXin LI    lib_path=$file
88f0574f5cSXin LI  else
89f0574f5cSXin LI    lib_path="$lib_path;$file"
90f0574f5cSXin LI  fi
91f0574f5cSXin LI  linker_opts="$linker_opts -LIBPATH:$file"
92f0574f5cSXin LI}
93f0574f5cSXin LI
94f0574f5cSXin LI# func_cl_dashl library
95f0574f5cSXin LI# Do a library search-path lookup for cl
96f0574f5cSXin LIfunc_cl_dashl ()
97f0574f5cSXin LI{
98f0574f5cSXin LI  lib=$1
99f0574f5cSXin LI  found=no
100f0574f5cSXin LI  save_IFS=$IFS
101f0574f5cSXin LI  IFS=';'
102f0574f5cSXin LI  for dir in $lib_path $LIB
103f0574f5cSXin LI  do
104f0574f5cSXin LI    IFS=$save_IFS
105f0574f5cSXin LI    if $shared && test -f "$dir/$lib.dll.lib"; then
106f0574f5cSXin LI      found=yes
107f0574f5cSXin LI      lib=$dir/$lib.dll.lib
108f0574f5cSXin LI      break
109f0574f5cSXin LI    fi
110f0574f5cSXin LI    if test -f "$dir/$lib.lib"; then
111f0574f5cSXin LI      found=yes
112f0574f5cSXin LI      lib=$dir/$lib.lib
113f0574f5cSXin LI      break
114f0574f5cSXin LI    fi
115f0574f5cSXin LI    if test -f "$dir/lib$lib.a"; then
116f0574f5cSXin LI      found=yes
117f0574f5cSXin LI      lib=$dir/lib$lib.a
118f0574f5cSXin LI      break
119f0574f5cSXin LI    fi
120f0574f5cSXin LI  done
121f0574f5cSXin LI  IFS=$save_IFS
122f0574f5cSXin LI
123f0574f5cSXin LI  if test "$found" != yes; then
124f0574f5cSXin LI    lib=$lib.lib
125f0574f5cSXin LI  fi
126f0574f5cSXin LI}
127f0574f5cSXin LI
128f0574f5cSXin LI# func_cl_wrapper cl arg...
129f0574f5cSXin LI# Adjust compile command to suit cl
130f0574f5cSXin LIfunc_cl_wrapper ()
131f0574f5cSXin LI{
132f0574f5cSXin LI  # Assume a capable shell
133f0574f5cSXin LI  lib_path=
134f0574f5cSXin LI  shared=:
135f0574f5cSXin LI  linker_opts=
136f0574f5cSXin LI  for arg
137f0574f5cSXin LI  do
138f0574f5cSXin LI    if test -n "$eat"; then
139f0574f5cSXin LI      eat=
140f0574f5cSXin LI    else
141f0574f5cSXin LI      case $1 in
142f0574f5cSXin LI	-o)
143f0574f5cSXin LI	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144f0574f5cSXin LI	  eat=1
145f0574f5cSXin LI	  case $2 in
146f0574f5cSXin LI	    *.o | *.[oO][bB][jJ])
147f0574f5cSXin LI	      func_file_conv "$2"
148f0574f5cSXin LI	      set x "$@" -Fo"$file"
149f0574f5cSXin LI	      shift
150f0574f5cSXin LI	      ;;
151f0574f5cSXin LI	    *)
152f0574f5cSXin LI	      func_file_conv "$2"
153f0574f5cSXin LI	      set x "$@" -Fe"$file"
154f0574f5cSXin LI	      shift
155f0574f5cSXin LI	      ;;
156f0574f5cSXin LI	  esac
157f0574f5cSXin LI	  ;;
158f0574f5cSXin LI	-I)
159f0574f5cSXin LI	  eat=1
160f0574f5cSXin LI	  func_file_conv "$2" mingw
161f0574f5cSXin LI	  set x "$@" -I"$file"
162f0574f5cSXin LI	  shift
163f0574f5cSXin LI	  ;;
164f0574f5cSXin LI	-I*)
165f0574f5cSXin LI	  func_file_conv "${1#-I}" mingw
166f0574f5cSXin LI	  set x "$@" -I"$file"
167f0574f5cSXin LI	  shift
168f0574f5cSXin LI	  ;;
169f0574f5cSXin LI	-l)
170f0574f5cSXin LI	  eat=1
171f0574f5cSXin LI	  func_cl_dashl "$2"
172f0574f5cSXin LI	  set x "$@" "$lib"
173f0574f5cSXin LI	  shift
174f0574f5cSXin LI	  ;;
175f0574f5cSXin LI	-l*)
176f0574f5cSXin LI	  func_cl_dashl "${1#-l}"
177f0574f5cSXin LI	  set x "$@" "$lib"
178f0574f5cSXin LI	  shift
179f0574f5cSXin LI	  ;;
180f0574f5cSXin LI	-L)
181f0574f5cSXin LI	  eat=1
182f0574f5cSXin LI	  func_cl_dashL "$2"
183f0574f5cSXin LI	  ;;
184f0574f5cSXin LI	-L*)
185f0574f5cSXin LI	  func_cl_dashL "${1#-L}"
186f0574f5cSXin LI	  ;;
187f0574f5cSXin LI	-static)
188f0574f5cSXin LI	  shared=false
189f0574f5cSXin LI	  ;;
190f0574f5cSXin LI	-Wl,*)
191f0574f5cSXin LI	  arg=${1#-Wl,}
192f0574f5cSXin LI	  save_ifs="$IFS"; IFS=','
193f0574f5cSXin LI	  for flag in $arg; do
194f0574f5cSXin LI	    IFS="$save_ifs"
195f0574f5cSXin LI	    linker_opts="$linker_opts $flag"
196f0574f5cSXin LI	  done
197f0574f5cSXin LI	  IFS="$save_ifs"
198f0574f5cSXin LI	  ;;
199f0574f5cSXin LI	-Xlinker)
200f0574f5cSXin LI	  eat=1
201f0574f5cSXin LI	  linker_opts="$linker_opts $2"
202f0574f5cSXin LI	  ;;
203f0574f5cSXin LI	-*)
204f0574f5cSXin LI	  set x "$@" "$1"
205f0574f5cSXin LI	  shift
206f0574f5cSXin LI	  ;;
207f0574f5cSXin LI	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208f0574f5cSXin LI	  func_file_conv "$1"
209f0574f5cSXin LI	  set x "$@" -Tp"$file"
210f0574f5cSXin LI	  shift
211f0574f5cSXin LI	  ;;
212f0574f5cSXin LI	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213f0574f5cSXin LI	  func_file_conv "$1" mingw
214f0574f5cSXin LI	  set x "$@" "$file"
215f0574f5cSXin LI	  shift
216f0574f5cSXin LI	  ;;
217f0574f5cSXin LI	*)
218f0574f5cSXin LI	  set x "$@" "$1"
219f0574f5cSXin LI	  shift
220f0574f5cSXin LI	  ;;
221f0574f5cSXin LI      esac
222f0574f5cSXin LI    fi
223f0574f5cSXin LI    shift
224f0574f5cSXin LI  done
225f0574f5cSXin LI  if test -n "$linker_opts"; then
226f0574f5cSXin LI    linker_opts="-link$linker_opts"
227f0574f5cSXin LI  fi
228f0574f5cSXin LI  exec "$@" $linker_opts
229f0574f5cSXin LI  exit 1
230f0574f5cSXin LI}
231f0574f5cSXin LI
232f0574f5cSXin LIeat=
233f0574f5cSXin LI
2342b15cb3dSCy Schubertcase $1 in
2352b15cb3dSCy Schubert  '')
236f0574f5cSXin LI     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2372b15cb3dSCy Schubert     exit 1;
2382b15cb3dSCy Schubert     ;;
2392b15cb3dSCy Schubert  -h | --h*)
2402b15cb3dSCy Schubert    cat <<\EOF
2412b15cb3dSCy SchubertUsage: compile [--help] [--version] PROGRAM [ARGS]
2422b15cb3dSCy Schubert
243f0574f5cSXin LIWrapper for compilers which do not understand '-c -o'.
244f0574f5cSXin LIRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2452b15cb3dSCy Schubertarguments, and rename the output as expected.
2462b15cb3dSCy Schubert
2472b15cb3dSCy SchubertIf you are trying to build a whole package this is not the
248f0574f5cSXin LIright script to run: please start by reading the file 'INSTALL'.
2492b15cb3dSCy Schubert
2502b15cb3dSCy SchubertReport bugs to <bug-automake@gnu.org>.
2512b15cb3dSCy SchubertEOF
2522b15cb3dSCy Schubert    exit $?
2532b15cb3dSCy Schubert    ;;
2542b15cb3dSCy Schubert  -v | --v*)
2552b15cb3dSCy Schubert    echo "compile $scriptversion"
2562b15cb3dSCy Schubert    exit $?
2572b15cb3dSCy Schubert    ;;
258*2d4e511cSCy Schubert  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
259f0574f5cSXin LI    func_cl_wrapper "$@"      # Doesn't return...
260f0574f5cSXin LI    ;;
2612b15cb3dSCy Schubertesac
2622b15cb3dSCy Schubert
2632b15cb3dSCy Schubertofile=
2642b15cb3dSCy Schubertcfile=
2652b15cb3dSCy Schubert
2662b15cb3dSCy Schubertfor arg
2672b15cb3dSCy Schubertdo
2682b15cb3dSCy Schubert  if test -n "$eat"; then
2692b15cb3dSCy Schubert    eat=
2702b15cb3dSCy Schubert  else
2712b15cb3dSCy Schubert    case $1 in
2722b15cb3dSCy Schubert      -o)
273f0574f5cSXin LI	# configure might choose to run compile as 'compile cc -o foo foo.c'.
274f0574f5cSXin LI	# So we strip '-o arg' only if arg is an object.
2752b15cb3dSCy Schubert	eat=1
2762b15cb3dSCy Schubert	case $2 in
2772b15cb3dSCy Schubert	  *.o | *.obj)
2782b15cb3dSCy Schubert	    ofile=$2
2792b15cb3dSCy Schubert	    ;;
2802b15cb3dSCy Schubert	  *)
2812b15cb3dSCy Schubert	    set x "$@" -o "$2"
2822b15cb3dSCy Schubert	    shift
2832b15cb3dSCy Schubert	    ;;
2842b15cb3dSCy Schubert	esac
2852b15cb3dSCy Schubert	;;
2862b15cb3dSCy Schubert      *.c)
2872b15cb3dSCy Schubert	cfile=$1
2882b15cb3dSCy Schubert	set x "$@" "$1"
2892b15cb3dSCy Schubert	shift
2902b15cb3dSCy Schubert	;;
2912b15cb3dSCy Schubert      *)
2922b15cb3dSCy Schubert	set x "$@" "$1"
2932b15cb3dSCy Schubert	shift
2942b15cb3dSCy Schubert	;;
2952b15cb3dSCy Schubert    esac
2962b15cb3dSCy Schubert  fi
2972b15cb3dSCy Schubert  shift
2982b15cb3dSCy Schubertdone
2992b15cb3dSCy Schubert
3002b15cb3dSCy Schubertif test -z "$ofile" || test -z "$cfile"; then
301f0574f5cSXin LI  # If no '-o' option was seen then we might have been invoked from a
3022b15cb3dSCy Schubert  # pattern rule where we don't need one.  That is ok -- this is a
3032b15cb3dSCy Schubert  # normal compilation that the losing compiler can handle.  If no
304f0574f5cSXin LI  # '.c' file was seen then we are probably linking.  That is also
3052b15cb3dSCy Schubert  # ok.
3062b15cb3dSCy Schubert  exec "$@"
3072b15cb3dSCy Schubertfi
3082b15cb3dSCy Schubert
3092b15cb3dSCy Schubert# Name of file we expect compiler to create.
3102b15cb3dSCy Schubertcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3112b15cb3dSCy Schubert
3122b15cb3dSCy Schubert# Create the lock directory.
313f0574f5cSXin LI# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3142b15cb3dSCy Schubert# that we are using for the .o file.  Also, base the name on the expected
3152b15cb3dSCy Schubert# object file name, since that is what matters with a parallel build.
3162b15cb3dSCy Schubertlockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3172b15cb3dSCy Schubertwhile true; do
3182b15cb3dSCy Schubert  if mkdir "$lockdir" >/dev/null 2>&1; then
3192b15cb3dSCy Schubert    break
3202b15cb3dSCy Schubert  fi
3212b15cb3dSCy Schubert  sleep 1
3222b15cb3dSCy Schubertdone
3232b15cb3dSCy Schubert# FIXME: race condition here if user kills between mkdir and trap.
3242b15cb3dSCy Schuberttrap "rmdir '$lockdir'; exit 1" 1 2 15
3252b15cb3dSCy Schubert
3262b15cb3dSCy Schubert# Run the compile.
3272b15cb3dSCy Schubert"$@"
3282b15cb3dSCy Schubertret=$?
3292b15cb3dSCy Schubert
3302b15cb3dSCy Schubertif test -f "$cofile"; then
3312b15cb3dSCy Schubert  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3322b15cb3dSCy Schubertelif test -f "${cofile}bj"; then
3332b15cb3dSCy Schubert  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3342b15cb3dSCy Schubertfi
3352b15cb3dSCy Schubert
3362b15cb3dSCy Schubertrmdir "$lockdir"
3372b15cb3dSCy Schubertexit $ret
3382b15cb3dSCy Schubert
3392b15cb3dSCy Schubert# Local Variables:
3402b15cb3dSCy Schubert# mode: shell-script
3412b15cb3dSCy Schubert# sh-indentation: 2
3422b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
3432b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
3442b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
345*2d4e511cSCy Schubert# time-stamp-time-zone: "UTC"
3462b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
3472b15cb3dSCy Schubert# End:
348