2009-08-31

Compiling Python using Clang

[edit: added compilation timings]

Like many people (if Twitter is any indication), I upgraded to Snow Leopard and XCode 3.2 this past weekend. One of the nice things that came with the new Developer Tools is Clang 1.0. I have been anticipating the stable release of this tool ever since I watched a video from the LLVM conference on it over a year ago. With it's much improved warning output compared to gcc and it's faster compilation time I wanted to give it a try on CPython.

First off, though, credit needs to be given to the Unladen Swallow guys, and especially Jeffrey Yasskin, for working out some nasty bugs that used to prevent LLVM from compiling CPython over the past year. Without the fixes I would have just given up on using clang.

With CPython now cleanly compiling with clang, I decided to give it a spin. The environment variables I ended up using specific to clang were:
  • CC = clang
  • CFLAGS = -Qunused-arguments
  • CPPFLAGS = -Qunused-arguments
The "-Qunused-arguments" flag tells clang to not complain if it is given command-line arguments that are redundant or unused. If you don't do this you can end up with a ton of warnings about unneeded CPPFLAGS arguments. And it is used in both CFLAGS and CPPFLAGS as otherwise it isn't picked up when setup.py runs (I don't think setup.py or distutils uses CFLAGS at the moment). But otherwise CPython builds fine!

One other thing you might want to try using when building CPython is "-Wno-unused-value". It turns out that PyObject_INIT() and PyObject_INIT_VAR() never have their returned values used explicitly and this flag turns off those warnings as there are a bunch of them and each one refers to two other code locations.

After I originally posted this I got one comment here and a couple on Twitter about what the benchmarking timings were. I caved in and ran them with ``/configure --prefix=/dev/null --with-pydebug --with-computed-gotos --with-universal-archs="64-bit``. In Clang it took a total of 36 seconds while with gcc 37 seconds. So the speed increase is minimal, but the important thing to remember is that the debugging information that Clang spits out is far and away better than what gcc gives you. So while the performance difference is small, the debugging output are not even close to being equal in terms of readability.