[lldb/CMake] Autodetect Python dependency
authorJonas Devlieghere <jonas@devlieghere.com>
Mon, 6 Jan 2020 16:49:36 +0000 (08:49 -0800)
committerJonas Devlieghere <jonas@devlieghere.com>
Mon, 6 Jan 2020 17:05:05 +0000 (09:05 -0800)
Python was the last remaining "optional" dependency for LLDB. This moves
the code to find Python into FindPythonInterpAndLibs using the same
principles as FindCursesAndPanel.

Differential revision: https://reviews.llvm.org/D72107

lldb/cmake/modules/FindPythonInterpAndLibs.cmake [new file with mode: 0644]
lldb/cmake/modules/LLDBConfig.cmake
lldb/lldb/cmake/modules/FindPythonInterpAndLibs.cmake [new file with mode: 0644]

diff --git a/lldb/cmake/modules/FindPythonInterpAndLibs.cmake b/lldb/cmake/modules/FindPythonInterpAndLibs.cmake
new file mode 100644 (file)
index 0000000..fcbf021
--- /dev/null
@@ -0,0 +1,51 @@
+#.rst:
+# FindPythonInterpAndLibs
+# -----------
+#
+# Find the python interpreter and libraries as a whole.
+
+if(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS AND PYTHON_EXECUTABLE)
+  set(PYTHONINTERPANDLIBS_FOUND TRUE)
+else()
+  if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
+    find_package(Python3 COMPONENTS Interpreter Development QUIET)
+    if (Python3_FOUND AND Python3_Interpreter_FOUND)
+      set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
+      set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
+      set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
+      mark_as_advanced(
+        PYTHON_LIBRARIES
+        PYTHON_INCLUDE_DIRS
+        PYTHON_EXECUTABLE)
+    endif()
+  else()
+    find_package(PythonInterp QUIET)
+    find_package(PythonLibs QUIET)
+    if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
+      if (NOT CMAKE_CROSSCOMPILING)
+        string(REPLACE "." ";" pythonlibs_version_list ${PYTHONLIBS_VERSION_STRING})
+        list(GET pythonlibs_version_list 0 pythonlibs_major)
+        list(GET pythonlibs_version_list 1 pythonlibs_minor)
+
+        # Ignore the patch version. Some versions of macOS report a different
+        # patch version for the system provided interpreter and libraries.
+        if (CMAKE_CROSSCOMPILING OR (PYTHON_VERSION_MAJOR VERSION_EQUAL pythonlibs_major AND
+            PYTHON_VERSION_MINOR VERSION_EQUAL pythonlibs_minor))
+          mark_as_advanced(
+            PYTHON_LIBRARIES
+            PYTHON_INCLUDE_DIRS
+            PYTHON_EXECUTABLE)
+        endif()
+      endif()
+    endif()
+  endif()
+
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(PythonInterpAndLibs
+                                    FOUND_VAR
+                                      PYTHONINTERPANDLIBS_FOUND
+                                    REQUIRED_VARS
+                                      PYTHON_LIBRARIES
+                                      PYTHON_INCLUDE_DIRS
+                                      PYTHON_EXECUTABLE)
+endif()
index 52b8257..12fb97f 100644 (file)
@@ -51,16 +51,8 @@ add_optional_dependency(LLDB_ENABLE_LIBEDIT "Enable editline support in LLDB" Li
 add_optional_dependency(LLDB_ENABLE_CURSES "Enable curses support in LLDB" CursesAndPanel CURSESANDPANEL_FOUND)
 add_optional_dependency(LLDB_ENABLE_LZMA "Enable LZMA compression support in LLDB" LibLZMA LIBLZMA_FOUND)
 add_optional_dependency(LLDB_ENABLE_LUA "Enable Lua scripting support in LLDB" Lua LUA_FOUND)
+add_optional_dependency(LLDB_ENABLE_PYTHON "Enable Python scripting support in LLDB" PythonInterpAndLibs PYTHONINTERPANDLIBS_FOUND)
 
-set(default_enable_python ON)
-
-if(CMAKE_SYSTEM_NAME MATCHES "Android")
-  set(default_enable_python OFF)
-elseif(IOS)
-  set(default_enable_python OFF)
-endif()
-
-option(LLDB_ENABLE_PYTHON "Enable Python scripting integration." ${default_enable_python})
 option(LLDB_RELOCATABLE_PYTHON "Use the PYTHONHOME environment variable to locate Python." OFF)
 option(LLDB_USE_SYSTEM_SIX "Use six.py shipped with system and do not install a copy of it" OFF)
 option(LLDB_USE_ENTITLEMENTS "When codesigning, use entitlements if available" ON)
@@ -142,48 +134,13 @@ if (LLDB_ENABLE_LIBEDIT)
 endif()
 
 if (LLDB_ENABLE_PYTHON)
-  if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
-    find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
-    if(Python3_VERSION VERSION_LESS 3.5)
-      message(SEND_ERROR "Python 3.5 or newer is required (found: ${Python3_VERSION}")
-    endif()
-    set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
-    include_directories(${Python3_INCLUDE_DIRS})
-
-    if (NOT LLDB_RELOCATABLE_PYTHON)
-      get_filename_component(PYTHON_HOME "${Python3_EXECUTABLE}" DIRECTORY)
-      file(TO_CMAKE_PATH "${PYTHON_HOME}" LLDB_PYTHON_HOME)
-    endif()
-  else()
-    find_package(PythonInterp REQUIRED)
-    find_package(PythonLibs REQUIRED)
-
-    if (NOT CMAKE_CROSSCOMPILING)
-      string(REPLACE "." ";" pythonlibs_version_list ${PYTHONLIBS_VERSION_STRING})
-      list(GET pythonlibs_version_list 0 pythonlibs_major)
-      list(GET pythonlibs_version_list 1 pythonlibs_minor)
-
-      # Ignore the patch version. Some versions of macOS report a different patch
-      # version for the system provided interpreter and libraries.
-      if (NOT PYTHON_VERSION_MAJOR VERSION_EQUAL pythonlibs_major OR
-          NOT PYTHON_VERSION_MINOR VERSION_EQUAL pythonlibs_minor)
-        message(FATAL_ERROR "Found incompatible Python interpreter (${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})"
-                            " and Python libraries (${pythonlibs_major}.${pythonlibs_minor})")
-      endif()
-    endif()
-
-    if (PYTHON_INCLUDE_DIR)
-      include_directories(${PYTHON_INCLUDE_DIR})
-    endif()
+  include_directories(${PYTHON_INCLUDE_DIRS})
+  if (NOT LLDB_RELOCATABLE_PYTHON)
+    get_filename_component(PYTHON_HOME "${PYTHON_EXECUTABLE}" DIRECTORY)
+    file(TO_CMAKE_PATH "${PYTHON_HOME}" LLDB_PYTHON_HOME)
   endif()
 endif()
 
-if (NOT LLDB_ENABLE_PYTHON)
-  unset(PYTHON_INCLUDE_DIR)
-  unset(PYTHON_LIBRARIES)
-  unset(PYTHON_EXECUTABLE)
-endif()
-
 if (LLVM_EXTERNAL_CLANG_SOURCE_DIR)
   include_directories(${LLVM_EXTERNAL_CLANG_SOURCE_DIR}/include)
 else ()
diff --git a/lldb/lldb/cmake/modules/FindPythonInterpAndLibs.cmake b/lldb/lldb/cmake/modules/FindPythonInterpAndLibs.cmake
new file mode 100644 (file)
index 0000000..90c902f
--- /dev/null
@@ -0,0 +1,51 @@
+#.rst:
+# FindPythonInterpndLibs
+# -----------
+#
+# Find the python interpreter and libraries as a whole.
+
+if(PYTHON_LIBRARIES AND PYTHON_INCLUDE_DIRS AND PYTHON_EXECUTABLE)
+  set(PYTHONINTERPANDLIBS_FOUND TRUE)
+else()
+  if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
+    find_package(Python3 COMPONENTS Interpreter Development QUIET)
+    if (Python3_FOUND AND Python3_Interpreter_FOUND)
+      set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
+      set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
+      set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
+      mark_as_advanced(
+        PYTHON_LIBRARIES
+        PYTHON_INCLUDE_DIRS
+        PYTHON_EXECUTABLE)
+    endif()
+  else()
+    find_package(PythonInterp QUIET)
+    find_package(PythonLibs QUIET)
+    if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
+      if (NOT CMAKE_CROSSCOMPILING)
+        string(REPLACE "." ";" pythonlibs_version_list ${PYTHONLIBS_VERSION_STRING})
+        list(GET pythonlibs_version_list 0 pythonlibs_major)
+        list(GET pythonlibs_version_list 1 pythonlibs_minor)
+
+        # Ignore the patch version. Some versions of macOS report a different
+        # patch version for the system provided interpreter and libraries.
+        if (CMAKE_CROSSCOMPILING OR (PYTHON_VERSION_MAJOR VERSION_EQUAL pythonlibs_major AND
+            PYTHON_VERSION_MINOR VERSION_EQUAL pythonlibs_minor))
+          mark_as_advanced(
+            PYTHON_LIBRARIES
+            PYTHON_INCLUDE_DIRS
+            PYTHON_EXECUTABLE)
+        endif()
+      endif()
+    endif()
+  endif()
+
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(PythonInterpAndLibs
+                                    FOUND_VAR
+                                      PYTHONINTERPANDLIBS_FOUND
+                                    REQUIRED_VARS
+                                      PYTHON_LIBRARIES
+                                      PYTHON_INCLUDE_DIRS
+                                      PYTHON_EXECUTABLE)
+endif()