How to compile GDB and other Apple open source packages in Mac OS X

I wanted to recompile GDB so I can modify its source and add some custom patches to enhance its output… Easier said than done!
There’s not much information around about this and my first attempt was by downloading GDB source package from Apple and trying to compile it. Didn’t compile out of the box so I had to fix here and there and finally it compiled, but then it didn’t work. Searching the web for more ideas and finally understood I had to use darwinbuild environment for this task. Install darwinbuild, follow instructions (crappy ones I might add!) and bang, it doesn’t compile due to huge dependencies from include files. Fix here and there and still no luck. Searching the web again and finally the misterious parameter to darwinbuild, -nochroot. Compile and voila, it works 😄.

And now it’s very easy to do. You should have XCode installed. Follow these steps:

1) Download darwinbuild from their SVN repository (Mac SVN client available here http://homepage.mac.com/martinott/). 1.1) Snow Leopard already has SVN client by default so no need to download. Instructions on how to download, compile and install darwinbuild are here. Macports can too be used to install. 2) Compile and install darwinbuild:

$ make ; sudo make install

3) Create the DMG file and initialize darwinbuild environment (you should set 2 gigabytes for Snow Leopard because of the 64bit version):

The plists and build numbers are available at http://svn.macosforge.org/repository/darwinbuild/trunk/plists/.

$ hdiutil create -size 1G -type UDIF -fs HFSX -volname Builds -uid 0 -gid 0 -attach Builds.dmg
$ sudo sh
# vsdbutil -a /Volumes/Builds
# cd /Volumes/Builds
# mkdir Build9G55 (this is for Leopard 10.5.6) (Snow Leopard 10.6.2 is Build10C540)
# cd Build9G55
# darwinbuild -init 9G55 (you need Internet connection)
# darwinxref edit

Insert the following after darwin tag (this will make it compile only for i386):

environment = {
INSTALLED_PRODUCT_ASIDES = YES;
MACOSX_DEPLOYMENT_TARGET = 10.5;
NEXT_ROOT = “”;
RC_ARCHS = i386;
RC_JASPER = YES;
RC_NONARCH_CFLAGS = “-pipe -no-cpp-precomp”;
RC_OS = macos;
RC_PRIVATE = /private;
RC_RELEASE = Leopard;
RC_XBS = YES;
SEPARATE_STRIP = YES;
UNAME_RELEASE = 9.6;
UNAME_SYSNAME = Darwin;
};

For Snow Leopard use this instead (it will build 32 and 64 bit binaries):

environment = {
INSTALLED_PRODUCT_ASIDES = YES;
MACOSX_DEPLOYMENT_TARGET = 10.6;
NEXT_ROOT = “”;
RC_ARCHS = “i386 x86_64”;
RC_JASPER = YES;
RC_NONARCH_CFLAGS = “-pipe”;
RC_OS = macos;
RC_PRIVATE = /private;
RC_RELEASE = SnowLeopard;
RC_TARGET_CONFIG = MacOSX;
RC_XBS = YES;
SEPARATE_STRIP = YES;
UNAME_RELEASE = 10.0;
UNAME_SYSNAME = Darwin;
};

Editor used is vi. Save and quit.
Update: If you have a problem with an invalid property list, you need to replace the quotes in the block you just pasted (copy & paste problems). That should fix the problem.

# darwinbuild -nochroot gdb

Update: The -nosource option has been added to recent darwinbuild versions. This option will allow you to patch directly into BuildRoot/SourceCache/.
The first time you shouldn’t use this option so darwinbuild will download GDB package. After that you can use it if you want to patch directly GDB source files (that’s what I do with my GDB patches). It’s much easier and faster than having to patch and compress the whole GDB source. After you patch, you just issue darwinbuild -nochroot -nosource gdb and this will not unpack the original source but instead use whatever is at SourceCache.

There are problems with libiconv in Snow Leopard. Configure picks the lib available at /usr/local/lib and this generates undefined symbols when compiling. The solution is to link that to /usr/lib/libiconv.dylib or to edit the Makefile. To edit the Makefile you either need to edit the original tar.gz available at Sources dir (and repackage it), or you can issue darwinbuild -nochroot gdb, wait for error, then edit BuildRoot/SourceCache/gdb/gdb-1344/src/gdb/Makefile.in, search for LIBICONV and replace with LIBICONV = /usr/lib/libiconv.dylib. I have tried to modify the Makefile to pass the correct path to configure but it’s not working… Bah! I don’t feel like exploring this (darwinbuild documentation is CRAP) so MacGyver tactics will have to do the job 😄.

If you have some problems compiling for x86_64 then remove that architecture from RC_ARCHS. It worked without any problem for me. The final binary will be a fat binary having i386 and x86_64 versions.

Update End.

Wait for the compilation to finish… Go to Roots/gdb/gdb-768.root*/usr/libexec/gdb (in Snow Leopard it should be gdb-1344.root*). You should have a gdb-i386-apple-darwin. Backup the original and copy this one over.

# cp /usr/libexec/gdb/gdb-i386-apple-darwin /usr/libexec/gdb/gdb-i386-apple-darwin.orig
# cp gdb-i386-apple-darwin /usr/libexec/gdb/

Launch gdb and see if it works. It should 😃. It’s easy after you find how 😉.

Now I just need to finish the patches… And tha tha that that’s all folks!

fG!

References:
http://darwinbuild.macosforge.org/
http://wiki.osx86project.org/wiki/index.php/Building_Darwin
http://www.lartmaker.nl/rsync/
http://www.puredarwin.org/developers/darwinbuild