This is a short and sweet recipe for using pkg-config with your distutils extension modules.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
import commands
def pkgconfig(*packages, **kw):
flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
for token in commands.getoutput("pkg-config --libs --cflags %s" % ' '.join(packages)).split():
kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
return kw
setup(
name = "myPackage",
ext_modules=[
Extension("extension", ["extension_main.c"], **pkgconfig('glib-2.0')),
],
)
|
Pkg-config is a very common system for obtaining path information about system libraries in a portable way. Makefiles typically invoke "pkg-config --cflags" to generate compiler flags, and "pkg-config --libs" to generate library flags for the linker.
This recipe makes it easy to use pkg-config's output with distutils. Now you can use system libraries without hard-coding paths, as long as those libraries are distributed with pkg-config data files.
The example builds a module "extension.so" that requires the GLib library.
Problems with additional arguments. So I made some changes:
def pkgconfig(packages, *kw):
Here is a version that handles flags which aren't -I, -L, or -l:
Hi, I separated handling of compile and link flags, made the code a bit more general and Python 3 compatible. I made a gist also https://gist.github.com/smidm/ff4a2c079fed97a92e9518bd3fa4797c .