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