compile revision 27485fbc
1d6c0b56eSmrg#! /bin/sh
2d6c0b56eSmrg# Wrapper for compilers which do not understand '-c -o'.
3d6c0b56eSmrg
4d6c0b56eSmrgscriptversion=2018-03-07.03; # UTC
5d6c0b56eSmrg
6d6c0b56eSmrg# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7d6c0b56eSmrg# Written by Tom Tromey <tromey@cygnus.com>.
8d6c0b56eSmrg#
9d6c0b56eSmrg# This program is free software; you can redistribute it and/or modify
10d6c0b56eSmrg# it under the terms of the GNU General Public License as published by
11d6c0b56eSmrg# the Free Software Foundation; either version 2, or (at your option)
12d6c0b56eSmrg# any later version.
13d6c0b56eSmrg#
14d6c0b56eSmrg# This program is distributed in the hope that it will be useful,
15d6c0b56eSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
16d6c0b56eSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17d6c0b56eSmrg# GNU General Public License for more details.
18d6c0b56eSmrg#
19d6c0b56eSmrg# You should have received a copy of the GNU General Public License
20d6c0b56eSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21d6c0b56eSmrg
22d6c0b56eSmrg# As a special exception to the GNU General Public License, if you
23d6c0b56eSmrg# distribute this file as part of a program that contains a
24d6c0b56eSmrg# configuration script generated by Autoconf, you may include it under
25d6c0b56eSmrg# the same distribution terms that you use for the rest of that program.
26d6c0b56eSmrg
27d6c0b56eSmrg# This file is maintained in Automake, please report
28d6c0b56eSmrg# bugs to <bug-automake@gnu.org> or send patches to
29d6c0b56eSmrg# <automake-patches@gnu.org>.
30d6c0b56eSmrg
31d6c0b56eSmrgnl='
32d6c0b56eSmrg'
33d6c0b56eSmrg
34d6c0b56eSmrg# We need space, tab and new line, in precisely that order.  Quoting is
35d6c0b56eSmrg# there to prevent tools from complaining about whitespace usage.
36d6c0b56eSmrgIFS=" ""	$nl"
3724b90cf4Smrg
3824b90cf4Smrgfile_conv=
39d6c0b56eSmrg
4035d5b7c7Smrg# func_file_conv build_file lazy
41d6c0b56eSmrg# Convert a $build file to $host form and store it in $file
42504d986fSmrg# Currently only supports Windows hosts. If the determined conversion
4346845023Smrg# type is listed in (the comma separated) LAZY, no conversion will
44d6c0b56eSmrg# take place.
45d6c0b56eSmrgfunc_file_conv ()
46d6c0b56eSmrg{
47d6c0b56eSmrg  file=$1
48d6c0b56eSmrg  case $file in
49d6c0b56eSmrg    / | /[!/]*) # absolute file, and not a UNC file
50d6c0b56eSmrg      if test -z "$file_conv"; then
51d6c0b56eSmrg	# lazily determine how to convert abs files
52d6c0b56eSmrg	case `uname -s` in
53d6c0b56eSmrg	  MINGW*)
54d6c0b56eSmrg	    file_conv=mingw
55d6c0b56eSmrg	    ;;
56d6c0b56eSmrg	  CYGWIN* | MSYS*)
57d6c0b56eSmrg	    file_conv=cygwin
58d6c0b56eSmrg	    ;;
59d6c0b56eSmrg	  *)
60d6c0b56eSmrg	    file_conv=wine
61d6c0b56eSmrg	    ;;
62d6c0b56eSmrg	esac
63d6c0b56eSmrg      fi
64d6c0b56eSmrg      case $file_conv/,$2, in
65d6c0b56eSmrg	*,$file_conv,*)
66d6c0b56eSmrg	  ;;
67d6c0b56eSmrg	mingw/*)
68d6c0b56eSmrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69d6c0b56eSmrg	  ;;
70d6c0b56eSmrg	cygwin/* | msys/*)
71d6c0b56eSmrg	  file=`cygpath -m "$file" || echo "$file"`
72d6c0b56eSmrg	  ;;
73d6c0b56eSmrg	wine/*)
74d6c0b56eSmrg	  file=`winepath -w "$file" || echo "$file"`
75d6c0b56eSmrg	  ;;
76d6c0b56eSmrg      esac
77d6c0b56eSmrg      ;;
78d6c0b56eSmrg  esac
79d6c0b56eSmrg}
80d6c0b56eSmrg
81d6c0b56eSmrg# func_cl_dashL linkdir
82d6c0b56eSmrg# Make cl look for libraries in LINKDIR
83d6c0b56eSmrgfunc_cl_dashL ()
84d6c0b56eSmrg{
85d6c0b56eSmrg  func_file_conv "$1"
86d6c0b56eSmrg  if test -z "$lib_path"; then
87d6c0b56eSmrg    lib_path=$file
88d6c0b56eSmrg  else
89d6c0b56eSmrg    lib_path="$lib_path;$file"
90d6c0b56eSmrg  fi
91d6c0b56eSmrg  linker_opts="$linker_opts -LIBPATH:$file"
92d6c0b56eSmrg}
93d6c0b56eSmrg
94d6c0b56eSmrg# func_cl_dashl library
95d6c0b56eSmrg# Do a library search-path lookup for cl
96d6c0b56eSmrgfunc_cl_dashl ()
9724b90cf4Smrg{
98d6c0b56eSmrg  lib=$1
99d6c0b56eSmrg  found=no
100d6c0b56eSmrg  save_IFS=$IFS
101d6c0b56eSmrg  IFS=';'
102d6c0b56eSmrg  for dir in $lib_path $LIB
103d6c0b56eSmrg  do
104d6c0b56eSmrg    IFS=$save_IFS
105d6c0b56eSmrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106d6c0b56eSmrg      found=yes
107d6c0b56eSmrg      lib=$dir/$lib.dll.lib
108d6c0b56eSmrg      break
109d6c0b56eSmrg    fi
110d6c0b56eSmrg    if test -f "$dir/$lib.lib"; then
111d6c0b56eSmrg      found=yes
112d6c0b56eSmrg      lib=$dir/$lib.lib
113d6c0b56eSmrg      break
114d6c0b56eSmrg    fi
115d6c0b56eSmrg    if test -f "$dir/lib$lib.a"; then
116d6c0b56eSmrg      found=yes
117d6c0b56eSmrg      lib=$dir/lib$lib.a
118d6c0b56eSmrg      break
119d6c0b56eSmrg    fi
120d6c0b56eSmrg  done
121d6c0b56eSmrg  IFS=$save_IFS
122d6c0b56eSmrg
123d6c0b56eSmrg  if test "$found" != yes; then
124d6c0b56eSmrg    lib=$lib.lib
125d6c0b56eSmrg  fi
126d6c0b56eSmrg}
127d6c0b56eSmrg
128d6c0b56eSmrg# func_cl_wrapper cl arg...
129d6c0b56eSmrg# Adjust compile command to suit cl
130d6c0b56eSmrgfunc_cl_wrapper ()
131d6c0b56eSmrg{
132d6c0b56eSmrg  # Assume a capable shell
133d6c0b56eSmrg  lib_path=
134d6c0b56eSmrg  shared=:
135d6c0b56eSmrg  linker_opts=
136d6c0b56eSmrg  for arg
137d6c0b56eSmrg  do
138d6c0b56eSmrg    if test -n "$eat"; then
139d6c0b56eSmrg      eat=
140d6c0b56eSmrg    else
141d6c0b56eSmrg      case $1 in
142d6c0b56eSmrg	-o)
143d6c0b56eSmrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144d6c0b56eSmrg	  eat=1
145d6c0b56eSmrg	  case $2 in
146d6c0b56eSmrg	    *.o | *.[oO][bB][jJ])
147d6c0b56eSmrg	      func_file_conv "$2"
148d6c0b56eSmrg	      set x "$@" -Fo"$file"
149d6c0b56eSmrg	      shift
150d6c0b56eSmrg	      ;;
151d6c0b56eSmrg	    *)
152d6c0b56eSmrg	      func_file_conv "$2"
153d6c0b56eSmrg	      set x "$@" -Fe"$file"
154d6c0b56eSmrg	      shift
15524b90cf4Smrg	      ;;
15624b90cf4Smrg	  esac
15724b90cf4Smrg	  ;;
15824b90cf4Smrg	-I)
15924b90cf4Smrg	  eat=1
16024b90cf4Smrg	  func_file_conv "$2" mingw
16124b90cf4Smrg	  set x "$@" -I"$file"
16224b90cf4Smrg	  shift
16324b90cf4Smrg	  ;;
16424b90cf4Smrg	-I*)
16524b90cf4Smrg	  func_file_conv "${1#-I}" mingw
16624b90cf4Smrg	  set x "$@" -I"$file"
16724b90cf4Smrg	  shift
16824b90cf4Smrg	  ;;
16924b90cf4Smrg	-l)
17024b90cf4Smrg	  eat=1
17124b90cf4Smrg	  func_cl_dashl "$2"
17224b90cf4Smrg	  set x "$@" "$lib"
17324b90cf4Smrg	  shift
17424b90cf4Smrg	  ;;
17524b90cf4Smrg	-l*)
17624b90cf4Smrg	  func_cl_dashl "${1#-l}"
17724b90cf4Smrg	  set x "$@" "$lib"
17824b90cf4Smrg	  shift
17924b90cf4Smrg	  ;;
18024b90cf4Smrg	-L)
18124b90cf4Smrg	  eat=1
18224b90cf4Smrg	  func_cl_dashL "$2"
18324b90cf4Smrg	  ;;
18424b90cf4Smrg	-L*)
18524b90cf4Smrg	  func_cl_dashL "${1#-L}"
18624b90cf4Smrg	  ;;
18724b90cf4Smrg	-static)
18824b90cf4Smrg	  shared=false
18924b90cf4Smrg	  ;;
190d6c0b56eSmrg	-Wl,*)
191d6c0b56eSmrg	  arg=${1#-Wl,}
192d6c0b56eSmrg	  save_ifs="$IFS"; IFS=','
193d6c0b56eSmrg	  for flag in $arg; do
194d6c0b56eSmrg	    IFS="$save_ifs"
195d6c0b56eSmrg	    linker_opts="$linker_opts $flag"
196d6c0b56eSmrg	  done
197d6c0b56eSmrg	  IFS="$save_ifs"
198d6c0b56eSmrg	  ;;
199d6c0b56eSmrg	-Xlinker)
200d6c0b56eSmrg	  eat=1
201d6c0b56eSmrg	  linker_opts="$linker_opts $2"
202d6c0b56eSmrg	  ;;
203d6c0b56eSmrg	-*)
204d6c0b56eSmrg	  set x "$@" "$1"
205d6c0b56eSmrg	  shift
206d6c0b56eSmrg	  ;;
207d6c0b56eSmrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208d6c0b56eSmrg	  func_file_conv "$1"
209d6c0b56eSmrg	  set x "$@" -Tp"$file"
210d6c0b56eSmrg	  shift
211d6c0b56eSmrg	  ;;
212d6c0b56eSmrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213d6c0b56eSmrg	  func_file_conv "$1" mingw
214d6c0b56eSmrg	  set x "$@" "$file"
215d6c0b56eSmrg	  shift
216d6c0b56eSmrg	  ;;
217d6c0b56eSmrg	*)
218d6c0b56eSmrg	  set x "$@" "$1"
219d6c0b56eSmrg	  shift
22024b90cf4Smrg	  ;;
221d6c0b56eSmrg      esac
22224b90cf4Smrg    fi
223d6c0b56eSmrg    shift
224d6c0b56eSmrg  done
22524b90cf4Smrg  if test -n "$linker_opts"; then
226d6c0b56eSmrg    linker_opts="-link$linker_opts"
227d6c0b56eSmrg  fi
22824b90cf4Smrg  exec "$@" $linker_opts
229d6c0b56eSmrg  exit 1
230d6c0b56eSmrg}
231d6c0b56eSmrg
232d6c0b56eSmrgeat=
23390f2b693Smrg
23490f2b693Smrgcase $1 in
23590f2b693Smrg  '')
23690f2b693Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23790f2b693Smrg     exit 1;
23890f2b693Smrg     ;;
23990f2b693Smrg  -h | --h*)
24090f2b693Smrg    cat <<\EOF
24190f2b693SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24290f2b693Smrg
24390f2b693SmrgWrapper for compilers which do not understand '-c -o'.
24490f2b693SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24590f2b693Smrgarguments, and rename the output as expected.
24690f2b693Smrg
24790f2b693SmrgIf you are trying to build a whole package this is not the
24890f2b693Smrgright script to run: please start by reading the file 'INSTALL'.
24990f2b693Smrg
25090f2b693SmrgReport bugs to <bug-automake@gnu.org>.
25190f2b693SmrgEOF
25290f2b693Smrg    exit $?
25390f2b693Smrg    ;;
25490f2b693Smrg  -v | --v*)
25590f2b693Smrg    echo "compile $scriptversion"
25690f2b693Smrg    exit $?
25790f2b693Smrg    ;;
25890f2b693Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
25990f2b693Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26090f2b693Smrg    func_cl_wrapper "$@"      # Doesn't return...
26190f2b693Smrg    ;;
26290f2b693Smrgesac
26390f2b693Smrg
26490f2b693Smrgofile=
26590f2b693Smrgcfile=
26690f2b693Smrg
26790f2b693Smrgfor arg
26890f2b693Smrgdo
26990f2b693Smrg  if test -n "$eat"; then
27090f2b693Smrg    eat=
27190f2b693Smrg  else
27290f2b693Smrg    case $1 in
27390f2b693Smrg      -o)
27490f2b693Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27590f2b693Smrg	# So we strip '-o arg' only if arg is an object.
27690f2b693Smrg	eat=1
27790f2b693Smrg	case $2 in
27890f2b693Smrg	  *.o | *.obj)
27990f2b693Smrg	    ofile=$2
28090f2b693Smrg	    ;;
28190f2b693Smrg	  *)
28290f2b693Smrg	    set x "$@" -o "$2"
28390f2b693Smrg	    shift
28490f2b693Smrg	    ;;
28590f2b693Smrg	esac
28690f2b693Smrg	;;
28790f2b693Smrg      *.c)
28890f2b693Smrg	cfile=$1
28990f2b693Smrg	set x "$@" "$1"
29090f2b693Smrg	shift
29190f2b693Smrg	;;
29290f2b693Smrg      *)
29390f2b693Smrg	set x "$@" "$1"
29490f2b693Smrg	shift
29590f2b693Smrg	;;
296d6c0b56eSmrg    esac
297d6c0b56eSmrg  fi
298d6c0b56eSmrg  shift
299d6c0b56eSmrgdone
300d6c0b56eSmrg
301d6c0b56eSmrgif test -z "$ofile" || test -z "$cfile"; then
302d6c0b56eSmrg  # If no '-o' option was seen then we might have been invoked from a
303d6c0b56eSmrg  # pattern rule where we don't need one.  That is ok -- this is a
304d6c0b56eSmrg  # normal compilation that the losing compiler can handle.  If no
305d6c0b56eSmrg  # '.c' file was seen then we are probably linking.  That is also
30624b90cf4Smrg  # ok.
307d6c0b56eSmrg  exec "$@"
30835d5b7c7Smrgfi
309504d986fSmrg
310d6c0b56eSmrg# Name of file we expect compiler to create.
311d6c0b56eSmrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312d6c0b56eSmrg
313d6c0b56eSmrg# Create the lock directory.
31424b90cf4Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31524b90cf4Smrg# that we are using for the .o file.  Also, base the name on the expected
316d6c0b56eSmrg# object file name, since that is what matters with a parallel build.
317d6c0b56eSmrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318d6c0b56eSmrgwhile true; do
319d6c0b56eSmrg  if mkdir "$lockdir" >/dev/null 2>&1; then
320d6c0b56eSmrg    break
321d6c0b56eSmrg  fi
322d6c0b56eSmrg  sleep 1
323d6c0b56eSmrgdone
324d6c0b56eSmrg# FIXME: race condition here if user kills between mkdir and trap.
325d6c0b56eSmrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
326d6c0b56eSmrg
327d6c0b56eSmrg# Run the compile.
328d6c0b56eSmrg"$@"
329d6c0b56eSmrgret=$?
330d6c0b56eSmrg
331d6c0b56eSmrgif test -f "$cofile"; then
332d6c0b56eSmrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33335d5b7c7Smrgelif test -f "${cofile}bj"; then
33435d5b7c7Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33535d5b7c7Smrgfi
336d6c0b56eSmrg
337d6c0b56eSmrgrmdir "$lockdir"
338d6c0b56eSmrgexit $ret
339d6c0b56eSmrg
340d6c0b56eSmrg# Local Variables:
341d6c0b56eSmrg# mode: shell-script
342d6c0b56eSmrg# sh-indentation: 2
343d6c0b56eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
344d6c0b56eSmrg# time-stamp-start: "scriptversion="
345d6c0b56eSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
346d6c0b56eSmrg# time-stamp-time-zone: "UTC0"
347d6c0b56eSmrg# time-stamp-end: "; # UTC"
348d6c0b56eSmrg# End:
349d6c0b56eSmrg