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