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