Merge pull request #214 from tromey/document-enum-support
[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 -fexceptions)
59 # Don't enable exceptions for now.
60 #args="$args -EHac"
61 shift 1
62 ;;
63 -m32)
64 shift 1
65 ;;
66 -m64)
67 ml="ml64" # "$MSVC/x86_amd64/ml64"
68 safeseh=
69 shift 1
70 ;;
71 -clang-cl)
72 cl="clang-cl"
73 shift 1
74 ;;
75 -O0)
76 args="$args -Od"
77 shift 1
78 ;;
79 -O*)
80 # Runtime error checks (enabled by setting -RTC1 in the -DFFI_DEBUG
81 # case below) are not compatible with optimization flags and will
82 # cause the build to fail. Therefore, drop the optimization flag if
83 # -DFFI_DEBUG is also set.
84 case $args_orig in
85 *-DFFI_DEBUG*)
86 args="$args"
87 ;;
88 *)
89 # The ax_cc_maxopt.m4 macro from the upstream autoconf-archive
90 # project doesn't support MSVC and therefore ends up trying to
91 # use -O3. Use the equivalent "max optimization" flag for MSVC
92 # instead of erroring out.
93 case $1 in
94 -O3)
95 args="$args -O2"
96 ;;
97 *)
98 args="$args $1"
99 ;;
100 esac
101 opt="true"
102 ;;
103 esac
104 shift 1
105 ;;
106 -g)
107 # Enable debug symbol generation.
108 args="$args -Zi"
109 shift 1
110 ;;
111 -DFFI_DEBUG)
112 # Enable runtime error checks.
113 args="$args -RTC1"
114 defines="$defines $1"
115 shift 1
116 ;;
117 -DUSE_STATIC_RTL)
118 # Link against static CRT.
119 static_crt=1
120 shift 1
121 ;;
122 -DUSE_DEBUG_RTL)
123 # Link against debug CRT.
124 debug_crt=1
125 shift 1
126 ;;
127 -c)
128 args="$args -c"
129 args="$(echo $args | sed 's%/Fe%/Fo%g')"
130 single="-c"
131 shift 1
132 ;;
133 -D*=*)
134 name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
135 value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
136 args="$args -D${name}='$value'"
137 defines="$defines -D${name}='$value'"
138 shift 1
139 ;;
140 -D*)
141 args="$args $1"
142 defines="$defines $1"
143 shift 1
144 ;;
145 -I)
146 args="$args -I$2"
147 includes="$includes -I$2"
148 shift 2
149 ;;
150 -I*)
151 args="$args $1"
152 includes="$includes $1"
153 shift 1
154 ;;
155 -W|-Wextra)
156 # TODO map extra warnings
157 shift 1
158 ;;
159 -Wall)
160 # -Wall on MSVC is overzealous, and we already build with -W3. Nothing
161 # to do here.
162 shift 1
163 ;;
164 -pedantic)
165 # libffi tests -pedantic with -Wall, so drop it also.
166 shift 1
167 ;;
168 -Werror)
169 args="$args -WX"
170 shift 1
171 ;;
172 -W*)
173 # TODO map specific warnings
174 shift 1
175 ;;
176 -S)
177 args="$args -FAs"
178 shift 1
179 ;;
180 -o)
181 outdir="$(dirname $2)"
182 base="$(basename $2|sed 's/\.[^.]*//g')"
183 if [ -n "$single" ]; then
184 output="-Fo$2"
185 else
186 output="-Fe$2"
187 fi
188 if [ -n "$assembly" ]; then
189 args="$args $output"
190 else
191 args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
192 fi
193 shift 2
194 ;;
195 *.S)
196 src=$1
197 assembly="true"
198 shift 1
199 ;;
200 *.c)
201 args="$args $1"
202 shift 1
203 ;;
204 *)
205 # Assume it's an MSVC argument, and pass it through.
206 args="$args $1"
207 shift 1
208 ;;
209 esac
210 done
211
212 # If -Zi is specified, certain optimizations are implicitly disabled
213 # by MSVC. Add back those optimizations if this is an optimized build.
214 # NOTE: These arguments must come after all others.
215 if [ -n "$opt" ]; then
216 args="$args -link -OPT:REF -OPT:ICF -INCREMENTAL:NO"
217 fi
218
219 if [ -n "$static_crt" ]; then
220 md=-MT
221 else
222 md=-MD
223 fi
224
225 if [ -n "$debug_crt" ]; then
226 md="${md}d"
227 fi
228
229 if [ -n "$assembly" ]; then
230 if [ -z "$outdir" ]; then
231 outdir="."
232 fi
233 ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
234 echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
235 "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
236 output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
237 args="-nologo $safeseh $single $output $ppsrc"
238
239 echo "$ml $args"
240 eval "\"$ml\" $args"
241 result=$?
242
243 # required to fix ml64 broken output?
244 #mv *.obj $outdir
245 else
246 args="$md $args"
247 echo "$cl $args"
248 # Return an error code of 1 if an invalid command line parameter is passed
249 # instead of just ignoring it.
250 eval "(\"$cl\" $args 2>&1 1>&3 | \
251 awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1"
252 result=$?
253 fi
254
255 exit $result
256