1319fa471Smrg#! /bin/sh
2319fa471Smrg# Wrapper for compilers which do not understand '-c -o'.
3319fa471Smrg
47aed6334Smrgscriptversion=2018-03-07.03; # UTC
5319fa471Smrg
67aed6334Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7319fa471Smrg# Written by Tom Tromey <tromey@cygnus.com>.
8319fa471Smrg#
9319fa471Smrg# This program is free software; you can redistribute it and/or modify
10319fa471Smrg# it under the terms of the GNU General Public License as published by
11319fa471Smrg# the Free Software Foundation; either version 2, or (at your option)
12319fa471Smrg# any later version.
13319fa471Smrg#
14319fa471Smrg# This program is distributed in the hope that it will be useful,
15319fa471Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
16319fa471Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17319fa471Smrg# GNU General Public License for more details.
18319fa471Smrg#
19319fa471Smrg# You should have received a copy of the GNU General Public License
207aed6334Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21319fa471Smrg
22319fa471Smrg# As a special exception to the GNU General Public License, if you
23319fa471Smrg# distribute this file as part of a program that contains a
24319fa471Smrg# configuration script generated by Autoconf, you may include it under
25319fa471Smrg# the same distribution terms that you use for the rest of that program.
26319fa471Smrg
27319fa471Smrg# This file is maintained in Automake, please report
28319fa471Smrg# bugs to <bug-automake@gnu.org> or send patches to
29319fa471Smrg# <automake-patches@gnu.org>.
30319fa471Smrg
31319fa471Smrgnl='
32319fa471Smrg'
33319fa471Smrg
34319fa471Smrg# We need space, tab and new line, in precisely that order.  Quoting is
35319fa471Smrg# there to prevent tools from complaining about whitespace usage.
36319fa471SmrgIFS=" ""	$nl"
37319fa471Smrg
38319fa471Smrgfile_conv=
39319fa471Smrg
40319fa471Smrg# func_file_conv build_file lazy
41319fa471Smrg# Convert a $build file to $host form and store it in $file
42319fa471Smrg# Currently only supports Windows hosts. If the determined conversion
43319fa471Smrg# type is listed in (the comma separated) LAZY, no conversion will
44319fa471Smrg# take place.
45319fa471Smrgfunc_file_conv ()
46319fa471Smrg{
47319fa471Smrg  file=$1
48319fa471Smrg  case $file in
49319fa471Smrg    / | /[!/]*) # absolute file, and not a UNC file
50319fa471Smrg      if test -z "$file_conv"; then
51319fa471Smrg	# lazily determine how to convert abs files
52319fa471Smrg	case `uname -s` in
53319fa471Smrg	  MINGW*)
54319fa471Smrg	    file_conv=mingw
55319fa471Smrg	    ;;
567aed6334Smrg	  CYGWIN* | MSYS*)
57319fa471Smrg	    file_conv=cygwin
58319fa471Smrg	    ;;
59319fa471Smrg	  *)
60319fa471Smrg	    file_conv=wine
61319fa471Smrg	    ;;
62319fa471Smrg	esac
63319fa471Smrg      fi
64319fa471Smrg      case $file_conv/,$2, in
65319fa471Smrg	*,$file_conv,*)
66319fa471Smrg	  ;;
67319fa471Smrg	mingw/*)
68319fa471Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69319fa471Smrg	  ;;
707aed6334Smrg	cygwin/* | msys/*)
71319fa471Smrg	  file=`cygpath -m "$file" || echo "$file"`
72319fa471Smrg	  ;;
73319fa471Smrg	wine/*)
74319fa471Smrg	  file=`winepath -w "$file" || echo "$file"`
75319fa471Smrg	  ;;
76319fa471Smrg      esac
77319fa471Smrg      ;;
78319fa471Smrg  esac
79319fa471Smrg}
80319fa471Smrg
81319fa471Smrg# func_cl_dashL linkdir
82319fa471Smrg# Make cl look for libraries in LINKDIR
83319fa471Smrgfunc_cl_dashL ()
84319fa471Smrg{
85319fa471Smrg  func_file_conv "$1"
86319fa471Smrg  if test -z "$lib_path"; then
87319fa471Smrg    lib_path=$file
88319fa471Smrg  else
89319fa471Smrg    lib_path="$lib_path;$file"
90319fa471Smrg  fi
91319fa471Smrg  linker_opts="$linker_opts -LIBPATH:$file"
92319fa471Smrg}
93319fa471Smrg
94319fa471Smrg# func_cl_dashl library
95319fa471Smrg# Do a library search-path lookup for cl
96319fa471Smrgfunc_cl_dashl ()
97319fa471Smrg{
98319fa471Smrg  lib=$1
99319fa471Smrg  found=no
100319fa471Smrg  save_IFS=$IFS
101319fa471Smrg  IFS=';'
102319fa471Smrg  for dir in $lib_path $LIB
103319fa471Smrg  do
104319fa471Smrg    IFS=$save_IFS
105319fa471Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106319fa471Smrg      found=yes
107319fa471Smrg      lib=$dir/$lib.dll.lib
108319fa471Smrg      break
109319fa471Smrg    fi
110319fa471Smrg    if test -f "$dir/$lib.lib"; then
111319fa471Smrg      found=yes
112319fa471Smrg      lib=$dir/$lib.lib
113319fa471Smrg      break
114319fa471Smrg    fi
115319fa471Smrg    if test -f "$dir/lib$lib.a"; then
116319fa471Smrg      found=yes
117319fa471Smrg      lib=$dir/lib$lib.a
118319fa471Smrg      break
119319fa471Smrg    fi
120319fa471Smrg  done
121319fa471Smrg  IFS=$save_IFS
122319fa471Smrg
123319fa471Smrg  if test "$found" != yes; then
124319fa471Smrg    lib=$lib.lib
125319fa471Smrg  fi
126319fa471Smrg}
127319fa471Smrg
128319fa471Smrg# func_cl_wrapper cl arg...
129319fa471Smrg# Adjust compile command to suit cl
130319fa471Smrgfunc_cl_wrapper ()
131319fa471Smrg{
132319fa471Smrg  # Assume a capable shell
133319fa471Smrg  lib_path=
134319fa471Smrg  shared=:
135319fa471Smrg  linker_opts=
136319fa471Smrg  for arg
137319fa471Smrg  do
138319fa471Smrg    if test -n "$eat"; then
139319fa471Smrg      eat=
140319fa471Smrg    else
141319fa471Smrg      case $1 in
142319fa471Smrg	-o)
143319fa471Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144319fa471Smrg	  eat=1
145319fa471Smrg	  case $2 in
146319fa471Smrg	    *.o | *.[oO][bB][jJ])
147319fa471Smrg	      func_file_conv "$2"
148319fa471Smrg	      set x "$@" -Fo"$file"
149319fa471Smrg	      shift
150319fa471Smrg	      ;;
151319fa471Smrg	    *)
152319fa471Smrg	      func_file_conv "$2"
153319fa471Smrg	      set x "$@" -Fe"$file"
154319fa471Smrg	      shift
155319fa471Smrg	      ;;
156319fa471Smrg	  esac
157319fa471Smrg	  ;;
158319fa471Smrg	-I)
159319fa471Smrg	  eat=1
160319fa471Smrg	  func_file_conv "$2" mingw
161319fa471Smrg	  set x "$@" -I"$file"
162319fa471Smrg	  shift
163319fa471Smrg	  ;;
164319fa471Smrg	-I*)
165319fa471Smrg	  func_file_conv "${1#-I}" mingw
166319fa471Smrg	  set x "$@" -I"$file"
167319fa471Smrg	  shift
168319fa471Smrg	  ;;
169319fa471Smrg	-l)
170319fa471Smrg	  eat=1
171319fa471Smrg	  func_cl_dashl "$2"
172319fa471Smrg	  set x "$@" "$lib"
173319fa471Smrg	  shift
174319fa471Smrg	  ;;
175319fa471Smrg	-l*)
176319fa471Smrg	  func_cl_dashl "${1#-l}"
177319fa471Smrg	  set x "$@" "$lib"
178319fa471Smrg	  shift
179319fa471Smrg	  ;;
180319fa471Smrg	-L)
181319fa471Smrg	  eat=1
182319fa471Smrg	  func_cl_dashL "$2"
183319fa471Smrg	  ;;
184319fa471Smrg	-L*)
185319fa471Smrg	  func_cl_dashL "${1#-L}"
186319fa471Smrg	  ;;
187319fa471Smrg	-static)
188319fa471Smrg	  shared=false
189319fa471Smrg	  ;;
190319fa471Smrg	-Wl,*)
191319fa471Smrg	  arg=${1#-Wl,}
192319fa471Smrg	  save_ifs="$IFS"; IFS=','
193319fa471Smrg	  for flag in $arg; do
194319fa471Smrg	    IFS="$save_ifs"
195319fa471Smrg	    linker_opts="$linker_opts $flag"
196319fa471Smrg	  done
197319fa471Smrg	  IFS="$save_ifs"
198319fa471Smrg	  ;;
199319fa471Smrg	-Xlinker)
200319fa471Smrg	  eat=1
201319fa471Smrg	  linker_opts="$linker_opts $2"
202319fa471Smrg	  ;;
203319fa471Smrg	-*)
204319fa471Smrg	  set x "$@" "$1"
205319fa471Smrg	  shift
206319fa471Smrg	  ;;
207319fa471Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208319fa471Smrg	  func_file_conv "$1"
209319fa471Smrg	  set x "$@" -Tp"$file"
210319fa471Smrg	  shift
211319fa471Smrg	  ;;
212319fa471Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213319fa471Smrg	  func_file_conv "$1" mingw
214319fa471Smrg	  set x "$@" "$file"
215319fa471Smrg	  shift
216319fa471Smrg	  ;;
217319fa471Smrg	*)
218319fa471Smrg	  set x "$@" "$1"
219319fa471Smrg	  shift
220319fa471Smrg	  ;;
221319fa471Smrg      esac
222319fa471Smrg    fi
223319fa471Smrg    shift
224319fa471Smrg  done
225319fa471Smrg  if test -n "$linker_opts"; then
226319fa471Smrg    linker_opts="-link$linker_opts"
227319fa471Smrg  fi
228319fa471Smrg  exec "$@" $linker_opts
229319fa471Smrg  exit 1
230319fa471Smrg}
231319fa471Smrg
232319fa471Smrgeat=
233319fa471Smrg
234319fa471Smrgcase $1 in
235319fa471Smrg  '')
236319fa471Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237319fa471Smrg     exit 1;
238319fa471Smrg     ;;
239319fa471Smrg  -h | --h*)
240319fa471Smrg    cat <<\EOF
241319fa471SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
242319fa471Smrg
243319fa471SmrgWrapper for compilers which do not understand '-c -o'.
244319fa471SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245319fa471Smrgarguments, and rename the output as expected.
246319fa471Smrg
247319fa471SmrgIf you are trying to build a whole package this is not the
248319fa471Smrgright script to run: please start by reading the file 'INSTALL'.
249319fa471Smrg
250319fa471SmrgReport bugs to <bug-automake@gnu.org>.
251319fa471SmrgEOF
252319fa471Smrg    exit $?
253319fa471Smrg    ;;
254319fa471Smrg  -v | --v*)
255319fa471Smrg    echo "compile $scriptversion"
256319fa471Smrg    exit $?
257319fa471Smrg    ;;
2587aed6334Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
2597aed6334Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260319fa471Smrg    func_cl_wrapper "$@"      # Doesn't return...
261319fa471Smrg    ;;
262319fa471Smrgesac
263319fa471Smrg
264319fa471Smrgofile=
265319fa471Smrgcfile=
266319fa471Smrg
267319fa471Smrgfor arg
268319fa471Smrgdo
269319fa471Smrg  if test -n "$eat"; then
270319fa471Smrg    eat=
271319fa471Smrg  else
272319fa471Smrg    case $1 in
273319fa471Smrg      -o)
274319fa471Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275319fa471Smrg	# So we strip '-o arg' only if arg is an object.
276319fa471Smrg	eat=1
277319fa471Smrg	case $2 in
278319fa471Smrg	  *.o | *.obj)
279319fa471Smrg	    ofile=$2
280319fa471Smrg	    ;;
281319fa471Smrg	  *)
282319fa471Smrg	    set x "$@" -o "$2"
283319fa471Smrg	    shift
284319fa471Smrg	    ;;
285319fa471Smrg	esac
286319fa471Smrg	;;
287319fa471Smrg      *.c)
288319fa471Smrg	cfile=$1
289319fa471Smrg	set x "$@" "$1"
290319fa471Smrg	shift
291319fa471Smrg	;;
292319fa471Smrg      *)
293319fa471Smrg	set x "$@" "$1"
294319fa471Smrg	shift
295319fa471Smrg	;;
296319fa471Smrg    esac
297319fa471Smrg  fi
298319fa471Smrg  shift
299319fa471Smrgdone
300319fa471Smrg
301319fa471Smrgif test -z "$ofile" || test -z "$cfile"; then
302319fa471Smrg  # If no '-o' option was seen then we might have been invoked from a
303319fa471Smrg  # pattern rule where we don't need one.  That is ok -- this is a
304319fa471Smrg  # normal compilation that the losing compiler can handle.  If no
305319fa471Smrg  # '.c' file was seen then we are probably linking.  That is also
306319fa471Smrg  # ok.
307319fa471Smrg  exec "$@"
308319fa471Smrgfi
309319fa471Smrg
310319fa471Smrg# Name of file we expect compiler to create.
311319fa471Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312319fa471Smrg
313319fa471Smrg# Create the lock directory.
314319fa471Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315319fa471Smrg# that we are using for the .o file.  Also, base the name on the expected
316319fa471Smrg# object file name, since that is what matters with a parallel build.
317319fa471Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318319fa471Smrgwhile true; do
319319fa471Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
320319fa471Smrg    break
321319fa471Smrg  fi
322319fa471Smrg  sleep 1
323319fa471Smrgdone
324319fa471Smrg# FIXME: race condition here if user kills between mkdir and trap.
325319fa471Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
326319fa471Smrg
327319fa471Smrg# Run the compile.
328319fa471Smrg"$@"
329319fa471Smrgret=$?
330319fa471Smrg
331319fa471Smrgif test -f "$cofile"; then
332319fa471Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333319fa471Smrgelif test -f "${cofile}bj"; then
334319fa471Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335319fa471Smrgfi
336319fa471Smrg
337319fa471Smrgrmdir "$lockdir"
338319fa471Smrgexit $ret
339319fa471Smrg
340319fa471Smrg# Local Variables:
341319fa471Smrg# mode: shell-script
342319fa471Smrg# sh-indentation: 2
3437aed6334Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
344319fa471Smrg# time-stamp-start: "scriptversion="
345319fa471Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
3467aed6334Smrg# time-stamp-time-zone: "UTC0"
347319fa471Smrg# time-stamp-end: "; # UTC"
348319fa471Smrg# End:
349