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