16 July 2013

The Polar Game in Haskell, Day 6/12: Towards a GUI, Continued

OK, so when I left off last time, I was running into a gruesome link error. I found this Stack Overflow thread and the first accepted answer fixed the problem. However, it seems that the answer may be to avoid MacPorts versions of the libraries I need. So I'm going to attempt to clean that all out and use Homebrew. So, first:

sudo port -fp uninstall --follow-dependents installed

And then I'm manually cleaning out some of the stuff mentioned in this article.

Next, I'm removing this from my .profile (hey, I'm so pleased that it is clearly marked!

# MacPorts Installer addition on 2013-07-16_at_11:57:13:
adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for 
use with MacPorts.

Now to install Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

I ran brew doctor and wow, I have a mountain of warnings. I got rid of most of them except for several about "unbrewed" things -- static libraries, .la files, and dylibs in /usr/local/lib. I get a warning about MacGPG2 but that seems to be fixed by upgrading to the current version. So now I'm trying cabal install --reinstall gtk, and I get:

Configuring gtk-0.12.4...
setup: The pkg-config package gthread-2.0 is required but it could not be
found.

And so, attempting to follow the directions here:

brew install glib cairo gtk gettext fontconfig

...and that actually crashes. I get "confest cannot be opened because of a problem." In the console log:

Process:         conftest [12844]
Path:            /private/tmp/*/conftest
Identifier:      conftest
Version:         0
Code Type:       X86-64 (Native)
Parent Process:  sh [12843]
User ID:         501

Date/Time:       2013-07-16 15:42:18.117 -0400
OS Version:      Mac OS X 10.8.4 (12E55)
Report Version:  10

Crashed Thread:  0

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000

Application Specific Information:
dyld: launch, loading dependent libraries

Dyld Error Message:
  Library not loaded: /usr/local/lib/libintl.8.dylib
  Referenced from: /private/tmp/*/conftest
  Reason: no suitable image found.  Did find:
 /usr/local/lib/libintl.8.dylib:
            no matching architecture in universal wrapper
 /usr/local/lib/libintl.8.dylib:
            no matching architecture in universal wrapper

And I get an error about "GLib requires a 64 bit type." I also had to do some manual clean-out of some files that had the wrong permissions and were interfering with installing pkgconfig. I found a number of people reporting this problem, but none of the solutions they outlined seemed to work for me. So... what else can I try?

There's this: http://www.haskell.org/haskellwiki/Gtk2Hs/Mac#GTK.2B_OS_X_Framework

OK! Deep sigh... let's try this!

