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