Skip to content

[lldb] Add a CMake option to build agains the Python limited API #152034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JDevlieghere
Copy link
Member

This adds a CMake option (which defaults to OFF) to force building against the Python limited API. This makes iterating on #151617 easier and eventually will prevent us from regressing this configuration.

This adds a CMake option (which defaults to OFF) to force building
against the Python limited API. This makes iterating on llvm#151617 easier
and eventually will prevent us from regressing this configuration.
@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

This adds a CMake option (which defaults to OFF) to force building against the Python limited API. This makes iterating on #151617 easier and eventually will prevent us from regressing this configuration.


Full diff: https://github.com/llvm/llvm-project/pull/152034.diff

3 Files Affected:

  • (modified) lldb/cmake/modules/LLDBConfig.cmake (+1)
  • (modified) lldb/include/lldb/Host/Config.h.cmake (+2)
  • (modified) lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h (+9-1)
diff --git a/lldb/cmake/modules/LLDBConfig.cmake b/lldb/cmake/modules/LLDBConfig.cmake
index fc84e581cc188..a9679d63e010d 100644
--- a/lldb/cmake/modules/LLDBConfig.cmake
+++ b/lldb/cmake/modules/LLDBConfig.cmake
@@ -68,6 +68,7 @@ add_optional_dependency(LLDB_ENABLE_FBSDVMCORE "Enable libfbsdvmcore support in
 option(LLDB_USE_ENTITLEMENTS "When codesigning, use entitlements if available" ON)
 option(LLDB_BUILD_FRAMEWORK "Build LLDB.framework (Darwin only)" OFF)
 option(LLDB_ENABLE_PROTOCOL_SERVERS "Enable protocol servers (e.g. MCP) in LLDB" ON)
+option(LLDB_ENABLE_PYTHON_LIMITED_API "Force LLDB to only use the Python Limited API (requires SWIG 4.2 or later)" OFF)
 option(LLDB_NO_INSTALL_DEFAULT_RPATH "Disable default RPATH settings in binaries" OFF)
 option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver for testing (Darwin only)." OFF)
 option(LLDB_SKIP_STRIP "Whether to skip stripping of binaries when installing lldb." OFF)
diff --git a/lldb/include/lldb/Host/Config.h.cmake b/lldb/include/lldb/Host/Config.h.cmake
index 46e9a95781c32..79eaf88216450 100644
--- a/lldb/include/lldb/Host/Config.h.cmake
+++ b/lldb/include/lldb/Host/Config.h.cmake
@@ -45,6 +45,8 @@
 
 #cmakedefine01 LLDB_ENABLE_PYTHON
 
+#cmakedefine01 LLDB_ENABLE_PYTHON_LIMITED_API
+
 #cmakedefine01 LLDB_ENABLE_FBSDVMCORE
 
 #cmakedefine01 LLDB_EMBED_PYTHON_HOME
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h b/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h
index 4a6c11dba02e8..9b4a1dd2cecb2 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h
@@ -45,12 +45,20 @@ static llvm::Expected<bool> *g_fcxx_modules_workaround [[maybe_unused]];
 #include <locale>
 #endif
 
+#define LLDB_MINIMUM_PYTHON_VERSION 0x03080000
+
+#if LLDB_ENABLE_PYTHON_LIMITED_API
+// If defined, LLDB will be ABI-compatible with all Python 3 releases from the
+// specified one onward, and can use Limited API introduced up to that version.
+#define Py_LIMITED_API LLDB_MINIMUM_PYTHON_VERSION
+#endif
+
 // Include python for non windows machines
 #include <Python.h>
 
 // Provide a meaningful diagnostic error if someone tries to compile this file
 // with a version of Python we don't support.
-static_assert(PY_VERSION_HEX >= 0x03080000,
+static_assert(PY_VERSION_HEX >= LLDB_MINIMUM_PYTHON_VERSION,
               "LLDB requires at least Python 3.8");
 #endif
 

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this will be:

  • Can't enable it now because we use non-limited API things, but will be used locally for fixing that.
  • Once that's done, enable it where we have SWIG 4.2
  • Eventually require SWIG >= 4.2, default to ON
  • Forget the option exists for about a decade
  • Finally remove it :)

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants