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

橙三吉。 提交于 2019-12-08 08:21:49

问题


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?


回答1:


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 generate_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 = create_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.



来源:https://stackoverflow.com/questions/24241475/can-i-use-an-alternative-build-system-for-my-python-extension-module-written-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!