[Lldb-commits] [lldb] bf03e17 - [Lldb/Lua] Generate Lua Bindings

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Sat Dec 21 11:31:56 PST 2019


Author: Jonas Devlieghere
Date: 2019-12-21T11:28:41-08:00
New Revision: bf03e17c570171c7a52117fe63ace89d58f328d5

URL: https://github.com/llvm/llvm-project/commit/bf03e17c570171c7a52117fe63ace89d58f328d5
DIFF: https://github.com/llvm/llvm-project/commit/bf03e17c570171c7a52117fe63ace89d58f328d5.diff

LOG: [Lldb/Lua] Generate Lua Bindings

This patch uses SWIG to generate the Lua bindings for the SB API. It
covers most of the API, but some methods require a type map similar to
Python.

Discussion on the mailing list:
http://lists.llvm.org/pipermail/lldb-dev/2019-December/015812.html

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

Added: 
    lldb/scripts/lldb_lua.swig
    lldb/test/Shell/ScriptInterpreter/Lua/bindings.test

Modified: 
    lldb/CMakeLists.txt
    lldb/scripts/CMakeLists.txt
    lldb/source/API/CMakeLists.txt
    lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
    lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
    lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt
index 3747c9896525..39e12b251623 100644
--- a/lldb/CMakeLists.txt
+++ b/lldb/CMakeLists.txt
@@ -50,10 +50,10 @@ if (LLDB_ENABLE_PYTHON)
   file(TO_CMAKE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH} LLDB_PYTHON_DEFAULT_RELATIVE_PATH)
   set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
     CACHE STRING "Path where Python modules are installed, relative to install prefix")
-
-  add_subdirectory(scripts)
 endif ()
 
+add_subdirectory(scripts)
+
 # We need the headers generated by instrinsics_gen before we can compile
 # any source file in LLDB as the imported Clang modules might include
 # some of these generated headers. This approach is copied from Clang's main

diff  --git a/lldb/scripts/CMakeLists.txt b/lldb/scripts/CMakeLists.txt
index 5b86956f2a9e..8fa4e5f78916 100644
--- a/lldb/scripts/CMakeLists.txt
+++ b/lldb/scripts/CMakeLists.txt
@@ -27,31 +27,58 @@ else()
   set(DARWIN_EXTRAS "")
 endif()
 
-add_custom_command(
-  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
-  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
-  DEPENDS ${SWIG_SOURCES}
-  DEPENDS ${SWIG_INTERFACES}
-  DEPENDS ${SWIG_HEADERS}
-  COMMAND ${SWIG_EXECUTABLE} 
-      -c++
-      -shadow
-      -python
-      -features autodoc
-      -threads
-      -I${LLDB_SOURCE_DIR}/include
-      -I${CMAKE_CURRENT_SOURCE_DIR}
-      -D__STDC_LIMIT_MACROS
-      -D__STDC_CONSTANT_MACROS
-      ${DARWIN_EXTRAS}
-      -outdir ${CMAKE_CURRENT_BINARY_DIR}
-      -o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
-      ${LLDB_SOURCE_DIR}/scripts/lldb.swig
-  VERBATIM
-  COMMENT "Builds LLDB Python wrapper")
-
-add_custom_target(swig_wrapper ALL DEPENDS
-  ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
-  ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
+set(SWIG_COMMON_FLAGS
+  -c++
+  -features autodoc
+  -I${LLDB_SOURCE_DIR}/include
+  -I${CMAKE_CURRENT_SOURCE_DIR}
+  -D__STDC_LIMIT_MACROS
+  -D__STDC_CONSTANT_MACROS
+  ${DARWIN_EXTRAS}
+  -outdir ${CMAKE_CURRENT_BINARY_DIR}
 )
 
+
+if (LLDB_ENABLE_PYTHON)
+  add_custom_command(
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
+    DEPENDS ${SWIG_SOURCES}
+    DEPENDS ${SWIG_INTERFACES}
+    DEPENDS ${SWIG_HEADERS}
+    COMMAND ${SWIG_EXECUTABLE}
+        ${SWIG_COMMON_FLAGS}
+        -c++
+        -shadow
+        -python
+        -threads
+        -o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
+        ${LLDB_SOURCE_DIR}/scripts/lldb.swig
+    VERBATIM
+    COMMENT "Builds LLDB Python wrapper")
+
+  add_custom_target(swig_wrapper ALL DEPENDS
+    ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
+    ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
+  )
+endif()
+
+if (LLDB_ENABLE_LUA)
+  add_custom_command(
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
+    DEPENDS ${SWIG_SOURCES}
+    DEPENDS ${SWIG_INTERFACES}
+    DEPENDS ${SWIG_HEADERS}
+    COMMAND ${SWIG_EXECUTABLE}
+        ${SWIG_COMMON_FLAGS}
+        -lua
+        -w503
+        -o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
+        ${LLDB_SOURCE_DIR}/scripts/lldb_lua.swig
+    VERBATIM
+    COMMENT "Builds LLDB Lua wrapper")
+
+  add_custom_target(swig_wrapper_lua ALL DEPENDS
+    ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
+  )
+endif()

diff  --git a/lldb/scripts/lldb_lua.swig b/lldb/scripts/lldb_lua.swig
new file mode 100644
index 000000000000..85edefff76f2
--- /dev/null
+++ b/lldb/scripts/lldb_lua.swig
@@ -0,0 +1,18 @@
+/*
+   lldb.swig
+
+   This is the input file for SWIG, to create the appropriate C++ wrappers and
+   functions for various scripting languages, to enable them to call the
+   liblldb Script Bridge functions.
+*/
+
+%module lldb
+
+%include "./headers.swig"
+
+%{
+using namespace lldb_private;
+using namespace lldb;
+%}
+
+%include "./interfaces.swig"

diff  --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt
index 1a99a26dfc4f..7b9d4cb61e25 100644
--- a/lldb/source/API/CMakeLists.txt
+++ b/lldb/source/API/CMakeLists.txt
@@ -9,6 +9,11 @@ if(LLDB_ENABLE_PYTHON)
   set(lldb_python_wrapper ${lldb_scripts_dir}/LLDBWrapPython.cpp)
 endif()
 
+if(LLDB_ENABLE_LUA)
+  get_target_property(lldb_scripts_dir swig_wrapper_lua BINARY_DIR)
+  set(lldb_lua_wrapper ${lldb_scripts_dir}/LLDBWrapLua.cpp)
+endif()
+
 if(LLDB_BUILD_FRAMEWORK)
   set(option_install_prefix INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR})
   set(option_framework FRAMEWORK)
