[CMake] Issue with Python Libs in CMake 3.4 and Beyond on Linux

Harrison, Cyrus D. harrison37 at llnl.gov
Tue Jan 31 18:33:13 EST 2017


Hi Everyone,

Consistent with the docs here:

https://cmake.org/cmake/help/v3.7/module/FindPythonLibs.html

We want to seed which python libs we want to look for by calling find_package(PythonInterp), which sets the PYTHON_LIBRARY path, before calling find_package(PythonLibs)

Starting with CMake 3.4, this started failing for us on a few linux platforms.

Here is the problem:

get_filename_component inside of FindPythonLibs.cmake fails b/c PYTHON_LIBRARY contains two entires:

The python lib (libpython2.7.so) and a symlink to it (libpython2.7.so.1.0)

get_filename_component expects a single entry. This error propagates and halts the configure.

Things works for us with CMake 3.3, started seeing this issue with 3.4.

Below is a patch that avoids the issue, but not sure if it’s the ideal solution.

If PYTHON_LIBRARY is really supposed to be a single path, there could be other CMake logic undermined by the fact PythonInterp  produces a list in some cases.

====
diff -c cmake-3.7.2/Modules/FindPythonLibs.cmake cmake-3.7.2-patch/Modules/FindPythonLibs.cmake 
*** cmake-3.7.2/Modules/FindPythonLibs.cmake	2017-01-13 06:05:41.000000000 -0800
--- cmake-3.7.2-patch/Modules/FindPythonLibs.cmake	2017-01-31 14:28:21.000000000 -0800
***************
*** 168,174 ****
  
      # Use the library's install prefix as a hint
      set(_Python_INCLUDE_PATH_HINT)
!     get_filename_component(_Python_PREFIX ${PYTHON_LIBRARY} PATH)
      get_filename_component(_Python_PREFIX ${_Python_PREFIX} PATH)
      if(_Python_PREFIX)
        set(_Python_INCLUDE_PATH_HINT ${_Python_PREFIX}/include)
--- 168,181 ----
  
      # Use the library's install prefix as a hint
      set(_Python_INCLUDE_PATH_HINT)
!     # get_filename_component(_Python_PREFIX ${PYTHON_LIBRARY} PATH)
!     # the above fails when find_package(PythonInterp) returns a list with 
!     # both the python lib and a symlink to it.
!     # 
!     # prevent this case by only using the first item in ${PYTHON_LIBRARY}
!     # as input to get_filename_component
!     list(GET ${PYTHON_LIBRARY} 0 FIRST_FOUND_PYTHON_LIBRARY)
!     get_filename_component(_Python_PREFIX ${FIRST_FOUND_PYTHON_LIBRARY} PATH)
      get_filename_component(_Python_PREFIX ${_Python_PREFIX} PATH)
      if(_Python_PREFIX)
        set(_Python_INCLUDE_PATH_HINT ${_Python_PREFIX}/include)

====




-Cyrus


More information about the CMake mailing list