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