Teach CMake to find versions of Python != 2.7

llvm-svn: 249466
This commit is contained in:
Zachary Turner 2015-10-06 21:11:15 +00:00
parent dca46b426f
commit f2be3dfd81
2 changed files with 90 additions and 56 deletions

View File

@ -37,6 +37,88 @@ if (LLDB_DISABLE_CURSES)
add_definitions( -DLLDB_DISABLE_CURSES )
endif()
# On Windows, we can't use the normal FindPythonLibs module that comes with CMake,
# for a number of reasons.
# 1) Prior to MSVC 2015, it is only possible to embed Python if python itself was
# compiled with an identical version (and build configuration) of MSVC as LLDB.
# The standard algorithm does not take into account the differences between
# a binary release distribution of python and a custom built distribution.
# 2) From MSVC 2015 and onwards, it is only possible to use Python 3.5 or later.
# 3) FindPythonLibs queries the registry to locate Python, and when looking for a
# 64-bit version of Python, since cmake.exe is a 32-bit executable, it will see
# a 32-bit view of the registry. As such, it is impossible for FindPythonLibs to
# locate 64-bit Python libraries.
# This function is designed to address those limitations. Currently it only partially
# addresses them, but it can be improved and extended on an as-needed basis.
function(find_python_libs_windows)
if ("${PYTHON_HOME}" STREQUAL "")
message("LLDB embedded Python on Windows requires specifying a value for PYTHON_HOME. Python support disabled.")
set(LLDB_DISABLE_PYTHON 1 PARENT_SCOPE)
return()
endif()
file(TO_CMAKE_PATH "${PYTHON_HOME}/Include" PYTHON_INCLUDE_DIRS)
if(EXISTS "${PYTHON_INCLUDE_DIRS}/patchlevel.h")
file(STRINGS "${PYTHON_INCLUDE_DIRS}/patchlevel.h" python_version_str
REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
PYTHONLIBS_VERSION_STRING "${python_version_str}")
message("-- Found Python version ${PYTHONLIBS_VERSION_STRING}")
string(REGEX REPLACE "([0-9]+)[.]([0-9]+)[.][0-9]+" "python\\1\\2" PYTHONLIBS_BASE_NAME "${PYTHONLIBS_VERSION_STRING}")
unset(python_version_str)
else()
message("Unable to find ${PYTHON_INCLUDE_DIRS}/patchlevel.h, Python installation is corrupt.")
message("Python support will be disabled for this build.")
set(LLDB_DISABLE_PYTHON 1 PARENT_SCOPE)
return()
endif()
file(TO_CMAKE_PATH "${PYTHON_HOME}" PYTHON_HOME)
file(TO_CMAKE_PATH "${PYTHON_HOME}/python_d.exe" PYTHON_DEBUG_EXE)
file(TO_CMAKE_PATH "${PYTHON_HOME}/libs/${PYTHONLIBS_BASE_NAME}_d.lib" PYTHON_DEBUG_LIB)
file(TO_CMAKE_PATH "${PYTHON_HOME}/${PYTHONLIBS_BASE_NAME}_d.dll" PYTHON_DEBUG_DLL)
file(TO_CMAKE_PATH "${PYTHON_HOME}/python.exe" PYTHON_RELEASE_EXE)
file(TO_CMAKE_PATH "${PYTHON_HOME}/libs/${PYTHONLIBS_BASE_NAME}.lib" PYTHON_RELEASE_LIB)
file(TO_CMAKE_PATH "${PYTHON_HOME}/${PYTHONLIBS_BASE_NAME}.dll" PYTHON_RELEASE_DLL)
# Generator expressions are evaluated in the context of each build configuration generated
# by CMake. Here we use the $<CONFIG:Debug>:VALUE logical generator expression to ensure
# that the debug Python library, DLL, and executable are used in the Debug build configuration.
#
# Generator expressions can be difficult to grok at first so here's a breakdown of the one
# used for PYTHON_LIBRARY:
#
# 1. $<CONFIG:Debug> evaluates to 1 when the Debug configuration is being generated,
# or 0 in all other cases.
# 2. $<$<CONFIG:Debug>:${PYTHON_DEBUG_LIB}> expands to ${PYTHON_DEBUG_LIB} when the Debug
# configuration is being generated, or nothing (literally) in all other cases.
# 3. $<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_LIB}> expands to ${PYTHON_RELEASE_LIB} when
# any configuration other than Debug is being generated, or nothing in all other cases.
# 4. The conditionals in 2 & 3 are mutually exclusive.
# 5. A logical expression with a conditional that evaluates to 0 yields no value at all.
#
# Due to 4 & 5 it's possible to concatenate 2 & 3 to obtain a single value specific to each
# build configuration. In this example the value will be ${PYTHON_DEBUG_LIB} when generating the
# Debug configuration, or ${PYTHON_RELEASE_LIB} when generating any other configuration.
# Note that it's imperative that there is no whitespace between the two expressions, otherwise
# CMake will insert a semicolon between the two.
set (PYTHON_EXECUTABLE $<$<CONFIG:Debug>:${PYTHON_DEBUG_EXE}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_EXE}>)
set (PYTHON_LIBRARY $<$<CONFIG:Debug>:${PYTHON_DEBUG_LIB}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_LIB}>)
set (PYTHON_DLL $<$<CONFIG:Debug>:${PYTHON_DEBUG_DLL}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_DLL}>)
set (PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE} PARENT_SCOPE)
set (PYTHON_LIBRARY ${PYTHON_LIBRARY} PARENT_SCOPE)
set (PYTHON_DLL ${PYTHON_DLL} PARENT_SCOPE)
set (PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} PARENT_SCOPE)
message("-- LLDB Found PythonExecutable: ${PYTHON_EXECUTABLE}")
message("-- LLDB Found PythonLibs: ${PYTHON_LIBRARY}")
message("-- LLDB Found PythonDLL: ${PYTHON_DLL}")
message("-- LLDB Found PythonIncludeDirs: ${PYTHON_INCLUDE_DIRS}")
endfunction(find_python_libs_windows)
if (NOT LLDB_DISABLE_PYTHON)
if(UNIX)
# This is necessary for crosscompile on Ubuntu 14.04 64bit. Need a proper fix.
@ -46,69 +128,22 @@ if (NOT LLDB_DISABLE_PYTHON)
endif()
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
if (NOT "${PYTHON_HOME}" STREQUAL "")
file(TO_CMAKE_PATH "${PYTHON_HOME}" PYTHON_HOME)
file(TO_CMAKE_PATH "${PYTHON_HOME}/python_d.exe" PYTHON_DEBUG_EXE)
file(TO_CMAKE_PATH "${PYTHON_HOME}/libs/python27_d.lib" PYTHON_DEBUG_LIB)
file(TO_CMAKE_PATH "${PYTHON_HOME}/python27_d.dll" PYTHON_DEBUG_DLL)
find_python_libs_windows()
file(TO_CMAKE_PATH "${PYTHON_HOME}/python.exe" PYTHON_RELEASE_EXE)
file(TO_CMAKE_PATH "${PYTHON_HOME}/libs/python27.lib" PYTHON_RELEASE_LIB)
file(TO_CMAKE_PATH "${PYTHON_HOME}/python27.dll" PYTHON_RELEASE_DLL)
# Generator expressions are evaluated in the context of each build configuration generated
# by CMake. Here we use the $<CONFIG:Debug>:VALUE logical generator expression to ensure
# that the debug Python library, DLL, and executable are used in the Debug build configuration.
#
# Generator expressions can be difficult to grok at first so here's a breakdown of the one
# used for PYTHON_LIBRARY:
#
# 1. $<CONFIG:Debug> evaluates to 1 when the Debug configuration is being generated,
# or 0 in all other cases.
# 2. $<$<CONFIG:Debug>:${PYTHON_DEBUG_LIB}> expands to ${PYTHON_DEBUG_LIB} when the Debug
# configuration is being generated, or nothing (literally) in all other cases.
# 3. $<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_LIB}> expands to ${PYTHON_RELEASE_LIB} when
# any configuration other than Debug is being generated, or nothing in all other cases.
# 4. The conditionals in 2 & 3 are mutually exclusive.
# 5. A logical expression with a conditional that evaluates to 0 yields no value at all.
#
# Due to 4 & 5 it's possible to concatenate 2 & 3 to obtain a single value specific to each
# build configuration. In this example the value will be ${PYTHON_DEBUG_LIB} when generating the
# Debug configuration, or ${PYTHON_RELEASE_LIB} when generating any other configuration.
# Note that it's imperative that there is no whitespace between the two expressions, otherwise
# CMake will insert a semicolon between the two.
set (PYTHON_EXECUTABLE $<$<CONFIG:Debug>:${PYTHON_DEBUG_EXE}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_EXE}>)
set (PYTHON_LIBRARY $<$<CONFIG:Debug>:${PYTHON_DEBUG_LIB}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_LIB}>)
set (PYTHON_DLL $<$<CONFIG:Debug>:${PYTHON_DEBUG_DLL}>$<$<NOT:$<CONFIG:Debug>>:${PYTHON_RELEASE_DLL}>)
file(TO_CMAKE_PATH "${PYTHON_HOME}/Include" PYTHON_INCLUDE_DIR)
if (NOT LLDB_RELOCATABLE_PYTHON)
add_definitions( -DLLDB_PYTHON_HOME="${PYTHON_HOME}" )
endif()
else()
message("Embedding Python on Windows without specifying a value for PYTHON_HOME is deprecated. Support for this will be dropped soon.")
if ("${PYTHON_INCLUDE_DIR}" STREQUAL "" OR "${PYTHON_LIBRARY}" STREQUAL "")
message("-- LLDB Embedded python disabled. Embedding python on Windows requires "
"manually specifying PYTHON_INCLUDE_DIR *and* PYTHON_LIBRARY")
set(LLDB_DISABLE_PYTHON 1)
endif()
if (NOT LLDB_RELOCATABLE_PYTHON)
add_definitions( -DLLDB_PYTHON_HOME="${PYTHON_HOME}" )
endif()
if (PYTHON_LIBRARY)
message("-- Found PythonLibs: ${PYTHON_LIBRARY}")
include_directories(${PYTHON_INCLUDE_DIR})
endif()
else()
find_package(PythonLibs REQUIRED)
endif()
if (PYTHON_INCLUDE_DIRS)
include_directories(${PYTHON_INCLUDE_DIRS})
endif()
endif()
if (LLDB_DISABLE_PYTHON)
unset(PYTHON_INCLUDE_DIR)
unset(PYTHON_INCLUDE_DIRS)
unset(PYTHON_LIBRARY)
add_definitions( -DLLDB_DISABLE_PYTHON )
endif()

View File

@ -48,9 +48,8 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
include(AddLLVM)
include(HandleLLVMOptions)
# Verify that we can find a Python 2 interpreter. Python 3 is unsupported.
if (PYTHON_EXECUTABLE STREQUAL "")
set(Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5)
set(Python_ADDITIONAL_VERSIONS 3.5 3.4 3.3 3.2 3.1 3.0 2.7 2.6 2.5)
include(FindPythonInterp)
if( NOT PYTHONINTERP_FOUND )
message(FATAL_ERROR