Skip to content

Commit 07e6fb5

Browse files
committed
Windows Port
This allows IndexStoreDB to build on Windows.
1 parent 1143208 commit 07e6fb5

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

lib/Database/Database.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
#include "llvm/Support/Mutex.h"
2424
#include "llvm/Support/Path.h"
2525
#include "llvm/Support/raw_ostream.h"
26+
#if defined(_WIN32)
27+
#include "Windows.h"
28+
#endif
29+
30+
#if defined(_WIN32)
31+
typedef HANDLE indexstorePid_t;
32+
#else
33+
typedef pid_t indexstorePid_t;
34+
#endif
2635

2736
// Dispatch on Linux doesn't have QOS_* macros.
2837
#if !__has_include(<sys/qos.h>)
@@ -97,7 +106,11 @@ Database::Implementation::create(StringRef path, bool readonly, Optional<size_t>
97106
SmallString<128> savedPathBuf = versionPath;
98107
llvm::sys::path::append(savedPathBuf, "saved");
99108
SmallString<128> processPathBuf = versionPath;
109+
#if defined(WIN32)
110+
llvm::raw_svector_ostream(processPathBuf) << "/p" << GetCurrentProcess();
111+
#else
100112
llvm::raw_svector_ostream(processPathBuf) << "/p" << getpid();
113+
#endif
101114

102115
bool existingDB = true;
103116

@@ -269,10 +282,16 @@ void Database::Implementation::increaseMapSize() {
269282
LOG_INFO_FUNC(High, "increased lmdb map size to: " << MapSize);
270283
}
271284

272-
static bool isProcessStillExecuting(pid_t PID) {
285+
static bool isProcessStillExecuting(indexstorePid_t PID) {
286+
#if defined(_WIN32)
287+
DWORD dwExitCode;
288+
bool result = GetExitCodeProcess(PID, &dwExitCode);
289+
return result && (dwExitCode == STILL_ACTIVE);
290+
#else
273291
if (getsid(PID) == -1 && errno == ESRCH)
274292
return false;
275293
return true;
294+
#endif
276295
}
277296

278297
// This runs in a background priority queue.
@@ -283,7 +302,11 @@ static void cleanupDiscardedDBsImpl(StringRef versionedPath) {
283302
// A directory is dead if it has been marked with the suffix "-dead" or if it
284303
// has the name "p<PID>" where process PID is no longer running.
285304

286-
pid_t currPID = getpid();
305+
#if defined(WIN32)
306+
indexstorePid_t currPID = GetCurrentProcess();
307+
#else
308+
indexstorePid_t currPID = getpid();
309+
#endif
287310

288311
std::error_code EC;
289312
directory_iterator Begin(versionedPath, EC);
@@ -292,7 +315,7 @@ static void cleanupDiscardedDBsImpl(StringRef versionedPath) {
292315
auto &Item = *Begin;
293316
StringRef currPath = Item.path();
294317

295-
auto shouldRemove = [](StringRef fullpath, pid_t currPID) -> bool {
318+
auto shouldRemove = [](StringRef fullpath, indexstorePid_t currPID) -> bool {
296319
StringRef path = llvm::sys::path::filename(fullpath);
297320
if (path.endswith(DeadProcessDBSuffix))
298321
return true;
@@ -301,9 +324,9 @@ static void cleanupDiscardedDBsImpl(StringRef versionedPath) {
301324
size_t pathPID;
302325
if (path.substr(1).getAsInteger(10, pathPID))
303326
return false;
304-
if (pathPID == currPID)
327+
if ((indexstorePid_t)pathPID == currPID)
305328
return false;
306-
return !isProcessStillExecuting(pathPID);
329+
return !isProcessStillExecuting((indexstorePid_t)pathPID);
307330
};
308331

309332
if (shouldRemove(currPath, currPID)) {

lib/Index/IndexStoreLibraryProvider.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
#include "IndexStoreDB/Index/IndexStoreLibraryProvider.h"
1414
#include "indexstore/IndexStoreCXX.h"
1515
#include "llvm/ADT/StringRef.h"
16+
#include "llvm/Support/ConvertUTF.h"
17+
#if defined(_WIN32)
18+
#include <Windows.h>
19+
#else
1620
#include <dlfcn.h>
21+
#endif
1722

1823
using namespace IndexStoreDB;
1924
using namespace index;
@@ -28,11 +33,28 @@ IndexStoreLibraryRef GlobalIndexStoreLibraryProvider::getLibraryForStorePath(Str
2833

2934
// Note: we're using dlsym with RTLD_DEFAULT because we cannot #incldue indexstore.h and indexstore_functions.h
3035
std::string ignored;
31-
return loadIndexStoreLibraryFromDLHandle(RTLD_DEFAULT, ignored);
36+
#if defined(_WIN32)
37+
void* defaultHandle = GetModuleHandleW(NULL);
38+
#else
39+
void* defaultHandle = RTLD_DEFAULT;
40+
#endif
41+
return loadIndexStoreLibraryFromDLHandle(defaultHandle, ignored);
3242
}
3343

3444
IndexStoreLibraryRef index::loadIndexStoreLibrary(std::string dylibPath,
3545
std::string &error) {
46+
#if defined(_WIN32)
47+
llvm::SmallVector<llvm::UTF16, 30> u16Path;
48+
if (!convertUTF8ToUTF16String(dylibPath, u16Path)) {
49+
error += "Failed to convert path: " + dylibPath + " to UTF-16";
50+
return nullptr;
51+
}
52+
HMODULE dlHandle = LoadLibraryW((LPCWSTR)u16Path.data());
53+
if (dlHandle == NULL) {
54+
error += "Failed to load " + dylibPath + ". Error: " + std::to_string(GetLastError());
55+
return nullptr;
56+
}
57+
#else
3658
auto flags = RTLD_LAZY | RTLD_LOCAL;
3759
#ifdef RTLD_FIRST
3860
flags |= RTLD_FIRST;
@@ -44,6 +66,7 @@ IndexStoreLibraryRef index::loadIndexStoreLibrary(std::string dylibPath,
4466
error += dlerror();
4567
return nullptr;
4668
}
69+
#endif
4770

4871
// Intentionally leak the dlhandle; we have no reason to dlclose it and it may be unsafe.
4972
(void)dlHandle;
@@ -54,12 +77,21 @@ IndexStoreLibraryRef index::loadIndexStoreLibrary(std::string dylibPath,
5477
static IndexStoreLibraryRef loadIndexStoreLibraryFromDLHandle(void *dlHandle, std::string &error) {
5578
indexstore_functions_t api;
5679

80+
#if defined(_WIN32)
81+
#define INDEXSTORE_FUNCTION(func, required) \
82+
api.func = (decltype(indexstore_functions_t::func))GetProcAddress((HMODULE)dlHandle, "indexstore_" #func); \
83+
if (!api.func && required) { \
84+
error = "indexstore library missing required function indexstore_" #func; \
85+
return nullptr; \
86+
}
87+
#else
5788
#define INDEXSTORE_FUNCTION(func, required) \
5889
api.func = (decltype(indexstore_functions_t::func))dlsym(dlHandle, "indexstore_" #func); \
5990
if (!api.func && required) { \
6091
error = "indexstore library missing required function indexstore_" #func; \
6192
return nullptr; \
6293
}
94+
#endif
6395

6496
#include "indexstore_functions.def"
6597

lib/Support/Path.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,36 @@
1616
#include "llvm/Support/Path.h"
1717
#include "llvm/Support/FileSystem.h"
1818
#include "llvm/Support/Mutex.h"
19+
#if defined(_WIN32)
20+
#include <Windows.h>
21+
#else
1922
#include <unistd.h>
23+
#endif
2024
#include <limits.h>
2125
#include <stdlib.h>
2226

27+
#if defined(_WIN32)
28+
#define PATH_MAX MAX_PATH
29+
#endif
30+
2331
using namespace IndexStoreDB;
2432

2533
static StringRef getCanonicalPath(const char *Path, char *PathBuf) {
34+
#if defined(_WIN32)
35+
llvm::SmallString<128> buffer;
36+
if (llvm::sys::fs::real_path(Path, buffer, false)) {
37+
return StringRef();
38+
}
39+
if (!PathBuf) {
40+
PathBuf = (char *)malloc(sizeof(char) * buffer.size());
41+
}
42+
std::copy(buffer.begin(), buffer.end(), PathBuf);
43+
return PathBuf;
44+
#else
2645
if (const char *path = realpath(Path, PathBuf))
2746
return path;
2847
return StringRef();
48+
#endif
2949
}
3050

3151
namespace {

0 commit comments

Comments
 (0)