Allow building libraries with setuptools that dont have abi suffix (#14130)
authorPeter Goldsborough <psag@fb.com>
Wed, 28 Nov 2018 01:33:54 +0000 (17:33 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 28 Nov 2018 01:35:53 +0000 (17:35 -0800)
commit6f2307ba6ac802f4c4650cd21869e49d1845188d
tree6bf9669e67297c974368101f79494cd20f2925bb
parent23d111c87fc2d4f0485ee300acc6aeca015b7965
Allow building libraries with setuptools that dont have abi suffix (#14130)

Summary:
When using `setuptools` to build a Python extension, setuptools will automatically add an ABI suffix like `cpython-37m-x86_64-linux-gnu` to the shared library name when using Python 3. This is required for extensions meant to be imported as Python modules. When we use setuptools to build shared libraries not meant as Python modules, for example libraries that define and register TorchScript custom ops, having your library called `my_ops.cpython-37m-x86_64-linux-gnu.so` is a bit annoying compared to just `my_ops.so`, especially since you have to reference the library name when loading it with `torch.ops.load_library` in Python.

This PR fixes this by adding a `with_options` class method to the `torch.utils.cpp_extension.BuildExtension` which allows configuring the `BuildExtension`. In this case, the first option we add is `no_python_abi_suffix`, which we then use in `get_ext_filename` (override from `setuptools.build_ext`) to throw away the ABI suffix.

I've added a test `setup.py` in a `no_python_abi_suffix_test` folder.

Fixes https://github.com/pytorch/pytorch/issues/14188

t-vi fmassa soumith
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14130

Differential Revision: D13216575

Pulled By: goldsborough

fbshipit-source-id: 67dc345c1278a1a4ee4ca907d848bc1fb4956cfa
test/cpp_extensions/no_python_abi_suffix_test/no_python_abi_suffix_test.cpp [new file with mode: 0644]
test/cpp_extensions/no_python_abi_suffix_test/setup.py [new file with mode: 0644]
test/cpp_extensions/setup.py
test/custom_operator/model.py
test/run_test.py
test/test_cpp_extensions.py
torch/utils/cpp_extension.py