FindSWIG prefers swig4.0 (or in general swig<swigmajorversion>.0) named executables over swig named executables
If you are in an Debian system, and in the system the swig
package is installed, and if you are running a CMake script with find_package(SWIG REQUIRED)
in a conda environment with both cmake
and swig
installed, the SWIG_EXECUTABLE
found by CMake will be /usr/bin/swig4.0
instead of the expected $CONDA_PREFIX/bin/swig
, even if which swig
returns $CONDA_PREFIX/bin/swig
.
Similarly, if you install vanilla swig from source, and you install it in a path that you prepend to the /usr/bin
system path (for example /opt/newswig
), which swig
will return /opt/newswig/bin/swig
, but CMake's find_package(SWIG REQUIRED)
will continue to find /usr/bin/swig4.0
.
I guess something similar could happen with spack or conan swig packages when using them on an apt system, but I did not tried.
This happens as CMake prefers executables called swig<pkgmajorversion>.0
(like swig4.0
) over executables called swig
, and the debian packaging installs swig4.0
, while conda-forge packaging only installs swig
, see the line find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig)
(https://github.com/Kitware/CMake/blob/v3.28.1/Modules/FindSWIG.cmake#L89).
Strictly speaking this is not a swig packaging issue, but rather an unexpected interaction between CMake, SWIG and the respective conda-forge and Debian packaging. However, I was quite confused by this, so I thought that it was useful to document it somewhere.
From what I understand, the symlink swig<swigmajorversion>.0
is something that is not done by SWIG's buildsystem upstream, but rather a packaging details of Debian distributions. Indeed, if you check for example Fedora (https://packages.fedoraproject.org/pkgs/swig/swig/fedora-rawhide.html#files) or arch (https://archlinux.org/packages/extra/x86_64/swig/), they both only install swig
.
For this reason, I would expect that CMake's FindSWIG would look first for swig
, and only as a fallback for swig<swigmajorversion>.0
. However, this would be technically a breaking change, so I am not sure how to handle that.