Friday, January 5, 2007

Building Pyrex with SCons

I have to admit that I like SCons. Maybe it is because I hate Make. Not really, SCons is really a great build tool. A few days ago, when I was working on the Pyrex section of the extensions chapter of my book, I got an itch to automate the building of Pyrex extension modules with SCons. As I decided to scratch that itch, I was surprised by the total lack of documentation on how to build Pyrex modules with SCons!

So I would have to do it the hard way. Fortunately, SCons makes it very easy to develop custom Builders. So after a couple of hours I had a working Pyrex Builder.

It could have been done quicker, If the LIBPATH keyword in the SharedLibrary function worked the way I expected. One other Annoyance was the fact that SCons prepends 'lib' to the name of a shared library, not respecting your wishes. This turned out to be a real problem: I painfully found out that if the name of the .so is not the same as the original .pyx source, You get an importError. I fixed that by manually renaming the .so file. If anyone knows of a better solution to this problem, please post a comment.

I also did not test this for building multiple files and other variations. If anyone feels like trying, please let me know how it goes.

here is the SConstruct code:

2 comments:

Greg said...

env.SharedLibrary('primespyx', ['primespyx.c'], LIBPREFIX = '')

removes the need to rename. I found this useful, thank you.

usagi said...

Thanks for the tip, Greg.

ccp

Amazon