[lldb/CMake] Add LLDB_PYTHON_VERSION to use Python 2 with CMake > 3.12

In addition to having the default fallback from Python 3 to Python 2, it
should also be possible to build against Python 2 explicitly. This patch
makes that possible by setting LLDB_PYTHON_VERSION. The variable only
has effect with CMake 3.12 or later.

Differential revision: https://reviews.llvm.org/D81501
This commit is contained in:
Jonas Devlieghere 2020-06-09 14:09:42 -07:00
parent 765ac39db2
commit 2c0afacada

View File

@ -4,54 +4,74 @@
#
# Find the python interpreter and libraries as a whole.
macro(FindPython3)
# Use PYTHON_HOME as a hint to find Python 3.
set(Python3_ROOT_DIR "${PYTHON_HOME}")
find_package(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND AND Python3_Interpreter_FOUND)
set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
# The install name for the Python 3 framework in Xcode is relative to
# the framework's location and not the dylib itself.
#
# @rpath/Python3.framework/Versions/3.x/Python3
#
# This means that we need to compute the path to the Python3.framework
# and use that as the RPATH instead of the usual dylib's directory.
#
# The check below shouldn't match Homebrew's Python framework as it is
# called Python.framework instead of Python3.framework.
if (APPLE AND Python3_LIBRARIES MATCHES "Python3.framework")
string(FIND "${Python3_LIBRARIES}" "Python3.framework" python_framework_pos)
string(SUBSTRING "${Python3_LIBRARIES}" "0" ${python_framework_pos} PYTHON_RPATH)
endif()
set(PYTHON3_FOUND TRUE)
mark_as_advanced(
PYTHON_LIBRARIES
PYTHON_INCLUDE_DIRS
PYTHON_EXECUTABLE
PYTHON_RPATH
SWIG_EXECUTABLE)
endif()
endmacro()
macro(FindPython2)
# Use PYTHON_HOME as a hint to find Python 2.
set(Python2_ROOT_DIR "${PYTHON_HOME}")
find_package(Python2 COMPONENTS Interpreter Development)
if(Python2_FOUND AND Python2_Interpreter_FOUND)
set(PYTHON_LIBRARIES ${Python2_LIBRARIES})
set(PYTHON_INCLUDE_DIRS ${Python2_INCLUDE_DIRS})
set(PYTHON_EXECUTABLE ${Python2_EXECUTABLE})
set(PYTHON2_FOUND TRUE)
mark_as_advanced(
PYTHON_LIBRARIES
PYTHON_INCLUDE_DIRS
PYTHON_EXECUTABLE
SWIG_EXECUTABLE)
endif()
endmacro()
if(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS AND PYTHON_EXECUTABLE AND SWIG_EXECUTABLE)
set(PYTHONINTERPANDLIBS_FOUND TRUE)
else()
find_package(SWIG 2.0)
if (SWIG_FOUND)
if(NOT CMAKE_VERSION VERSION_LESS 3.12)
# Use PYTHON_HOME as a hint to find Python 3.
set(Python3_ROOT_DIR "${PYTHON_HOME}")
find_package(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND AND Python3_Interpreter_FOUND)
set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
# The install name for the Python 3 framework in Xcode is relative to
# the framework's location and not the dylib itself.
#
# @rpath/Python3.framework/Versions/3.x/Python3
#
# This means that we need to compute the path to the Python3.framework
# and use that as the RPATH instead of the usual dylib's directory.
#
# The check below shouldn't match Homebrew's Python framework as it is
# called Python.framework instead of Python3.framework.
if (APPLE AND Python3_LIBRARIES MATCHES "Python3.framework")
string(FIND "${Python3_LIBRARIES}" "Python3.framework" python_framework_pos)
string(SUBSTRING "${Python3_LIBRARIES}" "0" ${python_framework_pos} PYTHON_RPATH)
if (LLDB_PYTHON_VERSION)
if (LLDB_PYTHON_VERSION VERSION_EQUAL "2")
FindPython2()
elseif(LLDB_PYTHON_VERSION VERSION_EQUAL "3")
FindPython3()
endif()
mark_as_advanced(
PYTHON_LIBRARIES
PYTHON_INCLUDE_DIRS
PYTHON_EXECUTABLE
PYTHON_RPATH
SWIG_EXECUTABLE)
elseif(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
# Use PYTHON_HOME as a hint to find Python 2.
set(Python2_ROOT_DIR "${PYTHON_HOME}")
find_package(Python2 COMPONENTS Interpreter Development)
if(Python2_FOUND AND Python2_Interpreter_FOUND)
set(PYTHON_LIBRARIES ${Python2_LIBRARIES})
set(PYTHON_INCLUDE_DIRS ${Python2_INCLUDE_DIRS})
set(PYTHON_EXECUTABLE ${Python2_EXECUTABLE})
mark_as_advanced(
PYTHON_LIBRARIES
PYTHON_INCLUDE_DIRS
PYTHON_EXECUTABLE
SWIG_EXECUTABLE)
else()
FindPython3()
if (NOT PYTHON3_FOUND AND NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
FindPython2()
endif()
endif()
else()