Swig python target name and CMP0078
When using the CMP0078 NEW policy with ADD_SWIG_LIBRARY, and having a target name that differs from the module name, the generated Python module can't load the native wrapper .pyd file.
Use case: suppose I have a C++ library "mylib" that I want Python bindings for. I want the Python module to be called "mylib" as well, so in the SWIG input file I use %module mylib
. This will do what I want and generate a mylib.py file which I can use from Python with import mylib
. However, under the new policy, I can't use "mylib" as the target name, as that is already the target name of the C++ library to wrap. So I want to use the target name "mylib_python" instead, which creates mylib.py and _mylib_python.pyd. However, in CMake 3.13.2, the generated mylib.py file tries to incorrectly load "_mylib.pyd".
Solution: ADD_SWIG_LIBRARY should pass the extra flag -interface <lib>
to the SWIG command line. So in the above example, pass -interface _mylib_python
. This is similar to what ADD_SWIG_LIBRARY does for the CSHARP modules (using the equivalent -dllimport switch defined for CSHARP), where I was able to use "mylib_csharp" as a target name just fine.
Workaround: Use set(CMAKE_SWIG_FLAGS -interface _mylib_python)
before calling ADD_SWIG_LIBRARY.
Note: the CMP0078 OLD policy creates a target named "_mylib", which avoids the target name conflict, but isn't as descriptive as "mylib_python".