Pauls-Mac-Pro:Downloads paul$ sh ./gtk-osx-build-setup.sh 
Checking out jhbuild (07b5a7d) from git...
Cloning into 'jhbuild'...
remote: Counting objects: 37027, done.
remote: Compressing objects: 100% (14715/14715), done.
remote: Total 37027 (delta 28610), reused 28612 (delta 22178)
Receiving objects: 100% (37027/37027), 7.27 MiB | 2.27 MiB/s, done.
Resolving deltas: 100% (28610/28610), done.
Switched to a new branch 'stable'
Patch is empty.  Was it split wrong?
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
Installing jhbuild...
gnome-autogen.sh not available
yelp-tools not available
Configuring jhbuild without autotools
Now type `make' to compile jhbuild
Installing jhbuild configuration...
Installing gtk-osx moduleset files...
PATH does not contain /Users/paul/.local/bin, it is recommended that you add that.

Done.

Ummm... OK, wow, that installed source in my home directory build tools in a hidden directory (prefaced with a period) under my home directory. There are warning notes about how the build process conflicts with MacPorts and fink. There's also a note that says "Note: jhbuild requires Python 2.5 to unpack tar files" (of course it does... that's the simplest and most system-compatible way to unpack tar files, right?) Ugh. Anyway... in ~/Source/jhbuild I type ~/.local/bin/jhbuild bootstrap and it builds about a bazillion things including cmake. (Talk amongst yourselves, this is going to take a while... time for another snack...)

That seemed to work. And so: ~/.local/bin/jhbuild build meta-gtk-osx-bootstrap and ~/.local/bin/jhbuild build meta-gtk-osx-core. Somewhat to my shock, everything succeeded! I tried to build gimp, but that failed with "we require Pango with the optional support for Cairo compiled in," and I don't want to go too far down that rabbit hole, so I gave up on that. So let's see if I can make that work with GHC. The next step is package-config. Which requires glib. Ummm, wait a minute... oh, crap. That's still broken with homebrew. Ummm. What about package-config from MacPorts, which the instructions for GTK OSX warned me about? Sure, let's try it, what the hell... after all, I've wasted nearly a full day already... so, sudo port selfupdate, sudo port install pkg-config... that seemed to work. So then we download the Gtk2HS tarball... ummm, the link from the instructions is broken. Ummm... from SourceForge here... but that version is looking much older than the one described here. I'm getting a bad feeling about this. But anyway... 0.10.1 it is! Configure away!

checking for pkg-config... /opt/local/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for GLIB... no
configure: error:

The development files for the glib-2.x library were not found.
Perhaps you need to install glib or glib-devel

Huh. Well. It's just about the end of my work day; I've got to go downstairs and help my wife get dinner ready. Ummm. So! I hope you've enjoyed this tutorial on how to use the GTK GUI library in Haskell! Please join me next time when I perform brain surgery on myself using a hacksaw, a folding mirror, and a bottle of Scotch!

4 comments:

Jeff Licquia said...

Hmm...

sudo apt-get install libghc-gtk-dev
cat > test-gtk.hs
ghc test-gtk.hs
./test-gtk

And there's a blank window on my screen.

Sorry... but it's not often that a Linux fanboy gets to wag his finger at Apple for not getting something right, so we have to enjoy our moments of glory when we find them. :-)

Seriously, a few options:

- Not Haskell-specific, but of the GUI toolkits I saw with Haskell stuff, something based on wxWidgets is likely your best bet. It will support native Mac OS X look-n-feel, and also port easily to Windows and Linux via GTK+. (And you can do GTK+ on Mac, but for some reason I don't suspect you're eager to try that option.)

- Qt would be a very close second. It's got a proven pedigree on Mac, too. The one drawback: things in the Qt world are in flux, which means that slightly older Qt code may require a little attention to get working with the latest shiny. wxWidgets is likely to have a slight edge in that department.

- After that, Tk has a very long reputation as a cross-platform GUI toolkit. The first GUI systems written for Python and Perl were based on Tk. That said, the link from the Haskell wiki to HTk seems dead, so maybe not.

- Something to consider: if you've got something like Parallels or VMWare or VirtualBox on your Mac, the path of least resistance might be to fire up an Ubuntu VM and do the tinkering in Linux. It probably could have saved you time today. OTOH, you could grow a big pain-point when it comes time to transfer your tinkerings back.

Matt Walton said...

It's not really a Haskell problem, the problem is GTK on a Mac.

May I suggest wxHaskell instead? It's based on wxWidgets which has an OS X backend, so should be a lot easier to install. There's also some interesting stuff going on around it, like the reactive-banana bindings for it that let you use FRP to wire up your Wx GUI's functionality.

Paul Potts said...

Thanks for reading, guys. I realize it turned out to be a pretty useless set of blog posts. I would have preferred if I'd been able to get something working and present a recipe for other people to follow rather than just a story of failure!

I do have a quad-core Xeon Ubuntu box sitting here. I work on various platforms including Windows (when I have to). The main thing that keeps me from firing it up is that we are having a heat wave and my office is in a half-attic. The idea of firing up another computer in here fills me with dread. I fried one mobo last summer even with an air conditioner in here and I'm not eager to fry another. But I'm glad to hear it is maintained well enough to work pretty effortlessly on Linux. I'm not sure just what the issue is with the Mac libraries but it seems like it is affecting more than just this. It certainly could be because I have tinkered with this system quite a bit and built a lot of things from scratch. But judging from various comments on message boards out there, I suspect it is not.

I will give wxWindows a shot and see how far I can get with that. I don't remember it fondly from attempting to write a cross-platform GUI a decade ago -- the code was pretty ugly then and I had to bail out and use Qt instead -- but most likely it has improved.

Paul Potts said...

Just FYI, wxHaskell is working for me, at least for the most basic Hello World app. So maybe I'll have some more significant progress to report soon!