Thursday, June 02, 2011

Building Cairo in Windows

Recently, for reasons best left aside for now, I tried (and succeeded) in building the Cairo graphics library in Windows. This is a bit more complicated than it sounds (although not as bad as I expected), so I thought I would document it in case someone else wants to do it too. For the record, I am running Windows 7 (32 bit), I used a combination of Visual Studio 2010 and GCC. You'll need Git installed. I use %zlib% to mean the (top) directory where zlib is installed on my computer, and similarly for the other libraries.

Step 1 - download

You will need the following:

Cairo (git://anongit.freedesktop.org/git/cairo)
Pixman (git://anongit.freedesktop.org/git/pixman.git)
LibPNG (http://www.libpng.org/pub/png/libpng.html)
Mozilla Build Environment (http://developer.mozilla.org/en/docs/Windows_Build_Prerequisites#MozillaBuild)
ZLib (http://zlib.net/) (sort of optional)

You will need to clone the first two Git repositories and download and extract the source zip files for the latter two (or three).


Step 2 - ZLib

You can build ZLib if you like. I built it, but got linking errors later on, instead I used the ZLib static library which builds as part of LibPNG (more later). If you want to do it, it's straightforward:

First you need to build the assembly files, run bld_ml32.bat found in %zlib%\contrib\masmx86. You will need to do this from the Visual Studio command prompt, not the usual windows one.

Open %zlib%\contrib\vstudio\vc10\zlibvc.sln in Visual Studio.

Change the configuration to 'Release' and build it. That should be it.


Step 3 - build LibPNG

Open %libpng%\projects\vstudio\zlib.props in a text editor and change the value of '' to %zlib%. (I used the zlib library which I downloaded, there is a ZLib library included with LibPNG and you could probably use this here, somehow). Save and close the file.

Open %libpng%\projects\vstudio\vstudio.sln in Visual Studio. Change the configuration to 'Release Library' and build it.


Step 4 - build Pixman

I couldn't figure out how to build Pixman or Cairo in Visual Studio, I got close with Eclipse, but couldn't quite figure it out using the Eclipse build system. In the end, I resorted to automating the following method using a batch file and a shell script (see below) and launching that when building from Eclipse. I expect I can do the same thing for Cairo, but haven't yet.

You might need to create pixman-version.h from pixman-version.h.in and configure.ac, I did this manually, but it might get done automatically.

Open %mozbuildenv%/start-msvc10.bat in a text editor and add the following lines:
set LIB="%LIB%;%zlib%\contrib\vstudio\vc10\x86\ZlibStatRelease;%libpng%\projects\vstudio\Release Library;%pixman%\pixman\release"
set INCLUDE="%INCLUDE%;%zlib%;%libpng%;%pixman%\pixman;%cairo%\src;%cairo%\boilerplate"

Note that '%LIB%' and '%INCLUDE%' are literal strings here.

Save the file and close it.

From the command line (Windows or Visual Studio), run %mozbuildenv%/start-msvc10.bat, cd to %pixman%\pixman. Run "make -f Makefile.win32 CFG=release OUT_PATH=%outpath%", where %out path% is the path where you want to output the compiled Pixman library.

That's all you need to do a manual build. To automate it from Eclipse, you will need to create the following batch and shell scripts and run the batch file as the build command in Eclipse. You will also need to set the MINGW_HOME variable in the Eclipse project properties.

build_pixman.bat:

@echo off
del %pixman%\pixman\release /Q
%mozbuildenv%\start-msvc10.bat %pixman%/build.sh

build_pixman.sh:

#!/bin/sh
cd %pixman%/pixman
make -f makefile.win32 CFG=release

Note that the %paths will need to be in Windows format in the first two cases, and *nix format in the latter two. You could do the same for Cairo.


Step 5 - Build Cairo

I needed to apply a couple of patches before I could build Cairo. If you get a more recent version these patches may already have been applied, or be unnecessary. The patches are at http://lists.cairographics.org/archives/cairo/2011-May/021927.html and http://lists.cairographics.org/archives/cairo/2011-June/021985.html. Apply these using Git.

Open %cairo%/build/Makefile.win32.common in a text editor and find the reference to 'zdll.lib' and change it to 'zlib.lib'. If you do not have the zlib, libpng, pixman, and cairo directories nested in the same directory, you will need to change the paths in this file accordingly. Save and close the file.

Copy and rename %libpng%\projects\vstudio\Release Library\libpng15.lib to %libpng%\libpng.lib. Copy %libpng%\projects\vstudio\Release Library\zlib.lib to %zlib%. Alternatively, you could change the paths in Makefile.win32.common, in fact this is a better way of doing it, if you plan to re-build libpng or zlib. Note that I am using the version zlib.lib built by libpng, I could not get Cairo to build using the version built by zlib - I got a bunch of errors linking libpng.

From the command line (Windows or Visual Studio), run %mozbuildenv%/start-msvc10.bat, cd to %cairo%. Run "make -f Makefile.win32 CFG=release OUT_PATH=%outpath%", where %out path% is the path where you want to output the compiled Pixman library. Note that you need to run the makefile in the cairo root, not the src directory.

2 comments:

Anonymous said...

Would you be so kind to upload the windows binaries, including the headers and the .lib files that can be readily used for building any other project using cairo!

Ryan said...

Gone are the days when we needed to pick up the remote control to access the desired channels. Things have changed since then. When you use the new Google Chromecast streaming stick to watch TV, based on its knowledge, it tries to find out the content that matches you, or you may want to watch.