From bf03e17c570171c7a52117fe63ace89d58f328d5 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Sun, 8 Dec 2019 15:32:57 -0800 Subject: [PATCH] [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 --- lldb/CMakeLists.txt | 4 +- lldb/scripts/CMakeLists.txt | 79 +++++++++++++++------- lldb/scripts/lldb_lua.swig | 18 +++++ lldb/source/API/CMakeLists.txt | 19 ++++++ .../Plugins/ScriptInterpreter/Lua/CMakeLists.txt | 2 + lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h | 5 ++ .../test/Shell/ScriptInterpreter/Lua/bindings.test | 6 ++ lldb/unittests/ScriptInterpreter/Lua/LuaTests.cpp | 2 + 8 files changed, 107 insertions(+), 28 deletions(-) create mode 100644 lldb/scripts/lldb_lua.swig create mode 100644 lldb/test/Shell/ScriptInterpreter/Lua/bindings.test diff --git a/lldb/CMakeLists.txt b/lldb/CMakeLists.txt index 3747c98..39e12b2 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 5b86956..8fa4e5f 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 0000000..85edeff --- /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 1a99a26..7b9d4cb 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 498bd97..f5c62ee 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 50b7ade..073e7e2 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 0000000..00e00d4 --- /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 fa8e61a..464babc 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"); -- 2.7.4