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
Hi,

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
OPTIONAL_COMPONENTS Development)

I now do this:

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake) #
contains FindPythonInterp.cmake
find_package(PythonCompat 2.7 EXACT REQUIRED COMPONENTS Interpreter
OPTIONAL_COMPONENTS Development)

The resulting targets and variables are the same as the ones from
FindPython.cmake.

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:

list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake) #
contains PythonConfig.cmake
find_package(Python 2.7 EXACT REQUIRED COMPONENTS Interpreter
OPTIONAL_COMPONENTS Development)

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

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? 
FindPythonForward.cmake?

Thanks for your feedback,
Bram



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

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

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

FindPythonCompat.cmake (6K) Download Attachment