1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
28
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
33 #ifndef NOMINMAX
34 #define NOMINMAX
35 #endif
36
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"
44 #include "llvm/Support/VersionTuple.h"
45 #include <cassert>
46 #include <string>
47 #include <system_error>
48 #include <windows.h>
49
50 // Must be included after windows.h
51 #include <wincrypt.h>
52
53 namespace llvm {
54
55 /// Determines if the program is running on Windows 8 or newer. This
56 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
57 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
58 /// yet have VersionHelpers.h, so we have our own helper.
59 bool RunningWindows8OrGreater();
60
61 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
62 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
63 /// GetVersionEx is deprecated, but this API exposes the build number which can
64 /// be useful for working around certain kernel bugs.
65 llvm::VersionTuple GetWindowsOSVersion();
66
67 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
68
69 template <typename HandleTraits>
70 class ScopedHandle {
71 typedef typename HandleTraits::handle_type handle_type;
72 handle_type Handle;
73
74 ScopedHandle(const ScopedHandle &other) = delete;
75 void operator=(const ScopedHandle &other) = delete;
76 public:
ScopedHandle()77 ScopedHandle()
78 : Handle(HandleTraits::GetInvalid()) {}
79
ScopedHandle(handle_type h)80 explicit ScopedHandle(handle_type h)
81 : Handle(h) {}
82
~ScopedHandle()83 ~ScopedHandle() {
84 if (HandleTraits::IsValid(Handle))
85 HandleTraits::Close(Handle);
86 }
87
take()88 handle_type take() {
89 handle_type t = Handle;
90 Handle = HandleTraits::GetInvalid();
91 return t;
92 }
93
94 ScopedHandle &operator=(handle_type h) {
95 if (HandleTraits::IsValid(Handle))
96 HandleTraits::Close(Handle);
97 Handle = h;
98 return *this;
99 }
100
101 // True if Handle is valid.
102 explicit operator bool() const {
103 return HandleTraits::IsValid(Handle) ? true : false;
104 }
105
handle_type()106 operator handle_type() const {
107 return Handle;
108 }
109 };
110
111 struct CommonHandleTraits {
112 typedef HANDLE handle_type;
113
GetInvalidCommonHandleTraits114 static handle_type GetInvalid() {
115 return INVALID_HANDLE_VALUE;
116 }
117
CloseCommonHandleTraits118 static void Close(handle_type h) {
119 ::CloseHandle(h);
120 }
121
IsValidCommonHandleTraits122 static bool IsValid(handle_type h) {
123 return h != GetInvalid();
124 }
125 };
126
127 struct JobHandleTraits : CommonHandleTraits {
GetInvalidJobHandleTraits128 static handle_type GetInvalid() {
129 return NULL;
130 }
131 };
132
133 struct CryptContextTraits : CommonHandleTraits {
134 typedef HCRYPTPROV handle_type;
135
GetInvalidCryptContextTraits136 static handle_type GetInvalid() {
137 return 0;
138 }
139
CloseCryptContextTraits140 static void Close(handle_type h) {
141 ::CryptReleaseContext(h, 0);
142 }
143
IsValidCryptContextTraits144 static bool IsValid(handle_type h) {
145 return h != GetInvalid();
146 }
147 };
148
149 struct RegTraits : CommonHandleTraits {
150 typedef HKEY handle_type;
151
GetInvalidRegTraits152 static handle_type GetInvalid() {
153 return NULL;
154 }
155
CloseRegTraits156 static void Close(handle_type h) {
157 ::RegCloseKey(h);
158 }
159
IsValidRegTraits160 static bool IsValid(handle_type h) {
161 return h != GetInvalid();
162 }
163 };
164
165 struct FindHandleTraits : CommonHandleTraits {
CloseFindHandleTraits166 static void Close(handle_type h) {
167 ::FindClose(h);
168 }
169 };
170
171 struct FileHandleTraits : CommonHandleTraits {};
172
173 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
174 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
175 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
176 typedef ScopedHandle<RegTraits> ScopedRegHandle;
177 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
178 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
179
180 template <class T>
181 class SmallVectorImpl;
182
183 template <class T>
184 typename SmallVectorImpl<T>::const_pointer
c_str(SmallVectorImpl<T> & str)185 c_str(SmallVectorImpl<T> &str) {
186 str.push_back(0);
187 str.pop_back();
188 return str.data();
189 }
190
191 namespace sys {
192
toDuration(FILETIME Time)193 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
194 ULARGE_INTEGER TimeInteger;
195 TimeInteger.LowPart = Time.dwLowDateTime;
196 TimeInteger.HighPart = Time.dwHighDateTime;
197
198 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
199 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
200 }
201
toTimePoint(FILETIME Time)202 inline TimePoint<> toTimePoint(FILETIME Time) {
203 ULARGE_INTEGER TimeInteger;
204 TimeInteger.LowPart = Time.dwLowDateTime;
205 TimeInteger.HighPart = Time.dwHighDateTime;
206
207 // Adjust for different epoch
208 TimeInteger.QuadPart -= 11644473600ll * 10000000;
209
210 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
211 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
212 }
213
toFILETIME(TimePoint<> TP)214 inline FILETIME toFILETIME(TimePoint<> TP) {
215 ULARGE_INTEGER TimeInteger;
216 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
217 TimeInteger.QuadPart += 11644473600ll * 10000000;
218
219 FILETIME Time;
220 Time.dwLowDateTime = TimeInteger.LowPart;
221 Time.dwHighDateTime = TimeInteger.HighPart;
222 return Time;
223 }
224
225 namespace windows {
226 // Returns command line arguments. Unlike arguments given to main(),
227 // this function guarantees that the returned arguments are encoded in
228 // UTF-8 regardless of the current code page setting.
229 std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
230 BumpPtrAllocator &Alloc);
231 } // end namespace windows
232 } // end namespace sys
233 } // end namespace llvm.
234
235 #endif
236