Allow building libraries with setuptools that dont have abi suffix #14130
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using
setuptoolsto build a Python extension, setuptools will automatically add an ABI suffix likecpython-37m-x86_64-linux-gnuto 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 calledmy_ops.cpython-37m-x86_64-linux-gnu.sois a bit annoying compared to justmy_ops.so, especially since you have to reference the library name when loading it withtorch.ops.load_libraryin Python.This PR fixes this by adding a
with_optionsclass method to thetorch.utils.cpp_extension.BuildExtensionwhich allows configuring theBuildExtension. In this case, the first option we add isno_python_abi_suffix, which we then use inget_ext_filename(override fromsetuptools.build_ext) to throw away the ABI suffix.I've added a test
setup.pyin ano_python_abi_suffix_testfolder.Fixes #14188
@t-vi @fmassa @soumith