llvm-project/lldb/tools/lldb-rpc/LLDBRPCGeneration.cmake
Chelsea Cassanova 68c8c8ceeb
Reland "[lldb][RPC] Upstream lldb-rpc-gen tool" (#146969)" Attempt 2 (#148996)
Second attempt at relanding the lldb-rpc-gen tool. This should fix 2
issues:

- An assert that was hitting when building on Linux. The assert would
hit in the server source emitter, specifically when attemping to
determine the storage size for a return type is that is a pointer, but
isn't a const char *, const char ** or void pointer.

The assert would hit when attempting to generate
SBAttachInfo::GetProcessPluginName, which returns a const char *
(meaning it shouldn't have been in the code block for the assert at
all). The reason that it was hitting the assert when generating this
function is that lldb_rpc_gen::TypeIsConstCharPtr was returning false
for this function even though it did return a const char *. This was
happening because when checking the return type for a const char *,
TypeIsConstCharPtr would only check that the underlying type was a
signed char. This failed on Linux (but was fine on Darwin), as the
underlying type also needs to be checked for being an unsigned char.

- Cross compiling support

The build for lldb-rpc-gen had no support for cross compiling and as
such, the sources generated for lldb-rpc-gen would get compiled too
early in phase 2 when cross compiling, before the Clang toolchain was
built and this led to an error when trying to include stdlib files. This
reland splits this build into 2 by building the tool first and then
compiling the sources in the second stage of the cross-compiled build.

Original PR Description:
This commit upstreams the lldb-rpc-gen tool, a ClangTool that generates
the LLDB RPC client and server interfaces. This tool, as well as LLDB
RPC itself is built by default. If it needs to be disabled, put
-DLLDB_BUILD_LLDBRPC=OFF in your CMake invocation.

https://discourse.llvm.org/t/rfc-upstreaming-lldb-rpc/85804

Original PR Link:
github.com/llvm/llvm-project/pull/138031
2025-07-23 18:10:16 -07:00

81 lines
3.0 KiB
CMake

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")
set(lldb_rpc_server_generated_source_dir "${lldb_rpc_generated_dir}/server")
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
)
set(lldb_rpc_gen_server_impl_files)
foreach(path ${api_headers})
get_filename_component(filename_no_ext ${path} NAME_WLE)
set(server_header_file "Server_${filename_no_ext}.h")
list(APPEND lldb_rpc_gen_byproducts "${lldb_rpc_server_generated_source_dir}/${server_header_file}")
set(server_impl_file "Server_${filename_no_ext}.cpp")
list(APPEND lldb_rpc_gen_byproducts "${lldb_rpc_server_generated_source_dir}/${server_impl_file}")
list(APPEND lldb_rpc_gen_server_impl_files "${lldb_rpc_server_generated_source_dir}/${server_impl_file}")
endforeach()
# 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 (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()
set(sysroot_arg)
if (DEFINED TOOLCHAIN_TARGET_SYSROOTFS)
set(sysroot_arg --extra-arg="-resource-dir=${TOOLCHAIN_TARGET_SYSROOTFS}")
endif()
add_custom_command(OUTPUT ${lldb_rpc_gen_byproducts}
COMMAND ${CMAKE_COMMAND} -E make_directory
${lldb_rpc_generated_dir}
COMMAND ${CMAKE_COMMAND} -E make_directory
${lldb_rpc_server_generated_source_dir}
COMMAND ${LLDB_RPC_GEN_EXE}
-p ${CMAKE_BINARY_DIR}
--output-dir=${lldb_rpc_generated_dir}
${sysroot_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)
add_dependencies(lldb-rpc-generate-sources clang-resource-headers)