FindPythonX only defines PythonX_add_library() if Development component is requested
According to the documentation for the FindPython3
module (and FindPython2 is analogous):
This module defines the command Python_add_library (when CMAKE_ROLE is PROJECT)...
But the implementation only defines the command if either of the Development.Module
or Development.Embed
components are both requested and found. If no components are requested, only the Interpreter
component is found by default, resulting in Python3_add_library()
not being defined.
This can lead to unexpected and confusing errors. For example, I am currently experimenting with vcpkg. I call find_package(Python3 3.7 EXACT REQUIRED)
and later find_package(pybind11 REQUIRED)
. The latter call fails because files processed as part of that expect Python3_add_library()
to exist if Python3_FOUND
is true. This could be seen as an error in vcpkg, but strictly speaking, they are relying on the documented behavior.
It is unclear what is the right thing to do here. If we update the docs to reflect the current implementation, we are technically changing the API. We could change the implementation to always define the command, but error upon executing it if the necessary preconditions are not met at that point. I'd personally favour the latter, as it is more predictable and more robust when there could be multiple find_package()
calls with varying components specified, etc.