This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathplatform_win.cc
296 lines (249 loc) · 8.58 KB
/
platform_win.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#if defined(_WIN32)
#include "platform.h"
#include "utils.h"
#include <loguru.hpp>
#include <windows.h>
// Has to be included before knownFolders.h
#include <initguid.h>
#include <Knownfolders.h>
#include <Objbase.h>
#include <Shlobj.h>
#include <direct.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <cassert>
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
namespace {
void EmitError(const std::string& message) {
LOG_S(ERROR) << message << " (errorcode=" << GetLastError() << ")";
}
} // namespace
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);
// `clang++ -E -x c++ - -v < nul` will not find system include directories if
// ProgramData is not set.
if (!getenv("ProgramData")) {
LOG_S(WARNING) << "The \"ProgramData\" environment variable is not set. "
<< "System includes like <vector> may not be resolved "
<< "properly.";
}
}
// See
// https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from
AbsolutePath GetExecutablePath() {
char result[MAX_PATH] = {0};
GetModuleFileName(NULL, result, MAX_PATH);
return *NormalizePath(result);
}
// See http://stackoverflow.com/a/19535628
AbsolutePath GetWorkingDirectory() {
char result[MAX_PATH];
std::string binary_path(result, GetModuleFileName(NULL, result, MAX_PATH));
return *NormalizePath(GetDirName(binary_path));
}
optional<AbsolutePath> NormalizePath(const std::string& path0,
bool ensure_exists,
bool force_lower_on_windows) {
// Requires Windows 8
/*
if (!PathCanonicalize(buffer, path.c_str()))
return nullopt;
*/
// Returns the volume name, ie, c:/
/*
if (!GetVolumePathName(path.c_str(), buffer, MAX_PATH))
return nullopt;
*/
std::string path = path0;
TCHAR buffer[MAX_PATH] = TEXT("");
// Normalize the path name, ie, resolve `..`.
DWORD len = GetFullPathName(path.c_str(), MAX_PATH, buffer, nullptr);
if (!len)
return nullopt;
path = std::string(buffer, len);
// Get the actual casing of the path, ie, if the file on disk is `C:\FooBar`
// and this function is called with `c:\fooBar` this will return `c:\FooBar`.
// (drive casing is lowercase).
if (ensure_exists) {
// Ensure 'GetLongPathName' actually transforms every path segment to the correct
// case by first making all directories in 8.3 format.
len = GetShortPathName(path.c_str(), buffer, MAX_PATH);
if (!len)
return nullopt;
std::string shortPath(buffer, len);
len = GetLongPathName(shortPath.c_str(), buffer, MAX_PATH);
if (!len)
return nullopt;
path = std::string(buffer, len);
}
// Empty paths have no meaning.
if (path.empty())
return nullopt;
// We may need to normalize the drive name to upper-case; at the moment
// vscode sends lower-case path names.
/*
path[0] = toupper(path[0]);
*/
// Make the path all lower-case, since windows is case-insensitive.
if (force_lower_on_windows) {
for (size_t i = 0; i < path.size(); ++i)
path[i] = (char)tolower(path[i]);
}
// cquery assumes forward-slashes.
std::replace(path.begin(), path.end(), '\\', '/');
return AbsolutePath(path, false /*validate*/);
}
void RemoveDirectoryRecursive(const AbsolutePath &path) {
// We use SHFileOperation, because its FO_DELETE operation is recursive.
std::string pathString = path.path;
// parameter 'pFrom' must be double NULL-terminated:
pathString.append(2, 0);
SHFILEOPSTRUCT op;
memset(&op, 0, sizeof(op));
op.wFunc = FO_DELETE;
op.pFrom = pathString.c_str();
op.fFlags = FOF_NO_UI;
SHFileOperation(&op);
}
bool TryMakeDirectory(const AbsolutePath& absolute_path) {
if (_mkdir(absolute_path.path.c_str()) == -1) {
// Success if the directory exists.
return errno == EEXIST;
}
return true;
}
optional<AbsolutePath> TryMakeTempDirectory() {
// get "temp" dir
char tmpdir_buf[MAX_PATH];
DWORD len = GetTempPath(MAX_PATH, tmpdir_buf);
// Unfortunately, there is no mkdtemp() on windows. We append a (random)
// GUID to use as a unique directory name.
GUID guid;
CoCreateGuid(&guid);
// simplest way to append the guid to the existing c string:
len += snprintf(tmpdir_buf + len, MAX_PATH - len,
"cquery-%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
AbsolutePath dirPath(std::string(tmpdir_buf, len));
// finally, create the dir
LOG_S(2) << "Creating temporary path " << dirPath;
if(!TryMakeDirectory(dirPath))
{
return nullopt;
}
return dirPath;
}
// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#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) {
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 {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
(ULONG_PTR*)&info);
#if defined(_MSC_VER)
} __except (EXCEPTION_EXECUTE_HANDLER) {
#else
} catch (...) {
#endif
}
}
optional<int64_t> GetLastModificationTime(const AbsolutePath& absolute_path) {
struct _stat buf;
if (_stat(absolute_path.path.c_str(), &buf) != 0) {
switch (errno) {
case ENOENT:
// std::cerr << "GetLastModificationTime: unable to find file " <<
// absolute_path << std::endl;
return nullopt;
case EINVAL:
// std::cerr << "GetLastModificationTime: invalid param to _stat for
// file file " << absolute_path << std::endl;
return nullopt;
default:
// std::cerr << "GetLastModificationTime: unhandled for " <<
// absolute_path << std::endl; exit(1);
return nullopt;
}
}
return buf.st_mtime;
}
void MoveFileTo(const AbsolutePath& destination, const AbsolutePath& source) {
MoveFile(source.path.c_str(), destination.path.c_str());
}
void CopyFileTo(const AbsolutePath& destination, const AbsolutePath& source) {
CopyFile(source.path.c_str(), destination.path.c_str(),
false /*failIfExists*/);
}
bool IsSymLink(const AbsolutePath& path) {
return false;
}
std::vector<const char*> GetPlatformClangArguments() {
//
// Found by executing
//
// $ clang++ -E -x c++ - -v
//
// https://clang.llvm.org/docs/MSVCCompatibility.html
//
//
// These options are only needed if clang is targeting the msvc triple,
// which depends on how clang was build for windows. clang downloaded from
// releases.llvm.org defaults to msvc, so cquery does as well.
//
// https://github.com/cquery-project/cquery/issues/509 has more context.
//
return {"-fms-extensions", "-fms-compatibility",
"-fdelayed-template-parsing"};
}
void FreeUnusedMemory() {}
bool RunObjectiveCIndexTests() {
return false;
}
// TODO Wait for debugger to attach
void TraceMe() {}
optional<std::string> GetGlobalConfigDirectory() {
wchar_t* roaming_path = NULL;
optional<std::string> cfg_path = {};
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT,
NULL, &roaming_path))) {
std::wstringstream roaming_stream;
roaming_stream << roaming_path << L"/cquery";
// Convert the roaming path string to a normal string so it
// is analogous with the string returned by the posix version.
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
cfg_path = converter.to_bytes(roaming_stream.str());
// As per the docs for SHGetKnownFolderPath
// (https://msdn.microsoft.com/en-us/library/bb762188(VS.85).aspx)
// we must free roaming_path using CoTaskMemFree once
// finished with it.
CoTaskMemFree(static_cast<void*>(roaming_path));
}
return cfg_path;
}
#endif