Friday, March 30, 2007

More on Gtk2Hs on Mac Os X

It turns out that the problem with Gtk2Hs was not related at all to GMP or OS X frameworks. The issue is related to ticket #957 and libraries not in the default search path. The problem is fixed by passing the flags '-L/opt/local/lib -I/opt/local/include' to ghc when compiling a Haskell module that uses Gtk2Hs. The right thing to do, copied from the MacPorts book, is to edit your ghc driver (mine lives at /usr/local/bin/ghc) and add these flags there:

Contents of my /usr/local/bin/ghc file:
GHCBIN="/usr/local/lib/ghc-6.7.20070328/ghc-6.7.20070328";
TOPDIROPT="-B/usr/local/lib/ghc-6.7.20070328";
# Mini-driver for GHC
exec $GHCBIN $TOPDIROPT ${1+"$@"} -L/opt/local/lib -I/opt/local/include

Friday, March 09, 2007

If I had time

Projects I would work on if I had a bit more time.
  • An improved way to handle exceptions for Haskell I have half-thought. More on this soon
  • review hs-plugins and get it to work on the Mac
  • A Dashboard widget for the awesome Lambdabot
  • Read all those papers I want to read (sic)
  • Finally get the mutable engine in the Omega Gameboy emulator to work, after all the time I have invested already (should put a Darcs repo up). Then the fun begins: profile and optimize extensively
  • Blog more
  • Get a website for myself
  • Definitely... read GTD and start getting things off this list
Projects I will work on as soon as I get the time.
  • The ghc 6.7 branch of Shim: Integrating the debugger. I have already some code but I'm waiting for soon-to-come changes in the debugging primitives in the ghc-api
  • Send patches for the GHCi debugger... bugfixes, new bugs, new commands, experiments, etc.

Wednesday, March 07, 2007

Gtk2Hs on Mac Os

Omegagb-0005

It all started with the Omega Gameboy Emulator written in Haskell. I got interested by the optimization challenge, it's a cool project after all, even though Haskell is not the most appropriate choice of a language. As a matter of fact I've never been a low-level guy, a systems programmer, but this project had its allure. After having played with the Universal Machine VM this summer during the ICFP contest, I found that I could understand the code very quickly.

After a few days hacking on Omega (it turns out that of the two alternative implementations of the CPU, the fast one was half-coded only, so I wasted a lot of time trying to debug it until I found this out), I got the unstoppable desire of seeing the emulator in action. I had been trying to avoid this, because I knew that setting up Gtk2hs on my Mac Os X86 would be a nightmare, but I just couldn't let go.

So here go my findings. These apply to the current release of Gtk2Hs, that is 0.9.11. I installed gtk2 via MacPorts, and hand compiled Gtk2Hs.

First, home compiled versions of GHC won't work. Gtk2Hs itself will make and install fine, but the helloworld demo will die with a "bus error". GDB is pretty useless to debug the problem. But someone in #haskell told me that MacPorts GHC would work, though he didn't know why. I tested this and he was right. So I digged into the MacPort install script for GHC, and it seemingly has to do with treating GMP as a lib instead of as a framework.
So this is what I found. GHC has a section in the configure.ac file to look for a GMP framework and use that. MacPorts installs GMP as a lib, and since all MacPorts ports try and succeed in eating their own food, the GHC port includes a patch to teach the configure script to use their GMP lib, and ignore any framework.

I don't really understand how frameworks are supposed to work in Mac Os, so I don't know if this makes any sense at all, but now my home compiled GHC is able to compile Gtk2Hs and Haskell code using Gtk, and everything works. I guess it should be possible to manually apply the patch at the ghc port, but I haven't tested it; I did my changes by hand to my package.conf file.

UPDATE: The real fix is given in a later post