Subject: Compiling Emacs trunk with MSVC

From: Eli Zaretskii <[email-masked]>
Date: Sat, 05 Nov 2011 13:19:28 +0200

See

  http://lists.gnu.org/archive/html/emacs-devel/2010-12/msg00213.html

and the rest of that thread for the initial discussion and patches by
Fabrice.

The essence of this bug report is that Emacs development trunk does
not compile with latest versions of MSVC.

What follows is discussion of some of the changes suggested by
Fabrice, including the description of how they were adapted to the
current trunk, and the diffs that will be actually committed shortly.

> +#ifdef _MSC_VER
> +  unsigned short redirect : 3;
> +#else
>    enum symbol_redirect redirect : 3;
> +#endif

I used a macro ENUM_BF, shamelessly stolen from GDB, to work around
this and other similar issues with enumerated types in bitfields.
This solution was suggested by the discussion in the above thread.

> --- ..\mirror\emacs-bzr\trunk\src/makefile.w32-in	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\src/makefile.w32-in	2010-12-13 21:48:10.000000000 +0100
> @@ -303,14 +303,14 @@
>  	$(MAKE) $(MFLAGS) TAGS-LISP-$(MAKETYPE)
> 
>  TAGS-gmake:
> -	../lib-src/$(BLD)/etags.exe --include=TAGS-LISP --include=../nt/TAGS \
> -	  --regex=@../nt/emacs-src.tags \
> -	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ0))
> -	../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
> -	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ1))
> -	../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
> -	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ2)) \
> -	  $(CURDIR)/*.h
> +# ../lib-src/$(BLD)/etags.exe --include=TAGS-LISP --include=../nt/TAGS \
> +#   --regex=@../nt/emacs-src.tags \
> +#   $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ0))
> +# ../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
> +#   $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ1))
> +# ../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
> +#   $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ2)) \
> +#   $(CURDIR)/*.h

This was fixed by hiding the use of $patsubst in nt/gmake.defs, so
only a build that uses GNU Make will see that.

> --- ..\mirror\emacs-bzr\trunk\src/s/ms-w32.h	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\src/s/ms-w32.h	2010-12-13 23:20:18.000000000 +0100
> @@ -208,6 +218,9 @@
>  #undef dup2
>  #define dup2    sys_dup2
>  #define fopen   sys_fopen
> +#if 1
> +#define fstat(a, b)   sys_fstat(a, b)
> +#endif
>  #define link    sys_link
>  #define mkdir   sys_mkdir
>  #undef mktemp
> @@ -221,9 +234,13 @@
>  #define rmdir   sys_rmdir
>  #define select  sys_select
>  #define sleep   sys_sleep
> +#if 1
> +#define stat(a, b)   sys_stat(a, b)
> +#endif
>  #define strerror sys_strerror
>  #undef unlink
>  #define unlink  sys_unlink
> +#define utime   sys_utime

These and other similar changes were added, but conditioned on
_MSC_VER, so as to avoid any possible adverse effects on MinGW builds,
since we are in a pretest.  (I don't see any adverse effects, but I'd
like to err on the safe side.)

> -      if (tmp && _access (tmp, D_OK) == 0)
> +      if (tmp && sys_access (tmp, D_OK) == 0)

Again, added only for MSVC, for the same reasons.

> +#define stringer( x ) ( #x )
> +
>  char *
>  get_emacs_configuration_options (void)
>  {
> @@ -1900,10 +1905,10 @@
>      /* configure.bat already sets USER_CFLAGS and USER_LDFLAGS
>         with a starting space to save work here.  */
>  #ifdef USER_CFLAGS
> -    " --cflags", USER_CFLAGS,
> +    " --cflags", stringer(USER_CFLAGS),
>  #endif
>  #ifdef USER_LDFLAGS
> -    " --ldflags", USER_LDFLAGS,
> +    " --ldflags", stringer(USER_LDFLAGS),
>  #endif
>      NULL
>    };

I didn't do anything with this issue.  Fabrice wrote further down the
thread:

