1bccedf53Smrg#! /bin/sh
289afc689Smrg# Wrapper for compilers which do not understand '-c -o'.
3bccedf53Smrg
4a2394c98Smrgscriptversion=2024-06-19.01; # UTC
5bccedf53Smrg
6a2394c98Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
7bccedf53Smrg# Written by Tom Tromey <tromey@cygnus.com>.
8bccedf53Smrg#
9bccedf53Smrg# This program is free software; you can redistribute it and/or modify
10bccedf53Smrg# it under the terms of the GNU General Public License as published by
11bccedf53Smrg# the Free Software Foundation; either version 2, or (at your option)
12bccedf53Smrg# any later version.
13bccedf53Smrg#
14bccedf53Smrg# This program is distributed in the hope that it will be useful,
15bccedf53Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
16bccedf53Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17bccedf53Smrg# GNU General Public License for more details.
18bccedf53Smrg#
19bccedf53Smrg# You should have received a copy of the GNU General Public License
20ec318dbfSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21bccedf53Smrg
22bccedf53Smrg# As a special exception to the GNU General Public License, if you
23bccedf53Smrg# distribute this file as part of a program that contains a
24bccedf53Smrg# configuration script generated by Autoconf, you may include it under
25bccedf53Smrg# the same distribution terms that you use for the rest of that program.
26bccedf53Smrg
2789afc689Smrg# This file is maintained in Automake, please report
2889afc689Smrg# bugs to <bug-automake@gnu.org> or send patches to
2989afc689Smrg# <automake-patches@gnu.org>.
30bccedf53Smrg
3189afc689Smrgnl='
3289afc689Smrg'
3389afc689Smrg
3489afc689Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3589afc689Smrg# there to prevent tools from complaining about whitespace usage.
3689afc689SmrgIFS=" ""	$nl"
3789afc689Smrg
3889afc689Smrgfile_conv=
3989afc689Smrg
4089afc689Smrg# func_file_conv build_file lazy
4189afc689Smrg# Convert a $build file to $host form and store it in $file
4289afc689Smrg# Currently only supports Windows hosts. If the determined conversion
4389afc689Smrg# type is listed in (the comma separated) LAZY, no conversion will
4489afc689Smrg# take place.
4589afc689Smrgfunc_file_conv ()
4689afc689Smrg{
4789afc689Smrg  file=$1
4889afc689Smrg  case $file in
4989afc689Smrg    / | /[!/]*) # absolute file, and not a UNC file
5089afc689Smrg      if test -z "$file_conv"; then
5189afc689Smrg	# lazily determine how to convert abs files
5289afc689Smrg	case `uname -s` in
5389afc689Smrg	  MINGW*)
5489afc689Smrg	    file_conv=mingw
5589afc689Smrg	    ;;
56ec318dbfSmrg	  CYGWIN* | MSYS*)
5789afc689Smrg	    file_conv=cygwin
5889afc689Smrg	    ;;
5989afc689Smrg	  *)
6089afc689Smrg	    file_conv=wine
6189afc689Smrg	    ;;
6289afc689Smrg	esac
6389afc689Smrg      fi
6489afc689Smrg      case $file_conv/,$2, in
6589afc689Smrg	*,$file_conv,*)
6689afc689Smrg	  ;;
6789afc689Smrg	mingw/*)
6889afc689Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6989afc689Smrg	  ;;
70ec318dbfSmrg	cygwin/* | msys/*)
7189afc689Smrg	  file=`cygpath -m "$file" || echo "$file"`
7289afc689Smrg	  ;;
7389afc689Smrg	wine/*)
7489afc689Smrg	  file=`winepath -w "$file" || echo "$file"`
7589afc689Smrg	  ;;
7689afc689Smrg      esac
7789afc689Smrg      ;;
7889afc689Smrg  esac
7989afc689Smrg}
8089afc689Smrg
8189afc689Smrg# func_cl_dashL linkdir
8289afc689Smrg# Make cl look for libraries in LINKDIR
8389afc689Smrgfunc_cl_dashL ()
8489afc689Smrg{
8589afc689Smrg  func_file_conv "$1"
8689afc689Smrg  if test -z "$lib_path"; then
8789afc689Smrg    lib_path=$file
8889afc689Smrg  else
8989afc689Smrg    lib_path="$lib_path;$file"
9089afc689Smrg  fi
9189afc689Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9289afc689Smrg}
9389afc689Smrg
9489afc689Smrg# func_cl_dashl library
9589afc689Smrg# Do a library search-path lookup for cl
9689afc689Smrgfunc_cl_dashl ()
9789afc689Smrg{
9889afc689Smrg  lib=$1
9989afc689Smrg  found=no
10089afc689Smrg  save_IFS=$IFS
10189afc689Smrg  IFS=';'
10289afc689Smrg  for dir in $lib_path $LIB
10389afc689Smrg  do
10489afc689Smrg    IFS=$save_IFS
10589afc689Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10689afc689Smrg      found=yes
10789afc689Smrg      lib=$dir/$lib.dll.lib
10889afc689Smrg      break
10989afc689Smrg    fi
11089afc689Smrg    if test -f "$dir/$lib.lib"; then
11189afc689Smrg      found=yes
11289afc689Smrg      lib=$dir/$lib.lib
11389afc689Smrg      break
11489afc689Smrg    fi
11589afc689Smrg    if test -f "$dir/lib$lib.a"; then
11689afc689Smrg      found=yes
11789afc689Smrg      lib=$dir/lib$lib.a
11889afc689Smrg      break
11989afc689Smrg    fi
12089afc689Smrg  done
12189afc689Smrg  IFS=$save_IFS
12289afc689Smrg
12389afc689Smrg  if test "$found" != yes; then
12489afc689Smrg    lib=$lib.lib
12589afc689Smrg  fi
12689afc689Smrg}
12789afc689Smrg
12889afc689Smrg# func_cl_wrapper cl arg...
12989afc689Smrg# Adjust compile command to suit cl
13089afc689Smrgfunc_cl_wrapper ()
13189afc689Smrg{
13289afc689Smrg  # Assume a capable shell
13389afc689Smrg  lib_path=
13489afc689Smrg  shared=:
13589afc689Smrg  linker_opts=
13689afc689Smrg  for arg
13789afc689Smrg  do
13889afc689Smrg    if test -n "$eat"; then
13989afc689Smrg      eat=
14089afc689Smrg    else
14189afc689Smrg      case $1 in
14289afc689Smrg	-o)
14389afc689Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14489afc689Smrg	  eat=1
14589afc689Smrg	  case $2 in
146a2394c98Smrg	    *.o | *.lo | *.[oO][bB][jJ])
14789afc689Smrg	      func_file_conv "$2"
14889afc689Smrg	      set x "$@" -Fo"$file"
14989afc689Smrg	      shift
15089afc689Smrg	      ;;
15189afc689Smrg	    *)
15289afc689Smrg	      func_file_conv "$2"
15389afc689Smrg	      set x "$@" -Fe"$file"
15489afc689Smrg	      shift
15589afc689Smrg	      ;;
15689afc689Smrg	  esac
15789afc689Smrg	  ;;
15889afc689Smrg	-I)
15989afc689Smrg	  eat=1
16089afc689Smrg	  func_file_conv "$2" mingw
16189afc689Smrg	  set x "$@" -I"$file"
16289afc689Smrg	  shift
16389afc689Smrg	  ;;
16489afc689Smrg	-I*)
16589afc689Smrg	  func_file_conv "${1#-I}" mingw
16689afc689Smrg	  set x "$@" -I"$file"
16789afc689Smrg	  shift
16889afc689Smrg	  ;;
16989afc689Smrg	-l)
17089afc689Smrg	  eat=1
17189afc689Smrg	  func_cl_dashl "$2"
17289afc689Smrg	  set x "$@" "$lib"
17389afc689Smrg	  shift
17489afc689Smrg	  ;;
17589afc689Smrg	-l*)
17689afc689Smrg	  func_cl_dashl "${1#-l}"
17789afc689Smrg	  set x "$@" "$lib"
17889afc689Smrg	  shift
17989afc689Smrg	  ;;
18089afc689Smrg	-L)
18189afc689Smrg	  eat=1
18289afc689Smrg	  func_cl_dashL "$2"
18389afc689Smrg	  ;;
18489afc689Smrg	-L*)
18589afc689Smrg	  func_cl_dashL "${1#-L}"
18689afc689Smrg	  ;;
18789afc689Smrg	-static)
18889afc689Smrg	  shared=false
18989afc689Smrg	  ;;
19089afc689Smrg	-Wl,*)
19189afc689Smrg	  arg=${1#-Wl,}
19289afc689Smrg	  save_ifs="$IFS"; IFS=','
19389afc689Smrg	  for flag in $arg; do
19489afc689Smrg	    IFS="$save_ifs"
19589afc689Smrg	    linker_opts="$linker_opts $flag"
19689afc689Smrg	  done
19789afc689Smrg	  IFS="$save_ifs"
19889afc689Smrg	  ;;
19989afc689Smrg	-Xlinker)
20089afc689Smrg	  eat=1
20189afc689Smrg	  linker_opts="$linker_opts $2"
20289afc689Smrg	  ;;
20389afc689Smrg	-*)
20489afc689Smrg	  set x "$@" "$1"
20589afc689Smrg	  shift
20689afc689Smrg	  ;;
20789afc689Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20889afc689Smrg	  func_file_conv "$1"
20989afc689Smrg	  set x "$@" -Tp"$file"
21089afc689Smrg	  shift
21189afc689Smrg	  ;;
21289afc689Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21389afc689Smrg	  func_file_conv "$1" mingw
21489afc689Smrg	  set x "$@" "$file"
21589afc689Smrg	  shift
21689afc689Smrg	  ;;
21789afc689Smrg	*)
21889afc689Smrg	  set x "$@" "$1"
21989afc689Smrg	  shift
22089afc689Smrg	  ;;
22189afc689Smrg      esac
22289afc689Smrg    fi
22389afc689Smrg    shift
22489afc689Smrg  done
22589afc689Smrg  if test -n "$linker_opts"; then
22689afc689Smrg    linker_opts="-link$linker_opts"
22789afc689Smrg  fi
22889afc689Smrg  exec "$@" $linker_opts
22989afc689Smrg  exit 1
23089afc689Smrg}
23189afc689Smrg
23289afc689Smrgeat=
23389afc689Smrg
23489afc689Smrgcase $1 in
23589afc689Smrg  '')
23689afc689Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23789afc689Smrg     exit 1;
23889afc689Smrg     ;;
23989afc689Smrg  -h | --h*)
24089afc689Smrg    cat <<\EOF
24189afc689SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24289afc689Smrg
24389afc689SmrgWrapper for compilers which do not understand '-c -o'.
24489afc689SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24589afc689Smrgarguments, and rename the output as expected.
24689afc689Smrg
24789afc689SmrgIf you are trying to build a whole package this is not the
24889afc689Smrgright script to run: please start by reading the file 'INSTALL'.
24989afc689Smrg
25089afc689SmrgReport bugs to <bug-automake@gnu.org>.
251a2394c98SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
252a2394c98SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
25389afc689SmrgEOF
25489afc689Smrg    exit $?
25589afc689Smrg    ;;
25689afc689Smrg  -v | --v*)
257a2394c98Smrg    echo "compile (GNU Automake) $scriptversion"
25889afc689Smrg    exit $?
25989afc689Smrg    ;;
260ec318dbfSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
261a2394c98Smrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262ec318dbfSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26389afc689Smrg    func_cl_wrapper "$@"      # Doesn't return...
26489afc689Smrg    ;;
26589afc689Smrgesac
266bccedf53Smrg
267bccedf53Smrgofile=
268bccedf53Smrgcfile=
26989afc689Smrg
27089afc689Smrgfor arg
27189afc689Smrgdo
27289afc689Smrg  if test -n "$eat"; then
27389afc689Smrg    eat=
27489afc689Smrg  else
27589afc689Smrg    case $1 in
27689afc689Smrg      -o)
27789afc689Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27889afc689Smrg	# So we strip '-o arg' only if arg is an object.
27989afc689Smrg	eat=1
28089afc689Smrg	case $2 in
28189afc689Smrg	  *.o | *.obj)
28289afc689Smrg	    ofile=$2
28389afc689Smrg	    ;;
28489afc689Smrg	  *)
28589afc689Smrg	    set x "$@" -o "$2"
28689afc689Smrg	    shift
28789afc689Smrg	    ;;
28889afc689Smrg	esac
28989afc689Smrg	;;
29089afc689Smrg      *.c)
29189afc689Smrg	cfile=$1
29289afc689Smrg	set x "$@" "$1"
29389afc689Smrg	shift
29489afc689Smrg	;;
29589afc689Smrg      *)
29689afc689Smrg	set x "$@" "$1"
29789afc689Smrg	shift
29889afc689Smrg	;;
29989afc689Smrg    esac
30089afc689Smrg  fi
30189afc689Smrg  shift
302bccedf53Smrgdone
303bccedf53Smrg
304bccedf53Smrgif test -z "$ofile" || test -z "$cfile"; then
30589afc689Smrg  # If no '-o' option was seen then we might have been invoked from a
30689afc689Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30789afc689Smrg  # normal compilation that the losing compiler can handle.  If no
30889afc689Smrg  # '.c' file was seen then we are probably linking.  That is also
30989afc689Smrg  # ok.
31089afc689Smrg  exec "$@"
311bccedf53Smrgfi
312bccedf53Smrg
313bccedf53Smrg# Name of file we expect compiler to create.
31489afc689Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
315bccedf53Smrg
316bccedf53Smrg# Create the lock directory.
31789afc689Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
318bccedf53Smrg# that we are using for the .o file.  Also, base the name on the expected
319bccedf53Smrg# object file name, since that is what matters with a parallel build.
32089afc689Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
321bccedf53Smrgwhile true; do
32289afc689Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32389afc689Smrg    break
32489afc689Smrg  fi
32589afc689Smrg  sleep 1
326bccedf53Smrgdone
327bccedf53Smrg# FIXME: race condition here if user kills between mkdir and trap.
32889afc689Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
329bccedf53Smrg
330bccedf53Smrg# Run the compile.
33189afc689Smrg"$@"
33289afc689Smrgret=$?
333bccedf53Smrg
334bccedf53Smrgif test -f "$cofile"; then
33589afc689Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33689afc689Smrgelif test -f "${cofile}bj"; then
33789afc689Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
338bccedf53Smrgfi
339bccedf53Smrg
34089afc689Smrgrmdir "$lockdir"
34189afc689Smrgexit $ret
34289afc689Smrg
34389afc689Smrg# Local Variables:
34489afc689Smrg# mode: shell-script
34589afc689Smrg# sh-indentation: 2
346ec318dbfSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34789afc689Smrg# time-stamp-start: "scriptversion="
34889afc689Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349ec318dbfSmrg# time-stamp-time-zone: "UTC0"
35089afc689Smrg# time-stamp-end: "; # UTC"
35189afc689Smrg# End:
352