Skip to content

[lldb][RPC] Upstream lldb-rpc-gen tool #138031

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# For this test, we're not checking any specific output from a generated file,
# but we do need a file to pass into lldb-rpc-gen so use SBAddress.h from source.
RUN: %lldb-rpc-gen --output-dir=%t %S/../../../../../include/lldb/API/SBAddress.h

RUN: ls %t | FileCheck %s

# We're just making sure that the tool emits the class names,
# methods and skipped methods file in the output directory.
CHECK: SBAPI.def
CHECK: SBClasses.def
CHECK: SkippedMethods.txt
6 changes: 6 additions & 0 deletions lldb/test/Shell/helper/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ def use_lldb_substitutions(config):
extra_args=["platform"],
unresolved="ignore",
),
ToolSubst(
"%lldb-rpc-gen",
command=FindTool("lldb-rpc-gen"),
extra_args=['--extra-arg="-resource-dir=' + config.clang_resource_dir + '"'],
unresolved="ignore",
),
"lldb-test",
"lldb-dap",
ToolSubst(
Expand Down
1 change: 1 addition & 0 deletions lldb/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_subdirectory(lldb-fuzzer EXCLUDE_FROM_ALL)

add_lldb_tool_subdirectory(lldb-instr)
add_lldb_tool_subdirectory(lldb-dap)
add_lldb_tool_subdirectory(lldb-rpc)

if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
add_lldb_tool_subdirectory(darwin-debug)
Expand Down
22 changes: 22 additions & 0 deletions lldb/tools/lldb-rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
include(CheckCXXCompilerFlag)
# Umbrella target for the entire framework is a default target.
add_custom_target(lldb-rpc ALL)

if(LLDB_CODESIGN_IDENTITY)
# Use explicit LLDB identity
set(LLVM_CODESIGNING_IDENTITY ${LLDB_CODESIGN_IDENTITY})
else()
# Use explicit LLVM identity or default to ad-hoc signing if empty
if(NOT LLVM_CODESIGNING_IDENTITY)
set(LLVM_CODESIGNING_IDENTITY -)
endif()
endif()

# LLDBRPCGeneration.cmake needs the LLDB_RPC_GEN_EXE variable
# which gets defined in the lldb-rpc-gen folder, so we're adding
# this folder before we add that file.
add_lldb_tool_subdirectory(lldb-rpc-gen)
include(${CMAKE_CURRENT_SOURCE_DIR}/LLDBRPCGeneration.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/LLDBRPCHeaders.cmake)

add_dependencies(lldb-rpc lldb-rpc-generate-sources liblldbrpc-headers)
58 changes: 58 additions & 0 deletions lldb/tools/lldb-rpc/LLDBRPCGeneration.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
if (NOT DEFINED LLDB_RPC_GEN_EXE)
message(FATAL_ERROR
"Unable to generate lldb-rpc sources because LLDB_RPC_GEN_EXE is not
defined. If you are cross-compiling, please build lldb-rpc-gen for your host
platform.")
endif()
set(lldb_rpc_generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")

file(GLOB api_headers ${LLDB_SOURCE_DIR}/include/lldb/API/SB*.h)
# We don't generate SBCommunication
list(REMOVE_ITEM api_headers ${LLDB_SOURCE_DIR}/include/lldb/API/SBCommunication.h)
# SBDefines.h is mostly definitions and forward declarations, nothing to
# generate.
list(REMOVE_ITEM api_headers ${LLDB_SOURCE_DIR}/include/lldb/API/SBDefines.h)

# Generate the list of byproducts. Note that we cannot just glob the files in
# the directory with the generated sources because BYPRODUCTS needs to be known
# at configure time but the files are generated at build time.
set(lldb_rpc_gen_byproducts
${lldb_rpc_generated_dir}/SBClasses.def
${lldb_rpc_generated_dir}/SBAPI.def
${lldb_rpc_generated_dir}/lldb.py
${lldb_rpc_server_generated_source_dir}/SBAPI.h
)

# Make sure that the clang-resource-dir is set correctly or else the tool will
# fail to run. This is only needed when we do a standalone build.
set(clang_resource_dir_arg)
if (LLDB_BUILT_STANDALONE)
if (TARGET clang-resource-headers)
set(clang_resource_headers_dir
$<TARGET_PROPERTY:clang-resource-headers,INTERFACE_INCLUDE_DIRECTORIES>)
set(clang_resource_dir_arg --extra-arg="-resource-dir=${clang_resource_headers_dir}/..")
else()
set(clang_resource_dir_arg --extra-arg="-resource-dir=${LLDB_EXTERNAL_CLANG_RESOURCE_DIR}")
endif()
endif()

add_custom_command(OUTPUT ${lldb_rpc_gen_byproducts}
COMMAND ${CMAKE_COMMAND} -E make_directory
${lldb_rpc_generated_dir}

COMMAND ${LLDB_RPC_GEN_EXE}
-p ${CMAKE_BINARY_DIR}
--output-dir=${lldb_rpc_generated_dir}
${clang_resource_dir_arg}
--extra-arg="-USWIG"
${api_headers}

DEPENDS ${LLDB_RPC_GEN_EXE} ${api_headers}
COMMENT "Generating sources for lldb-rpc-server..."
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_custom_target(lldb-rpc-generate-sources
DEPENDS
${lldb_rpc_gen_byproducts}
lldb-sbapi-dwarf-enums)
101 changes: 101 additions & 0 deletions lldb/tools/lldb-rpc/LLDBRPCHeaders.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
set(derived_headers_location "${CMAKE_CURRENT_BINARY_DIR}/DerivedHeaders")

# Obtain the original headers from their staged location in the build directory.
set(original_headers_location "${CMAKE_BINARY_DIR}/include/lldb")
set(headers_to_process
SBDefines.h
lldb-defines.h
lldb-enumerations.h
lldb-types.h
)

file(MAKE_DIRECTORY ${derived_headers_location})

# Take the original headers and convert them RPC as necessary using the conversion script.
set(original_headers)
set(derived_headers)
foreach(header ${headers_to_process})
set(original_header "${original_headers_location}/${header}")

get_filename_component(header_filename ${header} NAME)
string(REPLACE "lldb-" "lldb-rpc-" rpc_header_filename "${header_filename}")
set(derived_header "${derived_headers_location}/${rpc_header_filename}")

list(APPEND original_headers "${original_header}")
list(APPEND derived_headers "${derived_header}")
add_custom_command(OUTPUT ${derived_header}
COMMAND ${Python3_EXECUTABLE} ${LLDB_SOURCE_DIR}/scripts/convert-lldb-header-to-rpc-header.py
${original_header} ${derived_header}
DEPENDS ${original_header}

COMMENT "Creating ${derived_header}"
)
endforeach()

# Do the same thing for any header files that were autogenerated.
set(generated_headers_to_process
API/SBLanguages.h
)
foreach(header ${generated_headers_to_process})
set(original_header "${LLDB_OBJ_DIR}/include/lldb/${header}")

get_filename_component(header_filename ${header} NAME)
string(REPLACE "lldb-" "lldb-rpc-" rpc_header_filename "${header_filename}")
set(derived_header "${derived_headers_location}/${rpc_header_filename}")

list(APPEND original_headers "${original_header}")
list(APPEND derived_headers "${derived_header}")
add_custom_command(OUTPUT ${derived_header}
COMMAND ${CMAKE_COMMAND} -E copy ${original_header} ${derived_header}
COMMAND ${Python3_EXECUTABLE} ${LLDB_SOURCE_DIR}/scripts/convert-lldb-header-to-rpc-header.py
${original_header} ${derived_header}
DEPENDS lldb-sbapi-dwarf-enums

COMMENT "Creating ${derived_header}"
)
endforeach()

add_custom_target(copy-aux-rpc-headers DEPENDS ${derived_headers})
add_dependencies(copy-aux-rpc-headers liblldb-header-staging)

list(APPEND public_headers
${derived_headers_location}/SBDefines.h
${derived_headers_location}/SBLanguages.h
${derived_headers_location}/lldb-rpc-enumerations.h
${derived_headers_location}/lldb-rpc-types.h
${derived_headers_location}/lldb-rpc-defines.h
)

# Collect and preprocess headers for the framework bundle
set(version_header
${derived_headers_location}/lldb-rpc-defines.h
)

function(FixIncludePaths in subfolder out)
get_filename_component(base_name ${in} NAME)
set(parked_header ${CMAKE_CURRENT_BINARY_DIR}/ParkedHeaders/${subfolder}/${base_name})
set(${out} ${parked_header} PARENT_SCOPE)
find_program(unifdef_EXECUTABLE unifdef)

add_custom_command(OUTPUT ${parked_header}
COMMAND ${LLDB_SOURCE_DIR}/scripts/framework-header-fix.py
-f lldb_rpc -i ${in} -o ${parked_header} -p ${unifdef_EXECUTABLE} USWIG
DEPENDS ${in}
COMMENT "Fixing includes in ${in}"
)
endfunction()

set(preprocessed_headers)

# Apply include-paths fix and any version fix on all headers and park them.
foreach(source_header ${public_headers})
FixIncludePaths(${source_header} Headers parked_header)
list(APPEND preprocessed_headers ${parked_header})
endforeach()

# Wrap header preprocessing in a target, so liblldbrpc can depend on.
add_custom_target(liblldbrpc-headers DEPENDS ${preprocessed_headers})
add_dependencies(liblldbrpc-headers copy-aux-rpc-headers liblldb-header-staging)
set_target_properties(liblldbrpc-headers PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ParkedHeaders
)
21 changes: 21 additions & 0 deletions lldb/tools/lldb-rpc/lldb-rpc-gen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
add_lldb_tool(lldb-rpc-gen
RPCCommon.cpp
lldb-rpc-gen.cpp

CLANG_LIBS
clangAST
clangBasic
clangCodeGen
clangFrontend
clangLex
clangRewrite
clangSerialization
clangTooling

LINK_COMPONENTS
Support
)

if (NOT DEFINED LLDB_RPC_GEN_EXE)
set(LLDB_RPC_GEN_EXE $<TARGET_FILE:lldb-rpc-gen> CACHE STRING "Executable that generates lldb-rpc-server")
endif()
Loading