Commit ff3cd68b8cf2d9a28cad7aa9beff46236eacec8c

Anthony Green 2010-01-15T11:27:24

Add msvc.sh wrapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
diff --git a/.pc/msvc/README b/.pc/msvc/README
new file mode 100644
index 0000000..8a03bf9
--- /dev/null
+++ b/.pc/msvc/README
@@ -0,0 +1,312 @@
+Status
+======
+
+libffi-3.0.10 was released on XXXXXXXXXX, 2010. Check the libffi web
+page for updates: <URL:http://sourceware.org/libffi/>.
+
+
+What is libffi?
+===============
+
+Compilers for high level languages generate code that follow certain
+conventions. These conventions are necessary, in part, for separate
+compilation to work. One such convention is the "calling
+convention". The "calling convention" is essentially a set of
+assumptions made by the compiler about where function arguments will
+be found on entry to a function. A "calling convention" also specifies
+where the return value for a function is found.
+
+Some programs may not know at the time of compilation what arguments
+are to be passed to a function. For instance, an interpreter may be
+told at run-time about the number and types of arguments used to call
+a given function. Libffi can be used in such programs to provide a
+bridge from the interpreter program to compiled code.
+
+The libffi library provides a portable, high level programming
+interface to various calling conventions. This allows a programmer to
+call any function specified by a call interface description at run
+time.  
+
+FFI stands for Foreign Function Interface.  A foreign function
+interface is the popular name for the interface that allows code
+written in one language to call code written in another language. The
+libffi library really only provides the lowest, machine dependent
+layer of a fully featured foreign function interface. A layer must
+exist above libffi that handles type conversions for values passed
+between the two languages.
+
+
+Supported Platforms
+===================
+
+Libffi has been ported to many different platforms.
+For specific configuration details and testing status, please
+refer to the wiki page here:
+
+ http://www.moxielogic.org/wiki/index.php?title=Libffi_3.0.10
+
+At the time of release, the following basic configurations have been
+tested:
+
+|--------------+------------------|
+| Architecture | Operating System |
+|--------------+------------------|
+| Alpha        | Linux            |
+| Alpha        | Tru64            |
+| ARM          | Linux            |
+| AVR32        | Linux            |
+| HPPA         | HPUX             |
+| IA-64        | Linux            |
+| MIPS         | IRIX             |
+| MIPS         | Linux            |
+| MIPS64       | Linux            |
+| PowerPC      | Linux            |
+| PowerPC      | Mac OSX          |
+| PowerPC      | FreeBSD          |
+| PowerPC64    | Linux            |
+| S390         | Linux            |
+| S390X        | Linux            |
+| SPARC        | Linux            |
+| SPARC        | Solaris          |
+| SPARC64      | Linux            |
+| SPARC64      | FreeBSD          |
+| X86          | FreeBSD          |
+| X86          | kFreeBSD         |
+| X86          | Linux            |
+| X86          | Mac OSX          |
+| X86          | OpenBSD          |
+| X86          | Solaris          |
+| X86          | Windows/Cygwin   |
+| X86          | Windows/MingW    |
+| X86-64       | FreeBSD          |
+| X86-64       | Linux            |
+| X86-64       | OpenBSD          |
+| X86-64       | Windows/MingW    |
+|--------------+------------------|
+
+Please send additional platform test results to
+libffi-discuss@sourceware.org and feel free to update the wiki page
+above.
+
+Installing libffi
+=================
+
+First you must configure the distribution for your particular
+system. Go to the directory you wish to build libffi in and run the
+"configure" program found in the root directory of the libffi source
+distribution.
+
+You may want to tell configure where to install the libffi library and
+header files. To do that, use the --prefix configure switch.  Libffi
+will install under /usr/local by default. 
+
+If you want to enable extra run-time debugging checks use the the
+--enable-debug configure switch. This is useful when your program dies
+mysteriously while using libffi. 
+
+Another useful configure switch is --enable-purify-safety. Using this
+will add some extra code which will suppress certain warnings when you
+are using Purify with libffi. Only use this switch when using 
+Purify, as it will slow down the library.
+
+Configure has many other options. Use "configure --help" to see them all.
+
+Once configure has finished, type "make". Note that you must be using
+GNU make.  You can ftp GNU make from prep.ai.mit.edu:/pub/gnu.
+
+To ensure that libffi is working as advertised, type "make check".
+This will require that you have DejaGNU installed.
+
+To install the library and header files, type "make install".
+
+
+History
+=======
+
+See the ChangeLog files for details.
+
+3.0.10 ???-??-??
+        Fix the N64 build on mips-sgi-irix6.5.
+	Testsuite fixes for Tru64 Unix.
+
+3.0.9 Dec-31-09
+        Add AVR32 and win64 ports.  Add ARM softfp support.
+	Many fixes for AIX, Solaris, HP-UX, *BSD.
+	Several PowerPC and x86-64 bug fixes.
+	Build DLL for windows.
+
+3.0.8 Dec-19-08
+        Add *BSD, BeOS, and PA-Linux support.
+
+3.0.7 Nov-11-08
+        Fix for ppc FreeBSD.
+	(thanks to Andreas Tobler)
+
+3.0.6 Jul-17-08
+        Fix for closures on sh.
+	Mark the sh/sh64 stack as non-executable.
+	(both thanks to Kaz Kojima)
+
+3.0.5 Apr-3-08
+        Fix libffi.pc file.
+	Fix #define ARM for IcedTea users.
+	Fix x86 closure bug.
+
+3.0.4 Feb-24-08
+        Fix x86 OpenBSD configury.
+
+3.0.3 Feb-22-08
+        Enable x86 OpenBSD thanks to Thomas Heller, and
+	x86-64 FreeBSD thanks to Björn König and Andreas Tobler.
+	Clean up test instruction in README.
+
+3.0.2 Feb-21-08
+        Improved x86 FreeBSD support.
+	Thanks to Björn König.
+
+3.0.1 Feb-15-08
+        Fix instruction cache flushing bug on MIPS.
+	Thanks to David Daney.
+
+3.0.0 Feb-15-08
+        Many changes, mostly thanks to the GCC project.
+	Cygnus Solutions is now Red Hat.
+
+  [10 years go by...]
+
+1.20 Oct-5-98
+	Raffaele Sena produces ARM port.
+
+1.19 Oct-5-98
+	Fixed x86 long double and long long return support.
+	m68k bug fixes from Andreas Schwab.
+	Patch for DU assembler compatibility for the Alpha from Richard
+	Henderson.
+
+1.18 Apr-17-98
+	Bug fixes and MIPS configuration changes.
+
+1.17 Feb-24-98
+	Bug fixes and m68k port from Andreas Schwab. PowerPC port from
+	Geoffrey Keating. Various bug x86, Sparc and MIPS bug fixes.
+
+1.16 Feb-11-98
+	Richard Henderson produces Alpha port.
+
+1.15 Dec-4-97
+	Fixed an n32 ABI bug. New libtool, auto* support.
+
+1.14 May-13-97
+	libtool is now used to generate shared and static libraries.
+	Fixed a minor portability problem reported by Russ McManus
+	<mcmanr@eq.gs.com>.
+
+1.13 Dec-2-96
+	Added --enable-purify-safety to keep Purify from complaining
+	about certain low level code.
+	Sparc fix for calling functions with < 6 args.
+	Linux x86 a.out fix.
+
+1.12 Nov-22-96
+	Added missing ffi_type_void, needed for supporting void return 
+	types. Fixed test case for non MIPS machines. Cygnus Support 
+	is now Cygnus Solutions. 
+
+1.11 Oct-30-96
+	Added notes about GNU make.
+
+1.10 Oct-29-96
+	Added configuration fix for non GNU compilers.
+
+1.09 Oct-29-96
+	Added --enable-debug configure switch. Clean-ups based on LCLint 
+	feedback. ffi_mips.h is always installed. Many configuration 
+	fixes. Fixed ffitest.c for sparc builds.
+
+1.08 Oct-15-96
+	Fixed n32 problem. Many clean-ups.
+
+1.07 Oct-14-96
+	Gordon Irlam rewrites v8.S again. Bug fixes.
+
+1.06 Oct-14-96
+	Gordon Irlam improved the sparc port. 
+
+1.05 Oct-14-96
+	Interface changes based on feedback.
+
+1.04 Oct-11-96
+	Sparc port complete (modulo struct passing bug).
+
+1.03 Oct-10-96
+	Passing struct args, and returning struct values works for
+	all architectures/calling conventions. Expanded tests.
+
+1.02 Oct-9-96
+	Added SGI n32 support. Fixed bugs in both o32 and Linux support.
+	Added "make test".
+
+1.01 Oct-8-96
+	Fixed float passing bug in mips version. Restructured some
+	of the code. Builds cleanly with SGI tools.
+
+1.00 Oct-7-96
+	First release. No public announcement.
+
+
+Authors & Credits
+=================
+
+libffi was originally written by Anthony Green <green@redhat.com>.
+
+The developers of the GNU Compiler Collection project have made
+innumerable valuable contributions.  See the ChangeLog file for
+details.
+
+Some of the ideas behind libffi were inspired by Gianni Mariani's free
+gencall library for Silicon Graphics machines.
+
+The closure mechanism was designed and implemented by Kresten Krab
+Thorup.
+
+Major processor architecture ports were contributed by the following
+developers:
+
+alpha		Richard Henderson
+arm		Raffaele Sena
+cris		Simon Posnjak, Hans-Peter Nilsson
+frv		Anthony Green
+ia64		Hans Boehm
+m32r		Kazuhiro Inaoka
+m68k		Andreas Schwab
+mips		Anthony Green, Casey Marshall
+mips64		David Daney
+pa		Randolph Chung, Dave Anglin, Andreas Tobler
+powerpc		Geoffrey Keating, Andreas Tobler, 
+			 David Edelsohn, John Hornkvist
+powerpc64	Jakub Jelinek
+s390		Gerhard Tonn, Ulrich Weigand
+sh		Kaz Kojima
+sh64		Kaz Kojima
+sparc		Anthony Green, Gordon Irlam
+x86		Anthony Green, Jon Beniston
+x86-64		Bo Thorsen
+
+Jesper Skov and Andrew Haley both did more than their fair share of
+stepping through the code and tracking down bugs.
+
+Thanks also to Tom Tromey for bug fixes, documentation and
+configuration help.
+
+Thanks to Jim Blandy, who provided some useful feedback on the libffi
+interface.
+
+Andreas Tobler has done a tremendous amount of work on the testsuite.
+
+Alex Oliva solved the executable page problem for SElinux.
+
+The list above is almost certainly incomplete and inaccurate.  I'm
+happy to make corrections or additions upon request.
+
+If you have a problem, or have found a bug, please send a note to
+green@redhat.com.
diff --git a/.pc/msvc/msvcc.sh b/.pc/msvc/msvcc.sh
new file mode 100644
index 0000000..8301839
--- /dev/null
+++ b/.pc/msvc/msvcc.sh
@@ -0,0 +1,185 @@
+#!/bin/sh
+
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is the MSVC wrappificator.
+#
+# The Initial Developer of the Original Code is
+# Timothy Wall <twalljava@dev.java.net>.
+# Portions created by the Initial Developer are Copyright (C) 2009
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#   Daniel Witte <dwitte@mozilla.com>
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+#
+# GCC-compatible wrapper for cl.exe and ml.exe. Arguments are given in GCC
+# format and translated into something sensible for cl or ml.
+#
+
+# Disable specific warnings, and enable warnings-as-errors so we catch any
+# mistranslated args.
+nowarn="-wd4127 -wd4820 -wd4706 -wd4100 -wd4255 -wd4668 -wd4053 -wd4324"
+args="-nologo -W3 -WX $nowarn"
+md=-MD
+cl="cl"
+ml="ml"
+output=
+
+while [ $# -gt 0 ]
+do
+  case $1
+  in
+    -fexceptions)
+      # Don't enable exceptions for now.
+      #args="$args -EHac"
+      shift 1
+    ;;
+    -m32)
+      shift 1
+    ;;
+    -m64)
+      cl="cl"   # "$MSVC/x86_amd64/cl"
+      ml="ml64" # "$MSVC/x86_amd64/ml64"
+      shift 1
+    ;;
+    -O*)
+      args="$args $1"
+      shift 1
+    ;;
+    -g)
+      # Can't specify -RTC1 or -Zi in opt. -Gy is ok. Use -OPT:REF?
+      args="$args -D_DEBUG -RTC1 -Zi"
+      md=-MDd
+      shift 1
+    ;;
+    -c)
+      args="$args -c"
+      args="$(echo $args | sed 's%/Fe%/Fo%g')"
+      single="-c"
+      shift 1
+    ;;
+    -D*=*)
+      name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
+      value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
+      args="$args -D${name}='$value'"
+      defines="$defines -D${name}='$value'"
+      shift 1
+    ;;
+    -D*)
+      args="$args $1"
+      defines="$defines $1"
+      shift 1
+    ;;
+    -I)
+      args="$args -I$2"
+      includes="$includes -I$2"
+      shift 2
+    ;;
+    -I*)
+      args="$args $1"
+      includes="$includes $1"
+      shift 1
+    ;;
+    -W|-Wextra)
+      # TODO map extra warnings
+      shift 1
+    ;;
+    -Wall)
+      args="$args -Wall"
+      shift 1
+    ;;
+    -Werror)
+      args="$args -WX"
+      shift 1
+    ;;
+    -W*)
+      # TODO map specific warnings
+      shift 1
+    ;;
+    -S)
+      args="$args -FAs"
+      shift 1
+    ;;
+    -o)
+      outdir="$(dirname $2)"
+      base="$(basename $2|sed 's/\.[^.]*//g')"
+      if [ -n "$single" ]; then 
+        output="-Fo$2"
+      else
+        output="-Fe$2"
+      fi
+      if [ -n "$assembly" ]; then
+        args="$args $output"
+      else
+        args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
+      fi
+      shift 2
+    ;;
+    *.S)
+      src=$1
+      assembly="true"
+      shift 1
+    ;;
+    *.c)
+      args="$args $1"
+      shift 1
+    ;;
+    *)
+      # Assume it's an MSVC argument, and pass it through.
+      args="$args $1"
+      shift 1
+    ;;
+  esac
+done
+
+if [ -n "$assembly" ]; then
+    if [ -z "$outdir" ]; then
+      outdir="."
+    fi
+    ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
+    echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
+    "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
+    output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
+    args="-nologo -safeseh $single $output $ppsrc"
+
+    echo "$ml $args"
+    eval "\"$ml\" $args"
+    result=$?
+
+    # required to fix ml64 broken output?
+    #mv *.obj $outdir
+else
+    args="$md $args"
+    echo "$cl $args"
+    eval "\"$cl\" $args"
+    result=$?
+fi
+
+exit $result
+
diff --git a/ChangeLog.libffi b/ChangeLog.libffi
index b3c333c..5272f33 100644
--- a/ChangeLog.libffi
+++ b/ChangeLog.libffi
@@ -1,5 +1,11 @@
+2010-01-15  Anthony Green  <green@redhat.com>
+
+	* README: Add notes on building with Microsoft Visual C++.
+
 2010-01-15  Daniel Witte  <dwitte@mozilla.com>
 
