xref: /llvm-project/lldb/cmake/modules/FindPythonAndSwig.cmake (revision 3ce0dbb718c9df123fd1cb87623aa31b3376fb61)
1#.rst:
2# FindPythonAndSwig
3# -----------
4#
5# Find the python interpreter and libraries as a whole.
6
7macro(FindPython3)
8  # Use PYTHON_HOME as a hint to find Python 3.
9  set(Python3_ROOT_DIR "${PYTHON_HOME}")
10  find_package(Python3 COMPONENTS Interpreter Development)
11  if(Python3_FOUND AND Python3_Interpreter_FOUND)
12
13    # The install name for the Python 3 framework in Xcode is relative to
14    # the framework's location and not the dylib itself.
15    #
16    #   @rpath/Python3.framework/Versions/3.x/Python3
17    #
18    # This means that we need to compute the path to the Python3.framework
19    # and use that as the RPATH instead of the usual dylib's directory.
20    #
21    # The check below shouldn't match Homebrew's Python framework as it is
22    # called Python.framework instead of Python3.framework.
23    if (APPLE AND Python3_LIBRARIES MATCHES "Python3.framework")
24      string(FIND "${Python3_LIBRARIES}" "Python3.framework" python_framework_pos)
25      string(SUBSTRING "${Python3_LIBRARIES}" "0" ${python_framework_pos} Python3_RPATH)
26    endif()
27
28    set(PYTHON3_FOUND TRUE)
29    mark_as_advanced(
30      Python3_LIBRARIES
31      Python3_INCLUDE_DIRS
32      Python3_EXECUTABLE
33      Python3_RPATH)
34  endif()
35endmacro()
36
37if(Python3_LIBRARIES AND Python3_INCLUDE_DIRS AND Python3_EXECUTABLE AND LLDB_ENABLE_SWIG)
38  set(PYTHONANDSWIG_FOUND TRUE)
39else()
40  if (LLDB_ENABLE_SWIG)
41    FindPython3()
42  else()
43    message(STATUS "SWIG 4 or later is required for Python support in LLDB but could not be found")
44  endif()
45
46  get_property(MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
47  if ("${Python3_VERSION}" VERSION_GREATER_EQUAL "3.7" AND
48      "${SWIG_VERSION}" VERSION_LESS "4.0" AND WIN32 AND (
49      ${MULTI_CONFIG} OR (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")))
50    # Technically this can happen with non-Windows builds too, but we are not
51    # able to detect whether Python was built with assertions, and only Windows
52    # has the requirement that Debug LLDB must use Debug Python.
53    message(WARNING "Debug builds of LLDB are likely to be unusable due to "
54      "<https://github.com/swig/swig/issues/1321>. Please use SWIG >= 4.0.")
55  endif()
56
57  include(FindPackageHandleStandardArgs)
58  find_package_handle_standard_args(PythonAndSwig
59                                    FOUND_VAR
60                                      PYTHONANDSWIG_FOUND
61                                    REQUIRED_VARS
62                                      Python3_LIBRARIES
63                                      Python3_INCLUDE_DIRS
64                                      Python3_EXECUTABLE
65                                      LLDB_ENABLE_SWIG)
66endif()
67
68set(LLDB_RECOMMENDED_PYTHON "3.8")
69if(PYTHONANDSWIG_FOUND AND "${Python3_VERSION}" VERSION_LESS "${LLDB_RECOMMENDED_PYTHON}")
70  message(WARNING "Using Python ${Python3_VERSION}. ${LLDB_RECOMMENDED_PYTHON} "
71                  "is recommended and will be required from LLDB 21.")
72endif()