pythonpython-3.xsetup.pydistutilsdistutils2

Change output filename in setup.py (distutils.extension)


Here's my setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

wrapper = Extension(
    name="libwrapper",
    ...
)
setup(
    name="libwrapper",
    ext_modules=cythonize([wrapper])
)

When I run python3 setup.py build_ext the output file is called libwrapper.cpython-36m-x86_64-linux-gnu.so, but I want to name it only libwrapper.so, how can I do that?


Solution

  • Try the following code. sysconfig.get_config_var('EXT_SUFFIX') returns platform-specific suffix which can be removed from the final filename by subclassing build_ext and overriding get_ext_filename.

    from distutils import sysconfig
    from Cython.Distutils import build_ext
    from distutils.core import setup
    import os
    
    class NoSuffixBuilder(build_ext):
        def get_ext_filename(self, ext_name):
            filename = super().get_ext_filename(ext_name)
            suffix = sysconfig.get_config_var('EXT_SUFFIX')
            ext = os.path.splitext(filename)[1]
            return filename.replace(suffix, "") + ext
    
    
    setup(
        ....
        cmdclass={"build_ext": NoSuffixBuilder},
    )
    

    Final filename will be test.so