+	* msvcc.sh: New file.
+
 	* src/x86/win32.S: Port assembly routines to MSVC and #ifdef.
 	* src/x86/ffi.c: Tweak function declaration and remove excess
 	parens.
diff --git a/README b/README
index 8a03bf9..7be8356 100644
--- a/README
+++ b/README
@@ -109,6 +109,14 @@ will add some extra code which will suppress certain warnings when you
 are using Purify with libffi. Only use this switch when using 
 Purify, as it will slow down the library.
 
+It's also possible to build libffi on Windows platforms with
+Microsoft's Visual C++ compiler.  In this case, use the msvcc.sh
+wrapper script during configuration like so:
+
+path/to/configure --enable-shared --enable-static \
+	CC=path/to/msvcc.sh LD=link \
+	CPP=\"cl -nologo -EP\"
+
 Configure has many other options. Use "configure --help" to see them all.
 
 Once configure has finished, type "make". Note that you must be using
diff --git a/msvcc.sh b/msvcc.sh
new file mode 100755
index 0000000..8301839
--- /dev/null
+++ b/msvcc.sh
@@ -0,0 +1,185 @@
+#!/bin/sh
+
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is the MSVC wrappificator.
+#
+# The Initial Developer of the Original Code is
+# Timothy Wall <twalljava@dev.java.net>.
+# Portions created by the Initial Developer are Copyright (C) 2009
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#   Daniel Witte <dwitte@mozilla.com>
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+#
+# GCC-compatible wrapper for cl.exe and ml.exe. Arguments are given in GCC
+# format and translated into something sensible for cl or ml.
+#
+
+# Disable specific warnings, and enable warnings-as-errors so we catch any
+# mistranslated args.
+nowarn="-wd4127 -wd4820 -wd4706 -wd4100 -wd4255 -wd4668 -wd4053 -wd4324"
+args="-nologo -W3 -WX $nowarn"
+md=-MD
+cl="cl"
+ml="ml"
+output=
+
+while [ $# -gt 0 ]
+do
+  case $1
+  in
+    -fexceptions)
+      # Don't enable exceptions for now.
+      #args="$args -EHac"
+      shift 1
+    ;;
+    -m32)
+      shift 1
+    ;;
+    -m64)
+      cl="cl"   # "$MSVC/x86_amd64/cl"
+      ml="ml64" # "$MSVC/x86_amd64/ml64"
+      shift 1
+    ;;
+    -O*)
+      args="$args $1"
+      shift 1
+    ;;
+    -g)
+      # Can't specify -RTC1 or -Zi in opt. -Gy is ok. Use -OPT:REF?
+      args="$args -D_DEBUG -RTC1 -Zi"
+      md=-MDd
+      shift 1
+    ;;
+    -c)
+      args="$args -c"
+      args="$(echo $args | sed 's%/Fe%/Fo%g')"
+      single="-c"
+      shift 1
+    ;;
+    -D*=*)
+      name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
+      value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
+      args="$args -D${name}='$value'"
+      defines="$defines -D${name}='$value'"
+      shift 1
+    ;;
+    -D*)
+      args="$args $1"
+      defines="$defines $1"
+      shift 1
+    ;;
+    -I)
+      args="$args -I$2"
+      includes="$includes -I$2"
+      shift 2
+    ;;
+    -I*)
+      args="$args $1"
+      includes="$includes $1"
+      shift 1
+    ;;
+    -W|-Wextra)
+      # TODO map extra warnings
+      shift 1
+    ;;
+    -Wall)
+      args="$args -Wall"
+      shift 1
+    ;;
+    -Werror)
+      args="$args -WX"
+      shift 1
+    ;;
+    -W*)
+      # TODO map specific warnings
+      shift 1
+    ;;
+    -S)
+      args="$args -FAs"
+      shift 1
+    ;;
+    -o)
+      outdir="$(dirname $2)"
+      base="$(basename $2|sed 's/\.[^.]*//g')"
+      if [ -n "$single" ]; then 
+        output="-Fo$2"
+      else
+        output="-Fe$2"
+      fi
+      if [ -n "$assembly" ]; then
+        args="$args $output"
+      else
+        args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
+      fi
+      shift 2
+    ;;
+    *.S)
+      src=$1
+      assembly="true"
+      shift 1
+    ;;
+    *.c)
+      args="$args $1"
+      shift 1
+    ;;
+    *)
+      # Assume it's an MSVC argument, and pass it through.
+      args="$args $1"
+      shift 1
+    ;;
+  esac
+done
+
+if [ -n "$assembly" ]; then
+    if [ -z "$outdir" ]; then
+      outdir="."
+    fi
+    ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
+    echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
+    "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
+    output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
+    args="-nologo -safeseh $single $output $ppsrc"
+
+    echo "$ml $args"
+    eval "\"$ml\" $args"
+    result=$?
+
+    # required to fix ml64 broken output?
+    #mv *.obj $outdir
+else
+    args="$md $args"
+    echo "$cl $args"
+    eval "\"$cl\" $args"
+    result=$?
+fi
+
+exit $result
+
diff --git a/patches/msvc b/patches/msvc
index 1856fd5..1a5c031 100644
--- a/patches/msvc
+++ b/patches/msvc
@@ -2,9 +2,15 @@ Index: libffi/ChangeLog.libffi
 ===================================================================
 --- libffi.orig/ChangeLog.libffi
 +++ libffi/ChangeLog.libffi
