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