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