hsSDL on Mac OS X as of February 26, 2006.
------------------------------------------

Developing software with SDL and hsSDL on Mac OS X can be a bit painful.  There are at least three problems, two of which are essentially solved at this time.

Building hsSDL on Mac OS X
--------------------------

We need to give options to hsc2hs to play nicely with SDL, but there is a problem with hsc2hs options on Mac; dons explained it to me on #haskell but I can't remember the details.  (I think the --ld=gcc is the crucial option.)  I don't think there's anyway to give hsc2hs arguments on the cabal Setup.lhs command line, or in the .cabal file.  Here's one way to fix it:

Create your own hsc2hs wrapper that has the arguments.  Let's call it
myhsc2hs:

#!/bin/sh
echo "/usr/local/bin/hsc2hs -v --cc=gcc --ld=gcc --lflag="-L/sw/lib -lSDLmain -framework AppKit -framework SDL" $@"
/usr/local/bin/hsc2hs -v --cc=gcc --ld=gcc --lflag="-L/sw/lib" --lflag="-lSDLmain" --lflag="-framework" --lflag="AppKit" --lflag="-framework" --lflag="SDL" "$@"

Run cabal configure with your own hsc2hs:

$ runhaskell ./Setup.lhs configure --with-hsc2hs ~/bin/myhsc2hs
Configuring SDL-0.3.0...
configure: Using install prefix: /usr/local
configure: Using compiler: /usr/local/bin/ghc
configure: Compiler flavor: GHC
configure: Compiler version: 6.4.1
configure: Using package tool: /usr/local/bin/ghc-pkg
configure: No haddock found
configure: Using happy: /usr/local/bin/happy
configure: Using alex: /usr/local/bin/alex
configure: Using hsc2hs: /Users/nalexand/bin/myhsc2hs
configure: No c2hs found
configure: No cpphs found
configure: No greencard found
configure: Dependency base-any: using base-1.0

Run cabal build (verbosely to see if your hsc2hs is being used):

$ runhaskell ./Setup.lhs build -v
Preprocessing library SDL-0.2.0...
/Users/nalexand/bin/myhsc2hs -D__GLASGOW_HASKELL__=604 -I/usr/local/
lib/ghc-6.4.1/include -I/sw/include/SDL -o Graphics/UI/SDL/General.hs
Graphics/UI/SDL/General.hsc
/usr/local/bin/hsc2hs -v --cc=gcc --ld=gcc --lflag=-L/sw/lib -
lSDLmain -framework AppKit -framework SDL -D__GLASGOW_HASKELL__=604 -
I/usr/local/lib/ghc-6.4.1/include -I/sw/include/SDL -o Graphics/UI/
SDL/General.hs Graphics/UI/SDL/General.hsc
Executing: gcc -c -I/usr/local/lib/ghc-6.4.1/include -I/sw/include/
SDL Graphics/UI/SDL/General_hsc_make.c -o Graphics/UI/SDL/
General_hsc_make.o
...

Running programs built with hsSDL on Mac OS X
---------------------------------------------

All our troubles stem from SDL's startup procedure on Mac OS X.  The fundamental problem is that SDL uses Objective-C and Cocoa to open a UI window; this means that Cocoa must be initialized, and in particular, an NSAutoReleasePool be in place.  This initialization is done in libSDLmain.  For C/C++/Objective-C programs, libSDLmain #defines the developer's main to be SDL_main and piggy backs SDL_main onto it's own Cocoa main, using the C preprocessor and abusing the linker.  Of course, this technique will never work for Haskell: there is no clean entry point to a Haskell runtime in this fashion.

But we can fake it. In Examples/MacOSX, there is an example program using SDL in Main.hs. It doesn't need to be modified to make it work on OS X. Instead we put two helper files in the same directory and compile them along with it: MainWrapper.hs imports Main.hs and foreign-exports main as haskell_main. mainc.c includes SDL.h and contains a main function to make the preprocessor magic of SDL happen; it also initializes the GHC runtime system and calls haskell_main. Some makefile rules link our objects with the GHC RTS and SDL.

For your own projects, copy the two helper files mainc.c and MainWrapper.hs and copy/mimic the build rules from the example Makefile, optionally changing PROGNAME.

GHCi and hsSDL on Mac OS X
--------------------------

At the moment, hsSDL can be loaded in GHCi.  Unfortunately, no programs will run correctly.  The startup procedure described above has not been implemented in GHCi, although there is no deep problem (that I can see) preventing a customized GHCi that starts SDL first from being built.  I, for one, would truly appreciate an implementation.

Contact information
-------------------

All Mac OS X patches should be routed to Lemmih (lemmih@gmail.com).  Any Mac OS X specific questions/comments could be forwarded to me, Nick Alexander (ncalexan@uci.edu).

Best of luck,
Nick
