Haskell (including GTK) on Mavericks

Posted on April 27, 2014
This post is out of date. See Comprehensive Haskell Sandboxes, Revisited instead.

Installing GTK

Although the GTK libraries can be installed through fink, macports, or brew, I’ve never had much luck with any of them. I recently tried installing them through brew and although they appeared to install ok, as soon as I tried to run a Haskell GTK application it segfaulted with a null pointer exception somewhere in libcairo. I therefore prefer to install from source. This is a fairly painless process; some hints are in my Comprehensive Haskell Sandboxes post. You can follow those instructions or just follow the instructions on the GTK+ website. The only important thing is that you need set set LDFLAGS so that it can find -lpython:

export LDFLAGS=-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config

Poppler

If additionally you need poppler (not necessary for gtk2hs itself, only necessary for the Haskell poppler bindings), you must first

jhbuild build fontconfig

(Not sure why that is necessary as a separate step; missing dependency?) Then when you install poppler (jhbuild build poppler), compilation will fail with an error about -fno-check-new; start a shell, edit ./configure, remove that flag, and build again.

Installing ghc

OSX Mavericks switched from gcc to clang. Versions of ghc older than 7.8 cannot be built with clang, and will even have problems after building because, embarrassingly, Haskell still relies on the C preprocessor (cpp) for compile time configuration and the version of cpp that comes with clang is not fully compatible with the version that comes with gcc. So whether you install from source or from a binary distribution, it is very important that you configure with

./configure --with-gcc=gcc-4.9

or whichever version of gcc you have installed.

WARNING: gcc can be installed through brew, but make sure to have the Xcode command line utilities installed before installing gcc.

Installing the Haskell GTK bindings

We need to patch the gtk package, but first we can install its dependencies. Run

cabal install gtk2hs-buildtools
cabal unpack gtk

and then inside the new gtk directory start a jhbuild shell and run

cabal install --only-dependencies --with-gcc=gcc-4.9

The --with-gcc flag is necessary because the gtk build tools call gcc directly (rather than through ghc, which is what they ought to do) and then run into the aforementioned problems with the cpp preprocessor bundled with clang. (Thankfully, clang is ABI compatible with gcc so we don’t have to worry about mixing object files generated by clang or by gcc.)

Unfortunately, there is a bug in the most recent gtk (gtk-0.12.5.*) which prevents it from building with the more recent versions of gtk+ in OSX. The gtk+ bindings export a function called drawableGetID; in the gtk bindings prior to version 0.12.5 was defined as

error "drawableGetID: not supported with a GTK using a Quartz backend"

In version 0.12.5.* this call to error is gone, but the code that is there does not seem to be compatible with recent versions of the GTK+ libraries. Rather than reintroducing the call to error, gtk-0.12.5.7-disable-drawableGetID.diff simply removes drawableGetID completely. (This cannot be done in the official library code because Haskell libraries cannot change their API based on compile time flags.) By removing it completely, any application that will try to build against our patched library will fail at compile time, rather than with some mysterious runtime failure. Apply the patch and install:

patch -p1 -i gtk-0.12.5.7-disable-drawableGetID.diff
cabal install --with-gcc=gcc-4.9 -f have-quartz-gtk

At this point you can exit the jhbuild shell and any future applications that link against gtk should be compilable without re-entering it.

(Incidentally, do not try to build gtk2hs-buildtools inside a jhbuild shell, as you will get an error when building vector, something to do with libiconv.dylib ; I’m not sure why.)

ThreadScope

You might want to try to install threadscope as a simple test if the GTK install worked ok; sadly, the version of threadscope on Hackage is somewhat outdated. Your best bet is to install from the official repos at https://github.com/haskell/ghc-events and https://github.com/haskell/ThreadScope.