blob: c358b99ab96aaa941b14784901ad6b493d10eaa4 [file] [log] [blame]
Reid Klecknerd59e2fa2014-02-12 21:26:20 +00001//===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00008//===----------------------------------------------------------------------===//
9//
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000010// This file defines things specific to Windows implementations. In addition to
11// providing some helpers for working with win32 APIs, this header wraps
12// <windows.h> with some portability macros. Always include WindowsSupport.h
13// instead of including <windows.h> directly.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000014//
15//===----------------------------------------------------------------------===//
16
17//===----------------------------------------------------------------------===//
Jeff Cohen3800a282005-07-13 02:15:18 +000018//=== WARNING: Implementation here must contain only generic Win32 code that
19//=== is guaranteed to work on *all* Win32 variants.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000020//===----------------------------------------------------------------------===//
21
Yaron Keren3f02c142015-01-21 16:20:38 +000022#ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23#define LLVM_SUPPORT_WINDOWSSUPPORT_H
24
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000025// mingw-w64 tends to define it as 0x0502 in its headers.
26#undef _WIN32_WINNT
Aaron Ballman42f66222014-02-03 17:20:26 +000027#undef _WIN32_IE
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000028
Pawel Bylica6b129bd2015-10-15 14:50:31 +000029// Require at least Windows 7 API.
30#define _WIN32_WINNT 0x0601
31#define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000032#define WIN32_LEAN_AND_MEAN
Yunzhong Gaod7009f32016-01-06 01:36:45 +000033#ifndef NOMINMAX
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000034#define NOMINMAX
Yunzhong Gaod7009f32016-01-06 01:36:45 +000035#endif
Jeff Cohen3800a282005-07-13 02:15:18 +000036
Rui Ueyama471d0c52013-09-10 19:45:51 +000037#include "llvm/ADT/SmallVector.h"
Paul Robinsonaf19bc32015-11-23 17:34:20 +000038#include "llvm/ADT/StringExtras.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000039#include "llvm/ADT/StringRef.h"
Paul Robinsonc38deee2014-11-24 18:05:29 +000040#include "llvm/ADT/Twine.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000041#include "llvm/Config/config.h" // Get build system configuration settings
Pavel Labath757ca882016-10-24 10:59:17 +000042#include "llvm/Support/Chrono.h"
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000043#include "llvm/Support/Compiler.h"
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000044#include <cassert>
45#include <string>
Pavel Labath757ca882016-10-24 10:59:17 +000046#include <system_error>
Pavel Labath757ca882016-10-24 10:59:17 +000047#include <windows.h>
Simon Pilgrima072e372016-10-24 17:15:05 +000048#include <wincrypt.h> // Must be included after windows.h
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000049
Reid Klecknerffbe12f2016-01-11 20:35:45 +000050/// Determines if the program is running on Windows 8 or newer. This
Reid Kleckner5fb7a582016-01-11 23:33:03 +000051/// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
52/// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
53/// yet have VersionHelpers.h, so we have our own helper.
54inline bool RunningWindows8OrGreater() {
55 // Windows 8 is version 6.2, service pack 0.
56 OSVERSIONINFOEXW osvi = {};
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000057 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
Reid Kleckner5fb7a582016-01-11 23:33:03 +000058 osvi.dwMajorVersion = 6;
59 osvi.dwMinorVersion = 2;
60 osvi.wServicePackMajor = 0;
61
62 DWORDLONG Mask = 0;
63 Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
64 Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
65 Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
66
67 return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
68 VER_SERVICEPACKMAJOR,
69 Mask) != FALSE;
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000070}
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000071
Aaron Ballman0da8b2e2016-06-23 14:45:54 +000072inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000073 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000074 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000075 char *buffer = NULL;
Paul Robinsonaf19bc32015-11-23 17:34:20 +000076 DWORD LastError = GetLastError();
Aaron Ballman0da8b2e2016-06-23 14:45:54 +000077 DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
78 FORMAT_MESSAGE_FROM_SYSTEM |
79 FORMAT_MESSAGE_MAX_WIDTH_MASK,
80 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000081 if (R)
Paul Robinsonaf19bc32015-11-23 17:34:20 +000082 *ErrMsg = prefix + ": " + buffer;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000083 else
Paul Robinsonaf19bc32015-11-23 17:34:20 +000084 *ErrMsg = prefix + ": Unknown error";
85 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
Aaron Ballman69c55ed2013-11-18 17:43:22 +000086
Reid Spencer42bcf6e2006-08-21 06:02:44 +000087 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000088 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000089}
Jeff Cohena531d042007-03-05 05:22:08 +000090
Michael J. Spencer513f1b62011-12-12 06:03:33 +000091template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000092class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000093 typedef typename HandleTraits::handle_type handle_type;
94 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000095
Michael J. Spencer513f1b62011-12-12 06:03:33 +000096 ScopedHandle(const ScopedHandle &other); // = delete;
97 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000098public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000099 ScopedHandle()
100 : Handle(HandleTraits::GetInvalid()) {}
101
102 explicit ScopedHandle(handle_type h)
103 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +0000104
105 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000106 if (HandleTraits::IsValid(Handle))
107 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +0000108 }
109
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000110 handle_type take() {
111 handle_type t = Handle;
112 Handle = HandleTraits::GetInvalid();
113 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000114 }
115
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000116 ScopedHandle &operator=(handle_type h) {
117 if (HandleTraits::IsValid(Handle))
118 HandleTraits::Close(Handle);
119 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000120 return *this;
121 }
122
Michael J. Spencer39c46212010-12-06 04:28:13 +0000123 // True if Handle is valid.
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000124 explicit operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000125 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000126 }
127
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000128 operator handle_type() const {
129 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000130 }
Michael J. Spencer39c46212010-12-06 04:28:13 +0000131};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000132
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000133struct CommonHandleTraits {
134 typedef HANDLE handle_type;
135
136 static handle_type GetInvalid() {
137 return INVALID_HANDLE_VALUE;
138 }
139
140 static void Close(handle_type h) {
141 ::CloseHandle(h);
142 }
143
144 static bool IsValid(handle_type h) {
145 return h != GetInvalid();
146 }
147};
148
149struct JobHandleTraits : CommonHandleTraits {
150 static handle_type GetInvalid() {
151 return NULL;
152 }
153};
154
155struct CryptContextTraits : CommonHandleTraits {
156 typedef HCRYPTPROV handle_type;
157
158 static handle_type GetInvalid() {
159 return 0;
160 }
161
162 static void Close(handle_type h) {
163 ::CryptReleaseContext(h, 0);
164 }
165
166 static bool IsValid(handle_type h) {
167 return h != GetInvalid();
168 }
169};
170
Leny Kholodov1b73e662016-05-04 16:56:51 +0000171struct RegTraits : CommonHandleTraits {
172 typedef HKEY handle_type;
173
174 static handle_type GetInvalid() {
175 return NULL;
176 }
177
178 static void Close(handle_type h) {
179 ::RegCloseKey(h);
180 }
181
182 static bool IsValid(handle_type h) {
183 return h != GetInvalid();
184 }
185};
186
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000187struct FindHandleTraits : CommonHandleTraits {
188 static void Close(handle_type h) {
189 ::FindClose(h);
190 }
191};
192
193struct FileHandleTraits : CommonHandleTraits {};
194
195typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
196typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
197typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
Leny Kholodov1b73e662016-05-04 16:56:51 +0000198typedef ScopedHandle<RegTraits> ScopedRegHandle;
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000199typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
200typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000201
202namespace llvm {
203template <class T>
204class SmallVectorImpl;
205
206template <class T>
207typename SmallVectorImpl<T>::const_pointer
208c_str(SmallVectorImpl<T> &str) {
209 str.push_back(0);
210 str.pop_back();
211 return str.data();
212}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000213
214namespace sys {
Pavel Labath757ca882016-10-24 10:59:17 +0000215
216inline std::chrono::nanoseconds toDuration(FILETIME Time) {
217 ULARGE_INTEGER TimeInteger;
218 TimeInteger.LowPart = Time.dwLowDateTime;
219 TimeInteger.HighPart = Time.dwHighDateTime;
220
221 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
222 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
223}
224
225inline TimePoint<> toTimePoint(FILETIME Time) {
226 ULARGE_INTEGER TimeInteger;
227 TimeInteger.LowPart = Time.dwLowDateTime;
228 TimeInteger.HighPart = Time.dwHighDateTime;
229
230 // Adjust for different epoch
231 TimeInteger.QuadPart -= 11644473600ll * 10000000;
232
233 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
234 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
235}
236
237inline FILETIME toFILETIME(TimePoint<> TP) {
238 ULARGE_INTEGER TimeInteger;
239 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
240 TimeInteger.QuadPart += 11644473600ll * 10000000;
241
242 FILETIME Time;
243 Time.dwLowDateTime = TimeInteger.LowPart;
244 Time.dwHighDateTime = TimeInteger.HighPart;
245 return Time;
246}
247
Paul Robinsonc38deee2014-11-24 18:05:29 +0000248namespace path {
249std::error_code widenPath(const Twine &Path8,
250 SmallVectorImpl<wchar_t> &Path16);
251} // end namespace path
252
Rui Ueyama471d0c52013-09-10 19:45:51 +0000253namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000254std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
255std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
256 SmallVectorImpl<char> &utf8);
Rafael Espindola9c359662014-09-03 20:02:00 +0000257/// Convert from UTF16 to the current code page used in the system
258std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
259 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000260} // end namespace windows
261} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000262} // end namespace llvm.
Yaron Keren3f02c142015-01-21 16:20:38 +0000263
264#endif