2008-03-03

ocamlsdl on MacOS X 10.5 "Leopard"

I've found that there a number of pitfalls that make getting ocamlsdl up and running on Leopard a less than straightforward experience. For the record, here are a number of fixes I found I needed to apply. I'm reconstructing the process after the fact, so I apologize if anything is missing.

0. Prepare your environment

You'll need to set up some environment variables first. I'm a tcsh user, while the default in Leopard is bash; hence, you'll have to modify the environment yourself depending on your shell.

Include X11 in your executable path
libpng and freetype2 live inside /usr/X11. The standard way to pass linker options is to use libpng-config and freetype-config, these are in /usr/X11/bin. Add this to your binary path.
Fix broken Leopard linker
Set and export LDFLAGS with value
-dylib_file \
  /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:\
  /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib

(this is one long option, broken into lines for ease of reading.)

1. Get SDL to compile

SDL proper builds and installs without problems; however, the tests need manual intervention.

  1. Enter the test directory and ./configure.
  2. Then manually edit Makefile and append $(LDFLAGS) to CFLAGS (line 7).
  3. Finally make and run the tests. A good test to try is testwm.

2. Compile attendant libraries

For some strange reason, SDL_image won't find the built-in libpng. Build and install it, together with libtiff. Then, build and install SDL_image, SDL_ttf and SDL_mixer. Everything should work; if not, check that the variables above are correctly set in your session's environment.

3. Compile ocamlsdl

I assume you're using ocamlfind; if not, you'll probably not find trouble with the last step.

  1. Start by doing ./configure; it should say that it'll build SDL_image, SDL_ttf and SDL_mixer.
  2. Then, edit makefile.config.gcc so that lines 29 and 30 read -framework Cocoa instead of the original -Wl,-framework,Cocoa.
  3. Also, edit src/Makefile so that line 89 reads $(RANLIB) $$($(OCAMLFIND) query sdl)/*.$(A) instead of the original $(RANLIB) $$($(OCAMLFIND) printconf destdir)/*.$(A).
  4. Finaly make it.

If you've installed a previous version of ocamlsdl and you're using ocamlfind, you'll need to manually remove the installation directory (ocamlfind query sdl) prior to sudo make install.

3. Test ocamlsdl

I've found a great "hello world"-type of walk-through., but it needs some tweaking for MacOS X, as the ocamlsdl README explains. Use the following Makefile:

RESULT        = testsdl_1
SOURCES       = testsdl_1.ml
PACKS         = bigarray sdl
OCAMLBLDFLAGS = -custom

include OCamlMakefile

and make sure that the example built with make nc (native code) and make (bytecode) both work. Again, I assume you're using ocamlfind.

Good luck!

2 comments:

efnx said...

I've followed all the steps and I'm sure my installations are correct, but it looks as if there's something wrong with OCamlMakefile. Here's the error that I'm getting:

make[1]: *** No rule to make target `testsdl_1.cmi', needed by `testsdl_1'. Stop.
make: *** [byte-code] Error 2

I'll admit I'm not familiar with ocamlc and ocamlopt, maybe using OCamlMakefile for this is a little overkill?

efnx said...

My problem was my ocamlsdl installation, I had installed ocamlsdl before installing ocamlfind. I hadn't followed the steps quite like I said I had.