[CMake] More distutils-ish FindPythonLibs.cmake?

Zachary Pincus zpincus at stanford.edu
Mon Jan 16 03:46:32 EST 2006


Hi folks,

I've been playing with using Python itself within a CMake file to  
look up include directories for python extensions. (e.g. query the  
Numeric module within python to find the Numeric C header file's  
location.)

It occurred to me that this might be a better way for  
FindPythonLibs.cmake to work too. Python comes with a 'distutils'  
package which is used to find the proper libraries and include  
directories for a given python executable. With this tool, everything  
works properly even when there are multiple versions of python  
installed on a system (not uncommon on, say, OS X, when there's a  
default python, but savvy users may have installed others via ports  
or some other package manager), or python versions installed in non- 
standard locations (also common). This stands in marked contrast to  
how the CMake file works now, with essentially hard-coded paths.

Anyhow, the general gist of the approach would be to run the python  
commands distutils.sysconfig.get_config_var('LIBDIR') and  
distutils.sysconfig.get_config_var('INCLUDEPY') to find the  
directories in which the python library and header files live.

In CMake, it would look something like this:

   IF(PYTHON_EXECUTABLE)
     EXEC_PROGRAM("${PYTHON_EXECUTABLE}"
       ARGS "-c 'try:\n  import distutils.sysconfig; print  
distutils.sysconfig.get_config_var('INCLUDEPY')\nexcept: pass\n'"
       OUTPUT_VARIABLE DISTUTILS_PYTHON_INCLUDE_PATH
     )
     EXEC_PROGRAM("${PYTHON_EXECUTABLE}"
       ARGS "-c 'try:\n  import distutils.sysconfig; print  
distutils.sysconfig.get_config_var('LIBDIR')\nexcept: pass\n'"
       OUTPUT_VARIABLE DISTUTILS_PYTHON_ILIBRARY_PATH
     )
   ENDIF(PYTHON_EXECUTABLE)

These paths could then be used by CMake in addition to the hard-coded  
search paths for the library and headers.

Is this worth pursuing? I could try to work up a patch if anyone  
thinks this is valuable.

Zach


More information about the CMake mailing list