Add support for building with clang-cl
[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 ml="ml64" # "$MSVC/x86_amd64/ml64"
67 safeseh=
68 shift 1
69 ;;
70 -clang-cl)
71 cl="clang-cl"
72 safeseh=
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_DEBUG_RTL)
118 # Link against debug CRT.
119 md=-MDd
120 shift 1
121 ;;
122 -c)
123 args="$args -c"
124 args="$(echo $args | sed 's%/Fe%/Fo%g')"
125 single="-c"
126 shift 1
127 ;;
128 -D*=*)
129 name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
130 value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
131 args="$args -D${name}='$value'"
132 defines="$defines -D${name}='$value'"
133 shift 1
134 ;;
135 -D*)
136 args="$args $1"
137 defines="$defines $1"
138 shift 1
139 ;;
140 -I)
141 args="$args -I$2"
142 includes="$includes -I$2"
143 shift 2
144 ;;
145 -I*)
146 args="$args $1"
147 includes="$includes $1"
148 shift 1
149 ;;
150 -W|-Wextra)
151 # TODO map extra warnings
152 shift 1
153 ;;
154 -Wall)
155 # -Wall on MSVC is overzealous, and we already build with -W3. Nothing
156 # to do here.
157 shift 1
158 ;;
159 -pedantic)
160 # libffi tests -pedantic with -Wall, so drop it also.
161 shift 1
162 ;;
163 -Werror)
164 args="$args -WX"
165 shift 1
166 ;;
167 -W*)
168 # TODO map specific warnings
169 shift 1
170 ;;
171 -S)
172 args="$args -FAs"
173 shift 1
174 ;;
175 -o)
176 outdir="$(dirname $2)"
177 base="$(basename $2|sed 's/\.[^.]*//g')"
178 if [ -n "$single" ]; then
179 output="-Fo$2"
180 else
181 output="-Fe$2"
182 fi
183 if [ -n "$assembly" ]; then
184 args="$args $output"
185 else
186 args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
187 fi
188 shift 2
189 ;;
190 *.S)
191 src=$1
192 assembly="true"
193 shift 1
194 ;;
195 *.c)
196 args="$args $1"
197 shift 1
198 ;;
199 *)
200 # Assume it's an MSVC argument, and pass it through.
201 args="$args $1"
202 shift 1
203 ;;
204 esac
205 done
206
207 # If -Zi is specified, certain optimizations are implicitly disabled
208 # by MSVC. Add back those optimizations if this is an optimized build.
209 # NOTE: These arguments must come after all others.
210 if [ -n "$opt" ]; then
211 args="$args -link -OPT:REF -OPT:ICF -INCREMENTAL:NO"
212 fi
213
214 if [ -n "$assembly" ]; then
215 if [ -z "$outdir" ]; then
216 outdir="."
217 fi
218 ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
219 echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
220 "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
221 output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
222 args="-nologo $safeseh $single $output $ppsrc"
223
224 echo "$ml $args"
225 eval "\"$ml\" $args"
226 result=$?
227
228 # required to fix ml64 broken output?
229 #mv *.obj $outdir
230 else
231 args="$md $args"
232 echo "$cl $args"
233 # Return an error code of 1 if an invalid command line parameter is passed
234 # instead of just ignoring it.
235 eval "(\"$cl\" $args 2>&1 1>&3 | \
236 awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1"
237 result=$?
238 fi
239
240 exit $result
241