1
# Copyright 2007 Joe Wreschnig
3
# This software and accompanying documentation, if any, may be freely
4
# used, distributed, and/or modified, in any form and for any purpose,
5
# as long as this notice is preserved. There is no warranty, either
6
# express or implied, for this software.
8
"""GObject extension support
10
This module contains commands and classes to support building
11
Python GObject wrappers.
16
from distutils.core import Extension
17
from distutils.command.build_ext import build_ext as distutils_build_ext
18
from gdist.core import GCommand
20
class GObjectExtension(Extension):
21
"""Generate and build GObject extensions
23
This class will build GObject extensions much like the normal
24
distutils Extension class builds normal CPython extensions.
25
Its constructor takes two more arguments, the defs file and
28
def __init__(self, pkgname, defs, override, sources, *args, **kwargs):
29
self._c_file = pkgname + "_py_codegen.c"
30
self._override = override
32
sources.append(self._c_file)
33
Extension.__init__(self, pkgname, sources, *args, **kwargs)
35
class build_gobject_ext(distutils_build_ext, GCommand):
36
"""build Python GObject extensions
38
This command builds Python GObject extensions by generating the
39
appropriate source file with pygtk-codegen-2.0, getting the
40
correct compiler flags with pkg-config, and then compiling
41
everything with the default distutils extension commands.
44
description = "build Python GObject extensions"
46
def finalize_options(self):
47
distutils_build_ext.finalize_options(self)
48
GCommand.finalize_options(self)
49
self.extensions = self.distribution.gobject_modules
50
self._defsdir = self.capture(
51
["pkg-config", "--variable", "defsdir", "pygtk-2.0"]).strip()
52
self._cargs = self.capture(
53
["pkg-config", "--cflags", "gtk+-2.0", "pygtk-2.0"]).split()
54
self._ldargs = self.capture(
55
["pkg-config", "--libs", "gtk+-2.0", "pygtk-2.0"]).split()
57
def build_extension(self, ext):
58
ext.extra_compile_args.extend(self._cargs)
59
ext.extra_link_args.extend(self._ldargs)
60
name = ext.name.split(".")[-1]
62
["pygtk-codegen-2.0", "--prefix", name,
63
"--register", os.path.join(self._defsdir, "gdk-types.defs"),
64
"--register", os.path.join(self._defsdir, "gtk-types.defs"),
65
"--override", ext._override, ext._def_file])
66
fileobj = file(ext._c_file, "w")
69
distutils_build_ext.build_extension(self, ext)
71
os.unlink(ext._c_file)
73
__all__ = ["build_gobject_ext"]