Skip to content

Windows Port #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/IndexStoreDB/Database/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "IndexStoreDB/Support/LLVM.h"
#include "IndexStoreDB/Support/Visibility.h"
#include <memory>
#include <string>

namespace IndexStoreDB {
namespace db {
Expand Down
33 changes: 28 additions & 5 deletions lib/Database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#if defined(_WIN32)
#include "Windows.h"
#endif

#if defined(_WIN32)
typedef HANDLE indexstorePid_t;
#else
typedef pid_t indexstorePid_t;
#endif

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

bool existingDB = true;

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

static bool isProcessStillExecuting(pid_t PID) {
static bool isProcessStillExecuting(indexstorePid_t PID) {
#if defined(_WIN32)
DWORD dwExitCode;
bool result = GetExitCodeProcess(PID, &dwExitCode);
return result && (dwExitCode == STILL_ACTIVE);
#else
if (getsid(PID) == -1 && errno == ESRCH)
return false;
return true;
#endif
}

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

pid_t currPID = getpid();
#if defined(WIN32)
indexstorePid_t currPID = GetCurrentProcess();
#else
indexstorePid_t currPID = getpid();
#endif

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

auto shouldRemove = [](StringRef fullpath, pid_t currPID) -> bool {
auto shouldRemove = [](StringRef fullpath, indexstorePid_t currPID) -> bool {
StringRef path = llvm::sys::path::filename(fullpath);
if (path.endswith(DeadProcessDBSuffix))
return true;
Expand All @@ -301,9 +324,9 @@ static void cleanupDiscardedDBsImpl(StringRef versionedPath) {
size_t pathPID;
if (path.substr(1).getAsInteger(10, pathPID))
return false;
if (pathPID == currPID)
if ((indexstorePid_t)pathPID == currPID)
return false;
return !isProcessStillExecuting(pathPID);
return !isProcessStillExecuting((indexstorePid_t)pathPID);
};

if (shouldRemove(currPath, currPID)) {
Expand Down
34 changes: 33 additions & 1 deletion lib/Index/IndexStoreLibraryProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
#include "IndexStoreDB/Index/IndexStoreLibraryProvider.h"
#include "indexstore/IndexStoreCXX.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ConvertUTF.h"
#if defined(_WIN32)
#include <Windows.h>
#else
#include <dlfcn.h>
#endif

using namespace IndexStoreDB;
using namespace index;
Expand All @@ -28,11 +33,28 @@ IndexStoreLibraryRef GlobalIndexStoreLibraryProvider::getLibraryForStorePath(Str

// Note: we're using dlsym with RTLD_DEFAULT because we cannot #incldue indexstore.h and indexstore_functions.h
std::string ignored;
return loadIndexStoreLibraryFromDLHandle(RTLD_DEFAULT, ignored);
#if defined(_WIN32)
void* defaultHandle = GetModuleHandleW(NULL);
#else
void* defaultHandle = RTLD_DEFAULT;
#endif
return loadIndexStoreLibraryFromDLHandle(defaultHandle, ignored);
}

IndexStoreLibraryRef index::loadIndexStoreLibrary(std::string dylibPath,
std::string &error) {
#if defined(_WIN32)
llvm::SmallVector<llvm::UTF16, 30> u16Path;
if (!convertUTF8ToUTF16String(dylibPath, u16Path)) {
error += "Failed to convert path: " + dylibPath + " to UTF-16";
return nullptr;
}
HMODULE dlHandle = LoadLibraryW((LPCWSTR)u16Path.data());
if (dlHandle == NULL) {
error += "Failed to load " + dylibPath + ". Error: " + std::to_string(GetLastError());
return nullptr;
}
#else
auto flags = RTLD_LAZY | RTLD_LOCAL;
#ifdef RTLD_FIRST
flags |= RTLD_FIRST;
Expand All @@ -44,6 +66,7 @@ IndexStoreLibraryRef index::loadIndexStoreLibrary(std::string dylibPath,
error += dlerror();
return nullptr;
}
#endif

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

#if defined(_WIN32)
#define INDEXSTORE_FUNCTION(func, required) \
api.func = (decltype(indexstore_functions_t::func))GetProcAddress((HMODULE)dlHandle, "indexstore_" #func); \
if (!api.func && required) { \
error = "indexstore library missing required function indexstore_" #func; \
return nullptr; \
}
#else
#define INDEXSTORE_FUNCTION(func, required) \
api.func = (decltype(indexstore_functions_t::func))dlsym(dlHandle, "indexstore_" #func); \
if (!api.func && required) { \
error = "indexstore library missing required function indexstore_" #func; \
return nullptr; \
}
#endif

#include "indexstore_functions.def"

Expand Down
20 changes: 11 additions & 9 deletions lib/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Mutex.h"
#if defined(_WIN32)
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <limits.h>
#include <stdlib.h>

using namespace IndexStoreDB;
#if defined(_WIN32)
#define PATH_MAX MAX_PATH
#endif

static StringRef getCanonicalPath(const char *Path, char *PathBuf) {
if (const char *path = realpath(Path, PathBuf))
return path;
return StringRef();
}
using namespace IndexStoreDB;

namespace {
class CanonicalPathCacheImpl {
Expand Down Expand Up @@ -61,11 +63,11 @@ CanonicalPathCacheImpl::getCanonicalPath(StringRef Path, StringRef WorkingDir) {
return It->second;
}

char PathBuf[PATH_MAX];
StringRef CanonPath = ::getCanonicalPath(AbsPath.c_str(), PathBuf);
if (CanonPath.empty()) {
llvm::SmallString<PATH_MAX> Buffer;
if (llvm::sys::fs::real_path(AbsPath.c_str(), Buffer, false)) {
return CanonicalFilePathRef::getAsCanonicalPath(AbsPath);
}
StringRef CanonPath = Buffer;

{
llvm::sys::ScopedLock L(StateMtx);
Expand Down