xref: /freebsd/contrib/libevent/build-aux/compile (revision b50261e2)
1*b50261e2SCy Schubert#! /bin/sh
2*b50261e2SCy Schubert# Wrapper for compilers which do not understand '-c -o'.
3*b50261e2SCy Schubert
4*b50261e2SCy Schubertscriptversion=2018-03-07.03; # UTC
5*b50261e2SCy Schubert
6*b50261e2SCy Schubert# Copyright (C) 1999-2020 Free Software Foundation, Inc.
7*b50261e2SCy Schubert# Written by Tom Tromey <tromey@cygnus.com>.
8*b50261e2SCy Schubert#
9*b50261e2SCy Schubert# This program is free software; you can redistribute it and/or modify
10*b50261e2SCy Schubert# it under the terms of the GNU General Public License as published by
11*b50261e2SCy Schubert# the Free Software Foundation; either version 2, or (at your option)
12*b50261e2SCy Schubert# any later version.
13*b50261e2SCy Schubert#
14*b50261e2SCy Schubert# This program is distributed in the hope that it will be useful,
15*b50261e2SCy Schubert# but WITHOUT ANY WARRANTY; without even the implied warranty of
16*b50261e2SCy Schubert# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*b50261e2SCy Schubert# GNU General Public License for more details.
18*b50261e2SCy Schubert#
19*b50261e2SCy Schubert# You should have received a copy of the GNU General Public License
20*b50261e2SCy Schubert# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21*b50261e2SCy Schubert
22*b50261e2SCy Schubert# As a special exception to the GNU General Public License, if you
23*b50261e2SCy Schubert# distribute this file as part of a program that contains a
24*b50261e2SCy Schubert# configuration script generated by Autoconf, you may include it under
25*b50261e2SCy Schubert# the same distribution terms that you use for the rest of that program.
26*b50261e2SCy Schubert
27*b50261e2SCy Schubert# This file is maintained in Automake, please report
28*b50261e2SCy Schubert# bugs to <bug-automake@gnu.org> or send patches to
29*b50261e2SCy Schubert# <automake-patches@gnu.org>.
30*b50261e2SCy Schubert
31*b50261e2SCy Schubertnl='
32*b50261e2SCy Schubert'
33*b50261e2SCy Schubert
34*b50261e2SCy Schubert# We need space, tab and new line, in precisely that order.  Quoting is
35*b50261e2SCy Schubert# there to prevent tools from complaining about whitespace usage.
36*b50261e2SCy SchubertIFS=" ""	$nl"
37*b50261e2SCy Schubert
38*b50261e2SCy Schubertfile_conv=
39*b50261e2SCy Schubert
40*b50261e2SCy Schubert# func_file_conv build_file lazy
41*b50261e2SCy Schubert# Convert a $build file to $host form and store it in $file
42*b50261e2SCy Schubert# Currently only supports Windows hosts. If the determined conversion
43*b50261e2SCy Schubert# type is listed in (the comma separated) LAZY, no conversion will
44*b50261e2SCy Schubert# take place.
45*b50261e2SCy Schubertfunc_file_conv ()
46*b50261e2SCy Schubert{
47*b50261e2SCy Schubert  file=$1
48*b50261e2SCy Schubert  case $file in
49*b50261e2SCy Schubert    / | /[!/]*) # absolute file, and not a UNC file
50*b50261e2SCy Schubert      if test -z "$file_conv"; then
51*b50261e2SCy Schubert	# lazily determine how to convert abs files
52*b50261e2SCy Schubert	case `uname -s` in
53*b50261e2SCy Schubert	  MINGW*)
54*b50261e2SCy Schubert	    file_conv=mingw
55*b50261e2SCy Schubert	    ;;
56*b50261e2SCy Schubert	  CYGWIN* | MSYS*)
57*b50261e2SCy Schubert	    file_conv=cygwin
58*b50261e2SCy Schubert	    ;;
59*b50261e2SCy Schubert	  *)
60*b50261e2SCy Schubert	    file_conv=wine
61*b50261e2SCy Schubert	    ;;
62*b50261e2SCy Schubert	esac
63*b50261e2SCy Schubert      fi
64*b50261e2SCy Schubert      case $file_conv/,$2, in
65*b50261e2SCy Schubert	*,$file_conv,*)
66*b50261e2SCy Schubert	  ;;
67*b50261e2SCy Schubert	mingw/*)
68*b50261e2SCy Schubert	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69*b50261e2SCy Schubert	  ;;
70*b50261e2SCy Schubert	cygwin/* | msys/*)
71*b50261e2SCy Schubert	  file=`cygpath -m "$file" || echo "$file"`
72*b50261e2SCy Schubert	  ;;
73*b50261e2SCy Schubert	wine/*)
74*b50261e2SCy Schubert	  file=`winepath -w "$file" || echo "$file"`
75*b50261e2SCy Schubert	  ;;
76*b50261e2SCy Schubert      esac
77*b50261e2SCy Schubert      ;;
78*b50261e2SCy Schubert  esac
79*b50261e2SCy Schubert}
80*b50261e2SCy Schubert
81*b50261e2SCy Schubert# func_cl_dashL linkdir
82*b50261e2SCy Schubert# Make cl look for libraries in LINKDIR
83*b50261e2SCy Schubertfunc_cl_dashL ()
84*b50261e2SCy Schubert{
85*b50261e2SCy Schubert  func_file_conv "$1"
86*b50261e2SCy Schubert  if test -z "$lib_path"; then
87*b50261e2SCy Schubert    lib_path=$file
88*b50261e2SCy Schubert  else
89*b50261e2SCy Schubert    lib_path="$lib_path;$file"
90*b50261e2SCy Schubert  fi
91*b50261e2SCy Schubert  linker_opts="$linker_opts -LIBPATH:$file"
92*b50261e2SCy Schubert}
93*b50261e2SCy Schubert
94*b50261e2SCy Schubert# func_cl_dashl library
95*b50261e2SCy Schubert# Do a library search-path lookup for cl
96*b50261e2SCy Schubertfunc_cl_dashl ()
97*b50261e2SCy Schubert{
98*b50261e2SCy Schubert  lib=$1
99*b50261e2SCy Schubert  found=no
100*b50261e2SCy Schubert  save_IFS=$IFS
101*b50261e2SCy Schubert  IFS=';'
102*b50261e2SCy Schubert  for dir in $lib_path $LIB
103*b50261e2SCy Schubert  do
104*b50261e2SCy Schubert    IFS=$save_IFS
105*b50261e2SCy Schubert    if $shared && test -f "$dir/$lib.dll.lib"; then
106*b50261e2SCy Schubert      found=yes
107*b50261e2SCy Schubert      lib=$dir/$lib.dll.lib
108*b50261e2SCy Schubert      break
109*b50261e2SCy Schubert    fi
110*b50261e2SCy Schubert    if test -f "$dir/$lib.lib"; then
111*b50261e2SCy Schubert      found=yes
112*b50261e2SCy Schubert      lib=$dir/$lib.lib
113*b50261e2SCy Schubert      break
114*b50261e2SCy Schubert    fi
115*b50261e2SCy Schubert    if test -f "$dir/lib$lib.a"; then
116*b50261e2SCy Schubert      found=yes
117*b50261e2SCy Schubert      lib=$dir/lib$lib.a
118*b50261e2SCy Schubert      break
119*b50261e2SCy Schubert    fi
120*b50261e2SCy Schubert  done
121*b50261e2SCy Schubert  IFS=$save_IFS
122*b50261e2SCy Schubert
123*b50261e2SCy Schubert  if test "$found" != yes; then
124*b50261e2SCy Schubert    lib=$lib.lib
125*b50261e2SCy Schubert  fi
126*b50261e2SCy Schubert}
127*b50261e2SCy Schubert
128*b50261e2SCy Schubert# func_cl_wrapper cl arg...
129*b50261e2SCy Schubert# Adjust compile command to suit cl
130*b50261e2SCy Schubertfunc_cl_wrapper ()
131*b50261e2SCy Schubert{
132*b50261e2SCy Schubert  # Assume a capable shell
133*b50261e2SCy Schubert  lib_path=
134*b50261e2SCy Schubert  shared=:
135*b50261e2SCy Schubert  linker_opts=
136*b50261e2SCy Schubert  for arg
137*b50261e2SCy Schubert  do
138*b50261e2SCy Schubert    if test -n "$eat"; then
139*b50261e2SCy Schubert      eat=
140*b50261e2SCy Schubert    else
141*b50261e2SCy Schubert      case $1 in
142*b50261e2SCy Schubert	-o)
143*b50261e2SCy Schubert	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144*b50261e2SCy Schubert	  eat=1
145*b50261e2SCy Schubert	  case $2 in
146*b50261e2SCy Schubert	    *.o | *.[oO][bB][jJ])
147*b50261e2SCy Schubert	      func_file_conv "$2"
148*b50261e2SCy Schubert	      set x "$@" -Fo"$file"
149*b50261e2SCy Schubert	      shift
150*b50261e2SCy Schubert	      ;;
151*b50261e2SCy Schubert	    *)
152*b50261e2SCy Schubert	      func_file_conv "$2"
153*b50261e2SCy Schubert	      set x "$@" -Fe"$file"
154*b50261e2SCy Schubert	      shift
155*b50261e2SCy Schubert	      ;;
156*b50261e2SCy Schubert	  esac
157*b50261e2SCy Schubert	  ;;
158*b50261e2SCy Schubert	-I)
159*b50261e2SCy Schubert	  eat=1
160*b50261e2SCy Schubert	  func_file_conv "$2" mingw
161*b50261e2SCy Schubert	  set x "$@" -I"$file"
162*b50261e2SCy Schubert	  shift
163*b50261e2SCy Schubert	  ;;
164*b50261e2SCy Schubert	-I*)
165*b50261e2SCy Schubert	  func_file_conv "${1#-I}" mingw
166*b50261e2SCy Schubert	  set x "$@" -I"$file"
167*b50261e2SCy Schubert	  shift
168*b50261e2SCy Schubert	  ;;
169*b50261e2SCy Schubert	-l)
170*b50261e2SCy Schubert	  eat=1
171*b50261e2SCy Schubert	  func_cl_dashl "$2"
172*b50261e2SCy Schubert	  set x "$@" "$lib"
173*b50261e2SCy Schubert	  shift
174*b50261e2SCy Schubert	  ;;
175*b50261e2SCy Schubert	-l*)
176*b50261e2SCy Schubert	  func_cl_dashl "${1#-l}"
177*b50261e2SCy Schubert	  set x "$@" "$lib"
178*b50261e2SCy Schubert	  shift
179*b50261e2SCy Schubert	  ;;
180*b50261e2SCy Schubert	-L)
181*b50261e2SCy Schubert	  eat=1
182*b50261e2SCy Schubert	  func_cl_dashL "$2"
183*b50261e2SCy Schubert	  ;;
184*b50261e2SCy Schubert	-L*)
185*b50261e2SCy Schubert	  func_cl_dashL "${1#-L}"
186*b50261e2SCy Schubert	  ;;
187*b50261e2SCy Schubert	-static)
188*b50261e2SCy Schubert	  shared=false
189*b50261e2SCy Schubert	  ;;
190*b50261e2SCy Schubert	-Wl,*)
191*b50261e2SCy Schubert	  arg=${1#-Wl,}
192*b50261e2SCy Schubert	  save_ifs="$IFS"; IFS=','
193*b50261e2SCy Schubert	  for flag in $arg; do
194*b50261e2SCy Schubert	    IFS="$save_ifs"
195*b50261e2SCy Schubert	    linker_opts="$linker_opts $flag"
196*b50261e2SCy Schubert	  done
197*b50261e2SCy Schubert	  IFS="$save_ifs"
198*b50261e2SCy Schubert	  ;;
199*b50261e2SCy Schubert	-Xlinker)
200*b50261e2SCy Schubert	  eat=1
201*b50261e2SCy Schubert	  linker_opts="$linker_opts $2"
202*b50261e2SCy Schubert	  ;;
203*b50261e2SCy Schubert	-*)
204*b50261e2SCy Schubert	  set x "$@" "$1"
205*b50261e2SCy Schubert	  shift
206*b50261e2SCy Schubert	  ;;
207*b50261e2SCy Schubert	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208*b50261e2SCy Schubert	  func_file_conv "$1"
209*b50261e2SCy Schubert	  set x "$@" -Tp"$file"
210*b50261e2SCy Schubert	  shift
211*b50261e2SCy Schubert	  ;;
212*b50261e2SCy Schubert	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213*b50261e2SCy Schubert	  func_file_conv "$1" mingw
214*b50261e2SCy Schubert	  set x "$@" "$file"
215*b50261e2SCy Schubert	  shift
216*b50261e2SCy Schubert	  ;;
217*b50261e2SCy Schubert	*)
218*b50261e2SCy Schubert	  set x "$@" "$1"
219*b50261e2SCy Schubert	  shift
220*b50261e2SCy Schubert	  ;;
221*b50261e2SCy Schubert      esac
222*b50261e2SCy Schubert    fi
223*b50261e2SCy Schubert    shift
224*b50261e2SCy Schubert  done
225*b50261e2SCy Schubert  if test -n "$linker_opts"; then
226*b50261e2SCy Schubert    linker_opts="-link$linker_opts"
227*b50261e2SCy Schubert  fi
228*b50261e2SCy Schubert  exec "$@" $linker_opts
229*b50261e2SCy Schubert  exit 1
230*b50261e2SCy Schubert}
231*b50261e2SCy Schubert
232*b50261e2SCy Schuberteat=
233*b50261e2SCy Schubert
234*b50261e2SCy Schubertcase $1 in
235*b50261e2SCy Schubert  '')
236*b50261e2SCy Schubert     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237*b50261e2SCy Schubert     exit 1;
238*b50261e2SCy Schubert     ;;
239*b50261e2SCy Schubert  -h | --h*)
240*b50261e2SCy Schubert    cat <<\EOF
241*b50261e2SCy SchubertUsage: compile [--help] [--version] PROGRAM [ARGS]
242*b50261e2SCy Schubert
243*b50261e2SCy SchubertWrapper for compilers which do not understand '-c -o'.
244*b50261e2SCy SchubertRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245*b50261e2SCy Schubertarguments, and rename the output as expected.
246*b50261e2SCy Schubert
247*b50261e2SCy SchubertIf you are trying to build a whole package this is not the
248*b50261e2SCy Schubertright script to run: please start by reading the file 'INSTALL'.
249*b50261e2SCy Schubert
250*b50261e2SCy SchubertReport bugs to <bug-automake@gnu.org>.
251*b50261e2SCy SchubertEOF
252*b50261e2SCy Schubert    exit $?
253*b50261e2SCy Schubert    ;;
254*b50261e2SCy Schubert  -v | --v*)
255*b50261e2SCy Schubert    echo "compile $scriptversion"
256*b50261e2SCy Schubert    exit $?
257*b50261e2SCy Schubert    ;;
258*b50261e2SCy Schubert  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259*b50261e2SCy Schubert  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260*b50261e2SCy Schubert    func_cl_wrapper "$@"      # Doesn't return...
261*b50261e2SCy Schubert    ;;
262*b50261e2SCy Schubertesac
263*b50261e2SCy Schubert
264*b50261e2SCy Schubertofile=
265*b50261e2SCy Schubertcfile=
266*b50261e2SCy Schubert
267*b50261e2SCy Schubertfor arg
268*b50261e2SCy Schubertdo
269*b50261e2SCy Schubert  if test -n "$eat"; then
270*b50261e2SCy Schubert    eat=
271*b50261e2SCy Schubert  else
272*b50261e2SCy Schubert    case $1 in
273*b50261e2SCy Schubert      -o)
274*b50261e2SCy Schubert	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275*b50261e2SCy Schubert	# So we strip '-o arg' only if arg is an object.
276*b50261e2SCy Schubert	eat=1
277*b50261e2SCy Schubert	case $2 in
278*b50261e2SCy Schubert	  *.o | *.obj)
279*b50261e2SCy Schubert	    ofile=$2
280*b50261e2SCy Schubert	    ;;
281*b50261e2SCy Schubert	  *)
282*b50261e2SCy Schubert	    set x "$@" -o "$2"
283*b50261e2SCy Schubert	    shift
284*b50261e2SCy Schubert	    ;;
285*b50261e2SCy Schubert	esac
286*b50261e2SCy Schubert	;;
287*b50261e2SCy Schubert      *.c)
288*b50261e2SCy Schubert	cfile=$1
289*b50261e2SCy Schubert	set x "$@" "$1"
290*b50261e2SCy Schubert	shift
291*b50261e2SCy Schubert	;;
292*b50261e2SCy Schubert      *)
293*b50261e2SCy Schubert	set x "$@" "$1"
294*b50261e2SCy Schubert	shift
295*b50261e2SCy Schubert	;;
296*b50261e2SCy Schubert    esac
297*b50261e2SCy Schubert  fi
298*b50261e2SCy Schubert  shift
299*b50261e2SCy Schubertdone
300*b50261e2SCy Schubert
301*b50261e2SCy Schubertif test -z "$ofile" || test -z "$cfile"; then
302*b50261e2SCy Schubert  # If no '-o' option was seen then we might have been invoked from a
303*b50261e2SCy Schubert  # pattern rule where we don't need one.  That is ok -- this is a
304*b50261e2SCy Schubert  # normal compilation that the losing compiler can handle.  If no
305*b50261e2SCy Schubert  # '.c' file was seen then we are probably linking.  That is also
306*b50261e2SCy Schubert  # ok.
307*b50261e2SCy Schubert  exec "$@"
308*b50261e2SCy Schubertfi
309*b50261e2SCy Schubert
310*b50261e2SCy Schubert# Name of file we expect compiler to create.
311*b50261e2SCy Schubertcofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312*b50261e2SCy Schubert
313*b50261e2SCy Schubert# Create the lock directory.
314*b50261e2SCy Schubert# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315*b50261e2SCy Schubert# that we are using for the .o file.  Also, base the name on the expected
316*b50261e2SCy Schubert# object file name, since that is what matters with a parallel build.
317*b50261e2SCy Schubertlockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318*b50261e2SCy Schubertwhile true; do
319*b50261e2SCy Schubert  if mkdir "$lockdir" >/dev/null 2>&1; then
320*b50261e2SCy Schubert    break
321*b50261e2SCy Schubert  fi
322*b50261e2SCy Schubert  sleep 1
323*b50261e2SCy Schubertdone
324*b50261e2SCy Schubert# FIXME: race condition here if user kills between mkdir and trap.
325*b50261e2SCy Schuberttrap "rmdir '$lockdir'; exit 1" 1 2 15
326*b50261e2SCy Schubert
327*b50261e2SCy Schubert# Run the compile.
328*b50261e2SCy Schubert"$@"
329*b50261e2SCy Schubertret=$?
330*b50261e2SCy Schubert
331*b50261e2SCy Schubertif test -f "$cofile"; then
332*b50261e2SCy Schubert  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333*b50261e2SCy Schubertelif test -f "${cofile}bj"; then
334*b50261e2SCy Schubert  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335*b50261e2SCy Schubertfi
336*b50261e2SCy Schubert
337*b50261e2SCy Schubertrmdir "$lockdir"
338*b50261e2SCy Schubertexit $ret
339*b50261e2SCy Schubert
340*b50261e2SCy Schubert# Local Variables:
341*b50261e2SCy Schubert# mode: shell-script
342*b50261e2SCy Schubert# sh-indentation: 2
343*b50261e2SCy Schubert# eval: (add-hook 'before-save-hook 'time-stamp)
344*b50261e2SCy Schubert# time-stamp-start: "scriptversion="
345*b50261e2SCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
346*b50261e2SCy Schubert# time-stamp-time-zone: "UTC0"
347*b50261e2SCy Schubert# time-stamp-end: "; # UTC"
348*b50261e2SCy Schubert# End:
349