-@@ -1,3 +1,20 @@
+@@ -1,3 +1,26 @@
++2010-01-15  Anthony Green  <green@redhat.com>
++
++	* README: Add notes on building with Microsoft Visual C++.
++
 +2010-01-15  Daniel Witte  <dwitte@mozilla.com>
 +
++	* msvcc.sh: New file.
++
 +	* src/x86/win32.S: Port assembly routines to MSVC and #ifdef.
 +	* src/x86/ffi.c: Tweak function declaration and remove excess
 +	parens.
@@ -918,3 +924,22 @@ Index: libffi/src/x86/win32.S
 +
 +#endif /* !_MSC_VER */
 +
+Index: libffi/README
+===================================================================
+--- libffi.orig/README
++++ libffi/README
+@@ -109,6 +109,14 @@ will add some extra code which will supp
+ are using Purify with libffi. Only use this switch when using 
+ Purify, as it will slow down the library.
+ 
++It's also possible to build libffi on Windows platforms with
++Microsoft's Visual C++ compiler.  In this case, use the msvcc.sh
++wrapper script during configuration like so:
++
++path/to/configure --enable-shared --enable-static \
++	CC=path/to/msvcc.sh LD=link \
++	CPP=\"cl -nologo -EP\"
++
+ Configure has many other options. Use "configure --help" to see them all.
+ 
+ Once configure has finished, type "make". Note that you must be using