Fix configuring msvc compiler
[libffi.git] / msvcc.sh
1 #!/bin/sh
2
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is the MSVC wrappificator.
17 #
18 # The Initial Developer of the Original Code is
19 # Timothy Wall <twalljava@dev.java.net>.
20 # Portions created by the Initial Developer are Copyright (C) 2009
21 # the Initial Developer. All Rights Reserved.
22 #
23 # Contributor(s):
24 # Daniel Witte <dwitte@mozilla.com>
25 #
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
37 #
38 # ***** END LICENSE BLOCK *****
39
40 #
41 # GCC-compatible wrapper for cl.exe and ml.exe. Arguments are given in GCC
42 # format and translated into something sensible for cl or ml.
43 #
44
45 args_orig=$@
46 args="-nologo -W3"
47 static_crt=
48 debug_crt=
49 cl="cl"
50 ml="ml"
51 safeseh="-safeseh"
52 output=
53
54 while [ $# -gt 0 ]
55 do
56 case $1
57 in
58 --version)
59 args="-help"
60 shift 1
61 ;;
62 -fexceptions)
63 # Don't enable exceptions for now.
64 #args="$args -EHac"
65 shift 1
66 ;;
67 -m32)
68 shift 1
69 ;;
70 -m64)
71 ml="ml64" # "$MSVC/x86_amd64/ml64"
72 safeseh=
73 shift 1
74 ;;
75 -clang-cl)
76 cl="clang-cl"
77 safeseh=
78 shift 1
79 ;;
80 -O0)
81 args="$args -Od"
82 shift 1
83 ;;
84 -O*)
85 # Runtime error checks (enabled by setting -RTC1 in the -DFFI_DEBUG
86 # case below) are not compatible with optimization flags and will
87 # cause the build to fail. Therefore, drop the optimization flag if
88 # -DFFI_DEBUG is also set.
89 case $args_orig in
90 *-DFFI_DEBUG*)
91 args="$args"
92 ;;
93 *)
94 # The ax_cc_maxopt.m4 macro from the upstream autoconf-archive
95 # project doesn't support MSVC and therefore ends up trying to
96 # use -O3. Use the equivalent "max optimization" flag for MSVC
97 # instead of erroring out.
98 case $1 in
99 -O3)
100 args="$args -O2"
101 ;;
102 *)
103 args="$args $1"
104 ;;
105 esac
106 opt="true"
107 ;;
108 esac
109 shift 1
110 ;;
111 -g)
112 # Enable debug symbol generation.
113 args="$args -Zi"
114 shift 1
115 ;;
116 -DFFI_DEBUG)
117 # Enable runtime error checks.
118 args="$args -RTC1"
119 defines="$defines $1"
120 shift 1
121 ;;
122 -DUSE_STATIC_RTL)
123 # Link against static CRT.
124 static_crt=1
125 shift 1
126 ;;
127 -DUSE_DEBUG_RTL)
128 # Link against debug CRT.
129 debug_crt=1
130 shift 1
131 ;;
132 -c)
133 args="$args -c"
134 args="$(echo $args | sed 's%/Fe%/Fo%g')"
135 single="-c"
136 shift 1
137 ;;
138 -D*=*)
139 name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
140 value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
141 args="$args -D${name}='$value'"
142 defines="$defines -D${name}='$value'"
143 shift 1
144 ;;
145 -D*)
146 args="$args $1"
147 defines="$defines $1"
148 shift 1
149 ;;
150 -I)
151 args="$args -I$2"
152 includes="$includes -I$2"
153 shift 2
154 ;;
155 -I*)
156 args="$args $1"
157 includes="$includes $1"
158 shift 1
159 ;;
160 -W|-Wextra)
161 # TODO map extra warnings
162 shift 1
163 ;;
164 -Wall)
165 # -Wall on MSVC is overzealous, and we already build with -W3. Nothing
166 # to do here.
167 shift 1
168 ;;
169 -pedantic)
170 # libffi tests -pedantic with -Wall, so drop it also.
171 shift 1
172 ;;
173 -Werror)
174 args="$args -WX"
175 shift 1
176 ;;
177 -W*)
178 # TODO map specific warnings
179 shift 1
180 ;;
181 -S)
182 args="$args -FAs"
183 shift 1
184 ;;
185 -o)
186 outdir="$(dirname $2)"
187 base="$(basename $2|sed 's/\.[^.]*//g')"
188 if [ -n "$single" ]; then
189 output="-Fo$2"
190 else
191 output="-Fe$2"
192 fi
193 if [ -n "$assembly" ]; then
194 args="$args $output"
195 else
196 args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
197 fi
198 shift 2
199 ;;
200 *.S)
201 src=$1
202 assembly="true"
203 shift 1
204 ;;
205 *.c)
206 args="$args $1"
207 shift 1
208 ;;
209 *)
210 # Assume it's an MSVC argument, and pass it through.
211 args="$args $1"
212 shift 1
213 ;;
214 esac
215 done
216
217 # If -Zi is specified, certain optimizations are implicitly disabled
218 # by MSVC. Add back those optimizations if this is an optimized build.
219 # NOTE: These arguments must come after all others.
220 if [ -n "$opt" ]; then
221 args="$args -link -OPT:REF -OPT:ICF -INCREMENTAL:NO"
222 fi
223
224 if [ -n "$static_crt" ]; then
225 md=-MT
226 else
227 md=-MD
228 fi
229
230 if [ -n "$debug_crt" ]; then
231 md="${md}d"
232 fi
233
234 if [ -n "$assembly" ]; then
235 if [ -z "$outdir" ]; then
236 outdir="."
237 fi
238 ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
239 echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
240 "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
241 output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
242 args="-nologo $safeseh $single $output $ppsrc"
243
244 echo "$ml $args"
245 eval "\"$ml\" $args"
246 result=$?
247
248 # required to fix ml64 broken output?
249 #mv *.obj $outdir
250 else
251 args="$md $args"
252 echo "$cl $args"
253 # Return an error code of 1 if an invalid command line parameter is passed
254 # instead of just ignoring it.
255 eval "(\"$cl\" $args 2>&1 1>&3 | \
256 awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1"
257 result=$?
258 fi
259
260 exit $result
261