> > -    " --cflags", USER_CFLAGS,
> > > +    " --cflags", stringer(USER_CFLAGS),
> >
> > Why did you need to stringify here?  USER_CFLAGS is supposed to be
> > defined to a quoted string, like " -DFOO".  Can you tell why this
> > didn't work for you?
> 
> 
> Oops. I used this hack long ago, and I reintroduced it. But my real problem
> is that user_cflags contains
> various -I<path> to find include files, and these paths have \ that are not
> doubled.
> Stringifying is a bad idea and doesn't solve the problem at all. Need to
> find a better solution.

One possibility of a "better solution" would be to use only forward
slashes in user_cflags, as nt/INSTALL already tells.  Fabrice, will
this work with MSVC?  We already use "-I../something", so I hope this
can be the solution.

> --- ..\mirror\emacs-bzr\trunk\lib-src/movemail.c	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\lib-src/movemail.c	2010-12-13 23:21:27.000000000 +0100
> @@ -164,6 +164,10 @@
>  /* Nonzero means this is name of a lock file to delete on fatal error.  */
>  char *delete_lockname;
> 
> +#ifdef _MSC_VER
> +typedef int ssize_t;
> +#endif

Moved to src/s/ms-w32.h, as Emacs uses ssize_t in other places now.

> --- ..\mirror\emacs-bzr\trunk\nt/configure.bat	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\nt/configure.bat	2010-12-13 16:10:24.000000000 +0100
> @@ -294,6 +294,18 @@
>  rem   Auto-detect compiler if not specified, and validate GCC if chosen.
> 
>  :checkcompiler
> +
> +rem set SDK environment
> +if (%noopt%) == (Y) (
> +   call "c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x86 /win7 /Debug
> +   set nodebug=N
> +)
> +
> +if (%nodebug%) == (Y) (
> +   call "c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x86 /win7 /Release
> +   set noopt=N
> +)
> +

I don't see a clean and safe way of incorporating this into
configure.bat.  Instead, I added some text to nt/INSTALL telling
people to run SetEnv.cmd as appropriate, with the above 2 lines as
examples.  I hope this is sufficient; we already had a similar advice
there for Visual Studio .NET.

> --- ..\mirror\emacs-bzr\trunk\nt/makefile.w32-in	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\nt/makefile.w32-in	2010-12-13 21:43:45.000000000 +0100
> @@ -36,6 +36,7 @@
> 
>  .PHONY: $(ALL)
> 
> +ARCH_CFLAGS	= $(ARCH_CFLAGS) -I../nt/inc -I../src

I don't understand why is this necessary, especially since nmake.defs
already adds these flags to CFLAGS.  So I didn't add these flags.

