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