Skip to content

Instantly share code, notes, and snippets.

@rosjat
Last active January 25, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosjat/4252dd9e220f8fa210da17e2eb09b0a7 to your computer and use it in GitHub Desktop.
Save rosjat/4252dd9e220f8fa210da17e2eb09b0a7 to your computer and use it in GitHub Desktop.
Add Extention to a Python Distribution before the build
# This may help others to get started with there custom build process
#
# you find millions of post how to make a custom command and to run code
# but if you want to somewhat alter the way how extentions are build or
# not you may find yourself stuck ... like I was
from distutils.command.build import build as _build
from distutils.core import setup, Extension
foo_module = Extension(name='foo',
sources=['foo/foomodule.c'])
class FooBuildCommand(_build):
user_options = _build.user_options + [
('with-foo', None,
'build module with foo support'), ]
def initialize_options(self):
self.with_foo = None
_build.initialize_options(self)
def finalize_options(self):
_build.finalize_options(self)
def run(self):
if self.with_foo:
self.announce('build with foo support ...', 2)
self.distribution.ext_modules.append(foo_module)
self.distribution.packages.append('foo')
_build.run(self)
# the big take away here is to define ext_module even if you dont have extentions !
# if you dont do this you will get an error in the custom command because ext_modules is None !
setup_dict = {'name': 'BAR',
'version': '1.0',
'license': 'LGPLv2.1',
'author': 'Markus Rosjat',
'author_email': 'markus.rosjat@gmail.com',
'description': 'the awesome bar package',
'packages': ['bar', ],
'ext_modules': [],
'cmdclass': {'build': FooBuildCommand, }}
setup(**setup_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment