compile revision eb323118
129459361Smrg#! /bin/sh
2fb4ebca8Smrg# Wrapper for compilers which do not understand '-c -o'.
329459361Smrg
4eb323118Smrgscriptversion=2024-06-19.01; # UTC
529459361Smrg
6eb323118Smrg# Copyright (C) 1999-2024 Free Software Foundation, Inc.
729459361Smrg# Written by Tom Tromey <tromey@cygnus.com>.
829459361Smrg#
929459361Smrg# This program is free software; you can redistribute it and/or modify
1029459361Smrg# it under the terms of the GNU General Public License as published by
1129459361Smrg# the Free Software Foundation; either version 2, or (at your option)
1229459361Smrg# any later version.
1329459361Smrg#
1429459361Smrg# This program is distributed in the hope that it will be useful,
1529459361Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
1629459361Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1729459361Smrg# GNU General Public License for more details.
1829459361Smrg#
1929459361Smrg# You should have received a copy of the GNU General Public License
20e24f450bSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
2129459361Smrg
2229459361Smrg# As a special exception to the GNU General Public License, if you
2329459361Smrg# distribute this file as part of a program that contains a
2429459361Smrg# configuration script generated by Autoconf, you may include it under
2529459361Smrg# the same distribution terms that you use for the rest of that program.
2629459361Smrg
2729459361Smrg# This file is maintained in Automake, please report
2829459361Smrg# bugs to <bug-automake@gnu.org> or send patches to
2929459361Smrg# <automake-patches@gnu.org>.
3029459361Smrg
31fb4ebca8Smrgnl='
32fb4ebca8Smrg'
33fb4ebca8Smrg
34fb4ebca8Smrg# We need space, tab and new line, in precisely that order.  Quoting is
35fb4ebca8Smrg# there to prevent tools from complaining about whitespace usage.
36fb4ebca8SmrgIFS=" ""	$nl"
37fb4ebca8Smrg
38fb4ebca8Smrgfile_conv=
39fb4ebca8Smrg
40fb4ebca8Smrg# func_file_conv build_file lazy
41fb4ebca8Smrg# Convert a $build file to $host form and store it in $file
42fb4ebca8Smrg# Currently only supports Windows hosts. If the determined conversion
43fb4ebca8Smrg# type is listed in (the comma separated) LAZY, no conversion will
44fb4ebca8Smrg# take place.
45fb4ebca8Smrgfunc_file_conv ()
46fb4ebca8Smrg{
47fb4ebca8Smrg  file=$1
48fb4ebca8Smrg  case $file in
49fb4ebca8Smrg    / | /[!/]*) # absolute file, and not a UNC file
50fb4ebca8Smrg      if test -z "$file_conv"; then
51fb4ebca8Smrg	# lazily determine how to convert abs files
52fb4ebca8Smrg	case `uname -s` in
53fb4ebca8Smrg	  MINGW*)
54fb4ebca8Smrg	    file_conv=mingw
55fb4ebca8Smrg	    ;;
56e24f450bSmrg	  CYGWIN* | MSYS*)
57fb4ebca8Smrg	    file_conv=cygwin
58fb4ebca8Smrg	    ;;
59fb4ebca8Smrg	  *)
60fb4ebca8Smrg	    file_conv=wine
61fb4ebca8Smrg	    ;;
62fb4ebca8Smrg	esac
63fb4ebca8Smrg      fi
64fb4ebca8Smrg      case $file_conv/,$2, in
65fb4ebca8Smrg	*,$file_conv,*)
66fb4ebca8Smrg	  ;;
67fb4ebca8Smrg	mingw/*)
68fb4ebca8Smrg	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69fb4ebca8Smrg	  ;;
70e24f450bSmrg	cygwin/* | msys/*)
71fb4ebca8Smrg	  file=`cygpath -m "$file" || echo "$file"`
72fb4ebca8Smrg	  ;;
73fb4ebca8Smrg	wine/*)
74fb4ebca8Smrg	  file=`winepath -w "$file" || echo "$file"`
75fb4ebca8Smrg	  ;;
76fb4ebca8Smrg      esac
77fb4ebca8Smrg      ;;
78fb4ebca8Smrg  esac
79fb4ebca8Smrg}
80fb4ebca8Smrg
81fb4ebca8Smrg# func_cl_dashL linkdir
82fb4ebca8Smrg# Make cl look for libraries in LINKDIR
83fb4ebca8Smrgfunc_cl_dashL ()
84fb4ebca8Smrg{
85fb4ebca8Smrg  func_file_conv "$1"
86fb4ebca8Smrg  if test -z "$lib_path"; then
87fb4ebca8Smrg    lib_path=$file
88fb4ebca8Smrg  else
89fb4ebca8Smrg    lib_path="$lib_path;$file"
90fb4ebca8Smrg  fi
91fb4ebca8Smrg  linker_opts="$linker_opts -LIBPATH:$file"
92fb4ebca8Smrg}
93fb4ebca8Smrg
94fb4ebca8Smrg# func_cl_dashl library
95fb4ebca8Smrg# Do a library search-path lookup for cl
96fb4ebca8Smrgfunc_cl_dashl ()
97fb4ebca8Smrg{
98fb4ebca8Smrg  lib=$1
99fb4ebca8Smrg  found=no
100fb4ebca8Smrg  save_IFS=$IFS
101fb4ebca8Smrg  IFS=';'
102fb4ebca8Smrg  for dir in $lib_path $LIB
103fb4ebca8Smrg  do
104fb4ebca8Smrg    IFS=$save_IFS
105fb4ebca8Smrg    if $shared && test -f "$dir/$lib.dll.lib"; then
106fb4ebca8Smrg      found=yes
107fb4ebca8Smrg      lib=$dir/$lib.dll.lib
108fb4ebca8Smrg      break
109fb4ebca8Smrg    fi
110fb4ebca8Smrg    if test -f "$dir/$lib.lib"; then
111fb4ebca8Smrg      found=yes
112fb4ebca8Smrg      lib=$dir/$lib.lib
113fb4ebca8Smrg      break
114fb4ebca8Smrg    fi
115fb4ebca8Smrg    if test -f "$dir/lib$lib.a"; then
116fb4ebca8Smrg      found=yes
117fb4ebca8Smrg      lib=$dir/lib$lib.a
118fb4ebca8Smrg      break
119fb4ebca8Smrg    fi
120fb4ebca8Smrg  done
121fb4ebca8Smrg  IFS=$save_IFS
122fb4ebca8Smrg
123fb4ebca8Smrg  if test "$found" != yes; then
124fb4ebca8Smrg    lib=$lib.lib
125fb4ebca8Smrg  fi
126fb4ebca8Smrg}
127fb4ebca8Smrg
128fb4ebca8Smrg# func_cl_wrapper cl arg...
129fb4ebca8Smrg# Adjust compile command to suit cl
130fb4ebca8Smrgfunc_cl_wrapper ()
131fb4ebca8Smrg{
132fb4ebca8Smrg  # Assume a capable shell
133fb4ebca8Smrg  lib_path=
134fb4ebca8Smrg  shared=:
135fb4ebca8Smrg  linker_opts=
136fb4ebca8Smrg  for arg
137fb4ebca8Smrg  do
138fb4ebca8Smrg    if test -n "$eat"; then
139fb4ebca8Smrg      eat=
140fb4ebca8Smrg    else
141fb4ebca8Smrg      case $1 in
142fb4ebca8Smrg	-o)
143fb4ebca8Smrg	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144fb4ebca8Smrg	  eat=1
145fb4ebca8Smrg	  case $2 in
146eb323118Smrg	    *.o | *.lo | *.[oO][bB][jJ])
147fb4ebca8Smrg	      func_file_conv "$2"
148fb4ebca8Smrg	      set x "$@" -Fo"$file"
149fb4ebca8Smrg	      shift
150fb4ebca8Smrg	      ;;
151fb4ebca8Smrg	    *)
152fb4ebca8Smrg	      func_file_conv "$2"
153fb4ebca8Smrg	      set x "$@" -Fe"$file"
154fb4ebca8Smrg	      shift
155fb4ebca8Smrg	      ;;
156fb4ebca8Smrg	  esac
157fb4ebca8Smrg	  ;;
158fb4ebca8Smrg	-I)
159fb4ebca8Smrg	  eat=1
160fb4ebca8Smrg	  func_file_conv "$2" mingw
161fb4ebca8Smrg	  set x "$@" -I"$file"
162fb4ebca8Smrg	  shift
163fb4ebca8Smrg	  ;;
164fb4ebca8Smrg	-I*)
165fb4ebca8Smrg	  func_file_conv "${1#-I}" mingw
166fb4ebca8Smrg	  set x "$@" -I"$file"
167fb4ebca8Smrg	  shift
168fb4ebca8Smrg	  ;;
169fb4ebca8Smrg	-l)
170fb4ebca8Smrg	  eat=1
171fb4ebca8Smrg	  func_cl_dashl "$2"
172fb4ebca8Smrg	  set x "$@" "$lib"
173fb4ebca8Smrg	  shift
174fb4ebca8Smrg	  ;;
175fb4ebca8Smrg	-l*)
176fb4ebca8Smrg	  func_cl_dashl "${1#-l}"
177fb4ebca8Smrg	  set x "$@" "$lib"
178fb4ebca8Smrg	  shift
179fb4ebca8Smrg	  ;;
180fb4ebca8Smrg	-L)
181fb4ebca8Smrg	  eat=1
182fb4ebca8Smrg	  func_cl_dashL "$2"
183fb4ebca8Smrg	  ;;
184fb4ebca8Smrg	-L*)
185fb4ebca8Smrg	  func_cl_dashL "${1#-L}"
186fb4ebca8Smrg	  ;;
187fb4ebca8Smrg	-static)
188fb4ebca8Smrg	  shared=false
189fb4ebca8Smrg	  ;;
190fb4ebca8Smrg	-Wl,*)
191fb4ebca8Smrg	  arg=${1#-Wl,}
192fb4ebca8Smrg	  save_ifs="$IFS"; IFS=','
193fb4ebca8Smrg	  for flag in $arg; do
194fb4ebca8Smrg	    IFS="$save_ifs"
195fb4ebca8Smrg	    linker_opts="$linker_opts $flag"
196fb4ebca8Smrg	  done
197fb4ebca8Smrg	  IFS="$save_ifs"
198fb4ebca8Smrg	  ;;
199fb4ebca8Smrg	-Xlinker)
200fb4ebca8Smrg	  eat=1
201fb4ebca8Smrg	  linker_opts="$linker_opts $2"
202fb4ebca8Smrg	  ;;
203fb4ebca8Smrg	-*)
204fb4ebca8Smrg	  set x "$@" "$1"
205fb4ebca8Smrg	  shift
206fb4ebca8Smrg	  ;;
207fb4ebca8Smrg	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208fb4ebca8Smrg	  func_file_conv "$1"
209fb4ebca8Smrg	  set x "$@" -Tp"$file"
210fb4ebca8Smrg	  shift
211fb4ebca8Smrg	  ;;
212fb4ebca8Smrg	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213fb4ebca8Smrg	  func_file_conv "$1" mingw
214fb4ebca8Smrg	  set x "$@" "$file"
215fb4ebca8Smrg	  shift
216fb4ebca8Smrg	  ;;
217fb4ebca8Smrg	*)
218fb4ebca8Smrg	  set x "$@" "$1"
219fb4ebca8Smrg	  shift
220fb4ebca8Smrg	  ;;
221fb4ebca8Smrg      esac
222fb4ebca8Smrg    fi
223fb4ebca8Smrg    shift
224fb4ebca8Smrg  done
225fb4ebca8Smrg  if test -n "$linker_opts"; then
226fb4ebca8Smrg    linker_opts="-link$linker_opts"
227fb4ebca8Smrg  fi
228fb4ebca8Smrg  exec "$@" $linker_opts
229fb4ebca8Smrg  exit 1
230fb4ebca8Smrg}
231fb4ebca8Smrg
232fb4ebca8Smrgeat=
233fb4ebca8Smrg
23429459361Smrgcase $1 in
23529459361Smrg  '')
236fb4ebca8Smrg     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
23729459361Smrg     exit 1;
23829459361Smrg     ;;
23929459361Smrg  -h | --h*)
24029459361Smrg    cat <<\EOF
24129459361SmrgUsage: compile [--help] [--version] PROGRAM [ARGS]
24229459361Smrg
243fb4ebca8SmrgWrapper for compilers which do not understand '-c -o'.
244fb4ebca8SmrgRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
24529459361Smrgarguments, and rename the output as expected.
24629459361Smrg
24729459361SmrgIf you are trying to build a whole package this is not the
248fb4ebca8Smrgright script to run: please start by reading the file 'INSTALL'.
24929459361Smrg
25029459361SmrgReport bugs to <bug-automake@gnu.org>.
251eb323118SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
252eb323118SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>.
25329459361SmrgEOF
25429459361Smrg    exit $?
25529459361Smrg    ;;
25629459361Smrg  -v | --v*)
257eb323118Smrg    echo "compile (GNU Automake) $scriptversion"
25829459361Smrg    exit $?
25929459361Smrg    ;;
260fa2b3b63Smrg  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
261eb323118Smrg  clang-cl | *[/\\]clang-cl | clang-cl.exe | *[/\\]clang-cl.exe | \
262fa2b3b63Smrg  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
263fb4ebca8Smrg    func_cl_wrapper "$@"      # Doesn't return...
264fb4ebca8Smrg    ;;
26529459361Smrgesac
26629459361Smrg
26729459361Smrgofile=
26829459361Smrgcfile=
26929459361Smrg
27029459361Smrgfor arg
27129459361Smrgdo
27229459361Smrg  if test -n "$eat"; then
27329459361Smrg    eat=
27429459361Smrg  else
27529459361Smrg    case $1 in
27629459361Smrg      -o)
277fb4ebca8Smrg	# configure might choose to run compile as 'compile cc -o foo foo.c'.
278fb4ebca8Smrg	# So we strip '-o arg' only if arg is an object.
27929459361Smrg	eat=1
28029459361Smrg	case $2 in
28129459361Smrg	  *.o | *.obj)
28229459361Smrg	    ofile=$2
28329459361Smrg	    ;;
28429459361Smrg	  *)
28529459361Smrg	    set x "$@" -o "$2"
28629459361Smrg	    shift
28729459361Smrg	    ;;
28829459361Smrg	esac
28929459361Smrg	;;
29029459361Smrg      *.c)
29129459361Smrg	cfile=$1
29229459361Smrg	set x "$@" "$1"
29329459361Smrg	shift
29429459361Smrg	;;
29529459361Smrg      *)
29629459361Smrg	set x "$@" "$1"
29729459361Smrg	shift
29829459361Smrg	;;
29929459361Smrg    esac
30029459361Smrg  fi
30129459361Smrg  shift
30229459361Smrgdone
30329459361Smrg
30429459361Smrgif test -z "$ofile" || test -z "$cfile"; then
305fb4ebca8Smrg  # If no '-o' option was seen then we might have been invoked from a
30629459361Smrg  # pattern rule where we don't need one.  That is ok -- this is a
30729459361Smrg  # normal compilation that the losing compiler can handle.  If no
308fb4ebca8Smrg  # '.c' file was seen then we are probably linking.  That is also
30929459361Smrg  # ok.
31029459361Smrg  exec "$@"
31129459361Smrgfi
31229459361Smrg
31329459361Smrg# Name of file we expect compiler to create.
314fb4ebca8Smrgcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
31529459361Smrg
31629459361Smrg# Create the lock directory.
317fb4ebca8Smrg# Note: use '[/\\:.-]' here to ensure that we don't use the same name
31829459361Smrg# that we are using for the .o file.  Also, base the name on the expected
31929459361Smrg# object file name, since that is what matters with a parallel build.
320fb4ebca8Smrglockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
32129459361Smrgwhile true; do
32229459361Smrg  if mkdir "$lockdir" >/dev/null 2>&1; then
32329459361Smrg    break
32429459361Smrg  fi
32529459361Smrg  sleep 1
32629459361Smrgdone
32729459361Smrg# FIXME: race condition here if user kills between mkdir and trap.
32829459361Smrgtrap "rmdir '$lockdir'; exit 1" 1 2 15
32929459361Smrg
33029459361Smrg# Run the compile.
33129459361Smrg"$@"
33229459361Smrgret=$?
33329459361Smrg
33429459361Smrgif test -f "$cofile"; then
335fb4ebca8Smrg  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
33629459361Smrgelif test -f "${cofile}bj"; then
337fb4ebca8Smrg  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
33829459361Smrgfi
33929459361Smrg
34029459361Smrgrmdir "$lockdir"
34129459361Smrgexit $ret
34229459361Smrg
34329459361Smrg# Local Variables:
34429459361Smrg# mode: shell-script
34529459361Smrg# sh-indentation: 2
346e24f450bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
34729459361Smrg# time-stamp-start: "scriptversion="
34829459361Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
349fa2b3b63Smrg# time-stamp-time-zone: "UTC0"
350fb4ebca8Smrg# time-stamp-end: "; # UTC"
35129459361Smrg# End:
352