Skip to content

Commit c0ad0b6

Browse files
committed
[SourceKit] Support Windows for the SourceKit plugin
1 parent 2447e74 commit c0ad0b6

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727
#include "llvm/Support/raw_ostream.h"
2828
#include "llvm/Support/YAMLParser.h"
2929
#include <mutex>
30+
31+
#if defined(_WIN32) && !defined(__CYGWIN__)
32+
#define NOMINMAX
33+
#include <Windows.h>
34+
#else
3035
#include <dlfcn.h>
36+
#endif
3137

3238
using namespace SourceKit;
3339
using namespace sourcekitd;
@@ -271,6 +277,31 @@ bool sourcekitd::shutdownClient() {
271277

272278
extern "C" const char __dso_handle[];
273279

280+
static void withCurrentLibraryPath(llvm::function_ref<void(const char *)> body) {
281+
#if defined(_WIN32) && !defined(__CYGWIN__)
282+
char path[MAX_PATH];
283+
HMODULE currentModule = NULL;
284+
285+
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
286+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
287+
(LPCWSTR) &withCurrentLibraryPath, &currentModule) == 0) {
288+
int error = GetLastError();
289+
LOG_WARN("plugin-loading", "failed to determine current Windows module. Error: " << error);
290+
return body(nullptr);
291+
}
292+
if (GetModuleFileNameA(currentModule, path, sizeof(path)) == 0) {
293+
int error = GetLastError();
294+
LOG_WARN("plugin-loading", "failed to path of current Windows module. Error: " << error);
295+
return body(nullptr);
296+
}
297+
return body(path);
298+
#else
299+
Dl_info dlinfo;
300+
dladdr(__dso_handle, &dlinfo);
301+
return body(dlinfo.dli_fname);
302+
#endif
303+
}
304+
274305
static void loadPlugin(StringRef plugin, PluginInitParams &pluginParams) {
275306
std::string err;
276307
auto *handle = swift::loadLibrary(plugin.str().c_str(), &err);
@@ -283,9 +314,9 @@ static void loadPlugin(StringRef plugin, PluginInitParams &pluginParams) {
283314
auto *plugin_init_2 = (sourcekitd_plugin_initialize_2_t)swift::getAddressOfSymbol(
284315
handle, "sourcekitd_plugin_initialize_2");
285316
if (plugin_init_2) {
286-
Dl_info dlinfo;
287-
dladdr(__dso_handle, &dlinfo);
288-
plugin_init_2(&pluginParams, dlinfo.dli_fname);
317+
withCurrentLibraryPath([&](const char *currentLibraryPath) {
318+
plugin_init_2(&pluginParams, currentLibraryPath);
319+
});
289320
return;
290321
}
291322

utils/build.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,7 @@ function Test-SourceKitLSP {
25782578
"-Xswiftc", "-I$($SourceCache)\sourcekit-lsp\Sources\CAtomics\include",
25792579
"-Xswiftc", "-I$($SourceCache)\sourcekit-lsp\Sources\CSourcekitd\include",
25802580
"-Xlinker", "$(Get-HostProjectBinaryCache SourceKitLSP)\lib\CSourcekitd.lib",
2581+
"-Xswiftc", "-I$($SourceCache)\sourcekit-lsp\Sources\CCompletionScoring\include",
25812582
"-Xswiftc", "-I$(Get-HostProjectBinaryCache SourceKitLSP)\swift",
25822583
"-Xlinker", "-L$(Get-HostProjectBinaryCache SourceKitLSP)\lib"
25832584
)
@@ -2591,6 +2592,10 @@ function Test-SourceKitLSP {
25912592
# Log with the highest log level to simplify debugging of CI failures.
25922593
$env:SOURCEKIT_LSP_LOG_LEVEL="debug"
25932594

2595+
# The Windows build doesn't build the SourceKit plugins into the SwiftPM build directory (it builds them using CMake).
2596+
# Tell the tests where to find the just-built plugins.
2597+
$env:SOURCEKIT_LSP_TEST_PLUGIN_PATHS="$($HostArch.ToolchainInstallRoot)\usr\lib"
2598+
25942599
Build-SPMProject `
25952600
-Action TestParallel `
25962601
-Src "$SourceCache\sourcekit-lsp" `

0 commit comments

Comments
 (0)