4

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?

user10207893
  • 243
  • 1
  • 2
  • 11

1 Answers1

4

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

hurlenko
  • 1,363
  • 2
  • 12
  • 17
  • Can you extend my `setup.py` file with your suggestion? It's not clear how to do it. – user10207893 Feb 19 '20 at 13:28
  • Just add `NoSuffixBuilder` to your `setup.py` file and `cmdclass=...` to your `setup` function and build the extension using `python setup.py build_ext` – hurlenko Feb 19 '20 at 15:51
  • Traceback (most recent call last): File "code/cython/setup.py", line 9, in class NoSuffixBuilder(build_ext): NameError: name 'build_ext' is not defined – user10207893 Feb 19 '20 at 16:22