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