ccls/src/platform_win.cc

150 lines
3.9 KiB
C++
Raw Normal View History

2017-03-19 22:06:22 +00:00
#if defined(_WIN32)
#include "platform.h"
2017-03-25 20:27:28 +00:00
#include "utils.h"
2017-07-28 02:14:33 +00:00
#include <loguru.hpp>
2017-09-22 01:14:57 +00:00
#include <Windows.h>
#include <direct.h>
2017-03-25 20:27:28 +00:00
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
2017-09-22 01:14:57 +00:00
#include <sys/types.h>
2017-03-29 06:33:38 +00:00
#include <algorithm>
2017-03-19 22:06:22 +00:00
#include <cassert>
#include <iostream>
#include <string>
2017-03-25 20:27:28 +00:00
void PlatformInit() {
// We need to write to stdout in binary mode because in Windows, writing
// \n will implicitly write \r\n. Language server API will ignore a
// \r\r\n split request.
_setmode(_fileno(stdout), O_BINARY);
_setmode(_fileno(stdin), O_BINARY);
}
2017-12-23 16:01:43 +00:00
// See
// https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from
std::string GetExecutablePath() {
char result[MAX_PATH] = {0};
GetModuleFileName(NULL, result, MAX_PATH);
return NormalizePath(result);
}
2017-03-19 22:06:22 +00:00
// See http://stackoverflow.com/a/19535628
std::string GetWorkingDirectory() {
char result[MAX_PATH];
2017-10-25 01:02:15 +00:00
std::string binary_path(result, GetModuleFileName(NULL, result, MAX_PATH));
return binary_path.substr(0, binary_path.find_last_of("\\/") + 1);
2017-03-19 22:06:22 +00:00
}
2017-03-28 05:27:06 +00:00
std::string NormalizePath(const std::string& path) {
DWORD retval = 0;
TCHAR buffer[MAX_PATH] = TEXT("");
TCHAR buf[MAX_PATH] = TEXT("");
2017-09-22 01:14:57 +00:00
TCHAR** lpp_part = {NULL};
2017-03-28 05:27:06 +00:00
retval = GetFullPathName(path.c_str(), MAX_PATH, buffer, lpp_part);
// fail, return original
if (retval == 0)
return path;
2017-03-29 06:33:38 +00:00
std::string result = buffer;
std::replace(result.begin(), result.end(), '\\', '/');
2017-09-22 01:14:57 +00:00
// std::transform(result.begin(), result.end(), result.begin(), ::tolower);
2017-03-29 06:33:38 +00:00
return result;
}
bool TryMakeDirectory(const std::string& absolute_path) {
if (_mkdir(absolute_path.c_str()) == -1) {
// Success if the directory exists.
return errno == EEXIST;
}
return true;
}
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD MS_VC_EXCEPTION = 0x406D1388;
2017-09-22 01:14:57 +00:00
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetCurrentThreadName(const std::string& thread_name) {
2017-07-28 02:14:33 +00:00
loguru::set_thread_name(thread_name.c_str());
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = thread_name.c_str();
info.dwThreadID = (DWORD)-1;
info.dwFlags = 0;
__try {
2017-09-22 01:14:57 +00:00
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
(ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
}
optional<int64_t> GetLastModificationTime(const std::string& absolute_path) {
struct _stat buf;
if (_stat(absolute_path.c_str(), &buf) != 0) {
switch (errno) {
case ENOENT:
2017-09-22 01:14:57 +00:00
// std::cerr << "GetLastModificationTime: unable to find file " <<
// absolute_path << std::endl;
return nullopt;
case EINVAL:
2017-09-22 01:14:57 +00:00
// std::cerr << "GetLastModificationTime: invalid param to _stat for
// file file " << absolute_path << std::endl;
return nullopt;
default:
2017-09-22 01:14:57 +00:00
// std::cerr << "GetLastModificationTime: unhandled for " <<
// absolute_path << std::endl; exit(1);
return nullopt;
}
}
return buf.st_mtime;
}
2017-07-29 00:08:18 +00:00
void MoveFileTo(const std::string& destination, const std::string& source) {
2017-09-22 01:14:57 +00:00
MoveFile(source.c_str(), destination.c_str());
2017-07-29 00:08:18 +00:00
}
void CopyFileTo(const std::string& destination, const std::string& source) {
2017-09-22 01:14:57 +00:00
CopyFile(source.c_str(), destination.c_str(), false /*failIfExists*/);
}
bool IsSymLink(const std::string& path) {
return false;
}
2017-03-29 06:33:38 +00:00
std::vector<std::string> GetPlatformClangArguments() {
2017-12-19 04:39:45 +00:00
//
// Found by executing
//
// $ clang++ -E -x c++ - -v
//
2017-12-23 16:01:43 +00:00
// clang-format off
2017-12-19 04:39:45 +00:00
return {
2018-01-04 02:32:15 +00:00
"-fms-extensions", "-fms-compatibility", "-fdelayed-template-parsing"
2017-12-19 04:39:45 +00:00
};
2017-12-23 16:01:43 +00:00
// clang-format on
2017-03-28 05:27:06 +00:00
}
2017-08-17 18:02:47 +00:00
void FreeUnusedMemory() {}
bool RunObjectiveCIndexTests() {
return false;
}
2017-03-19 22:06:22 +00:00
#endif