[ccan] Using ccan with a C python extension

Rusty Russell rusty at rustcorp.com.au
Tue Jun 16 08:01:11 AEST 2015


Stuart Longland <stuartl at longlandclan.yi.org> writes:
> On 15/06/15 12:01, Rusty Russell wrote:
>> Naveen Nathan <naveen at lastninja.net> writes:
>>> It is setup to be installed as a python package via pip/easy_install
>>> which will:
>>>     (1) fetch the python module
>>>     (2) build the C extension, and finally
>>>     (3) install it on the system (or virtual environment).
>>>
>>> I specifically want to use ccan/{bitmap,endian} but unsure how
>>> to integrate the code into our project's build process.
>>>
>>> I envision at step (2) above, we will build libccan and statically
>>> link it to the C extension. However, I will need a ccan config.h
>>> which will vary per system. So this will involve generating the
>>> file. Does that mean I should include the tools / configurator
>>> as part of our python package?
>> 
>>         I think you should use autoconf, as it's the standard.  I keep
>> hoping someone will write the autotools stuff for CCAN for me :)
>
> Yeah, the trouble with that approach is that autoconf does a pretty poor
> job of integrating into Python module builds.
>
> The usual build system in Python is the `distutils` module, which
> basically gets included into a Python script called 'setup.py' and called.
>
> Then it's a *magic happens here* affair, where out the other end is your
> Python module.
>
> You'd have to hook something in that could call ./configure at the
> appropriate time, and somehow convince the automated packaging tools
> (stdeb, gsourcery-pypi, etc) to ignore ./configure and use setup.py.
>
> I'm not sure what's needed in config.h; maybe there's some way to
> generate this using some basic Python calls before building?

It depends very much which modules.  For bitmap and endian you need:

HAVE_BUILTIN_CLZL
HAVE_BYTESWAP_H
HAVE_BSWAP_64
HAVE_LITTLE_ENDIAN / HAVE_BIG_ENDIAN

So you could get away with something like:

if sys.byteorder == 'little':
   print "#define HAVE_LITTLE_ENDIAN 1"
else:
   print "#define HAVE_BIG_ENDIAN 1"

print """#if __GNUC__ >= 4
#define HAVE_BUILTIN_CLZL 1
#endif
#ifdef __linux__
#define HAVE_BYTESWAP_H 1
#define HAVE_BSWAP_64 1
#endif"""

Cheers,
Rusty.
PS.  We should really enhance ccan/endian to use sys/endian.h on FreeBSD...


More information about the ccan mailing list