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