更改Cython的.so文件命名规则

时间:2016-07-22 10:18:52

标签: python cython

我正在使用Cython从Python模块生成共享对象。编译输出将写入build/lib.linux-x86_64-3.5/<Package>/<module>.cpython-35m-x86_64-linux-gnu.so。有没有更改命名规则的选项?我希望文件名为<module>.so而没有解释器版本或arch附录。

3 个答案:

答案 0 :(得分:7)

似乎setuptools没有提供完全更改或删除后缀的选项。神奇的事情发生在distutils/command/build_ext.py

def get_ext_filename(self, ext_name):
    from distutils.sysconfig import get_config_var
    ext_path = ext_name.split('.')
    ext_suffix = get_config_var('EXT_SUFFIX')
    return os.path.join(*ext_path) + ext_suffix

似乎我需要添加一个构建后的重命名操作。

2016年12月8日更新:

好的,我忘了实际发布解决方案。实际上,我通过重载内置的install_lib命令实现了重命名操作。这是逻辑:

from distutils.command.install_lib import install_lib as _install_lib

def batch_rename(src, dst, src_dir_fd=None, dst_dir_fd=None):
    '''Same as os.rename, but returns the renaming result.'''
    os.rename(src, dst,
              src_dir_fd=src_dir_fd,
              dst_dir_fd=dst_dir_fd)
    return dst

class _CommandInstallCythonized(_install_lib):
    def __init__(self, *args, **kwargs):
        _install_lib.__init__(self, *args, **kwargs)

    def install(self):
        # let the distutils' install_lib do the hard work
        outfiles = _install_lib.install(self)
        # batch rename the outfiles:
        # for each file, match string between
        # second last and last dot and trim it
        matcher = re.compile('\.([^.]+)\.so$')
        return [batch_rename(file, re.sub(matcher, '.so', file))
                for file in outfiles]

现在你所要做的就是在setup函数中重载命令:

setup(
    ...
    cmdclass={
        'install_lib': _CommandInstallCythonized,
    },
    ...
)

但是,我对重载标准命令并不满意;如果您找到更好的解决方案,请发布,我会接受您的回答。

答案 1 :(得分:4)

此行为已在distutils包中定义。 distutils使用sysconfig和&#34; EXT_SUFFIX&#34;配置变量:

# Lib\distutils\command\build_ext.py

def get_ext_filename(self, ext_name):
    r"""Convert the name of an extension (eg. "foo.bar") into the name
    of the file from which it will be loaded (eg. "foo/bar.so", or
    "foo\bar.pyd").
    """
    from distutils.sysconfig import get_config_var
    ext_path = ext_name.split('.')
    ext_suffix = get_config_var('EXT_SUFFIX')
    return os.path.join(*ext_path) + ext_suffix

从Python 3.5&#34; EXT_SUFFIX&#34;开始变量包含平台信息,例如&#34; .cp35-win_amd64&#34;。

我写了以下函数:

def get_ext_filename_without_platform_suffix(filename):
    name, ext = os.path.splitext(filename)
    ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')

    if ext_suffix == ext:
        return filename

    ext_suffix = ext_suffix.replace(ext, '')
    idx = name.find(ext_suffix)

    if idx == -1:
        return filename
    else:
        return name[:idx] + ext

自定义 build_ext 命令:

from Cython.Distutils import build_ext

class BuildExtWithoutPlatformSuffix(build_ext):
    def get_ext_filename(self, ext_name):
        filename = super().get_ext_filename(ext_name)
        return get_ext_filename_without_platform_suffix(filename)

用法:

setup(
    ...
    cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
    ...
)

答案 2 :(得分:1)

解决方案非常简单,只需在

build_ext.py中更改一行即可
    def get_ext_filename(self, ext_name):

        from distutils.sysconfig import get_config_var
        ext_path = ext_name.split('.')
        ext_suffix = get_config_var('EXT_SUFFIX') 
        return os.path.join(*ext_path) + ext_suffix

ext_suffix = get_config_var('EXT_SUFFIX')更改为        ext_suffix = ".so"或Windows上的".pyd"

就是这样,您不必再担心输出名称