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