xref: /minix3/external/bsd/flex/dist/compile (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1357f1050SThomas Veerman#! /bin/sh
2*0a6a1f1dSLionel Sambuc# Wrapper for compilers which do not understand '-c -o'.
3357f1050SThomas Veerman
4*0a6a1f1dSLionel Sambucscriptversion=2012-03-05.13; # UTC
5357f1050SThomas Veerman
6*0a6a1f1dSLionel Sambuc# Copyright (C) 1999, 2000, 2003, 2004, 2005, 2009, 2010, 2012 Free
7*0a6a1f1dSLionel Sambuc# Software Foundation, Inc.
8357f1050SThomas Veerman# Written by Tom Tromey <tromey@cygnus.com>.
9357f1050SThomas Veerman#
10357f1050SThomas Veerman# This program is free software; you can redistribute it and/or modify
11357f1050SThomas Veerman# it under the terms of the GNU General Public License as published by
12357f1050SThomas Veerman# the Free Software Foundation; either version 2, or (at your option)
13357f1050SThomas Veerman# any later version.
14357f1050SThomas Veerman#
15357f1050SThomas Veerman# This program is distributed in the hope that it will be useful,
16357f1050SThomas Veerman# but WITHOUT ANY WARRANTY; without even the implied warranty of
17357f1050SThomas Veerman# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18357f1050SThomas Veerman# GNU General Public License for more details.
19357f1050SThomas Veerman#
20357f1050SThomas Veerman# You should have received a copy of the GNU General Public License
2184d9c625SLionel Sambuc# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22357f1050SThomas Veerman
23357f1050SThomas Veerman# As a special exception to the GNU General Public License, if you
24357f1050SThomas Veerman# distribute this file as part of a program that contains a
25357f1050SThomas Veerman# configuration script generated by Autoconf, you may include it under
26357f1050SThomas Veerman# the same distribution terms that you use for the rest of that program.
27357f1050SThomas Veerman
28357f1050SThomas Veerman# This file is maintained in Automake, please report
29357f1050SThomas Veerman# bugs to <bug-automake@gnu.org> or send patches to
30357f1050SThomas Veerman# <automake-patches@gnu.org>.
31357f1050SThomas Veerman
32*0a6a1f1dSLionel Sambucnl='
33*0a6a1f1dSLionel Sambuc'
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc# We need space, tab and new line, in precisely that order.  Quoting is
36*0a6a1f1dSLionel Sambuc# there to prevent tools from complaining about whitespace usage.
37*0a6a1f1dSLionel SambucIFS=" ""	$nl"
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambucfile_conv=
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuc# func_file_conv build_file lazy
42*0a6a1f1dSLionel Sambuc# Convert a $build file to $host form and store it in $file
43*0a6a1f1dSLionel Sambuc# Currently only supports Windows hosts. If the determined conversion
44*0a6a1f1dSLionel Sambuc# type is listed in (the comma separated) LAZY, no conversion will
45*0a6a1f1dSLionel Sambuc# take place.
46*0a6a1f1dSLionel Sambucfunc_file_conv ()
47*0a6a1f1dSLionel Sambuc{
48*0a6a1f1dSLionel Sambuc  file=$1
49*0a6a1f1dSLionel Sambuc  case $file in
50*0a6a1f1dSLionel Sambuc    / | /[!/]*) # absolute file, and not a UNC file
51*0a6a1f1dSLionel Sambuc      if test -z "$file_conv"; then
52*0a6a1f1dSLionel Sambuc	# lazily determine how to convert abs files
53*0a6a1f1dSLionel Sambuc	case `uname -s` in
54*0a6a1f1dSLionel Sambuc	  MINGW*)
55*0a6a1f1dSLionel Sambuc	    file_conv=mingw
56*0a6a1f1dSLionel Sambuc	    ;;
57*0a6a1f1dSLionel Sambuc	  CYGWIN*)
58*0a6a1f1dSLionel Sambuc	    file_conv=cygwin
59*0a6a1f1dSLionel Sambuc	    ;;
60*0a6a1f1dSLionel Sambuc	  *)
61*0a6a1f1dSLionel Sambuc	    file_conv=wine
62*0a6a1f1dSLionel Sambuc	    ;;
63*0a6a1f1dSLionel Sambuc	esac
64*0a6a1f1dSLionel Sambuc      fi
65*0a6a1f1dSLionel Sambuc      case $file_conv/,$2, in
66*0a6a1f1dSLionel Sambuc	*,$file_conv,*)
67*0a6a1f1dSLionel Sambuc	  ;;
68*0a6a1f1dSLionel Sambuc	mingw/*)
69*0a6a1f1dSLionel Sambuc	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
70*0a6a1f1dSLionel Sambuc	  ;;
71*0a6a1f1dSLionel Sambuc	cygwin/*)
72*0a6a1f1dSLionel Sambuc	  file=`cygpath -m "$file" || echo "$file"`
73*0a6a1f1dSLionel Sambuc	  ;;
74*0a6a1f1dSLionel Sambuc	wine/*)
75*0a6a1f1dSLionel Sambuc	  file=`winepath -w "$file" || echo "$file"`
76*0a6a1f1dSLionel Sambuc	  ;;
77*0a6a1f1dSLionel Sambuc      esac
78*0a6a1f1dSLionel Sambuc      ;;
79*0a6a1f1dSLionel Sambuc  esac
80*0a6a1f1dSLionel Sambuc}
81*0a6a1f1dSLionel Sambuc
82*0a6a1f1dSLionel Sambuc# func_cl_dashL linkdir
83*0a6a1f1dSLionel Sambuc# Make cl look for libraries in LINKDIR
84*0a6a1f1dSLionel Sambucfunc_cl_dashL ()
85*0a6a1f1dSLionel Sambuc{
86*0a6a1f1dSLionel Sambuc  func_file_conv "$1"
87*0a6a1f1dSLionel Sambuc  if test -z "$lib_path"; then
88*0a6a1f1dSLionel Sambuc    lib_path=$file
89*0a6a1f1dSLionel Sambuc  else
90*0a6a1f1dSLionel Sambuc    lib_path="$lib_path;$file"
91*0a6a1f1dSLionel Sambuc  fi
92*0a6a1f1dSLionel Sambuc  linker_opts="$linker_opts -LIBPATH:$file"
93*0a6a1f1dSLionel Sambuc}
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambuc# func_cl_dashl library
96*0a6a1f1dSLionel Sambuc# Do a library search-path lookup for cl
97*0a6a1f1dSLionel Sambucfunc_cl_dashl ()
98*0a6a1f1dSLionel Sambuc{
99*0a6a1f1dSLionel Sambuc  lib=$1
100*0a6a1f1dSLionel Sambuc  found=no
101*0a6a1f1dSLionel Sambuc  save_IFS=$IFS
102*0a6a1f1dSLionel Sambuc  IFS=';'
103*0a6a1f1dSLionel Sambuc  for dir in $lib_path $LIB
104*0a6a1f1dSLionel Sambuc  do
105*0a6a1f1dSLionel Sambuc    IFS=$save_IFS
106*0a6a1f1dSLionel Sambuc    if $shared && test -f "$dir/$lib.dll.lib"; then
107*0a6a1f1dSLionel Sambuc      found=yes
108*0a6a1f1dSLionel Sambuc      lib=$dir/$lib.dll.lib
109*0a6a1f1dSLionel Sambuc      break
110*0a6a1f1dSLionel Sambuc    fi
111*0a6a1f1dSLionel Sambuc    if test -f "$dir/$lib.lib"; then
112*0a6a1f1dSLionel Sambuc      found=yes
113*0a6a1f1dSLionel Sambuc      lib=$dir/$lib.lib
114*0a6a1f1dSLionel Sambuc      break
115*0a6a1f1dSLionel Sambuc    fi
116*0a6a1f1dSLionel Sambuc  done
117*0a6a1f1dSLionel Sambuc  IFS=$save_IFS
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc  if test "$found" != yes; then
120*0a6a1f1dSLionel Sambuc    lib=$lib.lib
121*0a6a1f1dSLionel Sambuc  fi
122*0a6a1f1dSLionel Sambuc}
123*0a6a1f1dSLionel Sambuc
124*0a6a1f1dSLionel Sambuc# func_cl_wrapper cl arg...
125*0a6a1f1dSLionel Sambuc# Adjust compile command to suit cl
126*0a6a1f1dSLionel Sambucfunc_cl_wrapper ()
127*0a6a1f1dSLionel Sambuc{
128*0a6a1f1dSLionel Sambuc  # Assume a capable shell
129*0a6a1f1dSLionel Sambuc  lib_path=
130*0a6a1f1dSLionel Sambuc  shared=:
131*0a6a1f1dSLionel Sambuc  linker_opts=
132*0a6a1f1dSLionel Sambuc  for arg
133*0a6a1f1dSLionel Sambuc  do
134*0a6a1f1dSLionel Sambuc    if test -n "$eat"; then
135*0a6a1f1dSLionel Sambuc      eat=
136*0a6a1f1dSLionel Sambuc    else
137*0a6a1f1dSLionel Sambuc      case $1 in
138*0a6a1f1dSLionel Sambuc	-o)
139*0a6a1f1dSLionel Sambuc	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
140*0a6a1f1dSLionel Sambuc	  eat=1
141*0a6a1f1dSLionel Sambuc	  case $2 in
142*0a6a1f1dSLionel Sambuc	    *.o | *.[oO][bB][jJ])
143*0a6a1f1dSLionel Sambuc	      func_file_conv "$2"
144*0a6a1f1dSLionel Sambuc	      set x "$@" -Fo"$file"
145*0a6a1f1dSLionel Sambuc	      shift
146*0a6a1f1dSLionel Sambuc	      ;;
147*0a6a1f1dSLionel Sambuc	    *)
148*0a6a1f1dSLionel Sambuc	      func_file_conv "$2"
149*0a6a1f1dSLionel Sambuc	      set x "$@" -Fe"$file"
150*0a6a1f1dSLionel Sambuc	      shift
151*0a6a1f1dSLionel Sambuc	      ;;
152*0a6a1f1dSLionel Sambuc	  esac
153*0a6a1f1dSLionel Sambuc	  ;;
154*0a6a1f1dSLionel Sambuc	-I)
155*0a6a1f1dSLionel Sambuc	  eat=1
156*0a6a1f1dSLionel Sambuc	  func_file_conv "$2" mingw
157*0a6a1f1dSLionel Sambuc	  set x "$@" -I"$file"
158*0a6a1f1dSLionel Sambuc	  shift
159*0a6a1f1dSLionel Sambuc	  ;;
160*0a6a1f1dSLionel Sambuc	-I*)
161*0a6a1f1dSLionel Sambuc	  func_file_conv "${1#-I}" mingw
162*0a6a1f1dSLionel Sambuc	  set x "$@" -I"$file"
163*0a6a1f1dSLionel Sambuc	  shift
164*0a6a1f1dSLionel Sambuc	  ;;
165*0a6a1f1dSLionel Sambuc	-l)
166*0a6a1f1dSLionel Sambuc	  eat=1
167*0a6a1f1dSLionel Sambuc	  func_cl_dashl "$2"
168*0a6a1f1dSLionel Sambuc	  set x "$@" "$lib"
169*0a6a1f1dSLionel Sambuc	  shift
170*0a6a1f1dSLionel Sambuc	  ;;
171*0a6a1f1dSLionel Sambuc	-l*)
172*0a6a1f1dSLionel Sambuc	  func_cl_dashl "${1#-l}"
173*0a6a1f1dSLionel Sambuc	  set x "$@" "$lib"
174*0a6a1f1dSLionel Sambuc	  shift
175*0a6a1f1dSLionel Sambuc	  ;;
176*0a6a1f1dSLionel Sambuc	-L)
177*0a6a1f1dSLionel Sambuc	  eat=1
178*0a6a1f1dSLionel Sambuc	  func_cl_dashL "$2"
179*0a6a1f1dSLionel Sambuc	  ;;
180*0a6a1f1dSLionel Sambuc	-L*)
181*0a6a1f1dSLionel Sambuc	  func_cl_dashL "${1#-L}"
182*0a6a1f1dSLionel Sambuc	  ;;
183*0a6a1f1dSLionel Sambuc	-static)
184*0a6a1f1dSLionel Sambuc	  shared=false
185*0a6a1f1dSLionel Sambuc	  ;;
186*0a6a1f1dSLionel Sambuc	-Wl,*)
187*0a6a1f1dSLionel Sambuc	  arg=${1#-Wl,}
188*0a6a1f1dSLionel Sambuc	  save_ifs="$IFS"; IFS=','
189*0a6a1f1dSLionel Sambuc	  for flag in $arg; do
190*0a6a1f1dSLionel Sambuc	    IFS="$save_ifs"
191*0a6a1f1dSLionel Sambuc	    linker_opts="$linker_opts $flag"
192*0a6a1f1dSLionel Sambuc	  done
193*0a6a1f1dSLionel Sambuc	  IFS="$save_ifs"
194*0a6a1f1dSLionel Sambuc	  ;;
195*0a6a1f1dSLionel Sambuc	-Xlinker)
196*0a6a1f1dSLionel Sambuc	  eat=1
197*0a6a1f1dSLionel Sambuc	  linker_opts="$linker_opts $2"
198*0a6a1f1dSLionel Sambuc	  ;;
199*0a6a1f1dSLionel Sambuc	-*)
200*0a6a1f1dSLionel Sambuc	  set x "$@" "$1"
201*0a6a1f1dSLionel Sambuc	  shift
202*0a6a1f1dSLionel Sambuc	  ;;
203*0a6a1f1dSLionel Sambuc	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
204*0a6a1f1dSLionel Sambuc	  func_file_conv "$1"
205*0a6a1f1dSLionel Sambuc	  set x "$@" -Tp"$file"
206*0a6a1f1dSLionel Sambuc	  shift
207*0a6a1f1dSLionel Sambuc	  ;;
208*0a6a1f1dSLionel Sambuc	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
209*0a6a1f1dSLionel Sambuc	  func_file_conv "$1" mingw
210*0a6a1f1dSLionel Sambuc	  set x "$@" "$file"
211*0a6a1f1dSLionel Sambuc	  shift
212*0a6a1f1dSLionel Sambuc	  ;;
213*0a6a1f1dSLionel Sambuc	*)
214*0a6a1f1dSLionel Sambuc	  set x "$@" "$1"
215*0a6a1f1dSLionel Sambuc	  shift
216*0a6a1f1dSLionel Sambuc	  ;;
217*0a6a1f1dSLionel Sambuc      esac
218*0a6a1f1dSLionel Sambuc    fi
219*0a6a1f1dSLionel Sambuc    shift
220*0a6a1f1dSLionel Sambuc  done
221*0a6a1f1dSLionel Sambuc  if test -n "$linker_opts"; then
222*0a6a1f1dSLionel Sambuc    linker_opts="-link$linker_opts"
223*0a6a1f1dSLionel Sambuc  fi
224*0a6a1f1dSLionel Sambuc  exec "$@" $linker_opts
225*0a6a1f1dSLionel Sambuc  exit 1
226*0a6a1f1dSLionel Sambuc}
227*0a6a1f1dSLionel Sambuc
228*0a6a1f1dSLionel Sambuceat=
229*0a6a1f1dSLionel Sambuc
230357f1050SThomas Veermancase $1 in
231357f1050SThomas Veerman  '')
232*0a6a1f1dSLionel Sambuc     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
233357f1050SThomas Veerman     exit 1;
234357f1050SThomas Veerman     ;;
235357f1050SThomas Veerman  -h | --h*)
236357f1050SThomas Veerman    cat <<\EOF
237357f1050SThomas VeermanUsage: compile [--help] [--version] PROGRAM [ARGS]
238357f1050SThomas Veerman
239*0a6a1f1dSLionel SambucWrapper for compilers which do not understand '-c -o'.
240*0a6a1f1dSLionel SambucRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
241357f1050SThomas Veermanarguments, and rename the output as expected.
242357f1050SThomas Veerman
243357f1050SThomas VeermanIf you are trying to build a whole package this is not the
244*0a6a1f1dSLionel Sambucright script to run: please start by reading the file 'INSTALL'.
245357f1050SThomas Veerman
246357f1050SThomas VeermanReport bugs to <bug-automake@gnu.org>.
247357f1050SThomas VeermanEOF
248357f1050SThomas Veerman    exit $?
249357f1050SThomas Veerman    ;;
250357f1050SThomas Veerman  -v | --v*)
251357f1050SThomas Veerman    echo "compile $scriptversion"
252357f1050SThomas Veerman    exit $?
253357f1050SThomas Veerman    ;;
254*0a6a1f1dSLionel Sambuc  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
255*0a6a1f1dSLionel Sambuc    func_cl_wrapper "$@"      # Doesn't return...
256*0a6a1f1dSLionel Sambuc    ;;
257357f1050SThomas Veermanesac
258357f1050SThomas Veerman
259357f1050SThomas Veermanofile=
260357f1050SThomas Veermancfile=
261357f1050SThomas Veerman
262357f1050SThomas Veermanfor arg
263357f1050SThomas Veermando
264357f1050SThomas Veerman  if test -n "$eat"; then
265357f1050SThomas Veerman    eat=
266357f1050SThomas Veerman  else
267357f1050SThomas Veerman    case $1 in
268357f1050SThomas Veerman      -o)
269*0a6a1f1dSLionel Sambuc	# configure might choose to run compile as 'compile cc -o foo foo.c'.
270*0a6a1f1dSLionel Sambuc	# So we strip '-o arg' only if arg is an object.
271357f1050SThomas Veerman	eat=1
272357f1050SThomas Veerman	case $2 in
273357f1050SThomas Veerman	  *.o | *.obj)
274357f1050SThomas Veerman	    ofile=$2
275357f1050SThomas Veerman	    ;;
276357f1050SThomas Veerman	  *)
277357f1050SThomas Veerman	    set x "$@" -o "$2"
278357f1050SThomas Veerman	    shift
279357f1050SThomas Veerman	    ;;
280357f1050SThomas Veerman	esac
281357f1050SThomas Veerman	;;
282357f1050SThomas Veerman      *.c)
283357f1050SThomas Veerman	cfile=$1
284357f1050SThomas Veerman	set x "$@" "$1"
285357f1050SThomas Veerman	shift
286357f1050SThomas Veerman	;;
287357f1050SThomas Veerman      *)
288357f1050SThomas Veerman	set x "$@" "$1"
289357f1050SThomas Veerman	shift
290357f1050SThomas Veerman	;;
291357f1050SThomas Veerman    esac
292357f1050SThomas Veerman  fi
293357f1050SThomas Veerman  shift
294357f1050SThomas Veermandone
295357f1050SThomas Veerman
296357f1050SThomas Veermanif test -z "$ofile" || test -z "$cfile"; then
297*0a6a1f1dSLionel Sambuc  # If no '-o' option was seen then we might have been invoked from a
298357f1050SThomas Veerman  # pattern rule where we don't need one.  That is ok -- this is a
299357f1050SThomas Veerman  # normal compilation that the losing compiler can handle.  If no
300*0a6a1f1dSLionel Sambuc  # '.c' file was seen then we are probably linking.  That is also
301357f1050SThomas Veerman  # ok.
302357f1050SThomas Veerman  exec "$@"
303357f1050SThomas Veermanfi
304357f1050SThomas Veerman
305357f1050SThomas Veerman# Name of file we expect compiler to create.
30684d9c625SLionel Sambuccofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
307357f1050SThomas Veerman
308357f1050SThomas Veerman# Create the lock directory.
309*0a6a1f1dSLionel Sambuc# Note: use '[/\\:.-]' here to ensure that we don't use the same name
310357f1050SThomas Veerman# that we are using for the .o file.  Also, base the name on the expected
311357f1050SThomas Veerman# object file name, since that is what matters with a parallel build.
31284d9c625SLionel Sambuclockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
313357f1050SThomas Veermanwhile true; do
314357f1050SThomas Veerman  if mkdir "$lockdir" >/dev/null 2>&1; then
315357f1050SThomas Veerman    break
316357f1050SThomas Veerman  fi
317357f1050SThomas Veerman  sleep 1
318357f1050SThomas Veermandone
319357f1050SThomas Veerman# FIXME: race condition here if user kills between mkdir and trap.
320357f1050SThomas Veermantrap "rmdir '$lockdir'; exit 1" 1 2 15
321357f1050SThomas Veerman
322357f1050SThomas Veerman# Run the compile.
323357f1050SThomas Veerman"$@"
324357f1050SThomas Veermanret=$?
325357f1050SThomas Veerman
326357f1050SThomas Veermanif test -f "$cofile"; then
32784d9c625SLionel Sambuc  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
328357f1050SThomas Veermanelif test -f "${cofile}bj"; then
32984d9c625SLionel Sambuc  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
330357f1050SThomas Veermanfi
331357f1050SThomas Veerman
332357f1050SThomas Veermanrmdir "$lockdir"
333357f1050SThomas Veermanexit $ret
334357f1050SThomas Veerman
335357f1050SThomas Veerman# Local Variables:
336357f1050SThomas Veerman# mode: shell-script
337357f1050SThomas Veerman# sh-indentation: 2
338357f1050SThomas Veerman# eval: (add-hook 'write-file-hooks 'time-stamp)
339357f1050SThomas Veerman# time-stamp-start: "scriptversion="
340357f1050SThomas Veerman# time-stamp-format: "%:y-%02m-%02d.%02H"
34184d9c625SLionel Sambuc# time-stamp-time-zone: "UTC"
34284d9c625SLionel Sambuc# time-stamp-end: "; # UTC"
343357f1050SThomas Veerman# End:
344