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