File indexing completed on 2024-12-22 04:04:16

0001 #! /bin/sh
0002 # Wrapper for compilers which do not understand '-c -o'.
0003 
0004 scriptversion=2018-03-07.03; # UTC
0005 
0006 # Copyright (C) 1999-2018 Free Software Foundation, Inc.
0007 # Written by Tom Tromey <tromey@cygnus.com>.
0008 #
0009 # This program is free software; you can redistribute it and/or modify
0010 # it under the terms of the GNU General Public License as published by
0011 # the Free Software Foundation; either version 2, or (at your option)
0012 # any later version.
0013 #
0014 # This program is distributed in the hope that it will be useful,
0015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 # GNU General Public License for more details.
0018 #
0019 # You should have received a copy of the GNU General Public License
0020 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
0021 
0022 # As a special exception to the GNU General Public License, if you
0023 # distribute this file as part of a program that contains a
0024 # configuration script generated by Autoconf, you may include it under
0025 # the same distribution terms that you use for the rest of that program.
0026 
0027 # This file is maintained in Automake, please report
0028 # bugs to <bug-automake@gnu.org> or send patches to
0029 # <automake-patches@gnu.org>.
0030 
0031 nl='
0032 '
0033 
0034 # We need space, tab and new line, in precisely that order.  Quoting is
0035 # there to prevent tools from complaining about whitespace usage.
0036 IFS=" ""        $nl"
0037 
0038 file_conv=
0039 
0040 # func_file_conv build_file lazy
0041 # Convert a $build file to $host form and store it in $file
0042 # Currently only supports Windows hosts. If the determined conversion
0043 # type is listed in (the comma separated) LAZY, no conversion will
0044 # take place.
0045 func_file_conv ()
0046 {
0047   file=$1
0048   case $file in
0049     / | /[!/]*) # absolute file, and not a UNC file
0050       if test -z "$file_conv"; then
0051         # lazily determine how to convert abs files
0052         case `uname -s` in
0053           MINGW*)
0054             file_conv=mingw
0055             ;;
0056           CYGWIN*)
0057             file_conv=cygwin
0058             ;;
0059           *)
0060             file_conv=wine
0061             ;;
0062         esac
0063       fi
0064       case $file_conv/,$2, in
0065         *,$file_conv,*)
0066           ;;
0067         mingw/*)
0068           file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
0069           ;;
0070         cygwin/*)
0071           file=`cygpath -m "$file" || echo "$file"`
0072           ;;
0073         wine/*)
0074           file=`winepath -w "$file" || echo "$file"`
0075           ;;
0076       esac
0077       ;;
0078   esac
0079 }
0080 
0081 # func_cl_dashL linkdir
0082 # Make cl look for libraries in LINKDIR
0083 func_cl_dashL ()
0084 {
0085   func_file_conv "$1"
0086   if test -z "$lib_path"; then
0087     lib_path=$file
0088   else
0089     lib_path="$lib_path;$file"
0090   fi
0091   linker_opts="$linker_opts -LIBPATH:$file"
0092 }
0093 
0094 # func_cl_dashl library
0095 # Do a library search-path lookup for cl
0096 func_cl_dashl ()
0097 {
0098   lib=$1
0099   found=no
0100   save_IFS=$IFS
0101   IFS=';'
0102   for dir in $lib_path $LIB
0103   do
0104     IFS=$save_IFS
0105     if $shared && test -f "$dir/$lib.dll.lib"; then
0106       found=yes
0107       lib=$dir/$lib.dll.lib
0108       break
0109     fi
0110     if test -f "$dir/$lib.lib"; then
0111       found=yes
0112       lib=$dir/$lib.lib
0113       break
0114     fi
0115     if test -f "$dir/lib$lib.a"; then
0116       found=yes
0117       lib=$dir/lib$lib.a
0118       break
0119     fi
0120   done
0121   IFS=$save_IFS
0122 
0123   if test "$found" != yes; then
0124     lib=$lib.lib
0125   fi
0126 }
0127 
0128 # func_cl_wrapper cl arg...
0129 # Adjust compile command to suit cl
0130 func_cl_wrapper ()
0131 {
0132   # Assume a capable shell
0133   lib_path=
0134   shared=:
0135   linker_opts=
0136   for arg
0137   do
0138     if test -n "$eat"; then
0139       eat=
0140     else
0141       case $1 in
0142         -o)
0143           # configure might choose to run compile as 'compile cc -o foo foo.c'.
0144           eat=1
0145           case $2 in
0146             *.o | *.[oO][bB][jJ])
0147               func_file_conv "$2"
0148               set x "$@" -Fo"$file"
0149               shift
0150               ;;
0151             *)
0152               func_file_conv "$2"
0153               set x "$@" -Fe"$file"
0154               shift
0155               ;;
0156           esac
0157           ;;
0158         -I)
0159           eat=1
0160           func_file_conv "$2" mingw
0161           set x "$@" -I"$file"
0162           shift
0163           ;;
0164         -I*)
0165           func_file_conv "${1#-I}" mingw
0166           set x "$@" -I"$file"
0167           shift
0168           ;;
0169         -l)
0170           eat=1
0171           func_cl_dashl "$2"
0172           set x "$@" "$lib"
0173           shift
0174           ;;
0175         -l*)
0176           func_cl_dashl "${1#-l}"
0177           set x "$@" "$lib"
0178           shift
0179           ;;
0180         -L)
0181           eat=1
0182           func_cl_dashL "$2"
0183           ;;
0184         -L*)
0185           func_cl_dashL "${1#-L}"
0186           ;;
0187         -static)
0188           shared=false
0189           ;;
0190         -Wl,*)
0191           arg=${1#-Wl,}
0192           save_ifs="$IFS"; IFS=','
0193           for flag in $arg; do
0194             IFS="$save_ifs"
0195             linker_opts="$linker_opts $flag"
0196           done
0197           IFS="$save_ifs"
0198           ;;
0199         -Xlinker)
0200           eat=1
0201           linker_opts="$linker_opts $2"
0202           ;;
0203         -*)
0204           set x "$@" "$1"
0205           shift
0206           ;;
0207         *.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
0208           func_file_conv "$1"
0209           set x "$@" -Tp"$file"
0210           shift
0211           ;;
0212         *.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
0213           func_file_conv "$1" mingw
0214           set x "$@" "$file"
0215           shift
0216           ;;
0217         *)
0218           set x "$@" "$1"
0219           shift
0220           ;;
0221       esac
0222     fi
0223     shift
0224   done
0225   if test -n "$linker_opts"; then
0226     linker_opts="-link$linker_opts"
0227   fi
0228   exec "$@" $linker_opts
0229   exit 1
0230 }
0231 
0232 eat=
0233 
0234 case $1 in
0235   '')
0236      echo "$0: No command.  Try '$0 --help' for more information." 1>&2
0237      exit 1;
0238      ;;
0239   -h | --h*)
0240     cat <<\EOF
0241 Usage: compile [--help] [--version] PROGRAM [ARGS]
0242 
0243 Wrapper for compilers which do not understand '-c -o'.
0244 Remove '-o dest.o' from ARGS, run PROGRAM with the remaining
0245 arguments, and rename the output as expected.
0246 
0247 If you are trying to build a whole package this is not the
0248 right script to run: please start by reading the file 'INSTALL'.
0249 
0250 Report bugs to <bug-automake@gnu.org>.
0251 EOF
0252     exit $?
0253     ;;
0254   -v | --v*)
0255     echo "compile $scriptversion"
0256     exit $?
0257     ;;
0258   cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
0259   icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
0260     func_cl_wrapper "$@"      # Doesn't return...
0261     ;;
0262 esac
0263 
0264 ofile=
0265 cfile=
0266 
0267 for arg
0268 do
0269   if test -n "$eat"; then
0270     eat=
0271   else
0272     case $1 in
0273       -o)
0274         # configure might choose to run compile as 'compile cc -o foo foo.c'.
0275         # So we strip '-o arg' only if arg is an object.
0276         eat=1
0277         case $2 in
0278           *.o | *.obj)
0279             ofile=$2
0280             ;;
0281           *)
0282             set x "$@" -o "$2"
0283             shift
0284             ;;
0285         esac
0286         ;;
0287       *.c)
0288         cfile=$1
0289         set x "$@" "$1"
0290         shift
0291         ;;
0292       *)
0293         set x "$@" "$1"
0294         shift
0295         ;;
0296     esac
0297   fi
0298   shift
0299 done
0300 
0301 if test -z "$ofile" || test -z "$cfile"; then
0302   # If no '-o' option was seen then we might have been invoked from a
0303   # pattern rule where we don't need one.  That is ok -- this is a
0304   # normal compilation that the losing compiler can handle.  If no
0305   # '.c' file was seen then we are probably linking.  That is also
0306   # ok.
0307   exec "$@"
0308 fi
0309 
0310 # Name of file we expect compiler to create.
0311 cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
0312 
0313 # Create the lock directory.
0314 # Note: use '[/\\:.-]' here to ensure that we don't use the same name
0315 # that we are using for the .o file.  Also, base the name on the expected
0316 # object file name, since that is what matters with a parallel build.
0317 lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
0318 while true; do
0319   if mkdir "$lockdir" >/dev/null 2>&1; then
0320     break
0321   fi
0322   sleep 1
0323 done
0324 # FIXME: race condition here if user kills between mkdir and trap.
0325 trap "rmdir '$lockdir'; exit 1" 1 2 15
0326 
0327 # Run the compile.
0328 "$@"
0329 ret=$?
0330 
0331 if test -f "$cofile"; then
0332   test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
0333 elif test -f "${cofile}bj"; then
0334   test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
0335 fi
0336 
0337 rmdir "$lockdir"
0338 exit $ret
0339 
0340 # Local Variables:
0341 # mode: shell-script
0342 # sh-indentation: 2
0343 # eval: (add-hook 'before-save-hook 'time-stamp)
0344 # time-stamp-start: "scriptversion="
0345 # time-stamp-format: "%:y-%02m-%02d.%02H"
0346 # time-stamp-time-zone: "UTC0"
0347 # time-stamp-end: "; # UTC"
0348 # End: