1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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.
15 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
25 // mingw-w64 tends to define it as 0x0502 in its headers.
29 // 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.
32 #define WIN32_LEAN_AND_MEAN
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Config/config.h" // Get build system configuration settings
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
46 #include <system_error>
49 // Must be included after windows.h
52 /// Determines if the program is running on Windows 8 or newer. This
53 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
54 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
55 /// yet have VersionHelpers.h, so we have our own helper.
56 inline bool RunningWindows8OrGreater() {
57 // Windows 8 is version 6.2, service pack 0.
58 OSVERSIONINFOEXW osvi
= {};
59 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
60 osvi
.dwMajorVersion
= 6;
61 osvi
.dwMinorVersion
= 2;
62 osvi
.wServicePackMajor
= 0;
65 Mask
= VerSetConditionMask(Mask
, VER_MAJORVERSION
, VER_GREATER_EQUAL
);
66 Mask
= VerSetConditionMask(Mask
, VER_MINORVERSION
, VER_GREATER_EQUAL
);
67 Mask
= VerSetConditionMask(Mask
, VER_SERVICEPACKMAJOR
, VER_GREATER_EQUAL
);
69 return VerifyVersionInfoW(&osvi
, VER_MAJORVERSION
| VER_MINORVERSION
|
74 inline bool MakeErrMsg(std::string
*ErrMsg
, const std::string
&prefix
) {
78 DWORD LastError
= GetLastError();
79 DWORD R
= FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
80 FORMAT_MESSAGE_FROM_SYSTEM
|
81 FORMAT_MESSAGE_MAX_WIDTH_MASK
,
82 NULL
, LastError
, 0, (LPSTR
)&buffer
, 1, NULL
);
84 *ErrMsg
= prefix
+ ": " + buffer
;
86 *ErrMsg
= prefix
+ ": Unknown error";
87 *ErrMsg
+= " (0x" + llvm::utohexstr(LastError
) + ")";
93 template <typename HandleTraits
>
95 typedef typename
HandleTraits::handle_type handle_type
;
98 ScopedHandle(const ScopedHandle
&other
); // = delete;
99 void operator=(const ScopedHandle
&other
); // = delete;
102 : Handle(HandleTraits::GetInvalid()) {}
104 explicit ScopedHandle(handle_type h
)
108 if (HandleTraits::IsValid(Handle
))
109 HandleTraits::Close(Handle
);
113 handle_type t
= Handle
;
114 Handle
= HandleTraits::GetInvalid();
118 ScopedHandle
&operator=(handle_type h
) {
119 if (HandleTraits::IsValid(Handle
))
120 HandleTraits::Close(Handle
);
125 // True if Handle is valid.
126 explicit operator bool() const {
127 return HandleTraits::IsValid(Handle
) ? true : false;
130 operator handle_type() const {
135 struct CommonHandleTraits
{
136 typedef HANDLE handle_type
;
138 static handle_type
GetInvalid() {
139 return INVALID_HANDLE_VALUE
;
142 static void Close(handle_type h
) {
146 static bool IsValid(handle_type h
) {
147 return h
!= GetInvalid();
151 struct JobHandleTraits
: CommonHandleTraits
{
152 static handle_type
GetInvalid() {
157 struct CryptContextTraits
: CommonHandleTraits
{
158 typedef HCRYPTPROV handle_type
;
160 static handle_type
GetInvalid() {
164 static void Close(handle_type h
) {
165 ::CryptReleaseContext(h
, 0);
168 static bool IsValid(handle_type h
) {
169 return h
!= GetInvalid();
173 struct RegTraits
: CommonHandleTraits
{
174 typedef HKEY handle_type
;
176 static handle_type
GetInvalid() {
180 static void Close(handle_type h
) {
184 static bool IsValid(handle_type h
) {
185 return h
!= GetInvalid();
189 struct FindHandleTraits
: CommonHandleTraits
{
190 static void Close(handle_type h
) {
195 struct FileHandleTraits
: CommonHandleTraits
{};
197 typedef ScopedHandle
<CommonHandleTraits
> ScopedCommonHandle
;
198 typedef ScopedHandle
<FileHandleTraits
> ScopedFileHandle
;
199 typedef ScopedHandle
<CryptContextTraits
> ScopedCryptContext
;
200 typedef ScopedHandle
<RegTraits
> ScopedRegHandle
;
201 typedef ScopedHandle
<FindHandleTraits
> ScopedFindHandle
;
202 typedef ScopedHandle
<JobHandleTraits
> ScopedJobHandle
;
206 class SmallVectorImpl
;
209 typename SmallVectorImpl
<T
>::const_pointer
210 c_str(SmallVectorImpl
<T
> &str
) {
218 inline std::chrono::nanoseconds
toDuration(FILETIME Time
) {
219 ULARGE_INTEGER TimeInteger
;
220 TimeInteger
.LowPart
= Time
.dwLowDateTime
;
221 TimeInteger
.HighPart
= Time
.dwHighDateTime
;
223 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
224 return std::chrono::nanoseconds(100 * TimeInteger
.QuadPart
);
227 inline TimePoint
<> toTimePoint(FILETIME Time
) {
228 ULARGE_INTEGER TimeInteger
;
229 TimeInteger
.LowPart
= Time
.dwLowDateTime
;
230 TimeInteger
.HighPart
= Time
.dwHighDateTime
;
232 // Adjust for different epoch
233 TimeInteger
.QuadPart
-= 11644473600ll * 10000000;
235 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
236 return TimePoint
<>(std::chrono::nanoseconds(100 * TimeInteger
.QuadPart
));
239 inline FILETIME
toFILETIME(TimePoint
<> TP
) {
240 ULARGE_INTEGER TimeInteger
;
241 TimeInteger
.QuadPart
= TP
.time_since_epoch().count() / 100;
242 TimeInteger
.QuadPart
+= 11644473600ll * 10000000;
245 Time
.dwLowDateTime
= TimeInteger
.LowPart
;
246 Time
.dwHighDateTime
= TimeInteger
.HighPart
;
251 // Returns command line arguments. Unlike arguments given to main(),
252 // this function guarantees that the returned arguments are encoded in
253 // UTF-8 regardless of the current code page setting.
254 std::error_code
GetCommandLineArguments(SmallVectorImpl
<const char *> &Args
,
255 BumpPtrAllocator
&Alloc
);
256 } // end namespace windows
257 } // end namespace sys
258 } // end namespace llvm.