[CMake] Finding PythonLibs using PythonInterp

Michael Hertling mhertling at online.de
Mon Nov 28 15:37:16 EST 2011


On 11/28/2011 03:54 PM, Felipe Lema wrote:
> Hello, everyone
> 
> I was trying to use cmake to build python c++ extensions in win7, but
> couldn't get cmake to detect PythonLibs. At least, not without editing the
> windows registry. Since it could detect PythonInterp, I got it working
> using the directories provided from methods inside the interpreter.
> 
> -- code snippet --
> execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys;from
> distutils.sysconfig import
> get_python_inc;sys.stdout.write(get_python_inc())"
> OUTPUT_VARIABLE PYTHON_INCLUDE_DIRS
> ERROR_VARIABLE ERROR_FINDING_INCLUDES)
> 
> execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys;from
> numpy.distutils.numpy_distribution import NumpyDistribution;from
> numpy.distutils.command.build_ext import
> build_ext;a=build_ext(NumpyDistribution());a.ensure_finalized();sys.stdout.write(';'.join(a.library_dirs))"
> OUTPUT_VARIABLE PYTHON_LIBRARIES_DIR
> ERROR_VARIABLE ERROR_FINDING_LIBRARIES)
> 
>   -- /code snippet --
> 
> These directories can be used later for finding the python .lib files and
> version number, as cmake currently does (although using the directories
> from the windows registry)
> 
> The only setback (at least for finding the python lib files) is that the
> user must have numpy correctly installed with the found interpreter.
> Haven't tried it, but there should be a way to get it to work without numpy
> installed.
> 
> In spite of the latest, I still think that this code is very useful.
> 
> PS=Just in case, the python script embedded in the execute_process args
> should be a single line

The main drawback of your approach is that one has to invoke the Python
interpreter which is probably not acceptable in certain cross-compiling
scenarios. There've been several discussions about improvements of the
FindPython*.cmake modules, see [1,2], and the consensus is, IIRC, that
there should be one comprehensive and component-aware FindPython.cmake
module, knowing at least the components "interpreter" and "libraries"
and ensuring that both are consistent w.r.t. their version. Moreover,
the said module should allow the user to specify the desired version
via FIND_PACKAGE()'s respective interface, but the prevalent obstacle
in this regard is the FIND_*() commands' current inability to look for
patterns, e.g. FIND_PROGRAM() cannot search for "python[0-9]+\.[0-9]+".
IMO, addressing these issues should be preferred to pouring resources
into those rather outmoded FindPython{Interp,Libs}.cmake find modules.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg30086.html
[2] http://www.mail-archive.com/cmake@cmake.org/msg29487.html


More information about the CMake mailing list