Skip to content

Installation: Windows (using MSYS2)

Zach Bjornson edited this page Jul 7, 2018 · 1 revision

Why?

MSYS is currently the recommended way to get Pango and the whole GTK stack, so you probably won't get more up-to-date libraries any other way. It is possible but difficult to compile Cairo on Windows, and the Installation: Windows wiki does have less steps but the files are at a fixed, older version

Part 1 - prepare node-gyp

Follow the instructions here - Installation: Windows#1-installing-node-gyp

Part 2 - install MSYS2 and required packages

  1. Get the latest 64 bit MSYS2 installer from here
  2. In the MSYS2 shell, install cairo and its dependencies with pacman:
pacman -S mingw64/mingw-w64-x86_64-cairo 

and the tools that will be needed to create .lib files for the Visual Studio linker:

pacman -S mingw64/mingw-w64-x86_64-binutils mingw64/mingw-w64-x86_64-tools
  1. Now you just need to generate .lib files for the DLLs you need to link to. To do that:
/mingw64_shell.bat # gives you a MinGW64 shell

gendef /mingw64/bin/libcairo-2.dll
dlltool -d libcairo-2.def -l /mingw64/lib/libcairo-2.lib

gendef /mingw64/bin/libpng16-16.dll
dlltool -d libpng16-16.def -l /mingw64/lib/libpng16-16.lib

rm libcairo-2.def libpng16-16.def

(Full script below)

  1. Inside binding.gyp from node-canvas's root, the paths to the lib files are hard-coded on the Windows platform. You will have to edit it. Change GTK_ROOT to be C:/msys64/mingw64, and change png.lib to be libpng16-16.lib and cairo.lib to libcairo-2.lib and then run node-gyp rebuild.

  2. The final step just involves copying all of the DLLs in /mingw64/bin into Canvas's build/Release folder. There are a lot of them, around 20MB worth. You could use Dependency Walker to figure out which ones are really needed, or you can copy them all. After that you're all done!

JPEG, FreeType and Pango support

By hand-editing the binding.gyp you can get all of the features of node-canvas - full Pango, FreeType and JPEG support even with libjpeg-turbo for super fast compression. You just have to generate the .lib files as shown above and edit the GYP file's paths, also don't forget to set the with_<lib> to true at the very top.

Full script to generate lib files:

Node script (just type node, and paste it in REPL):

var dir='c:/msys64/mingw64'
var printdir='/c/msys64/mingw64'
function findandmakelibscommands(libnames)
{
 var libs=libnames.split("|")
 var re=new RegExp( "^(lib|)("+libs.join('|')+")[\\d\\b\\-\\.]+dll$" );
 var dlls=require('fs').readdirSync(dir+'/bin').filter(function(a){ return a.match(re)}) 
 var lines=dlls.map(function(file){

 var dllfile=printdir+'/bin/'+file;
 var libfile=printdir+'/lib/'+file.replace(/dll$/,'lib');
 var deffile=file.replace(/dll$/,'def');

 return 'gendef '+dllfile+'\r\n'+
        'dlltool -d '+deffile+' -l '+libfile+' \r\n'+
        'rm '+deffile+'\r\n'

}).join('\r\n')

var newlibs=dlls.map(function(file){
 return "            '-l<(GTK_Root)/lib/"+file.replace(/dll$/,'lib')+"'";
}).join(',\r\n')

console.log('\r\n'+lines+'\r\n\r\n new lib lines: \r\n'+newlibs+'\r\n\r\n')

}
findandmakelibscommands("cairo|png|pangocairo|pango|freetype|glib|gobject") // canvas 
findandmakelibscommands("rsvg|gio|gdk_pixbuf|intl") // rsvg
 

the result is:

open C:\msys64\mingw64.exe and paste it

> findandmakelibscommands("cairo|png|pangocairo|pango|freetype|glib|gobject") // canvas

gendef /c/msys64/mingw64/bin/libcairo-2.dll
dlltool -d libcairo-2.def -l /c/msys64/mingw64/lib/libcairo-2.lib
rm libcairo-2.def

gendef /c/msys64/mingw64/bin/libfreetype-6.dll
dlltool -d libfreetype-6.def -l /c/msys64/mingw64/lib/libfreetype-6.lib
rm libfreetype-6.def

gendef /c/msys64/mingw64/bin/libglib-2.0-0.dll
dlltool -d libglib-2.0-0.def -l /c/msys64/mingw64/lib/libglib-2.0-0.lib
rm libglib-2.0-0.def

gendef /c/msys64/mingw64/bin/libgobject-2.0-0.dll
dlltool -d libgobject-2.0-0.def -l /c/msys64/mingw64/lib/libgobject-2.0-0.lib
rm libgobject-2.0-0.def

gendef /c/msys64/mingw64/bin/libpango-1.0-0.dll
dlltool -d libpango-1.0-0.def -l /c/msys64/mingw64/lib/libpango-1.0-0.lib
rm libpango-1.0-0.def

gendef /c/msys64/mingw64/bin/libpangocairo-1.0-0.dll
dlltool -d libpangocairo-1.0-0.def -l /c/msys64/mingw64/lib/libpangocairo-1.0-0.lib
rm libpangocairo-1.0-0.def

gendef /c/msys64/mingw64/bin/libpng16-16.dll
dlltool -d libpng16-16.def -l /c/msys64/mingw64/lib/libpng16-16.lib
rm libpng16-16.def


 new lib lines:
            '-l<(GTK_Root)/lib/libcairo-2.lib',
            '-l<(GTK_Root)/lib/libfreetype-6.lib',
            '-l<(GTK_Root)/lib/libglib-2.0-0.lib',
            '-l<(GTK_Root)/lib/libgobject-2.0-0.lib',
            '-l<(GTK_Root)/lib/libpango-1.0-0.lib',
            '-l<(GTK_Root)/lib/libpangocairo-1.0-0.lib',
            '-l<(GTK_Root)/lib/libpng16-16.lib'



> findandmakelibscommands("rsvg|gio|gdk_pixbuf|intl") // rsvg

gendef /c/msys64/mingw64/bin/libgdk_pixbuf-2.0-0.dll
dlltool -d libgdk_pixbuf-2.0-0.def -l /c/msys64/mingw64/lib/libgdk_pixbuf-2.0-0.lib
rm libgdk_pixbuf-2.0-0.def

gendef /c/msys64/mingw64/bin/libgio-2.0-0.dll
dlltool -d libgio-2.0-0.def -l /c/msys64/mingw64/lib/libgio-2.0-0.lib
rm libgio-2.0-0.def

gendef /c/msys64/mingw64/bin/libintl-8.dll
dlltool -d libintl-8.def -l /c/msys64/mingw64/lib/libintl-8.lib
rm libintl-8.def

gendef /c/msys64/mingw64/bin/librsvg-2-2.dll
dlltool -d librsvg-2-2.def -l /c/msys64/mingw64/lib/librsvg-2-2.lib
rm librsvg-2-2.def


 new lib lines:
            '-l<(GTK_Root)/lib/libgdk_pixbuf-2.0-0.lib',
            '-l<(GTK_Root)/lib/libgio-2.0-0.lib',
            '-l<(GTK_Root)/lib/libintl-8.lib',
            '-l<(GTK_Root)/lib/librsvg-2-2.lib'