Compiling lxml on 64bit CentOS

A few days ago I encountered a problem while deploying Plone 4 with collective.xdv to a CentOS cloud instance. Since CentOS’ repos were a bit out of date I needed to statically compile lxml and it’s dependencies with z3c.recipe.staticlxml. Here’s what you need to add to your buildout.cfg to do so:

parts += lxml
eggs += lxml

# ===================================================================
# For collective.xdv to work properly, we need a static build of lxml
# and it's dependencies on OS X and x86_64 Linux                     
# ===================================================================
[lxml]
recipe = z3c.recipe.staticlxml
egg = lxml
force = false
static-build = true

And here’s the kicker: on some 64-bit Linux systems compiling lxml produces an error like this at compile-time:

lxml: Building lxml ...
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available.
Using build configuration of libxslt 1.1.24
Building against libxml2/libxslt in one of the following directories:
  /home/production/1.1/parts/lxml/libxslt/lib
  /home/production/1.1/parts/lxml/libxml2/lib
/usr/bin/ld: /home/production/1.1/parts/lxml/libxslt/lib/libxslt.a(xslt.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/home/production/1.1/parts/lxml/libxslt/lib/libxslt.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
error: Setup script exited with error: command 'gcc' failed with exit status 1
An error occured when trying to install lxml 2.2.6. Look above this message for any errors that were output by easy_install.
While:
  Updating lxml.
Error: Couldn't install: lxml 2.2.6

Some problem with -fPIC flag not being set or something? After googling around without much success I decided to take a look directly in trunk of z3c.recipe.staticlxml and luckily Reinout had the same problem before me and already committed a patch. So all that was needed was to pull z3c.recipe.staticlxml from trunk and the compile error went away.

UPDATE: I emailed Stephan Eletzhofer to make a new release of z3c.recipe.staticxml that would include Reinout’s patch. He responded quickly and now all you need to do is make sure you have z3x.recipe.staticxml of version of 0.7.2 or higher:

[versions]
# we need this so that -fPIC flag is set when compiling lxml on 64 bit Linux
z3c.recipe.staticlxml = 0.7.2