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