@@ -85,6 +90,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
   SBUnixSignals.cpp
   SystemInitializerFull.cpp
   ${lldb_python_wrapper}
+  ${lldb_lua_wrapper}
 
   LINK_LIBS
     lldbBase
@@ -130,6 +136,19 @@ if(lldb_python_wrapper)
   endif ()
 endif()
 
+if(lldb_lua_wrapper)
+  add_dependencies(liblldb swig_wrapper_lua)
+  target_include_directories(liblldb PRIVATE ${LUA_INCLUDE_DIR})
+
+  if (MSVC)
+    set_property(SOURCE ${lldb_lua_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " /W0")
+  else()
+    set_property(SOURCE ${lldb_lua_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " -w")
+  endif()
+
+  set_source_files_properties(${lldb_lua_wrapper} PROPERTIES GENERATED ON)
+endif()
+
 set_target_properties(liblldb
   PROPERTIES
   VERSION ${LLDB_VERSION}

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt b/lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
index 498bd9783951..f5c62ee3a54f 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -1,3 +1,5 @@
+find_package(Lua REQUIRED)
+
 add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
   Lua.cpp
   ScriptInterpreterLua.cpp

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
index 50b7ade4dc46..073e7e22e7d6 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
@@ -16,11 +16,16 @@
 
 namespace lldb_private {
 
+extern "C" {
+int luaopen_lldb(lua_State *L);
+}
+
 class Lua {
 public:
   Lua() : m_lua_state(luaL_newstate()) {
     assert(m_lua_state);
     luaL_openlibs(m_lua_state);
+    luaopen_lldb(m_lua_state);
   }
 
   ~Lua() {

diff  --git a/lldb/test/Shell/ScriptInterpreter/Lua/bindings.test b/lldb/test/Shell/ScriptInterpreter/Lua/bindings.test
new file mode 100644
index 000000000000..00e00d434793
--- /dev/null
+++ b/lldb/test/Shell/ScriptInterpreter/Lua/bindings.test
@@ -0,0 +1,6 @@
+# REQUIRES: lua
+# RUN: cat %s | %lldb --script-language lua 2>&1 | FileCheck %s
+script
+debugger = lldb.SBDebugger.Create()
+print(string.format("debugger is valid: %s", debugger:IsValid()))
+# CHECK: debugger is valid: true

diff  --git a/lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp b/lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
index fa8e61a69b3f..464babcb290a 100644
--- a/lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
+++ b/lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp
@@ -11,6 +11,8 @@
 
 using namespace lldb_private;
 
+extern "C" int luaopen_lldb(lua_State *L) { return 0; }
+
 TEST(LuaTest, RunValid) {
   Lua lua;
   llvm::Error error = lua.Run("foo = 1");


        


More information about the lldb-commits mailing list