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