>  frc:
>  TAGS-gmake: frc
> -	../lib-src/$(BLD)/etags $(CURDIR)/*.c
> -	$(MAKE) $(MFLAGS) -C ../src TAGS TAGS-LISP
> -	$(MAKE) $(MFLAGS) -C ../lib-src TAGS
> +# ../lib-src/$(BLD)/etags $(CURDIR)/*.c
> +# $(MAKE) $(MFLAGS) -C ../src TAGS TAGS-LISP
> +# $(MAKE) $(MFLAGS) -C ../lib-src TAGS

I don't see any problem here, so I didn't make this change.  If there
is a real problem, please explain what it is.

> --- ..\mirror\emacs-bzr\trunk\nt/nmake.defs	2010-11-18 08:53:04.000000000 +0100
> +++ emacs-gnu\nt/nmake.defs	2010-12-13 16:13:22.000000000 +0100
> @@ -110,7 +110,13 @@
>  RC_OUT		= -Fo
>  RC_INCLUDE	= -i
> 
> -libc		= libc.lib
> +!ifdef USE_CRT_DLL
> +libc		= msvcrt$(D).lib
> +XCFLAGS		= -I../nt/inc -I../src -D_DLL -D_MT -DUSE_CRT_DLL=1
> +!else
> +libc		= libcmt$(D).lib
> +XCFLAGS		= -I../nt/inc -I../src -D_MT
> +!endif
>  baselibs	=

I modified EMACS_EXTRA_C_FLAGS accordingly, instead of introducing
XCFLAGS.

> -SYS_LDFLAGS	= -nologo -release -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
> +!ifdef NOOPT
> +SYS_LDFLAGS	= -nologo -manifest -dynamicbase:no -debug -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
> +!else
> +SYS_LDFLAGS	= -nologo -manifest -dynamicbase:no -release -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
> +!endif

I made these changes, but left the old SYS_LDFLAGS as a comment, in
case older versions of MSVC don't grok the new switches.

> -ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Od -G3d -Zp8 $(DEBUG_FLAG)
> +ARCH_CFLAGS     = -nologo -D_X86_=1 -c -Zl -Zp8 -W2 -Od -Gd $(DEBUG_FLAG)
>  !else
> -ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Oxsb2 -Oy- -G6dF -Zp8 $(DEBUG_FLAG)
> +ARCH_CFLAGS     = -nologo -D_X86_=1 -c -Zl -Zp8 -W2 -Oi -Ot -Oy- -Ob2 -GF -Gy -Gd $(DEBUG_FLAG)

Likewise.

> Also, note that to copile the whole thing, I need to run :
> 
> nmake USE_CRT_DLL=1

I modified nmake.defs to define USE_CRT_DLL=1 unconditionally, I hope
this will take care of that.

The actual diffs I'm about to commit are below.  Fabrice, I'd
appreciate if you could check out the latest trunk, after these are
committed, and test it.  I attach the diffs below in case you would
want to try this on the version on which you worked a year ago
(because a lot has been changed in Emacs since then, and maybe the new
stuff needs further patches).

Thanks in advance.

------------------------------------------------------------

=== modified file 'lib/makefile.w32-in'
--- lib/makefile.w32-in	2011-07-24 22:15:47 +0000
+++ lib/makefile.w32-in	2011-11-05 10:02:34 +0000
@@ -50,7 +50,9 @@ all:		stamp_BLD $(ALL)
 
 ### TAGS ###
 
-TAGS:
+FRC:
+
+TAGS: FRC
 	 ../lib-src/$(BLD)/etags.exe *.c *.h
 
 ### DEPENDENCIES ###

=== modified file 'nt/INSTALL'
--- nt/INSTALL	2011-10-25 02:33:24 +0000
+++ nt/INSTALL	2011-11-05 09:30:33 +0000
@@ -21,19 +21,32 @@
 
        cd nt
 
-  2. Run configure.bat.  From the COMMAND.COM/CMD.EXE command prompt:
+  2. Run configure.bat.
+
+  2a.If you use MSVC, set up the build environment by running the
+     SetEnv.cmd batch file from the appropriate SDK directory.  (Skip
+     this step if you are using MinGW.)  For example:
+
+       "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x86 /Debug
+
+      if you are goiung to compile a debug version, or
+
+       "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x86 /Release
+
+      if you are going to compile an optimized version.
+
+  2b.From the COMMAND.COM/CMD.EXE command prompt type:
 
        configure
 
-     from a Unixy shell prompt:
+     From a Unixy shell prompt:
 
        cmd /c configure.bat
      or
        command.com /c configure.bat
 
   3. Run the Make utility suitable for your environment.  If you build
-     with the Microsoft's Visual C compiler (but see notes about using
-     VC++ 8.0 and later below):
+     with the Microsoft's Visual C compiler:
 
        nmake
 
@@ -101,24 +114,21 @@
 * Supported development environments
 
   To compile Emacs, you will need either Microsoft Visual C++ 2.0, or
-  later up to 7.0, and nmake, or a Windows port of GCC 2.95 or later
-  with MinGW and W32 API support and a port of GNU Make.  You can use
-  the Cygwin ports of GCC, but Emacs requires the MinGW headers and
-  libraries to build (latest versions of the Cygwin toolkit, at least
-  since v1.3.3, include the MinGW headers and libraries as an integral
-  part).
-
-  Note that building Emacs with Visual Studio 2005 (VC++ 8.0) and
-  later is not supported at this time, due to changes introduced by
-  Microsoft into the libraries shipped with the compiler.
+  later and nmake, or a Windows port of GCC 2.95 or later with MinGW
+  and W32 API support and a port of GNU Make.  You can use the Cygwin
+  ports of GCC, but Emacs requires the MinGW headers and libraries to
+  build (latest versions of the Cygwin toolkit, at least since v1.3.3,
+  include the MinGW headers and libraries as an integral part).
 
   The rest of this file assumes you have a working development
-  environment.  If you just installed  such an environment, try
+  environment.  If you just installed such an environment, try
   building a trivial C "Hello world" program, and see if it works.  If
   it doesn't work, resolve that problem first!  If you use Microsoft
   Visual Studio .NET 2003, don't forget to run the VCVARS32.BAT batch
   file from the `Bin' subdirectory of the directory where you have
-  installed VS.NET.
+  installed VS.NET.  With other versions of MSVC, run the SetEnv.cmd
+  batch file from the `Bin' subdirectory of the directory where you
+  have the SDK installed.
 
   If you use the MinGW port of GCC and GNU Make to build Emacs, there
   are some compatibility issues wrt Make and the shell that is run by

=== modified file 'nt/gmake.defs'
--- nt/gmake.defs	2011-05-07 04:00:12 +0000
+++ nt/gmake.defs	2011-11-05 08:54:22 +0000
@@ -193,6 +193,11 @@ OLE32		= -lole32
 UNISCRIBE	= -lusp10
 UUID		= -luuid
 
+# Used by src/makefile.w32-in, since Nmake barfs on $(func SOMETHING)
+OBJ0_c		= $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ0))
+OBJ1_c		= $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ1))
+OBJ2_c		= $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ2))
+
 ifdef NOOPT
 DEBUG_CFLAGS	= -DEMACSDEBUG
 else

=== modified file 'nt/makefile.w32-in'
--- nt/makefile.w32-in	2011-10-31 02:25:01 +0000
+++ nt/makefile.w32-in	2011-11-05 09:43:08 +0000
@@ -313,15 +313,15 @@ clean-other-dirs-nmake:
 	$(MAKE) $(MFLAGS) clean
 	cd ..\doc\lispintro
 	$(MAKE) $(MFLAGS) clean
-	cd ..\doc\lispref
+	cd ..\lispref
 	$(MAKE) $(MFLAGS) clean
-	cd ..\leim
+	cd ..\..\leim
 	$(MAKE) $(MFLAGS) clean
 	cd ..\doc\emacs
 	$(MAKE) $(MFLAGS) clean
-	cd ..\doc\misc
+	cd ..\misc
 	$(MAKE) $(MFLAGS) clean
-	cd ..\nt
+	cd ..\..\nt
 
 clean-other-dirs-gmake:
 	$(MAKE) $(MFLAGS) $(XMFLAGS) -C ../lib clean
@@ -381,13 +381,13 @@ distclean-other-dirs-nmake:
 	$(MAKE) $(MFLAGS) distclean
 	cd ..\doc\emacs
 	$(MAKE) $(MFLAGS) distclean
-	cd ..\doc\misc
+	cd ..\misc
 	$(MAKE) $(MFLAGS) distclean
-	cd ..\doc\lispintro
+	cd ..\lispintro
 	$(MAKE) $(MFLAGS) distclean
-	cd ..\doc\lispref
+	cd ..\lispref
 	$(MAKE) $(MFLAGS) distclean
-	cd ..\nt
+	cd ..\..\nt
 
 distclean-other-dirs-gmake:
 	$(MAKE) $(MFLAGS) $(XMFLAGS) -C ../lib distclean
@@ -415,13 +415,13 @@ maintainer-clean-other-dirs-nmake:
 	$(MAKE) $(MFLAGS) maintainer-clean
 	cd ..\doc\emacs
 	$(MAKE) $(MFLAGS) maintainer-clean
-	cd ..\doc\misc
+	cd ..\misc
 	$(MAKE) $(MFLAGS) maintainer-clean
-	cd ..\doc\lispintro
+	cd ..\lispintro
 	$(MAKE) $(MFLAGS) maintainer-clean
-	cd ..\doc\lispref
+	cd ..\lispref
 	$(MAKE) $(MFLAGS) maintainer-clean
-	cd ..\nt
+	cd ..\..\nt
 
 maintainer-clean-other-dirs-gmake:
 	$(MAKE) $(MFLAGS) $(XMFLAGS) -C ../lib maintainer-clean

=== modified file 'nt/nmake.defs'
--- nt/nmake.defs	2011-05-07 04:00:12 +0000
+++ nt/nmake.defs	2011-11-05 09:51:52 +0000
@@ -109,7 +109,15 @@ RC		= rc
 RC_OUT		= -Fo
 RC_INCLUDE	= -i
 
-libc		= libc.lib
+USE_CRT_DLL	= 1
+
+!ifdef USE_CRT_DLL
+libc		= msvcrt$(D).lib
+EMACS_EXTRA_C_FLAGS= -D_DLL -D_MT -DUSE_CRT_DLL=1
+!else
+libc		= libcmt$(D).lib
+EMACS_EXTRA_C_FLAGS= -D_MT
+!endif
 baselibs	=
 O		= obj
 A		= lib
@@ -146,9 +154,13 @@ CFLAGS          = -I. $(ARCH_CFLAGS) \
 		  $(DEBUG_CFLAGS) $(CHECKING_CFLAGS) $(USER_CFLAGS) $(LOCAL_FLAGS)
 ESC_CFLAGS      = -I. $(ARCH_CFLAGS) \
 		  $(DEBUG_CFLAGS) $(CHECKING_CFLAGS) $(ESC_USER_CFLAGS) $(LOCAL_FLAGS)
-EMACS_EXTRA_C_FLAGS =
 
-SYS_LDFLAGS	= -nologo -release -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
+#SYS_LDFLAGS	= -nologo -release -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
+!ifdef NOOPT
+SYS_LDFLAGS	= -nologo -manifest -dynamicbase:no -debug -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
+!else
+SYS_LDFLAGS	= -nologo -manifest -dynamicbase:no -release -incremental:no -version:3.10 -swaprun:cd -swaprun:net setargv.obj
+!endif
 
 # see comments in allocate_heap in w32heap.c before changing any of the
 # -stack, -heap, or -base settings.
@@ -184,16 +196,20 @@ DEL_TREE	= rm -r
 !ifdef NODEBUG
 DEBUG_FLAG =
 DEBUG_LINK =
+D =
 !else
 DEBUG_FLAG = -Zi
-DEBUG_LINK = -debug:full
+DEBUG_LINK = -debug
+D = d
 !endif
 
 !if "$(ARCH)" == "i386"
 !ifdef NOOPT
-ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Od -G3d -Zp8 $(DEBUG_FLAG)
+#ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Od -G3d -Zp8 $(DEBUG_FLAG)
+ARCH_CFLAGS     = -nologo -D_X86_=1 -c -Zl -Zp8 -W2 -Od -Gd $(DEBUG_FLAG)
 !else
-ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Oxsb2 -Oy- -G6dF -Zp8 $(DEBUG_FLAG)
+#ARCH_CFLAGS     = -nologo -c -Zel -W2 -H63 -Oxsb2 -Oy- -G6dF -Zp8 $(DEBUG_FLAG)
+ARCH_CFLAGS     = -nologo -D_X86_=1 -c -Zl -Zp8 -W2 -Oi -Ot -Oy- -Ob2 -GF -Gy -Gd $(DEBUG_FLAG)
 !endif
 ARCH_LDFLAGS	= $(SYS_LDFLAGS)

=== modified file 'src/lisp.h'
--- src/lisp.h	2011-10-28 13:48:19 +0000
+++ src/lisp.h	2011-11-05 10:16:18 +0000
@@ -168,6 +168,9 @@ extern int suppress_checking EXTERNALLY_
 #  if HAVE_ATTRIBUTE_ALIGNED
 #   define DECL_ALIGN(type, var) \
      type __attribute__ ((__aligned__ (1 << GCTYPEBITS))) var
+#  elif defined(_MSC_VER)
+#   define DECL_ALIGN(type, var) \
+     type __declspec(align(1 << GCTYPEBITS)) var
 #  else
      /* What directives do other compilers use?  */
 #  endif
