How to build Mercurial on Windows

This how-to is intended to give some hints about how to build Mercurial on Windows from the Mercurial sources. If you don't intend to fiddle with the sources, then simply use a pre-built binary package for Windows (for example [:TortoiseHg], or http://mercurial.berkwood.com/, see [:BinaryPackages]).

Mercurial is mostly programmed in Python (http://www.python.org/). The Python sources don't need a compilation step, but a few Mercurial modules are programmed in C: base85.c, bdiff.c, diffhelpers.c, mpatch.c and osutil.c (http://selenic.com/repo/hg/file/tip/mercurial/). These must be compiled with a C-compiler.

The steps below worked on a Windows XP SP2, that had a Microsoft Visual C++ installed.

Install Python 2.5, available from http://www.python.org/download/windows/

Default install path is C:\Python25. Best leave it at that.

Get the Mercurial sources (assuming you already have a working "hg" installed, see for example http://mercurial.berkwood.com/)

>cd C:\tmp\repos
>hg clone http://selenic.com/repo/hg mercurial

See [:DeveloperRepos] for other common repositories (the most other interesting one is the [:CrewRepository]).

Mercurial uses the "Python Distribution Utilities (Distutils)" install process, which also covers building of the extension modules (the files written in C-Code). See http://docs.python.org/inst/inst.html in the Python docs at http://docs.python.org/index.html.

The first step is doing a python setup.py build at the top level directory of the Mercurial package:

>cd mercurial
>python setup.py build
running build
running build_py
creating build
creating build\lib.win32-2.5
creating build\lib.win32-2.5\mercurial
copying mercurial\ancestor.py -> build\lib.win32-2.5\mercurial
copying mercurial\archival.py -> build\lib.win32-2.5\mercurial
copying mercurial\bundlerepo.py -> build\lib.win32-2.5\mercurial
...
building 'mercurial.mpatch' extension
creating build\temp.win32-2.5
creating build\temp.win32-2.5\Release
creating build\temp.win32-2.5\Release\mercurial
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c /nologo /Ox /MD /W3 /GX /DNDEBUG -IC:\Python25\include -IC:\Python25\PC /Tcmercurial/mpatch.c /Fobuild\temp.win32-2.5\Release\mercurial/mpatch.obj mpatch.c
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python25\libs /LIBPATH:C:\Python25\PCBuild /EXPORT:initmpatch build\temp.win32-2.5\Release\mercurial/mpatch.obj /OUT:build\lib.win32-2.5\mercurial\mpatch.pyd /IMPLIB:build\temp.win32-2.5\Release\mercurial\mpatch.lib
   Creating library build\temp.win32-2.5\Release\mercurial\mpatch.lib and object build\temp.win32-2.5\Release\mercurial\mpatch.exp
...
running build_scripts
creating build\scripts-2.5
copying and adjusting hg -> build\scripts-2.5

In the above example, Python found and used an installed Microsoft C compiler. The C-source files are compiled and linked into windows dll files using the file extension ".pyd". See also "[http://docs.python.org/ext/ext.html Extending and Embedding the Python Interpreter]" and the more specific "[http://docs.python.org/ext/building-on-windows.html Building C and C++ Extensions on Windows]" in the Python docs.

1. Global install

Execute python setup.py install

> python setup.py install
running install
running build
running build_py
running build_ext
running build_scripts
running install_lib
copying build\lib.win32-2.5\hgext\acl.py -> C:\Python25\Lib\site-packages\hgext
...
copying build\lib.win32-2.5\mercurial\ancestor.py -> C:\Python25\Lib\site-packages\mercurial
...
byte-compiling C:\Python25\Lib\site-packages\hgext\acl.py to acl.pyc
...
byte-compiling C:\Python25\Lib\site-packages\mercurial\ancestor.py to ancestor.pyc
...
running install_scripts
copying build\scripts-2.5\hg -> C:\Python25\Scripts
running install_data
creating C:\Python25\Lib\site-packages\mercurial\templates
copying templates\changelog.tmpl -> C:\Python25\Lib\site-packages\mercurial\templates
copying templates\changelogentry.tmpl -> C:\Python25\Lib\site-packages\mercurial\templates
...
running install_egg_info
Writing C:\Python25\Lib\site-packages\mercurial-fb259a3572e9-py2.5.egg-info

This installs hg in C:\Python25\Scripts:

> python C:\Python25\Scripts\hg version
Mercurial Distributed SCM (version fb259a3572e9)

Copyright (C) 2005-2008 Matt Mackall <mpm@selenic.com> and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. Local install for testing/development

If you want to have a locally ready to run Mercurial (for example for testing your patches, etc.), you can use the following batch file (named it "build-win32-2.5.cmd"):

del mercurial\*.pyd
rd /S /Q build
python setup.py build
copy build\lib.win32-2.5\mercurial\*.pyd mercurial

Run it from the working directory of your cloned repo. This will copy the compiled C modules (the *.pyd files) into the mercurial directory, giving you a ready to run local hg command.


CategoryContributing CategoryWindows CategoryHowTo