Search code examples
pythondeploymentcmakedistutilspremake

Can I use an alternative build system for my Python extension module (written in C++)?


While distutils works alright, I'm not entirely comfortable with it, and I also have performance problems with no apparent solution. Is it possible to integrate Premake or cmake in my setup.py script so that python setup.py build calls them and then places the output where install expects it?


Solution

  • I figured out a way, it's not pretty but it works.

    Here is a summation of my setup.py script file - it should be fairly self explanatory:

    import shutil
    import os
    
    from distutils.core import setup
    from distutils.core import Extension
    
    from distutils.command.build_ext import build_ext
    from distutils.command.install_lib import install_lib
    
    library = None
    
    def build_lib():
        """
            Build library and copy it to a temp folder.
            :returns: Location of the generated library is returned.
        """
        raise NotImplementedError
    
        
    class MyInstall(install_lib):
        def install(self):
            global library
            shutil.move(library, self.install_dir)
            return [os.path.join(self.install_dir, library.split(os.sep)[-1])]
    
    
    class MyBuildExtension(build_ext):
        def run(self):
            global library
            library = build_lib();
    
    module = Extension('name',
                       sources=[])
    
    setup(
        name='...',
        version='...',
        ext_modules=[module],
        description='...',
        long_description='...',
        author='...',
        author_email='...',
        url='...',
        keywords='...',
        license='...',
        platforms=[],
        classifiers=[],
        cmdclass={
            'build_ext': MyBuildExtension,
            'install_lib': MyInstall
        },
    )
    

    I hope it helps.