xref: /netbsd-src/external/gpl3/gdb/dist/compile (revision 9d2109275e137f9027abb05e859af639830e18f5)
1597410b8Schristos#! /bin/sh
2*9d210927Schristos# Wrapper for compilers which do not understand '-c -o'.
3597410b8Schristos
4*9d210927Schristosscriptversion=2012-10-14.11; # UTC
5597410b8Schristos
6*9d210927Schristos# Copyright (C) 1999-2014 Free Software Foundation, Inc.
7597410b8Schristos# Written by Tom Tromey <tromey@cygnus.com>.
8597410b8Schristos#
9597410b8Schristos# This program is free software; you can redistribute it and/or modify
10597410b8Schristos# it under the terms of the GNU General Public License as published by
11597410b8Schristos# the Free Software Foundation; either version 2, or (at your option)
12597410b8Schristos# any later version.
13597410b8Schristos#
14597410b8Schristos# This program is distributed in the hope that it will be useful,
15597410b8Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
16597410b8Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17597410b8Schristos# GNU General Public License for more details.
18597410b8Schristos#
19597410b8Schristos# You should have received a copy of the GNU General Public License
20597410b8Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21597410b8Schristos
22597410b8Schristos# As a special exception to the GNU General Public License, if you
23597410b8Schristos# distribute this file as part of a program that contains a
24597410b8Schristos# configuration script generated by Autoconf, you may include it under
25597410b8Schristos# the same distribution terms that you use for the rest of that program.
26597410b8Schristos
27597410b8Schristos# This file is maintained in Automake, please report
28597410b8Schristos# bugs to <bug-automake@gnu.org> or send patches to
29597410b8Schristos# <automake-patches@gnu.org>.
30597410b8Schristos
31*9d210927Schristosnl='
32*9d210927Schristos'
33*9d210927Schristos
34*9d210927Schristos# We need space, tab and new line, in precisely that order.  Quoting is
35*9d210927Schristos# there to prevent tools from complaining about whitespace usage.
36*9d210927SchristosIFS=" ""	$nl"
37*9d210927Schristos
38*9d210927Schristosfile_conv=
39*9d210927Schristos
40*9d210927Schristos# func_file_conv build_file lazy
41*9d210927Schristos# Convert a $build file to $host form and store it in $file
42*9d210927Schristos# Currently only supports Windows hosts. If the determined conversion
43*9d210927Schristos# type is listed in (the comma separated) LAZY, no conversion will
44*9d210927Schristos# take place.
45*9d210927Schristosfunc_file_conv ()
46*9d210927Schristos{
47*9d210927Schristos  file=$1
48*9d210927Schristos  case $file in
49*9d210927Schristos    / | /[!/]*) # absolute file, and not a UNC file
50*9d210927Schristos      if test -z "$file_conv"; then
51*9d210927Schristos	# lazily determine how to convert abs files
52*9d210927Schristos	case `uname -s` in
53*9d210927Schristos	  MINGW*)
54*9d210927Schristos	    file_conv=mingw
55*9d210927Schristos	    ;;
56*9d210927Schristos	  CYGWIN*)
57*9d210927Schristos	    file_conv=cygwin
58*9d210927Schristos	    ;;
59*9d210927Schristos	  *)
60*9d210927Schristos	    file_conv=wine
61*9d210927Schristos	    ;;
62*9d210927Schristos	esac
63*9d210927Schristos      fi
64*9d210927Schristos      case $file_conv/,$2, in
65*9d210927Schristos	*,$file_conv,*)
66*9d210927Schristos	  ;;
67*9d210927Schristos	mingw/*)
68*9d210927Schristos	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69*9d210927Schristos	  ;;
70*9d210927Schristos	cygwin/*)
71*9d210927Schristos	  file=`cygpath -m "$file" || echo "$file"`
72*9d210927Schristos	  ;;
73*9d210927Schristos	wine/*)
74*9d210927Schristos	  file=`winepath -w "$file" || echo "$file"`
75*9d210927Schristos	  ;;
76*9d210927Schristos      esac
77*9d210927Schristos      ;;
78*9d210927Schristos  esac
79*9d210927Schristos}
80*9d210927Schristos
81*9d210927Schristos# func_cl_dashL linkdir
82*9d210927Schristos# Make cl look for libraries in LINKDIR
83*9d210927Schristosfunc_cl_dashL ()
84*9d210927Schristos{
85*9d210927Schristos  func_file_conv "$1"
86*9d210927Schristos  if test -z "$lib_path"; then
87*9d210927Schristos    lib_path=$file
88*9d210927Schristos  else
89*9d210927Schristos    lib_path="$lib_path;$file"
90*9d210927Schristos  fi
91*9d210927Schristos  linker_opts="$linker_opts -LIBPATH:$file"
92*9d210927Schristos}
93*9d210927Schristos
94*9d210927Schristos# func_cl_dashl library
95*9d210927Schristos# Do a library search-path lookup for cl
96*9d210927Schristosfunc_cl_dashl ()
97*9d210927Schristos{
98*9d210927Schristos  lib=$1
99*9d210927Schristos  found=no
100*9d210927Schristos  save_IFS=$IFS
101*9d210927Schristos  IFS=';'
102*9d210927Schristos  for dir in $lib_path $LIB
103*9d210927Schristos  do
104*9d210927Schristos    IFS=$save_IFS
105*9d210927Schristos    if $shared && test -f "$dir/$lib.dll.lib"; then
106*9d210927Schristos      found=yes
107*9d210927Schristos      lib=$dir/$lib.dll.lib
108*9d210927Schristos      break
109*9d210927Schristos    fi
110*9d210927Schristos    if test -f "$dir/$lib.lib"; then
111*9d210927Schristos      found=yes
112*9d210927Schristos      lib=$dir/$lib.lib
113*9d210927Schristos      break
114*9d210927Schristos    fi
115*9d210927Schristos    if test -f "$dir/lib$lib.a"; then
116*9d210927Schristos      found=yes
117*9d210927Schristos      lib=$dir/lib$lib.a
118*9d210927Schristos      break
119*9d210927Schristos    fi
120*9d210927Schristos  done
121*9d210927Schristos  IFS=$save_IFS
122*9d210927Schristos
123*9d210927Schristos  if test "$found" != yes; then
124*9d210927Schristos    lib=$lib.lib
125*9d210927Schristos  fi
126*9d210927Schristos}
127*9d210927Schristos
128*9d210927Schristos# func_cl_wrapper cl arg...
129*9d210927Schristos# Adjust compile command to suit cl
130*9d210927Schristosfunc_cl_wrapper ()
131*9d210927Schristos{
132*9d210927Schristos  # Assume a capable shell
133*9d210927Schristos  lib_path=
134*9d210927Schristos  shared=:
135*9d210927Schristos  linker_opts=
136*9d210927Schristos  for arg
137*9d210927Schristos  do
138*9d210927Schristos    if test -n "$eat"; then
139*9d210927Schristos      eat=
140*9d210927Schristos    else
141*9d210927Schristos      case $1 in
142*9d210927Schristos	-o)
143*9d210927Schristos	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144*9d210927Schristos	  eat=1
145*9d210927Schristos	  case $2 in
146*9d210927Schristos	    *.o | *.[oO][bB][jJ])
147*9d210927Schristos	      func_file_conv "$2"
148*9d210927Schristos	      set x "$@" -Fo"$file"
149*9d210927Schristos	      shift
150*9d210927Schristos	      ;;
151*9d210927Schristos	    *)
152*9d210927Schristos	      func_file_conv "$2"
153*9d210927Schristos	      set x "$@" -Fe"$file"
154*9d210927Schristos	      shift
155*9d210927Schristos	      ;;
156*9d210927Schristos	  esac
157*9d210927Schristos	  ;;
158*9d210927Schristos	-I)
159*9d210927Schristos	  eat=1
160*9d210927Schristos	  func_file_conv "$2" mingw
161*9d210927Schristos	  set x "$@" -I"$file"
162*9d210927Schristos	  shift
163*9d210927Schristos	  ;;
164*9d210927Schristos	-I*)
165*9d210927Schristos	  func_file_conv "${1#-I}" mingw
166*9d210927Schristos	  set x "$@" -I"$file"
167*9d210927Schristos	  shift
168*9d210927Schristos	  ;;
169*9d210927Schristos	-l)
170*9d210927Schristos	  eat=1
171*9d210927Schristos	  func_cl_dashl "$2"
172*9d210927Schristos	  set x "$@" "$lib"
173*9d210927Schristos	  shift
174*9d210927Schristos	  ;;
175*9d210927Schristos	-l*)
176*9d210927Schristos	  func_cl_dashl "${1#-l}"
177*9d210927Schristos	  set x "$@" "$lib"
178*9d210927Schristos	  shift
179*9d210927Schristos	  ;;
180*9d210927Schristos	-L)
181*9d210927Schristos	  eat=1
182*9d210927Schristos	  func_cl_dashL "$2"
183*9d210927Schristos	  ;;
184*9d210927Schristos	-L*)
185*9d210927Schristos	  func_cl_dashL "${1#-L}"
186*9d210927Schristos	  ;;
187*9d210927Schristos	-static)
188*9d210927Schristos	  shared=false
189*9d210927Schristos	  ;;
190*9d210927Schristos	-Wl,*)
191*9d210927Schristos	  arg=${1#-Wl,}
192*9d210927Schristos	  save_ifs="$IFS"; IFS=','
193*9d210927Schristos	  for flag in $arg; do
194*9d210927Schristos	    IFS="$save_ifs"
195*9d210927Schristos	    linker_opts="$linker_opts $flag"
196*9d210927Schristos	  done
197*9d210927Schristos	  IFS="$save_ifs"
198*9d210927Schristos	  ;;
199*9d210927Schristos	-Xlinker)
200*9d210927Schristos	  eat=1
201*9d210927Schristos	  linker_opts="$linker_opts $2"
202*9d210927Schristos	  ;;
203*9d210927Schristos	-*)
204*9d210927Schristos	  set x "$@" "$1"
205*9d210927Schristos	  shift
206*9d210927Schristos	  ;;
207*9d210927Schristos	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208*9d210927Schristos	  func_file_conv "$1"
209*9d210927Schristos	  set x "$@" -Tp"$file"
210*9d210927Schristos	  shift
211*9d210927Schristos	  ;;
212*9d210927Schristos	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213*9d210927Schristos	  func_file_conv "$1" mingw
214*9d210927Schristos	  set x "$@" "$file"
215*9d210927Schristos	  shift
216*9d210927Schristos	  ;;
217*9d210927Schristos	*)
218*9d210927Schristos	  set x "$@" "$1"
219*9d210927Schristos	  shift
220*9d210927Schristos	  ;;
221*9d210927Schristos      esac
222*9d210927Schristos    fi
223*9d210927Schristos    shift
224*9d210927Schristos  done
225*9d210927Schristos  if test -n "$linker_opts"; then
226*9d210927Schristos    linker_opts="-link$linker_opts"
227*9d210927Schristos  fi
228*9d210927Schristos  exec "$@" $linker_opts
229*9d210927Schristos  exit 1
230*9d210927Schristos}
231*9d210927Schristos
232*9d210927Schristoseat=
233*9d210927Schristos
234597410b8Schristoscase $1 in
235597410b8Schristos  '')
236*9d210927Schristos     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237597410b8Schristos     exit 1;
238597410b8Schristos     ;;
239597410b8Schristos  -h | --h*)
240597410b8Schristos    cat <<\EOF
241597410b8SchristosUsage: compile [--help] [--version] PROGRAM [ARGS]
242597410b8Schristos
243*9d210927SchristosWrapper for compilers which do not understand '-c -o'.
244*9d210927SchristosRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245597410b8Schristosarguments, and rename the output as expected.
246597410b8Schristos
247597410b8SchristosIf you are trying to build a whole package this is not the
248*9d210927Schristosright script to run: please start by reading the file 'INSTALL'.
249597410b8Schristos
250597410b8SchristosReport bugs to <bug-automake@gnu.org>.
251597410b8SchristosEOF
252597410b8Schristos    exit $?
253597410b8Schristos    ;;
254597410b8Schristos  -v | --v*)
255597410b8Schristos    echo "compile $scriptversion"
256597410b8Schristos    exit $?
257597410b8Schristos    ;;
258*9d210927Schristos  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
259*9d210927Schristos    func_cl_wrapper "$@"      # Doesn't return...
260*9d210927Schristos    ;;
261597410b8Schristosesac
262597410b8Schristos
263597410b8Schristosofile=
264597410b8Schristoscfile=
265597410b8Schristos
266597410b8Schristosfor arg
267597410b8Schristosdo
268597410b8Schristos  if test -n "$eat"; then
269597410b8Schristos    eat=
270597410b8Schristos  else
271597410b8Schristos    case $1 in
272597410b8Schristos      -o)
273*9d210927Schristos	# configure might choose to run compile as 'compile cc -o foo foo.c'.
274*9d210927Schristos	# So we strip '-o arg' only if arg is an object.
275597410b8Schristos	eat=1
276597410b8Schristos	case $2 in
277597410b8Schristos	  *.o | *.obj)
278597410b8Schristos	    ofile=$2
279597410b8Schristos	    ;;
280597410b8Schristos	  *)
281597410b8Schristos	    set x "$@" -o "$2"
282597410b8Schristos	    shift
283597410b8Schristos	    ;;
284597410b8Schristos	esac
285597410b8Schristos	;;
286597410b8Schristos      *.c)
287597410b8Schristos	cfile=$1
288597410b8Schristos	set x "$@" "$1"
289597410b8Schristos	shift
290597410b8Schristos	;;
291597410b8Schristos      *)
292597410b8Schristos	set x "$@" "$1"
293597410b8Schristos	shift
294597410b8Schristos	;;
295597410b8Schristos    esac
296597410b8Schristos  fi
297597410b8Schristos  shift
298597410b8Schristosdone
299597410b8Schristos
300597410b8Schristosif test -z "$ofile" || test -z "$cfile"; then
301*9d210927Schristos  # If no '-o' option was seen then we might have been invoked from a
302597410b8Schristos  # pattern rule where we don't need one.  That is ok -- this is a
303597410b8Schristos  # normal compilation that the losing compiler can handle.  If no
304*9d210927Schristos  # '.c' file was seen then we are probably linking.  That is also
305597410b8Schristos  # ok.
306597410b8Schristos  exec "$@"
307597410b8Schristosfi
308597410b8Schristos
309597410b8Schristos# Name of file we expect compiler to create.
310597410b8Schristoscofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
311597410b8Schristos
312597410b8Schristos# Create the lock directory.
313*9d210927Schristos# Note: use '[/\\:.-]' here to ensure that we don't use the same name
314597410b8Schristos# that we are using for the .o file.  Also, base the name on the expected
315597410b8Schristos# object file name, since that is what matters with a parallel build.
316597410b8Schristoslockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
317597410b8Schristoswhile true; do
318597410b8Schristos  if mkdir "$lockdir" >/dev/null 2>&1; then
319597410b8Schristos    break
320597410b8Schristos  fi
321597410b8Schristos  sleep 1
322597410b8Schristosdone
323597410b8Schristos# FIXME: race condition here if user kills between mkdir and trap.
324597410b8Schristostrap "rmdir '$lockdir'; exit 1" 1 2 15
325597410b8Schristos
326597410b8Schristos# Run the compile.
327597410b8Schristos"$@"
328597410b8Schristosret=$?
329597410b8Schristos
330597410b8Schristosif test -f "$cofile"; then
331*9d210927Schristos  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
332597410b8Schristoselif test -f "${cofile}bj"; then
333*9d210927Schristos  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
334597410b8Schristosfi
335597410b8Schristos
336597410b8Schristosrmdir "$lockdir"
337597410b8Schristosexit $ret
338597410b8Schristos
339597410b8Schristos# Local Variables:
340597410b8Schristos# mode: shell-script
341597410b8Schristos# sh-indentation: 2
342597410b8Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
343597410b8Schristos# time-stamp-start: "scriptversion="
344597410b8Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
345597410b8Schristos# time-stamp-time-zone: "UTC"
346597410b8Schristos# time-stamp-end: "; # UTC"
347597410b8Schristos# End:
348