@@ -225,6 +228,15 @@ extern int suppress_checking EXTERNALLY_
 # endif
 #endif
 
+/* Stolen from GDB.  The only known compiler that doesn't support
+   enums in bitfields is MSVC.  */
+#ifdef _MSC_VER
+#define ENUM_BF(TYPE) unsigned int
+#else
+#define ENUM_BF(TYPE) enum TYPE
+#endif
+
+
 enum Lisp_Type
   {
     /* Integer.  XINT (obj) is the integer value.  */
@@ -315,12 +327,12 @@ union Lisp_Object
 	/* Use explict signed, the signedness of a bit-field of type
 	   int is implementation defined.  */
 	signed EMACS_INT val  : VALBITS;
-	enum Lisp_Type type : GCTYPEBITS;
+	ENUM_BF (Lisp_Type) type : GCTYPEBITS;
       } s;
     struct
       {
 	EMACS_UINT val : VALBITS;
-	enum Lisp_Type type : GCTYPEBITS;
+	ENUM_BF (Lisp_Type) type : GCTYPEBITS;
       } u;
   }
 Lisp_Object;
@@ -336,14 +348,14 @@ union Lisp_Object
 
     struct
       {
-	enum Lisp_Type type : GCTYPEBITS;
+	ENUM_BF (Lisp_Type) type : GCTYPEBITS;
 	/* Use explict signed, the signedness of a bit-field of type
 	   int is implementation defined.  */
 	signed EMACS_INT val  : VALBITS;
       } s;
     struct
       {
-	enum Lisp_Type type : GCTYPEBITS;
+	ENUM_BF (Lisp_Type) type : GCTYPEBITS;
 	EMACS_UINT val : VALBITS;
       } u;
   }
