10eb10989Smrg#! /bin/sh
2079e7944Smrg# Wrapper for compilers which do not understand '-c -o'.
30eb10989Smrg
474901992Smrgscriptversion=2018-03-07.03; # UTC
50eb10989Smrg
674901992Smrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
70eb10989Smrg# Written by Tom Tromey <tromey@cygnus.com>.
80eb10989Smrg#
90eb10989Smrg# This program is free software; you can redistribute it and/or modify
100eb10989Smrg# it under the terms of the GNU General Public License as published by
110eb10989Smrg# the Free Software Foundation; either version 2, or (at your option)
120eb10989Smrg# any later version.
130eb10989Smrg#
140eb10989Smrg# This program is distributed in the hope that it will be useful,
150eb10989Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
160eb10989Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
170eb10989Smrg# GNU General Public License for more details.
180eb10989Smrg#
190eb10989Smrg# You should have received a copy of the GNU General Public License
2074901992Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
210eb10989Smrg
220eb10989Smrg# As a special exception to the GNU General Public License, if you
230eb10989Smrg# distribute this file as part of a program that contains a
240eb10989Smrg# configuration script generated by Autoconf, you may include it under
250eb10989Smrg# the same distribution terms that you use for the rest of that program.
260eb10989Smrg
270eb10989Smrg# This file is maintained in Automake, please report
280eb10989Smrg# bugs to <bug-automake@gnu.org> or send patches to
290eb10989Smrg# <automake-patches@gnu.org>.
300eb10989Smrg
31079e7944Smrgnl='
32079e7944Smrg'
33079e7944Smrg
34079e7944Smrg# We need space, tab and new line, in precisely that order.  Quoting is
35079e7944Smrg# there to prevent tools from complaining about whitespace usage.
36079e7944SmrgIFS=" ""	$nl"
37079e7944Smrg
38079e7944Smrgfile_conv=
39079e7944Smrg
40079e7944Smrg# func_file_conv build_file lazy
41079e7944Smrg# Convert a $build file to $host form and store it in $file
42079e7944Smrg# Currently only supports Windows hosts. If the determined conversion
43079e7944Smrg# type is listed in (the comma separated) LAZY, no conversion will
44079e7944Smrg# take place.
45079e7944Smrgfunc_file_conv ()
46079e7944Smrg{
47079e7944Smrg  file=$1
48079e7944Smrg  case $file in
49079e7944Smrg    / | /[!/]*) # absolute file, and not a UNC file
50079e7944Smrg      if test -z "$file_conv"; then
51079e7944Smrg	# lazily determine how to convert abs files
52079e7944Smrg	case `uname -s` in
53079e7944Smrg	  MINGW*)
54079e7944Smrg	    file_conv=mingw
55079e7944Smrg	    ;;
5674901992Smrg	  CYGWIN* | MSYS*)
57079e7944Smrg	    file_conv=cygwin
58079e7944Smrg	    ;;
59079e7944Smrg	  *)
60079e7944Smrg	    file_conv=wine
61079e7944Smrg	    ;;
62079e7944Smrg	esac
63079e7944Smrg      fi
64079e7944Smrg      case $file_conv/,$2, in
65079e7944Smrg	*,$file_conv,*)
66079e7944Smrg	  ;;
67079e7944Smrg	mingw/*)
68079e7944Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69079e7944Smrg	  ;;
7074901992Smrg	cygwin/* | msys/*)
71079e7944Smrg	  file=`cygpath -m "$file" || echo "$file"`
72079e7944Smrg	  ;;
73079e7944Smrg	wine/*)
74079e7944Smrg	  file=`winepath -w "$file" || echo "$file"`
75079e7944Smrg	  ;;
76079e7944Smrg      esac
77079e7944Smrg      ;;
78079e7944Smrg  esac
79079e7944Smrg}
80079e7944Smrg
81079e7944Smrg# func_cl_dashL linkdir
82079e7944Smrg# Make cl look for libraries in LINKDIR
83079e7944Smrgfunc_cl_dashL ()
84079e7944Smrg{
85079e7944Smrg  func_file_conv "$1"
86079e7944Smrg  if test -z "$lib_path"; then
87079e7944Smrg    lib_path=$file
88079e7944Smrg  else
89079e7944Smrg    lib_path="$lib_path;$file"
90079e7944Smrg  fi
91079e7944Smrg  linker_opts="$linker_opts -LIBPATH:$file"
92079e7944Smrg}
93079e7944Smrg
94079e7944Smrg# func_cl_dashl library
95079e7944Smrg# Do a library search-path lookup for cl
96079e7944Smrgfunc_cl_dashl ()
97079e7944Smrg{
98079e7944Smrg  lib=$1
99079e7944Smrg  found=no
100079e7944Smrg  save_IFS=$IFS
101079e7944Smrg  IFS=';'
102079e7944Smrg  for dir in $lib_path $LIB
103079e7944Smrg  do
104079e7944Smrg    IFS=$save_IFS
105079e7944Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106079e7944Smrg      found=yes
107079e7944Smrg      lib=$dir/$lib.dll.lib
108079e7944Smrg      break
109079e7944Smrg    fi
110079e7944Smrg    if test -f "$dir/$lib.lib"; then
111079e7944Smrg      found=yes
112079e7944Smrg      lib=$dir/$lib.lib
113079e7944Smrg      break
114079e7944Smrg    fi
115079e7944Smrg    if test -f "$dir/lib$lib.a"; then
116079e7944Smrg      found=yes
117079e7944Smrg      lib=$dir/lib$lib.a
118079e7944Smrg      break
119079e7944Smrg    fi
120079e7944Smrg  done
121079e7944Smrg  IFS=$save_IFS
122079e7944Smrg
123079e7944Smrg  if test "$found" != yes; then
124079e7944Smrg    lib=$lib.lib
125079e7944Smrg  fi
126079e7944Smrg}
127079e7944Smrg
128079e7944Smrg# func_cl_wrapper cl arg...
129079e7944Smrg# Adjust compile command to suit cl
130079e7944Smrgfunc_cl_wrapper ()
131079e7944Smrg{
132079e7944Smrg  # Assume a capable shell
133079e7944Smrg  lib_path=
134079e7944Smrg  shared=:
135079e7944Smrg  linker_opts=
136079e7944Smrg  for arg
137079e7944Smrg  do
138079e7944Smrg    if test -n "$eat"; then
139079e7944Smrg      eat=
140079e7944Smrg    else
141079e7944Smrg      case $1 in
142079e7944Smrg	-o)
143079e7944Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144079e7944Smrg	  eat=1
145079e7944Smrg	  case $2 in
146079e7944Smrg	    *.o | *.[oO][bB][jJ])
147079e7944Smrg	      func_file_conv "$2"
148079e7944Smrg	      set x "$@" -Fo"$file"
149079e7944Smrg	      shift
150079e7944Smrg	      ;;
151079e7944Smrg	    *)
152079e7944Smrg	      func_file_conv "$2"
153079e7944Smrg	      set x "$@" -Fe"$file"
154079e7944Smrg	      shift
155079e7944Smrg	      ;;
156079e7944Smrg	  esac
157079e7944Smrg	  ;;
158079e7944Smrg	-I)
159079e7944Smrg	  eat=1
160079e7944Smrg	  func_file_conv "$2" mingw
161079e7944Smrg	  set x "$@" -I"$file"
162079e7944Smrg	  shift
163079e7944Smrg	  ;;
164079e7944Smrg	-I*)
165079e7944Smrg	  func_file_conv "${1#-I}" mingw
166079e7944Smrg	  set x "$@" -I"$file"
167079e7944Smrg	  shift
168079e7944Smrg	  ;;
169079e7944Smrg	-l)
170079e7944Smrg	  eat=1
171079e7944Smrg	  func_cl_dashl "$2"
172079e7944Smrg	  set x "$@" "$lib"
173079e7944Smrg	  shift
174079e7944Smrg	  ;;
175079e7944Smrg	-l*)
176079e7944Smrg	  func_cl_dashl "${1#-l}"
177079e7944Smrg	  set x "$@" "$lib"
178079e7944Smrg	  shift
179079e7944Smrg	  ;;
180079e7944Smrg	-L)
181079e7944Smrg	  eat=1
182079e7944Smrg	  func_cl_dashL "$2"
183079e7944Smrg	  ;;
184079e7944Smrg	-L*)
185079e7944Smrg	  func_cl_dashL "${1#-L}"
186079e7944Smrg	  ;;
187079e7944Smrg	-static)
188079e7944Smrg	  shared=false
189079e7944Smrg	  ;;
190079e7944Smrg	-Wl,*)
191079e7944Smrg	  arg=${1#-Wl,}
192079e7944Smrg	  save_ifs="$IFS"; IFS=','
193079e7944Smrg	  for flag in $arg; do
194079e7944Smrg	    IFS="$save_ifs"
195079e7944Smrg	    linker_opts="$linker_opts $flag"
196079e7944Smrg	  done
197079e7944Smrg	  IFS="$save_ifs"
198079e7944Smrg	  ;;
199079e7944Smrg	-Xlinker)
200079e7944Smrg	  eat=1
201079e7944Smrg	  linker_opts="$linker_opts $2"
202079e7944Smrg	  ;;
203079e7944Smrg	-*)
204079e7944Smrg	  set x "$@" "$1"
205079e7944Smrg	  shift
206079e7944Smrg	  ;;
207079e7944Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208079e7944Smrg	  func_file_conv "$1"
209079e7944Smrg	  set x "$@" -Tp"$file"
210079e7944Smrg	  shift
211079e7944Smrg	  ;;
212079e7944Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213079e7944Smrg	  func_file_conv "$1" mingw
214079e7944Smrg	  set x "$@" "$file"
215079e7944Smrg	  shift
216079e7944Smrg	  ;;
217079e7944Smrg	*)
218079e7944Smrg	  set x "$@" "$1"
219079e7944Smrg	  shift
220079e7944Smrg	  ;;
221079e7944Smrg      esac
222079e7944Smrg    fi
223079e7944Smrg    shift
224079e7944Smrg  done
225079e7944Smrg  if test -n "$linker_opts"; then
226079e7944Smrg    linker_opts="-link$linker_opts"
227079e7944Smrg  fi
228079e7944Smrg  exec "$@" $linker_opts
229079e7944Smrg  exit 1
230079e7944Smrg}
231079e7944Smrg
232079e7944Smrgeat=
233079e7944Smrg
2340eb10989Smrgcase $1 in
2350eb10989Smrg  '')
236079e7944Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
2370eb10989Smrg     exit 1;
2380eb10989Smrg     ;;
2390eb10989Smrg  -h | --h*)
2400eb10989Smrg    cat <<\EOF
2410eb10989SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
2420eb10989Smrg
243079e7944SmrgWrapper for compilers which do not understand '-c -o'.
244079e7944SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
2450eb10989Smrgarguments, and rename the output as expected.
2460eb10989Smrg
2470eb10989SmrgIf you are trying to build a whole package this is not the
248079e7944Smrgright script to run: please start by reading the file 'INSTALL'.
2490eb10989Smrg
2500eb10989SmrgReport bugs to <bug-automake@gnu.org>.
2510eb10989SmrgEOF
2520eb10989Smrg    exit $?
2530eb10989Smrg    ;;
2540eb10989Smrg  -v | --v*)
2550eb10989Smrg    echo "compile $scriptversion"
2560eb10989Smrg    exit $?
2570eb10989Smrg    ;;
25874901992Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25974901992Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260079e7944Smrg    func_cl_wrapper "$@"      # Doesn't return...
261079e7944Smrg    ;;
2620eb10989Smrgesac
2630eb10989Smrg
2640eb10989Smrgofile=
2650eb10989Smrgcfile=
2660eb10989Smrg
2670eb10989Smrgfor arg
2680eb10989Smrgdo
2690eb10989Smrg  if test -n "$eat"; then
2700eb10989Smrg    eat=
2710eb10989Smrg  else
2720eb10989Smrg    case $1 in
2730eb10989Smrg      -o)
274079e7944Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275079e7944Smrg	# So we strip '-o arg' only if arg is an object.
2760eb10989Smrg	eat=1
2770eb10989Smrg	case $2 in
2780eb10989Smrg	  *.o | *.obj)
2790eb10989Smrg	    ofile=$2
2800eb10989Smrg	    ;;
2810eb10989Smrg	  *)
2820eb10989Smrg	    set x "$@" -o "$2"
2830eb10989Smrg	    shift
2840eb10989Smrg	    ;;
2850eb10989Smrg	esac
2860eb10989Smrg	;;
2870eb10989Smrg      *.c)
2880eb10989Smrg	cfile=$1
2890eb10989Smrg	set x "$@" "$1"
2900eb10989Smrg	shift
2910eb10989Smrg	;;
2920eb10989Smrg      *)
2930eb10989Smrg	set x "$@" "$1"
2940eb10989Smrg	shift
2950eb10989Smrg	;;
2960eb10989Smrg    esac
2970eb10989Smrg  fi
2980eb10989Smrg  shift
2990eb10989Smrgdone
3000eb10989Smrg
3010eb10989Smrgif test -z "$ofile" || test -z "$cfile"; then
302079e7944Smrg  # If no '-o' option was seen then we might have been invoked from a
3030eb10989Smrg  # pattern rule where we don't need one.  That is ok -- this is a
3040eb10989Smrg  # normal compilation that the losing compiler can handle.  If no
305079e7944Smrg  # '.c' file was seen then we are probably linking.  That is also
3060eb10989Smrg  # ok.
3070eb10989Smrg  exec "$@"
3080eb10989Smrgfi
3090eb10989Smrg
3100eb10989Smrg# Name of file we expect compiler to create.
311079e7944Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
3120eb10989Smrg
3130eb10989Smrg# Create the lock directory.
314079e7944Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
3150eb10989Smrg# that we are using for the .o file.  Also, base the name on the expected
3160eb10989Smrg# object file name, since that is what matters with a parallel build.
317079e7944Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
3180eb10989Smrgwhile true; do
3190eb10989Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
3200eb10989Smrg    break
3210eb10989Smrg  fi
3220eb10989Smrg  sleep 1
3230eb10989Smrgdone
3240eb10989Smrg# FIXME: race condition here if user kills between mkdir and trap.
3250eb10989Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
3260eb10989Smrg
3270eb10989Smrg# Run the compile.
3280eb10989Smrg"$@"
3290eb10989Smrgret=$?
3300eb10989Smrg
3310eb10989Smrgif test -f "$cofile"; then
332079e7944Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
3330eb10989Smrgelif test -f "${cofile}bj"; then
334079e7944Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
3350eb10989Smrgfi
3360eb10989Smrg
3370eb10989Smrgrmdir "$lockdir"
3380eb10989Smrgexit $ret
3390eb10989Smrg
3400eb10989Smrg# Local Variables:
3410eb10989Smrg# mode: shell-script
3420eb10989Smrg# sh-indentation: 2
34374901992Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
3440eb10989Smrg# time-stamp-start: "scriptversion="
3450eb10989Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
34674901992Smrg# time-stamp-time-zone: "UTC0"
347079e7944Smrg# time-stamp-end: "; # UTC"
3480eb10989Smrg# End:
349