Packaging: How do a build Python PyTorch from a `-DBUILD_PYTHON=OFF`, library-only build?

I have packaged a library-only (non-Python) build of PyTorch (-DBUILD_PYTHON=OFF passed to cmake) for Slackware (using this SlackBuild), but now I’d like to make another Slackware package for Python PyTorch.

How do a build Python PyTorch? It seems -DBUILD_PYTHON=ON would have created all the necessary files, but when I run python3 setup.py install --root=$PKG, it complains that I’m not in a git repo; I downloaded pytorch-v2.3.0.tar.gz to make the library-only package.

It actually did build the site-package files. I just needed to manually move them to the $PKG by adding these lines to the SlackBuild (right before cd ..):

  mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/python3.11
  mv tmpxxx/usr/lib/python3.11/site-packages $PKG/usr/lib$LIBDIRSUFFIX/python3.11

But the problem here is that that site-packages directory doesn’t have any __init__.py files or anything. Why didn’t cmake make them? Must I use setup.py (which requires I be in a git directory)?

I tried using setup.py in a git clone:

git clone --recursive https://github.com/pytorch/pytorch

In the SlackBuild, I used this instead (paths specific to my CUDA install):

export CMAKE_PREFIX_PATH=$PKG
MAX_JOBS=32 \
USE_CUDA=1 \
CUDA_PATH=/usr/share/cuda \
CFLAGS="$SLKCFLAGS" \
CUDAHOSTCXX=/home/geremia/gcc-11/usr/bin/gcc \
USE_CUDNN=1 \
CUDNN_LIBRARY=/usr/share/cuda/lib64/libcudnn.so \
NVTOOLSEXT_PATH=/usr/lib64/libnvToolsExt.so.1 \
python setup.py develop

But ran into errors I didn’t have in the CMake build. I wish I didn’t have to use setup.py at all to build the Python bindings.

I needed to use Ninja, run setup.py build with the --cmake-only flag, and manually install Python site-package stuff, as described here.