From ba34cbad4132148447d5593127bbafec7b6f13b1 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Tue, 24 Feb 2026 19:56:16 -0800 Subject: [PATCH] [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. --- lldb/bindings/CMakeLists.txt | 10 +++++ lldb/bindings/lua/CMakeLists.txt | 8 ++++ lldb/bindings/python/CMakeLists.txt | 22 +++++++++++ lldb/source/API/CMakeLists.txt | 57 ++--------------------------- 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/lldb/bindings/CMakeLists.txt b/lldb/bindings/CMakeLists.txt index bec694e43bd7..6b81a15960ea 100644 --- a/lldb/bindings/CMakeLists.txt +++ b/lldb/bindings/CMakeLists.txt @@ -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() diff --git a/lldb/bindings/lua/CMakeLists.txt b/lldb/bindings/lua/CMakeLists.txt index 4a110f7829b0..677a07044c2a 100644 --- a/lldb/bindings/lua/CMakeLists.txt +++ b/lldb/bindings/lua/CMakeLists.txt @@ -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() diff --git a/lldb/bindings/python/CMakeLists.txt b/lldb/bindings/python/CMakeLists.txt index 2ebcf5a8e7ac..04457dfd32e3 100644 --- a/lldb/bindings/python/CMakeLists.txt +++ b/lldb/bindings/python/CMakeLists.txt @@ -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() diff --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt index 22f920a0368d..d921e52aa519 100644 --- a/lldb/source/API/CMakeLists.txt +++ b/lldb/source/API/CMakeLists.txt @@ -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})