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 Download). If you want to build an installer or Py2exe distribution, see BuildingWindowsInstaller.
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 (https://www.mercurial-scm.org/repo/hg/file/tip/mercurial/). These must be compiled with a C-compiler.
Contents
1. Common Steps
1.1. Installing Python
You will need to install Python to build Mercurial. Go to https://www.python.org/downloads/windows/ and download an appropriate MSI installer.
You almost certainly want the most recent Python 2.7 version. These instructions assume you are using version 2.7.
Installers come in x86 and x86-64 flavors (32 and 64 bit). It doesn't matter which version you use. But this will impact how the build is performed later.
Quick link: Python 2.7.14 x86-64 MSI installer.
Follow the instructions in the installer. The default install path is C:\python27. These instructions will assume this default is used.
1.2. Fetch Mercurial Source Code
You'll need a copy of the Mercurial source code to build it. If you have TortoiseHg or Mercurial installed already, simply clone the repository:
> hg clone https://www.mercurial-scm.org/repo/hg
Or, obtain a source archive from https://www.mercurial-scm.org/release/
2. Install a Compiler
Mercurial contains Python C extensions that drastically boost performance. While Mercurial can be built without C extensions, it isn't the full Mercurial experience, so installing a C compiler to build Mercurial is highly recommended.
There are a few options for compilers.
2.1. Microsoft Visual C++ Compiler for Python 2.7
If you want to use Microsoft's compiler, you will need to download and install Microsoft Visual C++ Compiler for Python 2.7.
This is required unless you have Visual Studio 2008 already installed.
You need to install a special compiler package because Python 2.7 itself is built with an old version of Visual Studio and Python C extensions need to be built with the same compiler Python was built with.
By default, the Microsoft Visual C++ Compiler for Python 2.7 installer will install things inside your user directory. This is a little odd for Windows installers. But it shouldn't be a problem. The aforementioned link contains install instructions for installing the package globally.
3. Start a Command Prompt
Mercurial can currently be built from a vanilla cmd.exe prompt, or from MSYS without any special actions. Prior to 718a57e95a89 (March 2017), these instructions may be useful for building without MSYS.
In the Windows Start Menu, navigate to Programs -> Microsoft Visual C++ Compiler Package for Python 2.7. On Windows 10, search for "Visual C++ 2008".
Launch either the Visual C++ 2008 32-bit Command Prompt or Visual C++ 2008 64-bit Command Prompt depending on whether you have the 32-bit or 64-bit version of Python installed.
You will need to export some variables to make Mercurial's build environment happy, per this StackOverflow question:
SET DISTUTILS_USE_SDK=1 SET MSSdk=1
4. Building in cmd.exe
Once you have a build environment activated, you should be able to build Mercurial!
All build steps are performed by executing setup.py with Python.
From a Visual Studio command prompt, this looks something like:
c:\Python27\python.exe setup.py
Assuming we're using the Visual C++ environment, here is what a build may look like:
> c:\Python27\python.exe setup.py build running build running build_mo warning: hgbuildmo: could not find msgfmt executable, no translations will be built running build_ext building 'mercurial.base85' extension creating build creating build\temp.win-amd64-2.7 creating build\temp.win-amd64-2.7\Release creating build\temp.win-amd64-2.7\Release\mercurial C:\Users\gps\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\x86_amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\dev\Python27\include -Ic:\dev\Python27\PC /Tcmercurial/base85.c /Fobuild\temp.win-amd64-2.7\Release\mercurial/base85.obj base85.c creating build\lib.win-amd64-2.7 creating build\lib.win-amd64-2.7\mercurial C:\Users\gps\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\x86_amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:c:\dev\Python27\libs /LIBPATH:c:\dev\Python27\PCbuild\amd64 /EXPORT:initbase85 build\temp.win-amd64-2.7\Release\mercurial/base85.obj /OUT:build\lib.win-amd64-2.7\mercurial\base85.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\mercurial\base85.lib /MANIFESTFILE:build\temp.win-amd64-2.7\Release\mercurial\base85.pyd.manifest base85.obj : warning LNK4197: export 'initbase85' specified multiple times; using first specification Creating library build\temp.win-amd64-2.7\Release\mercurial\base85.lib and object build\temp.win-amd64-2.7\Release\mercurial\base85.exp building 'mercurial.bdiff' extension C:\Users\gps\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\x86_amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\dev\Python27\include -Ic:\dev\Python27\PC /Tcmercurial/bdiff.c /Fobuild\temp.win-amd64-2.7\Release\mercurial/bdiff.obj bdiff.c C:\Users\gps\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\x86_amd64\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:c:\dev\Python27\libs /LIBPATH:c:\dev\Python27\PCbuild\amd64 /EXPORT:initbdiff build\temp.win-amd64-2.7\Release\mercurial/bdiff.obj /OUT:build\lib.win-amd64-2.7\mercurial\bdiff.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\mercurial\bdiff.lib /MANIFESTFILE:build\temp.win-amd64-2.7\Release\mercurial\bdiff.pyd.manifest bdiff.obj : warning LNK4197: export 'initbdiff' specified multiple times; using first specification Creating library build\temp.win-amd64-2.7\Release\mercurial\bdiff.lib and object build\temp.win-amd64-2.7\Release\mercurial\bdiff.exp ... running build_py copying mercurial\ancestor.py -> build\lib.win-amd64-2.7\mercurial copying mercurial\archival.py -> build\lib.win-amd64-2.7\mercurial copying mercurial\bookmarks.py -> build\lib.win-amd64-2.7\mercurial copying mercurial\branchmap.py -> build\lib.win-amd64-2.7\mercurial copying mercurial\bundle2.py -> build\lib.win-amd64-2.7\mercurial copying mercurial\bundlerepo.py -> build\lib.win-amd64-2.7\mercurial ... copying mercurial\templates\static\style-paper.css -> build\lib.win-amd64-2.7\mercurial\templates\static copying mercurial\templates\static\style.css -> build\lib.win-amd64-2.7\mercurial\templates\static running build_scripts creating build\scripts-2.7 copying and adjusting hg -> build\scripts-2.7 copying contrib\win32\hg.bat -> build\scripts-2.7
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". If the above step fails, you can find some additional tips in WindowsInstall. See also "Extending and Embedding the Python Interpreter" and the more specific "Building C and C++ Extensions on Windows" in the Python docs.
4.1. Local Builds
If you are hacking on the Mercurial source code, you'll want to perform a local, in-place build. In this build mode, files are installed directly in the source directory so you can run Mercurial directly from the source directory. The instructions in this section are essentially copied from the make local target from the Makefile (make is not always available on Windows).
> c:\Python27\python.exe setup.py build_py -c -d . build_ext -i build_hgexe -i build_mo
This is essentially 4 commands in one: build_py, build_ext, build_hgexe, build_mo.
build_py -c -d . says to build Python source files, producing .pyc files (-c) and building into the current directory (-d .).
build_exe -i says to build Python C extensions, compiling them in place (-i).
build_hgexe -i says to build the hg.exe wrapper program, compiling it in place (-i).
build_mo says to build some translation databases. (You can probably omit this for many developer workflows.) Also, the default Python install won't be able to build these files because it requires the msgfmt executable.
If the build completes successfully, there should be a hg.exe in the source directory and you should be able to run it:
> hg.exe --version Mercurial Distributed SCM (version 3.5+2-79f0cb97d753) (see http://mercurial.selenic.com for more information) Copyright (C) 2005-2015 Matt Mackall 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.
For developers, we recommend creating a .bat file with the above setup.py command so you can quickly build Mercurial.
4.2. Running Tests
Tests will not run under cmd.exe. MSYS/MinGW is required. WSL on Windows 10 does not currently run the tests.
5. Building in MSYS
An MSYS shell can build the application using the Microsoft C compiler with make, instead of a .bat file. Assuming python is in $PATH, run the following from the repository root.
make local
5.1. Running Tests
Install MinGW
- Install the required binaries:
C:\MinGW\bin\mingw-get.exe install msys-base msys-coreutils msys-diffutils msys-unzip msys-bash-lnk
If msys-bash-lnk doesn't create a shortcut to MinGW on the Desktop, you may want to either manually create a link to C:\MinGW\msys\1.0\msys.bat, or add it to %PATH% for easy access.
Adjust %TEMP% and %TMP% variables if it translates to a path with ~. C:\Temp works fine. (This happens with longer usernames- even when not shown in the environment variable editor, and several tests like test-subrepo-relative-path.t will fail with extra quoting.)
C:\Users\mharbison>echo %TEMP% C:\Users\MHARBI~1\AppData\Local\Temp
- MSYS can now be launched, and the tests run from the normal test directory:
$ cd /path/to/hg/tests $ make -C .. local >/dev/null $ python.exe run-tests.py --local -t900 running 851 tests using 8 parallel processes ....
This assumes that python2 is visible on $PATH. The binary for python3 is also python.exe, so replace python.exe with py -2 if python3 is in $PATH
Tests tend to take much longer on Windows. Be sure to use -t to increase the timeout.
- Python3 is not currently supported, but it can be tested as follows:
$ cd /path/to/hg/tests $ make clean -C .. >/dev/null $ make local -C .. PYTHON="py -3" >/dev/null $ py -3 run-tests.py --local -t900 running 850 tests using 12 parallel processes ...
Note that py -3 run-tests.py will actually silently test python2 if that's how hg.exe was built. However, py -2 run-tests.py will pop up error messages about hg.exe not being able to find python3X.dll if linked against python3. It is recommended to use the share extension to create dedicated working copies for each version being tested.
Previous details (such as they are) are listed in a mailing list thread.
6. Global Install
To install Mercurial into your global Python installation, use the install command from setup.py:
> c:\Python27\python.exe setup.py install
Then, you should be able to execute c:\Python27\Scripts\hg.exe.