File: MACOSX

package info (click to toggle)
haskell-sdl-ttf 0.6.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 240 kB
  • sloc: haskell: 301; ansic: 49; makefile: 3
file content (72 lines) | stat: -rw-r--r-- 4,373 bytes parent folder | download | duplicates (24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 Core/Examples/MacOSX, I've included the Core/Examples/Test.hs example and a small Cabal definition to build it.  Our actual 'main' is Test.sdl_main.  We provide a Main.main, 'foreign export'ed as 'hs_main', which we will call from C.  (We indirect so that the file Main_stub.o need not be rebuilt every time we modify module Test.)  We provide our own 'SDL_main' in c_main.c, which initializes the Haskell runtime and calls 'hs_main', running Test.main in an environment with SDL available.  Finally, libSDLmain will find our C 'SDL_main' and call it, starting the whole process.

I recommend modifying this example when starting new projects.

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