Skip to content

Commit ce8458e

Browse files
authored
Merge pull request #79530 from bnbarham/load-service-from-file
[SourceKit] Do not hardcode the XPC service name
2 parents 3b412ae + 652da55 commit ce8458e

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake

+5-1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ macro(add_sourcekit_framework name)
445445
"${framework_location}/Versions/A" "${SOURCEKIT_LIBRARY_OUTPUT_INTDIR}")
446446
list(APPEND RPATH_LIST "@loader_path/${relative_lib_path}")
447447

448+
file(GENERATE OUTPUT "xpc_service_name.txt" CONTENT "com.apple.SourceKitService.${SOURCEKIT_VERSION_STRING}_${SOURCEKIT_TOOLCHAIN_NAME}")
449+
target_sources(${name} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/xpc_service_name.txt")
450+
448451
set_target_properties(${name} PROPERTIES
449452
BUILD_WITH_INSTALL_RPATH TRUE
450453
FOLDER "SourceKit frameworks"
@@ -455,7 +458,8 @@ macro(add_sourcekit_framework name)
455458
MACOSX_FRAMEWORK_IDENTIFIER "com.apple.${name}"
456459
MACOSX_FRAMEWORK_SHORT_VERSION_STRING "1.0"
457460
MACOSX_FRAMEWORK_BUNDLE_VERSION "${SOURCEKIT_VERSION_STRING}"
458-
PUBLIC_HEADER "${headers}")
461+
PUBLIC_HEADER "${headers}"
462+
RESOURCE "${CMAKE_CURRENT_BINARY_DIR}/xpc_service_name.txt")
459463
add_dependencies(${SOURCEKITFW_INSTALL_IN_COMPONENT} ${name})
460464
swift_install_in_component(TARGETS ${name}
461465
FRAMEWORK

tools/SourceKit/tools/sourcekitd/bin/XPC/Client/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ add_sourcekit_framework(sourcekitd
1616
)
1717
target_link_libraries(sourcekitd PRIVATE sourcekitdAPI)
1818

19-
add_definitions(-DSOURCEKIT_XPCSERVICE_IDENTIFIER="com.apple.SourceKitService.${SOURCEKIT_VERSION_STRING}_${SOURCEKIT_TOOLCHAIN_NAME}")
20-
2119
if (SOURCEKIT_BUILT_STANDALONE)
2220
# Create the symlink necessary to find the swift stdlib.
2321
swift_create_post_build_symlink(sourcekitd

tools/SourceKit/tools/sourcekitd/bin/XPC/Client/sourcekitd.cpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
#include "SourceKit/Support/UIdent.h"
1616

1717
#include "llvm/Support/ErrorHandling.h"
18+
#include "llvm/Support/MemoryBuffer.h"
1819
#include "llvm/Support/Mutex.h"
20+
#include "llvm/Support/Path.h"
21+
22+
#include <Block.h>
1923
#include <chrono>
2024
#include <xpc/xpc.h>
2125
#include <dispatch/dispatch.h>
22-
23-
#include <Block.h>
26+
#include <dlfcn.h>
2427

2528
using namespace SourceKit;
2629
using namespace sourcekitd;
@@ -285,9 +288,31 @@ static void handleInternalUIDRequest(xpc_object_t XVal,
285288

286289
static void handleInterruptedConnection(xpc_object_t event, xpc_connection_t conn);
287290

291+
extern "C" const char __dso_handle[];
292+
288293
static void initializeXPCClient() {
289294
assert(!GlobalConn);
290-
GlobalConn = xpc_connection_create(SOURCEKIT_XPCSERVICE_IDENTIFIER, nullptr);
295+
296+
Dl_info dlinfo;
297+
dladdr(__dso_handle, &dlinfo);
298+
299+
// '.../usr/lib/sourcekitd.framework/sourcekitd'
300+
llvm::SmallString<128> serviceNamePath(dlinfo.dli_fname);
301+
if (serviceNamePath.empty()) {
302+
llvm::report_fatal_error("Unable to find service name path");
303+
}
304+
305+
llvm::sys::path::remove_filename(serviceNamePath);
306+
// '.../usr/lib/sourcekitd.framework/Resources/xpc_service_name.txt'
307+
llvm::sys::path::append(serviceNamePath, "Resources", "xpc_service_name.txt");
308+
309+
auto bufferOrErr = llvm::MemoryBuffer::getFile(serviceNamePath);
310+
if (!bufferOrErr) {
311+
llvm::report_fatal_error("Unable to find service name");
312+
}
313+
314+
std::string serviceName = (*bufferOrErr)->getBuffer().trim().str();
315+
GlobalConn = xpc_connection_create(serviceName.c_str(), nullptr);
291316

292317
xpc_connection_set_event_handler(GlobalConn, ^(xpc_object_t event) {
293318
xpc_type_t type = xpc_get_type(event);
@@ -390,7 +415,9 @@ void sourcekitd_register_plugin_path(const char *clientPlugin,
390415
}
391416

392417
static xpc_connection_t getGlobalConnection() {
393-
assert(GlobalConn);
418+
if (!GlobalConn) {
419+
llvm::report_fatal_error("Service is invalid");
420+
}
394421
return GlobalConn;
395422
}
396423

0 commit comments

Comments
 (0)