@@ -1096,7 +1108,7 @@ struct Lisp_Symbol
      1 : it's a varalias, the value is really in the `alias' symbol.
      2 : it's a localized var, the value is in the `blv' object.
      3 : it's a forwarding variable, the value is in `forward'.  */
-  enum symbol_redirect redirect : 3;
+  ENUM_BF (symbol_redirect) redirect : 3;
 
   /* Non-zero means symbol is constant, i.e. changing its value
      should signal an error.  If the value is 3, then the var
@@ -1309,7 +1321,7 @@ struct Lisp_Hash_Table
 
 struct Lisp_Misc_Any		/* Supertype of all Misc types.  */
 {
-  enum Lisp_Misc_Type type : 16;		/* = Lisp_Misc_??? */
+  ENUM_BF (Lisp_Misc_Type) type : 16;		/* = Lisp_Misc_??? */
   unsigned gcmarkbit : 1;
   int spacer : 15;
   /* Make it as long as "Lisp_Free without padding".  */
@@ -1318,7 +1330,7 @@ struct Lisp_Misc_Any		/* Supertype of al
 
 struct Lisp_Marker
 {
-  enum Lisp_Misc_Type type : 16;		/* = Lisp_Misc_Marker */
+  ENUM_BF (Lisp_Misc_Type) type : 16;		/* = Lisp_Misc_Marker */
   unsigned gcmarkbit : 1;
   int spacer : 13;
   /* This flag is temporarily used in the functions
@@ -1468,7 +1480,7 @@ struct Lisp_Overlay
    I.e. 9words plus 2 bits, 3words of which are for external linked lists.
 */
   {
-    enum Lisp_Misc_Type type : 16;	/* = Lisp_Misc_Overlay */
+    ENUM_BF (Lisp_Misc_Type) type : 16;	/* = Lisp_Misc_Overlay */
     unsigned gcmarkbit : 1;
     int spacer : 15;
     struct Lisp_Overlay *next;
@@ -1487,7 +1499,7 @@ struct Lisp_Kboard_Objfwd
    This type of object is used in the arg to record_unwind_protect.  */
 struct Lisp_Save_Value
   {
-    enum Lisp_Misc_Type type : 16;	/* = Lisp_Misc_Save_Value */
+    ENUM_BF (Lisp_Misc_Type) type : 16;	/* = Lisp_Misc_Save_Value */
     unsigned gcmarkbit : 1;
     int spacer : 14;
     /* If DOGC is set, POINTER is the address of a memory
@@ -1501,7 +1513,7 @@ struct Lisp_Save_Value
 /* A miscellaneous object, when it's on the free list.  */
 struct Lisp_Free
   {
-    enum Lisp_Misc_Type type : 16;	/* = Lisp_Misc_Free */
+    ENUM_BF (Lisp_Misc_Type) type : 16;	/* = Lisp_Misc_Free */
     unsigned gcmarkbit : 1;
     int spacer : 15;
     union Lisp_Misc *chain;
@@ -1896,13 +1908,23 @@ typedef struct {
 
 /* This version of DEFUN declares a function prototype with the right
    arguments, so we can catch errors with maxargs at compile-time.  */
-#define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc) \
-  Lisp_Object fnname DEFUN_ARGS_ ## maxargs ;				\
-  static DECL_ALIGN (struct Lisp_Subr, sname) =				\
-    { PVEC_SUBR,							\
-      { .a ## maxargs = fnname },				\
-      minargs, maxargs, lname, intspec, 0};				\
-  Lisp_Object fnname
+#ifdef _MSC_VER
+#define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc)	\
+   Lisp_Object fnname DEFUN_ARGS_ ## maxargs ;				\
+   static DECL_ALIGN (struct Lisp_Subr, sname) =			\
+     { PVEC_SUBR | (sizeof (struct Lisp_Subr) / sizeof (EMACS_INT)),	\
+      { (Lisp_Object (__cdecl *)(void))fnname },                        \
+       minargs, maxargs, lname, intspec, 0};				\
+   Lisp_Object fnname
+#else  /* not _MSC_VER */
+#define DEFUN(lname, fnname, sname, minargs, maxargs, intspec, doc)	\
+   Lisp_Object fnname DEFUN_ARGS_ ## maxargs ;				\
+   static DECL_ALIGN (struct Lisp_Subr, sname) =			\
+     { PVEC_SUBR,							\
+      { .a ## maxargs = fnname },					\
+       minargs, maxargs, lname, intspec, 0};				\
+   Lisp_Object fnname
+#endif
 
 /* Note that the weird token-substitution semantics of ANSI C makes
    this work for MANY and UNEVALLED.  */

=== modified file 'src/makefile.w32-in'
--- src/makefile.w32-in	2011-08-27 01:42:00 +0000
+++ src/makefile.w32-in	2011-11-05 08:55:15 +0000
@@ -348,11 +348,11 @@ TAGS-LISP: $(OBJ0) $(OBJ1) $(OBJ2)
 TAGS-gmake:
 	../lib-src/$(BLD)/etags.exe --include=TAGS-LISP --include=../nt/TAGS \
 	  --regex=@../nt/emacs-src.tags \
-	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ0))
+	  $(OBJ0_c)
 	../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
-	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ1))
+	  $(OBJ1_c)
 	../lib-src/$(BLD)/etags.exe -a --regex=@../nt/emacs-src.tags \
-	  $(patsubst $(BLD)%.$(O),$(CURDIR)%.c,$(OBJ2)) \
+	  $(OBJ2_c) \
 	  $(CURDIR)/*.h $(CURDIR)/m/intel386.h $(CURDIR)/s/ms-w32.h
 
 TAGS-nmake:

=== modified file 'src/regex.c'
--- src/regex.c	2011-09-09 01:06:52 +0000
+++ src/regex.c	2011-11-05 08:47:01 +0000
@@ -530,7 +530,11 @@ init_syntax_once (void)
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 
 /* Type of source-pattern and string chars.  */
+#ifdef _MSC_VER
+typedef unsigned char re_char;
+#else
 typedef const unsigned char re_char;
+#endif
 
 typedef char boolean;
 #define false 0

=== modified file 'src/s/ms-w32.h'
--- src/s/ms-w32.h	2011-07-07 01:32:56 +0000
+++ src/s/ms-w32.h	2011-11-05 09:21:05 +0000
@@ -86,6 +86,12 @@ along with GNU Emacs.  If not, see <http
 #define IS_ANY_SEP(_c_) (IS_DIRECTORY_SEP (_c_) || IS_DEVICE_SEP (_c_))
 
 #include <sys/types.h>
+
+#ifdef _MSC_VER
+typedef unsigned long sigset_t;
+typedef int ssize_t;
+#endif
+
 struct sigaction {
   int sa_flags;
   void (*sa_handler)(int);
@@ -181,6 +187,12 @@ struct sigaction {
 
 #ifdef emacs
 
+#ifdef _MSC_VER
+#include <sys/timeb.h>
+#include <sys/stat.h>
+#include <signal.h>
+#endif
+
 /* Calls that are emulated or shadowed.  */
 #undef access
 #define access  sys_access
@@ -270,6 +282,15 @@ typedef int pid_t;
 #define utime	  _utime
 #endif
 
+#ifdef _MSC_VER
+/* MSVC gets link-time errors without these redirections.  */
+#define fstat(a, b) sys_fstat(a, b)
+#define stat(a, b)  sys_stat(a, b)
+#if _MSC_VER >= 1400
+#define utime       sys_utime
+#endif
+#endif
+
 /* This is hacky, but is necessary to avoid warnings about macro
    redefinitions using the SDK compilers.  */
 #ifndef __STDC__
@@ -317,13 +338,17 @@ extern char *get_emacs_configuration_opt
 #define _WINSOCK_H
 
 /* Defines size_t and alloca ().  */
-#ifdef USE_CRT_DLL
+#if (defined(_MSC_VER) && defined(emacs)) || defined(USE_CRT_DLL)
 #define malloc e_malloc
 #define free   e_free
 #define realloc e_realloc
 #define calloc e_calloc
 #endif
+#ifdef _MSC_VER
+#define alloca _alloca
+#else
 #include <malloc.h>
+#endif
 
 #include <sys/stat.h>

=== modified file 'src/w32.c'
--- src/w32.c	2011-10-27 00:59:21 +0000
+++ src/w32.c	2011-11-05 09:18:42 +0000
@@ -94,7 +94,9 @@ typedef struct _MEMORY_STATUS_EX {
 
 #include <tlhelp32.h>
 #include <psapi.h>
+#ifndef _MSC_VER
 #include <w32api.h>
+#endif
 #if !defined (__MINGW32__) || __W32API_MAJOR_VERSION < 3 || (__W32API_MAJOR_VERSION == 3 && __W32API_MINOR_VERSION < 15)
 /* This either is not in psapi.h or guarded by higher value of
    _WIN32_WINNT than what we use.  w32api supplied with MinGW 3.15
@@ -1547,7 +1549,12 @@ init_environment (char ** argv)
 	 read-only filesystem, like CD-ROM or a write-protected floppy.
 	 The only way to be really sure is to actually create a file and
 	 see if it succeeds.  But I think that's too much to ask.  */
+#ifdef _MSC_VER
+      /* MSVC's _access crashes with D_OK.  */
+      if (tmp && sys_access (tmp, D_OK) == 0)
+#else
       if (tmp && _access (tmp, D_OK) == 0)
+#endif
 	{
 	  char * var = alloca (strlen (tmp) + 8);
 	  sprintf (var, "TMPDIR=%s", tmp);

=== modified file 'src/w32fns.c'
--- src/w32fns.c	2011-11-03 19:04:18 +0000
+++ src/w32fns.c	2011-11-05 09:19:45 +0000
@@ -140,8 +140,8 @@ struct MONITOR_INFO
     DWORD   dwFlags;
 };
 
-/* Reportedly, VS 6 does not have this in its headers.  */
-#if defined (_MSC_VER) && _MSC_VER < 1300
+/* Reportedly, MSVC does not have this in its headers.  */
+#ifdef _MSC_VER
 DECLARE_HANDLE(HMONITOR);
 #endif