Skip to content

Commit 40b91aa

Browse files
committed
Share logic to load dynamic libraries between SourceKit plugins and macro plugins
1 parent 6d0463a commit 40b91aa

File tree

5 files changed

+87
-56
lines changed

5 files changed

+87
-56
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_LOADDYNAMICLIBRARY_H
14+
#define SWIFT_BASIC_LOADDYNAMICLIBRARY_H
15+
16+
#include <string>
17+
18+
namespace swift {
19+
void *loadLibrary(const char *path, std::string *err);
20+
void *getAddressOfSymbol(void *handle, const char *symbol);
21+
} // end namespace swift
22+
23+
#endif // SWIFT_BASIC_LOADDYNAMICLIBRARY_H

lib/AST/PluginRegistry.cpp

+1-50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "swift/Basic/Assertions.h"
1616
#include "swift/Basic/Defer.h"
17+
#include "swift/Basic/LoadDynamicLibrary.h"
1718
#include "swift/Basic/LLVM.h"
1819
#include "swift/Basic/Program.h"
1920
#include "swift/Basic/Sandbox.h"
@@ -25,16 +26,6 @@
2526

2627
#include <signal.h>
2728

28-
#if defined(_WIN32)
29-
#define WIN32_LEAN_AND_MEAN
30-
#define NOMINMAX
31-
#include "llvm/Support/ConvertUTF.h"
32-
#include "llvm/Support/Windows/WindowsSupport.h"
33-
#include <windows.h>
34-
#else
35-
#include <dlfcn.h>
36-
#endif
37-
3829
#if HAVE_UNISTD_H
3930
#include <unistd.h>
4031
#elif defined(_WIN32)
@@ -43,46 +34,6 @@
4334

4435
using namespace swift;
4536

46-
namespace {
47-
void *loadLibrary(const char *path, std::string *err);
48-
void *getAddressOfSymbol(void *handle, const char *symbol);
49-
50-
#if defined(_WIN32)
51-
void *loadLibrary(const char *path, std::string *err) {
52-
SmallVector<wchar_t, MAX_PATH> pathUnicode;
53-
if (std::error_code ec = llvm::sys::windows::UTF8ToUTF16(path, pathUnicode)) {
54-
SetLastError(ec.value());
55-
llvm::MakeErrMsg(err, std::string(path) + ": Can't convert to UTF-16");
56-
return nullptr;
57-
}
58-
59-
HMODULE handle = LoadLibraryW(pathUnicode.data());
60-
if (handle == NULL) {
61-
llvm::MakeErrMsg(err, std::string(path) + ": Can't open");
62-
return nullptr;
63-
}
64-
return (void *)handle;
65-
}
66-
67-
void *getAddressOfSymbol(void *handle, const char *symbol) {
68-
return (void *)uintptr_t(GetProcAddress((HMODULE)handle, symbol));
69-
}
70-
71-
#else
72-
void *loadLibrary(const char *path, std::string *err) {
73-
void *handle = ::dlopen(path, RTLD_LAZY | RTLD_LOCAL);
74-
if (!handle)
75-
*err = ::dlerror();
76-
return handle;
77-
}
78-
79-
void *getAddressOfSymbol(void *handle, const char *symbol) {
80-
return ::dlsym(handle, symbol);
81-
}
82-
83-
#endif
84-
} // namespace
85-
8637
PluginRegistry::PluginRegistry() {
8738
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
8839
}

lib/Basic/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_swift_host_library(swiftBasic STATIC
5858
ParseableOutput.cpp
5959
JSONSerialization.cpp
6060
LangOptions.cpp
61+
LoadDynamicLibrary.cpp
6162
Located.cpp
6263
Mangler.cpp
6364
OutputFileMap.cpp

lib/Basic/LoadDynamicLibrary.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Basic/LoadDynamicLibrary.h"
14+
15+
#if defined(_WIN32)
16+
#define WIN32_LEAN_AND_MEAN
17+
#define NOMINMAX
18+
#include "llvm/Support/ConvertUTF.h"
19+
#include "llvm/Support/Windows/WindowsSupport.h"
20+
#include <windows.h>
21+
#else
22+
#include <dlfcn.h>
23+
#endif
24+
25+
#if defined(_WIN32)
26+
void *swift::loadLibrary(const char *path, std::string *err) {
27+
SmallVector<wchar_t, MAX_PATH> pathUnicode;
28+
if (std::error_code ec = llvm::sys::windows::UTF8ToUTF16(path, pathUnicode)) {
29+
SetLastError(ec.value());
30+
llvm::MakeErrMsg(err, std::string(path) + ": Can't convert to UTF-16");
31+
return nullptr;
32+
}
33+
34+
HMODULE handle = LoadLibraryW(pathUnicode.data());
35+
if (handle == NULL) {
36+
llvm::MakeErrMsg(err, std::string(path) + ": Can't open");
37+
return nullptr;
38+
}
39+
return (void *)handle;
40+
}
41+
42+
void *swift::getAddressOfSymbol(void *handle, const char *symbol) {
43+
return (void *)uintptr_t(GetProcAddress((HMODULE)handle, symbol));
44+
}
45+
46+
#else
47+
void *swift::loadLibrary(const char *path, std::string *err) {
48+
void *handle = ::dlopen(path, RTLD_LAZY | RTLD_LOCAL);
49+
if (!handle)
50+
*err = ::dlerror();
51+
return handle;
52+
}
53+
54+
void *swift::getAddressOfSymbol(void *handle, const char *symbol) {
55+
return ::dlsym(handle, symbol);
56+
}
57+
#endif

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "sourcekitd/RequestResponsePrinterBase.h"
1717
#include "SourceKit/Support/Logging.h"
1818
#include "SourceKit/Support/UIdent.h"
19+
#include "swift/Basic/LoadDynamicLibrary.h"
1920
#include "swift/Basic/StringExtras.h"
2021
#include "llvm/ADT/ArrayRef.h"
2122
#include "llvm/ADT/SmallString.h"
@@ -27,8 +28,6 @@
2728
#include "llvm/Support/YAMLParser.h"
2829
#include <mutex>
2930

30-
#include <dlfcn.h>
31-
3231
using namespace SourceKit;
3332
using namespace sourcekitd;
3433
using llvm::ArrayRef;
@@ -270,15 +269,15 @@ bool sourcekitd::shutdownClient() {
270269
}
271270

272271
static void loadPlugin(StringRef plugin, PluginInitParams &pluginParams) {
273-
274-
auto *handle = dlopen(plugin.str().c_str(), RTLD_FIRST | RTLD_LAZY);
272+
std::string err;
273+
auto *handle = swift::loadLibrary(plugin.str().c_str(), &err);
275274
if (!handle) {
276275
LOG_WARN("plugin-loading",
277-
"failed to load plugin '" << plugin << "': " << dlerror());
276+
"failed to load plugin '" << plugin << "': " << err);
278277
return;
279278
}
280279

281-
auto *plugin_init = (sourcekitd_plugin_initialize_t)dlsym(
280+
auto *plugin_init = (sourcekitd_plugin_initialize_t)swift::getAddressOfSymbol(
282281
handle, "sourcekitd_plugin_initialize");
283282
if (!plugin_init) {
284283
LOG_WARN("plugin-loading",

0 commit comments

Comments
 (0)