FindPython.cmake in older CMake version

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

FindPython.cmake in older CMake version

Bram de Greve

I'm testing out CMake 3.12, and I very much like the new
FindPython.cmake. Being able to use targets for the Python libraries
means I can much easier package my own library without hardcoded paths.

The only thing is that I want to be compatible with older CMakes >=
3.0.  Therefore I came up with following approach: write a
FindPythonCompat.cmake that forwards to FindPython.cmake if CMake >=
3.12, and uses FindPythonInterp.cmake and FindPythonLibs.cmake to mimic
the new FindPython.cmake if CMake < 3.12.

You can find that file attached.

Instead of calling this:

find_package(Python 2.7 EXACT REQUIRED COMPONENTS Interpreter

I now do this:

contains FindPythonInterp.cmake
find_package(PythonCompat 2.7 EXACT REQUIRED COMPONENTS Interpreter

The resulting targets and variables are the same as the ones from

Another approached I consider is a bit more cunning: add a
PythonConfig.cmake and PythonConfigVersion.cmake to CMAKE_PREFIX_PATH. 
PythonConfig.cmake would not contain the forward to FindPython.cmake,
but it would instead rely on the fallback from MODULE to CONFIG mode
when FindPython.cmake cannot be found in CMake < 3.12. I would then
still be calling find_package(Python) but I extend CMAKE_PREFIX_PATH:

contains PythonConfig.cmake
find_package(Python 2.7 EXACT REQUIRED COMPONENTS Interpreter

In both cases it is of course required that I install this
FindPythonCompat.cmake or PythonConfig.cmake next to my own library's

Also, in the compatibility mode, it's not my goal to get a 1-on-1 match
with FindPython.cmake. For my needs, I only require what
FindInterp.cmake and FindLibs.cmake can deliver.

Now my questions to you are:
- What do you think of these approaches?
- What's the most CMake-esque approach?
- How can I improve this?
- Any better names for FindPythonCompat.cmake? FindPythonShim.cmake? 

Thanks for your feedback,


Powered by

Please keep messages on-topic and check the CMake FAQ at:

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support:
CMake Consulting:
CMake Training Courses:

Visit other Kitware open-source projects at

Follow this link to subscribe/unsubscribe:

FindPythonCompat.cmake (6K) Download Attachment