Thursday, September 3, 2009

How to get protobuf-c compiled for linking to PHP extensions under OS X

I'm writing a PHP extension at the moment that uses the C variant of Google protocol buffers - here. Unfortunately the PHP build I'm using uses 32-bit code for the commandline php, but 64-bit when running under apache. Here is the magic recipe.


First, build the standard C++/Java protocol buffers code


jons-macpro:protobuf-2.1.0 jmeredith$ cat BUILDIT
ARCH='-arch i386 -arch x86_64'
./configure --prefix=/Users/jmeredith/Applications/protobuf CFLAGS="$ARCH" CXXFLAGS="$ARCH" --disable-dependency-tracking && \
make && \
make install


Then build protobuf-c which uses protobuf


jons-macpro:protobuf-c-0.11 jmeredith$ cat BUILDIT
## Make sure protoc is in your path or this will die
ARCH="-arch i386 -arch x86_64"
./configure --prefix=/Users/jmeredith/Applications/protobuf-c \
CXXFLAGS="-I/Users/jmeredith/Applications/protobuf/include $ARCH" \
CFLAGS="$ARCH" \
LDFLAGS="-L/Users/jmeredith/Applications/protobuf/lib" \
--disable-dependency-tracking && \
make && \
make install


Add this to the extension config.m4


dnl # protobuf-c
if test "$PHP_PROTOBUFC" != "no"; then
PHP_ADD_INCLUDE($PHP_PROTOBUFC/include)
PHP_ADD_LIBRARY_WITH_PATH(protobuf-c, $PHP_PROTOBUFC/lib, MAGNETO_SHARED_LIBADD)
fi


And finally, to build your PHP extension


phpize
CFLAGS='-arch i386 -arch x86_64' ./configure --enable-yourextension --with-protobufc=/path/to/pb-c && \
make && \
sudo make install