ae47d8bcf0cbd107950d0427746151d94e84ad0b
[gnulib.git] / build-aux / compile
1 #! /bin/sh
2 # Wrapper for compilers which do not understand `-c -o'.
3
4 scriptversion=2010-08-01.07; # 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
42 # Convert a $build file to $host form and store it in $file
43 # Currently only supports Win32 hosts.
44 func_file_conv ()
45 {
46   file=$1
47   case $file in
48     / | /[^/]*) # absolute file, and not a UNC file
49       if test -z "$file_conv"; then
50         # lazily determine how to convert abs files
51         case `uname -s` in
52           MINGW*)
53             file_conv=mingw
54             ;;
55           CYGWIN*)
56             file_conv=cygwin
57             ;;
58           *)
59             file_conv=wine
60             ;;
61         esac
62       fi
63       case $file_conv in
64         mingw)
65           file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
66           ;;
67         cygwin)
68           file=`cygpath -m "$file" || echo "$file"`
69           ;;
70         wine)
71           file=`winepath -w "$file" || echo "$file"`
72           ;;
73       esac
74       ;;
75   esac
76 }
77
78 # func_cl_wrapper cl arg...
79 # Adjust compile command to suite cl
80 func_cl_wrapper ()
81 {
82   # Assume a capable shell
83   linker_opts=
84   for arg
85   do
86     if test -n "$eat"; then
87       eat=
88     else
89       case $1 in
90         -o)
91           # configure might choose to run compile as `compile cc -o foo foo.c'.
92           eat=1
93           case $2 in
94             *.o | *.[oO][bB][jJ])
95               func_file_conv "$2"
96               set x "$@" -Fo"$file"
97               shift
98               ;;
99             *)
100               func_file_conv "$2"
101               set x "$@" -Fe"$file"
102               shift
103               ;;
104           esac
105           ;;
106         -I*)
107           func_file_conv "${1#-I}"
108           set x "$@" -I"$file"
109           shift
110           ;;
111         -l*)
112           set x "$@" "${1#-l}.lib"
113           shift
114           ;;
115         -L*)
116           func_file_conv "${1#-L}"
117           linker_opts="$linker_opts -LIBPATH:$file"
118           ;;
119         -Wl,*)
120           arg=${1#-Wl,}
121           save_ifs="$IFS"; IFS=','
122           for flag in $arg; do
123             IFS="$save_ifs"
124             linker_opts="$linker_opts $flag"
125           done
126           IFS="$save_ifs"
127           ;;
128         -Xlinker)
129           eat=1
130           linker_opts="$linker_opts $2"
131           ;;
132         *)
133           set x "$@" "$1"
134           shift
135           ;;
136       esac
137     fi
138     shift
139   done
140   if test -n "$linker_opts"; then
141     linker_opts="-link$linker_opts"
142   fi
143   exec "$@" $linker_opts
144   exit 1
145 }
146
147 case $1 in
148   '')
149      echo "$0: No command.  Try \`$0 --help' for more information." 1>&2
150      exit 1;
151      ;;
152   -h | --h*)
153     cat <<\EOF
154 Usage: compile [--help] [--version] PROGRAM [ARGS]
155
156 Wrapper for compilers which do not understand `-c -o'.
157 Remove `-o dest.o' from ARGS, run PROGRAM with the remaining
158 arguments, and rename the output as expected.
159
160 If you are trying to build a whole package this is not the
161 right script to run: please start by reading the file `INSTALL'.
162
163 Report bugs to <bug-automake@gnu.org>.
164 EOF
165     exit $?
166     ;;
167   -v | --v*)
168     echo "compile $scriptversion"
169     exit $?
170     ;;
171   cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
172     func_cl_wrapper "$@"      # Doesn't return...
173     ;;
174 esac
175
176 ofile=
177 cfile=
178 eat=
179
180 for arg
181 do
182   if test -n "$eat"; then
183     eat=
184   else
185     case $1 in
186       -o)
187         # configure might choose to run compile as `compile cc -o foo foo.c'.
188         # So we strip `-o arg' only if arg is an object.
189         eat=1
190         case $2 in
191           *.o | *.obj)
192             ofile=$2
193             ;;
194           *)
195             set x "$@" -o "$2"
196             shift
197             ;;
198         esac
199         ;;
200       *.c)
201         cfile=$1
202         set x "$@" "$1"
203         shift
204         ;;
205       *)
206         set x "$@" "$1"
207         shift
208         ;;
209     esac
210   fi
211   shift
212 done
213
214 if test -z "$ofile" || test -z "$cfile"; then
215   # If no `-o' option was seen then we might have been invoked from a
216   # pattern rule where we don't need one.  That is ok -- this is a
217   # normal compilation that the losing compiler can handle.  If no
218   # `.c' file was seen then we are probably linking.  That is also
219   # ok.
220   exec "$@"
221 fi
222
223 # Name of file we expect compiler to create.
224 cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
225
226 # Create the lock directory.
227 # Note: use `[/\\:.-]' here to ensure that we don't use the same name
228 # that we are using for the .o file.  Also, base the name on the expected
229 # object file name, since that is what matters with a parallel build.
230 lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
231 while true; do
232   if mkdir "$lockdir" >/dev/null 2>&1; then
233     break
234   fi
235   sleep 1
236 done
237 # FIXME: race condition here if user kills between mkdir and trap.
238 trap "rmdir '$lockdir'; exit 1" 1 2 15
239
240 # Run the compile.
241 "$@"
242 ret=$?
243
244 if test -f "$cofile"; then
245   test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
246 elif test -f "${cofile}bj"; then
247   test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
248 fi
249
250 rmdir "$lockdir"
251 exit $ret
252
253 # Local Variables:
254 # mode: shell-script
255 # sh-indentation: 2
256 # eval: (add-hook 'write-file-hooks 'time-stamp)
257 # time-stamp-start: "scriptversion="
258 # time-stamp-format: "%:y-%02m-%02d.%02H"
259 # time-stamp-time-zone: "UTC"
260 # time-stamp-end: "; # UTC"
261 # End: