[lldb] Extract CMake logic to add SWIG wrapper into helper function (#183203)

Extract the CMake logic to add SWIG wrapper into helper function defined
in the bindings directly. This avoid code duplication between Python and
Lua.

The function is parameterized in its target, making it possible to add
the wrapper to a different target, for example the respective script
interpreter plugin when building dynamic plugins.
This commit is contained in:
Jonas Devlieghere
2026-02-24 19:56:16 -08:00
committed by GitHub
parent 2ff512b967
commit ba34cbad41
4 changed files with 44 additions and 53 deletions

View File

@@ -50,6 +50,16 @@ function(create_relative_symlink swig_target dest_file output_dir output_name)
WORKING_DIRECTORY ${output_dir})
endfunction()
function(add_swig_wrapper target wrapper)
if (MSVC)
set_property(SOURCE ${wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " /W0")
else()
set_property(SOURCE ${wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " -w")
endif()
set_source_files_properties(${wrapper} PROPERTIES GENERATED ON)
target_sources(${target} PRIVATE ${wrapper})
endfunction()
if (LLDB_ENABLE_PYTHON)
add_subdirectory(python)
endif()

View File

@@ -66,3 +66,11 @@ function(finish_swig_lua swig_target lldb_lua_bindings_dir lldb_lua_target_dir)
DEPENDS ${lldb_lua_library_target})
endif()
endfunction()
function(add_lua_wrapper target)
get_target_property(lua_bindings_dir swig_wrapper_lua BINARY_DIR)
set(lldb_lua_wrapper ${lua_bindings_dir}/LLDBWrapLua.cpp)
add_dependencies(${target} swig_wrapper_lua)
add_swig_wrapper(${target} ${lldb_lua_wrapper})
endfunction()

View File

@@ -202,3 +202,25 @@ function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_tar
COMMENT "Copying Python DLL to LLDB binaries directory.")
endif()
endfunction()
function(add_python_wrapper target)
get_target_property(python_bindings_dir swig_wrapper_python BINARY_DIR)
set(lldb_python_wrapper ${python_bindings_dir}/LLDBWrapPython.cpp)
add_dependencies(${target} swig_wrapper_python)
add_swig_wrapper(${target} ${lldb_python_wrapper})
# lib/pythonX.Y/dist-packages/lldb/_lldb.so is a symlink to lib/liblldb.so,
# which depends on lib/libLLVM*.so (BUILD_SHARED_LIBS) or lib/libLLVM-10git.so
# (LLVM_LINK_LLVM_DYLIB). Add an additional rpath $ORIGIN/../../../../lib so
# that _lldb.so can be loaded from Python.
if(LLDB_ENABLE_PYTHON AND (BUILD_SHARED_LIBS OR LLVM_LINK_LLVM_DYLIB) AND UNIX AND NOT APPLE)
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "\$ORIGIN/../../../../lib${LLVM_LIBDIR_SUFFIX}")
endif()
if(Python3_RPATH)
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${Python3_RPATH}")
set_property(TARGET ${target} APPEND PROPERTY BUILD_RPATH "${Python3_RPATH}")
endif()
endfunction()

View File

@@ -11,13 +11,6 @@ if(LLDB_ENABLE_PYTHON)
# inside an extern "C" block.
remove_module_flags()
endif()
get_target_property(python_bindings_dir swig_wrapper_python BINARY_DIR)
set(lldb_python_wrapper ${python_bindings_dir}/LLDBWrapPython.cpp)
endif()
if(LLDB_ENABLE_LUA)
get_target_property(lua_bindings_dir swig_wrapper_lua BINARY_DIR)
set(lldb_lua_wrapper ${lua_bindings_dir}/LLDBWrapLua.cpp)
endif()
# Generate SBLanguages.h from Dwarf.def.
@@ -123,8 +116,6 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBWatchpoint.cpp
SBWatchpointOptions.cpp
SystemInitializerFull.cpp
${lldb_python_wrapper}
${lldb_lua_wrapper}
ADDITIONAL_HEADER_DIRS
${LLDB_INCLUDE_DIR}/lldb/API
@@ -151,52 +142,12 @@ add_lldb_library(liblldb SHARED ${option_framework}
${option_install_prefix}
)
# lib/pythonX.Y/dist-packages/lldb/_lldb.so is a symlink to lib/liblldb.so,
# which depends on lib/libLLVM*.so (BUILD_SHARED_LIBS) or lib/libLLVM-10git.so
# (LLVM_LINK_LLVM_DYLIB). Add an additional rpath $ORIGIN/../../../../lib so
# that _lldb.so can be loaded from Python.
if(LLDB_ENABLE_PYTHON AND (BUILD_SHARED_LIBS OR LLVM_LINK_LLVM_DYLIB) AND UNIX AND NOT APPLE)
set_property(TARGET liblldb APPEND PROPERTY INSTALL_RPATH "\$ORIGIN/../../../../lib${LLVM_LIBDIR_SUFFIX}")
endif()
if(Python3_RPATH)
set_property(TARGET liblldb APPEND PROPERTY INSTALL_RPATH "${Python3_RPATH}")
set_property(TARGET liblldb APPEND PROPERTY BUILD_RPATH "${Python3_RPATH}")
endif()
if(LLDB_ENABLE_PYTHON)
add_dependencies(liblldb swig_wrapper_python)
if (MSVC)
set_property(SOURCE ${lldb_python_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " /W0")
else()
set_property(SOURCE ${lldb_python_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " -w")
endif()
set_source_files_properties(${lldb_python_wrapper} PROPERTIES GENERATED ON)
if (CLANG_CL)
set_property(SOURCE ${lldb_python_wrapper} APPEND_STRING
PROPERTY COMPILE_FLAGS " -Wno-unused-function")
endif()
if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND
NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
set_property(SOURCE ${lldb_python_wrapper} APPEND_STRING
PROPERTY COMPILE_FLAGS " -Wno-sequence-point -Wno-cast-qual")
endif ()
if (LLDB_ENABLE_PYTHON)
add_python_wrapper(liblldb)
endif()
if(LLDB_ENABLE_LUA)
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)
add_lua_wrapper(liblldb)
endif()
set_target_properties(liblldb
@@ -342,7 +293,7 @@ foreach(header
add_custom_command(
OUTPUT ${staged_header}
COMMAND ${copy_command}
DEPENDS ${header}
DEPENDS ${header}
COMMENT "LLDB headers: stage LLDB headers in include directory")
list(APPEND lldb_staged_headers ${staged_header})