1576bbdfcSmrg#! /bin/sh
2576bbdfcSmrg# Wrapper for compilers which do not understand '-c -o'.
3576bbdfcSmrg
4c8df0c59Smrgscriptversion=2024-06-19.01; # UTC
5576bbdfcSmrg
6c8df0c59Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
7576bbdfcSmrg# Written by Tom Tromey <tromey@cygnus.com>.
8576bbdfcSmrg#
9576bbdfcSmrg# This program is free software; you can redistribute it and/or modify
10576bbdfcSmrg# it under the terms of the GNU General Public License as published by
11576bbdfcSmrg# the Free Software Foundation; either version 2, or (at your option)
12576bbdfcSmrg# any later version.
13576bbdfcSmrg#
14576bbdfcSmrg# This program is distributed in the hope that it will be useful,
15576bbdfcSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
16576bbdfcSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17576bbdfcSmrg# GNU General Public License for more details.
18576bbdfcSmrg#
19576bbdfcSmrg# You should have received a copy of the GNU General Public License
20f9c28e31Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21576bbdfcSmrg
22576bbdfcSmrg# As a special exception to the GNU General Public License, if you
23576bbdfcSmrg# distribute this file as part of a program that contains a
24576bbdfcSmrg# configuration script generated by Autoconf, you may include it under
25576bbdfcSmrg# the same distribution terms that you use for the rest of that program.
26576bbdfcSmrg
27576bbdfcSmrg# This file is maintained in Automake, please report
28576bbdfcSmrg# bugs to <bug-automake@gnu.org> or send patches to
29576bbdfcSmrg# <automake-patches@gnu.org>.
30576bbdfcSmrg
31576bbdfcSmrgnl='
32576bbdfcSmrg'
33576bbdfcSmrg
34576bbdfcSmrg# We need space, tab and new line, in precisely that order.  Quoting is
35576bbdfcSmrg# there to prevent tools from complaining about whitespace usage.
36576bbdfcSmrgIFS=" ""	$nl"
37576bbdfcSmrg
38576bbdfcSmrgfile_conv=
39576bbdfcSmrg
40576bbdfcSmrg# func_file_conv build_file lazy
41576bbdfcSmrg# Convert a $build file to $host form and store it in $file
42576bbdfcSmrg# Currently only supports Windows hosts. If the determined conversion
43576bbdfcSmrg# type is listed in (the comma separated) LAZY, no conversion will
44576bbdfcSmrg# take place.
45576bbdfcSmrgfunc_file_conv ()
46576bbdfcSmrg{
47576bbdfcSmrg  file=$1
48576bbdfcSmrg  case $file in
49576bbdfcSmrg    / | /[!/]*) # absolute file, and not a UNC file
50576bbdfcSmrg      if test -z "$file_conv"; then
51576bbdfcSmrg	# lazily determine how to convert abs files
52576bbdfcSmrg	case `uname -s` in
53576bbdfcSmrg	  MINGW*)
54576bbdfcSmrg	    file_conv=mingw
55576bbdfcSmrg	    ;;
56f9c28e31Smrg	  CYGWIN* | MSYS*)
57576bbdfcSmrg	    file_conv=cygwin
58576bbdfcSmrg	    ;;
59576bbdfcSmrg	  *)
60576bbdfcSmrg	    file_conv=wine
61576bbdfcSmrg	    ;;
62576bbdfcSmrg	esac
63576bbdfcSmrg      fi
64576bbdfcSmrg      case $file_conv/,$2, in
65576bbdfcSmrg	*,$file_conv,*)
66576bbdfcSmrg	  ;;
67576bbdfcSmrg	mingw/*)
68576bbdfcSmrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69576bbdfcSmrg	  ;;
70f9c28e31Smrg	cygwin/* | msys/*)
71576bbdfcSmrg	  file=`cygpath -m "$file" || echo "$file"`
72576bbdfcSmrg	  ;;
73576bbdfcSmrg	wine/*)
74576bbdfcSmrg	  file=`winepath -w "$file" || echo "$file"`
75576bbdfcSmrg	  ;;
76576bbdfcSmrg      esac
77576bbdfcSmrg      ;;
78576bbdfcSmrg  esac
79576bbdfcSmrg}
80576bbdfcSmrg
81576bbdfcSmrg# func_cl_dashL linkdir
82576bbdfcSmrg# Make cl look for libraries in LINKDIR
83576bbdfcSmrgfunc_cl_dashL ()
84576bbdfcSmrg{
85576bbdfcSmrg  func_file_conv "$1"
86576bbdfcSmrg  if test -z "$lib_path"; then
87576bbdfcSmrg    lib_path=$file
88576bbdfcSmrg  else
89576bbdfcSmrg    lib_path="$lib_path;$file"
90576bbdfcSmrg  fi
91576bbdfcSmrg  linker_opts="$linker_opts -LIBPATH:$file"
92576bbdfcSmrg}
93576bbdfcSmrg
94576bbdfcSmrg# func_cl_dashl library
95576bbdfcSmrg# Do a library search-path lookup for cl
96576bbdfcSmrgfunc_cl_dashl ()
97576bbdfcSmrg{
98576bbdfcSmrg  lib=$1
99576bbdfcSmrg  found=no
100576bbdfcSmrg  save_IFS=$IFS
101576bbdfcSmrg  IFS=';'
102576bbdfcSmrg  for dir in $lib_path $LIB
103576bbdfcSmrg  do
104576bbdfcSmrg    IFS=$save_IFS
105576bbdfcSmrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106576bbdfcSmrg      found=yes
107576bbdfcSmrg      lib=$dir/$lib.dll.lib
108576bbdfcSmrg      break
109576bbdfcSmrg    fi
110576bbdfcSmrg    if test -f "$dir/$lib.lib"; then
111576bbdfcSmrg      found=yes
112576bbdfcSmrg      lib=$dir/$lib.lib
113576bbdfcSmrg      break
114576bbdfcSmrg    fi
115576bbdfcSmrg    if test -f "$dir/lib$lib.a"; then
116576bbdfcSmrg      found=yes
117576bbdfcSmrg      lib=$dir/lib$lib.a
118576bbdfcSmrg      break
119576bbdfcSmrg    fi
120576bbdfcSmrg  done
121576bbdfcSmrg  IFS=$save_IFS
122576bbdfcSmrg
123576bbdfcSmrg  if test "$found" != yes; then
124576bbdfcSmrg    lib=$lib.lib
125576bbdfcSmrg  fi
126576bbdfcSmrg}
127576bbdfcSmrg
128576bbdfcSmrg# func_cl_wrapper cl arg...
129576bbdfcSmrg# Adjust compile command to suit cl
130576bbdfcSmrgfunc_cl_wrapper ()
131576bbdfcSmrg{
132576bbdfcSmrg  # Assume a capable shell
133576bbdfcSmrg  lib_path=
134576bbdfcSmrg  shared=:
135576bbdfcSmrg  linker_opts=
136576bbdfcSmrg  for arg
137576bbdfcSmrg  do
138576bbdfcSmrg    if test -n "$eat"; then
139576bbdfcSmrg      eat=
140576bbdfcSmrg    else
141576bbdfcSmrg      case $1 in
142576bbdfcSmrg	-o)
143576bbdfcSmrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144576bbdfcSmrg	  eat=1
145576bbdfcSmrg	  case $2 in
146c8df0c59Smrg	    *.o | *.lo | *.[oO][bB][jJ])
147576bbdfcSmrg	      func_file_conv "$2"
148576bbdfcSmrg	      set x "$@" -Fo"$file"
149576bbdfcSmrg	      shift
150576bbdfcSmrg	      ;;
151576bbdfcSmrg	    *)
152576bbdfcSmrg	      func_file_conv "$2"
153576bbdfcSmrg	      set x "$@" -Fe"$file"
154576bbdfcSmrg	      shift
155576bbdfcSmrg	      ;;
156576bbdfcSmrg	  esac
157576bbdfcSmrg	  ;;
158576bbdfcSmrg	-I)
159576bbdfcSmrg	  eat=1
160576bbdfcSmrg	  func_file_conv "$2" mingw
161576bbdfcSmrg	  set x "$@" -I"$file"
162576bbdfcSmrg	  shift
163576bbdfcSmrg	  ;;
164576bbdfcSmrg	-I*)
165576bbdfcSmrg	  func_file_conv "${1#-I}" mingw
166576bbdfcSmrg	  set x "$@" -I"$file"
167576bbdfcSmrg	  shift
168576bbdfcSmrg	  ;;
169576bbdfcSmrg	-l)
170576bbdfcSmrg	  eat=1
171576bbdfcSmrg	  func_cl_dashl "$2"
172576bbdfcSmrg	  set x "$@" "$lib"
173576bbdfcSmrg	  shift
174576bbdfcSmrg	  ;;
175576bbdfcSmrg	-l*)
176576bbdfcSmrg	  func_cl_dashl "${1#-l}"
177576bbdfcSmrg	  set x "$@" "$lib"
178576bbdfcSmrg	  shift
179576bbdfcSmrg	  ;;
180576bbdfcSmrg	-L)
181576bbdfcSmrg	  eat=1
182576bbdfcSmrg	  func_cl_dashL "$2"
183576bbdfcSmrg	  ;;
184576bbdfcSmrg	-L*)
185576bbdfcSmrg	  func_cl_dashL "${1#-L}"
186576bbdfcSmrg	  ;;
187576bbdfcSmrg	-static)
188576bbdfcSmrg	  shared=false
189576bbdfcSmrg	  ;;
190576bbdfcSmrg	-Wl,*)
191576bbdfcSmrg	  arg=${1#-Wl,}
192576bbdfcSmrg	  save_ifs="$IFS"; IFS=','
193576bbdfcSmrg	  for flag in $arg; do
194576bbdfcSmrg	    IFS="$save_ifs"
195576bbdfcSmrg	    linker_opts="$linker_opts $flag"
196576bbdfcSmrg	  done
197576bbdfcSmrg	  IFS="$save_ifs"
198576bbdfcSmrg	  ;;
199576bbdfcSmrg	-Xlinker)
200576bbdfcSmrg	  eat=1
201576bbdfcSmrg	  linker_opts="$linker_opts $2"
202576bbdfcSmrg	  ;;
203576bbdfcSmrg	-*)
204576bbdfcSmrg	  set x "$@" "$1"
205576bbdfcSmrg	  shift
206576bbdfcSmrg	  ;;
207576bbdfcSmrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208576bbdfcSmrg	  func_file_conv "$1"
209576bbdfcSmrg	  set x "$@" -Tp"$file"
210576bbdfcSmrg	  shift
211576bbdfcSmrg	  ;;
212576bbdfcSmrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213576bbdfcSmrg	  func_file_conv "$1" mingw
214576bbdfcSmrg	  set x "$@" "$file"
215576bbdfcSmrg	  shift
216576bbdfcSmrg	  ;;
217576bbdfcSmrg	*)
218576bbdfcSmrg	  set x "$@" "$1"
219576bbdfcSmrg	  shift
220576bbdfcSmrg	  ;;
221576bbdfcSmrg      esac
222576bbdfcSmrg    fi
223576bbdfcSmrg    shift
224576bbdfcSmrg  done
225576bbdfcSmrg  if test -n "$linker_opts"; then
226576bbdfcSmrg    linker_opts="-link$linker_opts"
227576bbdfcSmrg  fi
228576bbdfcSmrg  exec "$@" $linker_opts
229576bbdfcSmrg  exit 1
230576bbdfcSmrg}
231576bbdfcSmrg
232576bbdfcSmrgeat=
233576bbdfcSmrg
234576bbdfcSmrgcase $1 in
235576bbdfcSmrg  '')
236576bbdfcSmrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237576bbdfcSmrg     exit 1;
238576bbdfcSmrg     ;;
239576bbdfcSmrg  -h | --h*)
240576bbdfcSmrg    cat <<\EOF
241576bbdfcSmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
242576bbdfcSmrg
243576bbdfcSmrgWrapper for compilers which do not understand '-c -o'.
244576bbdfcSmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245576bbdfcSmrgarguments, and rename the output as expected.
246576bbdfcSmrg
247576bbdfcSmrgIf you are trying to build a whole package this is not the
248576bbdfcSmrgright script to run: please start by reading the file 'INSTALL'.
249576bbdfcSmrg
250576bbdfcSmrgReport bugs to <bug-automake@gnu.org>.
251c8df0c59SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
252c8df0c59SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
253576bbdfcSmrgEOF
254576bbdfcSmrg    exit $?
255576bbdfcSmrg    ;;
256576bbdfcSmrg  -v | --v*)
257c8df0c59Smrg    echo "compile (GNU Automake) $scriptversion"
258576bbdfcSmrg    exit $?
259576bbdfcSmrg    ;;
260f9c28e31Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
261c8df0c59Smrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262f9c28e31Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
263576bbdfcSmrg    func_cl_wrapper "$@"      # Doesn't return...
264576bbdfcSmrg    ;;
265576bbdfcSmrgesac
266576bbdfcSmrg
267576bbdfcSmrgofile=
268576bbdfcSmrgcfile=
269576bbdfcSmrg
270576bbdfcSmrgfor arg
271576bbdfcSmrgdo
272576bbdfcSmrg  if test -n "$eat"; then
273576bbdfcSmrg    eat=
274576bbdfcSmrg  else
275576bbdfcSmrg    case $1 in
276576bbdfcSmrg      -o)
277576bbdfcSmrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
278576bbdfcSmrg	# So we strip '-o arg' only if arg is an object.
279576bbdfcSmrg	eat=1
280576bbdfcSmrg	case $2 in
281576bbdfcSmrg	  *.o | *.obj)
282576bbdfcSmrg	    ofile=$2
283576bbdfcSmrg	    ;;
284576bbdfcSmrg	  *)
285576bbdfcSmrg	    set x "$@" -o "$2"
286576bbdfcSmrg	    shift
287576bbdfcSmrg	    ;;
288576bbdfcSmrg	esac
289576bbdfcSmrg	;;
290576bbdfcSmrg      *.c)
291576bbdfcSmrg	cfile=$1
292576bbdfcSmrg	set x "$@" "$1"
293576bbdfcSmrg	shift
294576bbdfcSmrg	;;
295576bbdfcSmrg      *)
296576bbdfcSmrg	set x "$@" "$1"
297576bbdfcSmrg	shift
298576bbdfcSmrg	;;
299576bbdfcSmrg    esac
300576bbdfcSmrg  fi
301576bbdfcSmrg  shift
302576bbdfcSmrgdone
303576bbdfcSmrg
304576bbdfcSmrgif test -z "$ofile" || test -z "$cfile"; then
305576bbdfcSmrg  # If no '-o' option was seen then we might have been invoked from a
306576bbdfcSmrg  # pattern rule where we don't need one.  That is ok -- this is a
307576bbdfcSmrg  # normal compilation that the losing compiler can handle.  If no
308576bbdfcSmrg  # '.c' file was seen then we are probably linking.  That is also
309576bbdfcSmrg  # ok.
310576bbdfcSmrg  exec "$@"
311576bbdfcSmrgfi
312576bbdfcSmrg
313576bbdfcSmrg# Name of file we expect compiler to create.
314576bbdfcSmrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
315576bbdfcSmrg
316576bbdfcSmrg# Create the lock directory.
317576bbdfcSmrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
318576bbdfcSmrg# that we are using for the .o file.  Also, base the name on the expected
319576bbdfcSmrg# object file name, since that is what matters with a parallel build.
320576bbdfcSmrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
321576bbdfcSmrgwhile true; do
322576bbdfcSmrg  if mkdir "$lockdir" >/dev/null 2>&1; then
323576bbdfcSmrg    break
324576bbdfcSmrg  fi
325576bbdfcSmrg  sleep 1
326576bbdfcSmrgdone
327576bbdfcSmrg# FIXME: race condition here if user kills between mkdir and trap.
328576bbdfcSmrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
329576bbdfcSmrg
330576bbdfcSmrg# Run the compile.
331576bbdfcSmrg"$@"
332576bbdfcSmrgret=$?
333576bbdfcSmrg
334576bbdfcSmrgif test -f "$cofile"; then
335576bbdfcSmrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
336576bbdfcSmrgelif test -f "${cofile}bj"; then
337576bbdfcSmrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
338576bbdfcSmrgfi
339576bbdfcSmrg
340576bbdfcSmrgrmdir "$lockdir"
341576bbdfcSmrgexit $ret
342576bbdfcSmrg
343576bbdfcSmrg# Local Variables:
344576bbdfcSmrg# mode: shell-script
345576bbdfcSmrg# sh-indentation: 2
346f9c28e31Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
347576bbdfcSmrg# time-stamp-start: "scriptversion="
348576bbdfcSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349f9c28e31Smrg# time-stamp-time-zone: "UTC0"
350576bbdfcSmrg# time-stamp-end: "; # UTC"
351576bbdfcSmrg# End:
352