131637056Smrg#! /bin/sh
231637056Smrg# Wrapper for compilers which do not understand '-c -o'.
331637056Smrg
410f94802Smrgscriptversion=2024-06-19.01; # UTC
531637056Smrg
610f94802Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
731637056Smrg# Written by Tom Tromey <tromey@cygnus.com>.
831637056Smrg#
931637056Smrg# This program is free software; you can redistribute it and/or modify
1031637056Smrg# it under the terms of the GNU General Public License as published by
1131637056Smrg# the Free Software Foundation; either version 2, or (at your option)
1231637056Smrg# any later version.
1331637056Smrg#
1431637056Smrg# This program is distributed in the hope that it will be useful,
1531637056Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1631637056Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1731637056Smrg# GNU General Public License for more details.
1831637056Smrg#
1931637056Smrg# You should have received a copy of the GNU General Public License
20b41a30aaSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2131637056Smrg
2231637056Smrg# As a special exception to the GNU General Public License, if you
2331637056Smrg# distribute this file as part of a program that contains a
2431637056Smrg# configuration script generated by Autoconf, you may include it under
2531637056Smrg# the same distribution terms that you use for the rest of that program.
2631637056Smrg
2731637056Smrg# This file is maintained in Automake, please report
2831637056Smrg# bugs to <bug-automake@gnu.org> or send patches to
2931637056Smrg# <automake-patches@gnu.org>.
3031637056Smrg
3131637056Smrgnl='
3231637056Smrg'
3331637056Smrg
3431637056Smrg# We need space, tab and new line, in precisely that order.  Quoting is
3531637056Smrg# there to prevent tools from complaining about whitespace usage.
3631637056SmrgIFS=" ""	$nl"
3731637056Smrg
3831637056Smrgfile_conv=
3931637056Smrg
4031637056Smrg# func_file_conv build_file lazy
4131637056Smrg# Convert a $build file to $host form and store it in $file
4231637056Smrg# Currently only supports Windows hosts. If the determined conversion
4331637056Smrg# type is listed in (the comma separated) LAZY, no conversion will
4431637056Smrg# take place.
4531637056Smrgfunc_file_conv ()
4631637056Smrg{
4731637056Smrg  file=$1
4831637056Smrg  case $file in
4931637056Smrg    / | /[!/]*) # absolute file, and not a UNC file
5031637056Smrg      if test -z "$file_conv"; then
5131637056Smrg	# lazily determine how to convert abs files
5231637056Smrg	case `uname -s` in
5331637056Smrg	  MINGW*)
5431637056Smrg	    file_conv=mingw
5531637056Smrg	    ;;
56b41a30aaSmrg	  CYGWIN* | MSYS*)
5731637056Smrg	    file_conv=cygwin
5831637056Smrg	    ;;
5931637056Smrg	  *)
6031637056Smrg	    file_conv=wine
6131637056Smrg	    ;;
6231637056Smrg	esac
6331637056Smrg      fi
6431637056Smrg      case $file_conv/,$2, in
6531637056Smrg	*,$file_conv,*)
6631637056Smrg	  ;;
6731637056Smrg	mingw/*)
6831637056Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
6931637056Smrg	  ;;
70b41a30aaSmrg	cygwin/* | msys/*)
7131637056Smrg	  file=`cygpath -m "$file" || echo "$file"`
7231637056Smrg	  ;;
7331637056Smrg	wine/*)
7431637056Smrg	  file=`winepath -w "$file" || echo "$file"`
7531637056Smrg	  ;;
7631637056Smrg      esac
7731637056Smrg      ;;
7831637056Smrg  esac
7931637056Smrg}
8031637056Smrg
8131637056Smrg# func_cl_dashL linkdir
8231637056Smrg# Make cl look for libraries in LINKDIR
8331637056Smrgfunc_cl_dashL ()
8431637056Smrg{
8531637056Smrg  func_file_conv "$1"
8631637056Smrg  if test -z "$lib_path"; then
8731637056Smrg    lib_path=$file
8831637056Smrg  else
8931637056Smrg    lib_path="$lib_path;$file"
9031637056Smrg  fi
9131637056Smrg  linker_opts="$linker_opts -LIBPATH:$file"
9231637056Smrg}
9331637056Smrg
9431637056Smrg# func_cl_dashl library
9531637056Smrg# Do a library search-path lookup for cl
9631637056Smrgfunc_cl_dashl ()
9731637056Smrg{
9831637056Smrg  lib=$1
9931637056Smrg  found=no
10031637056Smrg  save_IFS=$IFS
10131637056Smrg  IFS=';'
10231637056Smrg  for dir in $lib_path $LIB
10331637056Smrg  do
10431637056Smrg    IFS=$save_IFS
10531637056Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
10631637056Smrg      found=yes
10731637056Smrg      lib=$dir/$lib.dll.lib
10831637056Smrg      break
10931637056Smrg    fi
11031637056Smrg    if test -f "$dir/$lib.lib"; then
11131637056Smrg      found=yes
11231637056Smrg      lib=$dir/$lib.lib
11331637056Smrg      break
11431637056Smrg    fi
11531637056Smrg    if test -f "$dir/lib$lib.a"; then
11631637056Smrg      found=yes
11731637056Smrg      lib=$dir/lib$lib.a
11831637056Smrg      break
11931637056Smrg    fi
12031637056Smrg  done
12131637056Smrg  IFS=$save_IFS
12231637056Smrg
12331637056Smrg  if test "$found" != yes; then
12431637056Smrg    lib=$lib.lib
12531637056Smrg  fi
12631637056Smrg}
12731637056Smrg
12831637056Smrg# func_cl_wrapper cl arg...
12931637056Smrg# Adjust compile command to suit cl
13031637056Smrgfunc_cl_wrapper ()
13131637056Smrg{
13231637056Smrg  # Assume a capable shell
13331637056Smrg  lib_path=
13431637056Smrg  shared=:
13531637056Smrg  linker_opts=
13631637056Smrg  for arg
13731637056Smrg  do
13831637056Smrg    if test -n "$eat"; then
13931637056Smrg      eat=
14031637056Smrg    else
14131637056Smrg      case $1 in
14231637056Smrg	-o)
14331637056Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
14431637056Smrg	  eat=1
14531637056Smrg	  case $2 in
14610f94802Smrg	    *.o | *.lo | *.[oO][bB][jJ])
14731637056Smrg	      func_file_conv "$2"
14831637056Smrg	      set x "$@" -Fo"$file"
14931637056Smrg	      shift
15031637056Smrg	      ;;
15131637056Smrg	    *)
15231637056Smrg	      func_file_conv "$2"
15331637056Smrg	      set x "$@" -Fe"$file"
15431637056Smrg	      shift
15531637056Smrg	      ;;
15631637056Smrg	  esac
15731637056Smrg	  ;;
15831637056Smrg	-I)
15931637056Smrg	  eat=1
16031637056Smrg	  func_file_conv "$2" mingw
16131637056Smrg	  set x "$@" -I"$file"
16231637056Smrg	  shift
16331637056Smrg	  ;;
16431637056Smrg	-I*)
16531637056Smrg	  func_file_conv "${1#-I}" mingw
16631637056Smrg	  set x "$@" -I"$file"
16731637056Smrg	  shift
16831637056Smrg	  ;;
16931637056Smrg	-l)
17031637056Smrg	  eat=1
17131637056Smrg	  func_cl_dashl "$2"
17231637056Smrg	  set x "$@" "$lib"
17331637056Smrg	  shift
17431637056Smrg	  ;;
17531637056Smrg	-l*)
17631637056Smrg	  func_cl_dashl "${1#-l}"
17731637056Smrg	  set x "$@" "$lib"
17831637056Smrg	  shift
17931637056Smrg	  ;;
18031637056Smrg	-L)
18131637056Smrg	  eat=1
18231637056Smrg	  func_cl_dashL "$2"
18331637056Smrg	  ;;
18431637056Smrg	-L*)
18531637056Smrg	  func_cl_dashL "${1#-L}"
18631637056Smrg	  ;;
18731637056Smrg	-static)
18831637056Smrg	  shared=false
18931637056Smrg	  ;;
19031637056Smrg	-Wl,*)
19131637056Smrg	  arg=${1#-Wl,}
19231637056Smrg	  save_ifs="$IFS"; IFS=','
19331637056Smrg	  for flag in $arg; do
19431637056Smrg	    IFS="$save_ifs"
19531637056Smrg	    linker_opts="$linker_opts $flag"
19631637056Smrg	  done
19731637056Smrg	  IFS="$save_ifs"
19831637056Smrg	  ;;
19931637056Smrg	-Xlinker)
20031637056Smrg	  eat=1
20131637056Smrg	  linker_opts="$linker_opts $2"
20231637056Smrg	  ;;
20331637056Smrg	-*)
20431637056Smrg	  set x "$@" "$1"
20531637056Smrg	  shift
20631637056Smrg	  ;;
20731637056Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
20831637056Smrg	  func_file_conv "$1"
20931637056Smrg	  set x "$@" -Tp"$file"
21031637056Smrg	  shift
21131637056Smrg	  ;;
21231637056Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
21331637056Smrg	  func_file_conv "$1" mingw
21431637056Smrg	  set x "$@" "$file"
21531637056Smrg	  shift
21631637056Smrg	  ;;
21731637056Smrg	*)
21831637056Smrg	  set x "$@" "$1"
21931637056Smrg	  shift
22031637056Smrg	  ;;
22131637056Smrg      esac
22231637056Smrg    fi
22331637056Smrg    shift
22431637056Smrg  done
22531637056Smrg  if test -n "$linker_opts"; then
22631637056Smrg    linker_opts="-link$linker_opts"
22731637056Smrg  fi
22831637056Smrg  exec "$@" $linker_opts
22931637056Smrg  exit 1
23031637056Smrg}
23131637056Smrg
23231637056Smrgeat=
23331637056Smrg
23431637056Smrgcase $1 in
23531637056Smrg  '')
23631637056Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23731637056Smrg     exit 1;
23831637056Smrg     ;;
23931637056Smrg  -h | --h*)
24031637056Smrg    cat <<\EOF
24131637056SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24231637056Smrg
24331637056SmrgWrapper for compilers which do not understand '-c -o'.
24431637056SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24531637056Smrgarguments, and rename the output as expected.
24631637056Smrg
24731637056SmrgIf you are trying to build a whole package this is not the
24831637056Smrgright script to run: please start by reading the file 'INSTALL'.
24931637056Smrg
25031637056SmrgReport bugs to <bug-automake@gnu.org>.
25110f94802SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
25210f94802SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
25331637056SmrgEOF
25431637056Smrg    exit $?
25531637056Smrg    ;;
25631637056Smrg  -v | --v*)
25710f94802Smrg    echo "compile (GNU Automake) $scriptversion"
25831637056Smrg    exit $?
25931637056Smrg    ;;
260b41a30aaSmrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
26110f94802Smrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262b41a30aaSmrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
26331637056Smrg    func_cl_wrapper "$@"      # Doesn't return...
26431637056Smrg    ;;
26531637056Smrgesac
26631637056Smrg
26731637056Smrgofile=
26831637056Smrgcfile=
26931637056Smrg
27031637056Smrgfor arg
27131637056Smrgdo
27231637056Smrg  if test -n "$eat"; then
27331637056Smrg    eat=
27431637056Smrg  else
27531637056Smrg    case $1 in
27631637056Smrg      -o)
27731637056Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
27831637056Smrg	# So we strip '-o arg' only if arg is an object.
27931637056Smrg	eat=1
28031637056Smrg	case $2 in
28131637056Smrg	  *.o | *.obj)
28231637056Smrg	    ofile=$2
28331637056Smrg	    ;;
28431637056Smrg	  *)
28531637056Smrg	    set x "$@" -o "$2"
28631637056Smrg	    shift
28731637056Smrg	    ;;
28831637056Smrg	esac
28931637056Smrg	;;
29031637056Smrg      *.c)
29131637056Smrg	cfile=$1
29231637056Smrg	set x "$@" "$1"
29331637056Smrg	shift
29431637056Smrg	;;
29531637056Smrg      *)
29631637056Smrg	set x "$@" "$1"
29731637056Smrg	shift
29831637056Smrg	;;
29931637056Smrg    esac
30031637056Smrg  fi
30131637056Smrg  shift
30231637056Smrgdone
30331637056Smrg
30431637056Smrgif test -z "$ofile" || test -z "$cfile"; then
30531637056Smrg  # If no '-o' option was seen then we might have been invoked from a
30631637056Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30731637056Smrg  # normal compilation that the losing compiler can handle.  If no
30831637056Smrg  # '.c' file was seen then we are probably linking.  That is also
30931637056Smrg  # ok.
31031637056Smrg  exec "$@"
31131637056Smrgfi
31231637056Smrg
31331637056Smrg# Name of file we expect compiler to create.
31431637056Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31531637056Smrg
31631637056Smrg# Create the lock directory.
31731637056Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31831637056Smrg# that we are using for the .o file.  Also, base the name on the expected
31931637056Smrg# object file name, since that is what matters with a parallel build.
32031637056Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
32131637056Smrgwhile true; do
32231637056Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32331637056Smrg    break
32431637056Smrg  fi
32531637056Smrg  sleep 1
32631637056Smrgdone
32731637056Smrg# FIXME: race condition here if user kills between mkdir and trap.
32831637056Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32931637056Smrg
33031637056Smrg# Run the compile.
33131637056Smrg"$@"
33231637056Smrgret=$?
33331637056Smrg
33431637056Smrgif test -f "$cofile"; then
33531637056Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33631637056Smrgelif test -f "${cofile}bj"; then
33731637056Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33831637056Smrgfi
33931637056Smrg
34031637056Smrgrmdir "$lockdir"
34131637056Smrgexit $ret
34231637056Smrg
34331637056Smrg# Local Variables:
34431637056Smrg# mode: shell-script
34531637056Smrg# sh-indentation: 2
346b41a30aaSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34731637056Smrg# time-stamp-start: "scriptversion="
34831637056Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349b41a30aaSmrg# time-stamp-time-zone: "UTC0"
35031637056Smrg# time-stamp-end: "; # UTC"
35131637056Smrg# End:
352