1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines things specific to Windows implementations. In addition to
10 // providing some helpers for working with win32 APIs, this header wraps
11 // <windows.h> with some portability macros. Always include WindowsSupport.h
12 // instead of including <windows.h> directly.
14 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
17 //=== WARNING: Implementation here must contain only generic Win32 code that
18 //=== is guaranteed to work on *all* Win32 variants.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
22 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24 // mingw-w64 tends to define it as 0x0502 in its headers.
28 // Require at least Windows 7 API.
29 #define _WIN32_WINNT 0x0601
30 #define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
31 #define WIN32_LEAN_AND_MEAN
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/ADT/Twine.h"
40 #include "llvm/Config/config.h" // Get build system configuration settings
41 #include "llvm/Support/Chrono.h"
42 #include "llvm/Support/Compiler.h"
43 #include "llvm/Support/VersionTuple.h"
46 #include <system_error>
49 // Must be included after windows.h
54 /// Determines if the program is running on Windows 8 or newer. This
55 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
56 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
57 /// yet have VersionHelpers.h, so we have our own helper.
58 bool RunningWindows8OrGreater();
60 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
61 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
62 /// GetVersionEx is deprecated, but this API exposes the build number which can
63 /// be useful for working around certain kernel bugs.
64 llvm::VersionTuple
GetWindowsOSVersion();
66 bool MakeErrMsg(std::string
*ErrMsg
, const std::string
&prefix
);
68 template <typename HandleTraits
>
70 typedef typename
HandleTraits::handle_type handle_type
;
73 ScopedHandle(const ScopedHandle
&other
) = delete;
74 void operator=(const ScopedHandle
&other
) = delete;
77 : Handle(HandleTraits::GetInvalid()) {}
79 explicit ScopedHandle(handle_type h
)
83 if (HandleTraits::IsValid(Handle
))
84 HandleTraits::Close(Handle
);
88 handle_type t
= Handle
;
89 Handle
= HandleTraits::GetInvalid();
93 ScopedHandle
&operator=(handle_type h
) {
94 if (HandleTraits::IsValid(Handle
))
95 HandleTraits::Close(Handle
);
100 // True if Handle is valid.
101 explicit operator bool() const {
102 return HandleTraits::IsValid(Handle
) ? true : false;
105 operator handle_type() const {
110 struct CommonHandleTraits
{
111 typedef HANDLE handle_type
;
113 static handle_type
GetInvalid() {
114 return INVALID_HANDLE_VALUE
;
117 static void Close(handle_type h
) {
121 static bool IsValid(handle_type h
) {
122 return h
!= GetInvalid();
126 struct JobHandleTraits
: CommonHandleTraits
{
127 static handle_type
GetInvalid() {
132 struct CryptContextTraits
: CommonHandleTraits
{
133 typedef HCRYPTPROV handle_type
;
135 static handle_type
GetInvalid() {
139 static void Close(handle_type h
) {
140 ::CryptReleaseContext(h
, 0);
143 static bool IsValid(handle_type h
) {
144 return h
!= GetInvalid();
148 struct RegTraits
: CommonHandleTraits
{
149 typedef HKEY handle_type
;
151 static handle_type
GetInvalid() {
155 static void Close(handle_type h
) {
159 static bool IsValid(handle_type h
) {
160 return h
!= GetInvalid();
164 struct FindHandleTraits
: CommonHandleTraits
{
165 static void Close(handle_type h
) {
170 struct FileHandleTraits
: CommonHandleTraits
{};
172 typedef ScopedHandle
<CommonHandleTraits
> ScopedCommonHandle
;
173 typedef ScopedHandle
<FileHandleTraits
> ScopedFileHandle
;
174 typedef ScopedHandle
<CryptContextTraits
> ScopedCryptContext
;
175 typedef ScopedHandle
<RegTraits
> ScopedRegHandle
;
176 typedef ScopedHandle
<FindHandleTraits
> ScopedFindHandle
;
177 typedef ScopedHandle
<JobHandleTraits
> ScopedJobHandle
;
180 class SmallVectorImpl
;
183 typename SmallVectorImpl
<T
>::const_pointer
184 c_str(SmallVectorImpl
<T
> &str
) {
192 inline std::chrono::nanoseconds
toDuration(FILETIME Time
) {
193 ULARGE_INTEGER TimeInteger
;
194 TimeInteger
.LowPart
= Time
.dwLowDateTime
;
195 TimeInteger
.HighPart
= Time
.dwHighDateTime
;
197 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
198 return std::chrono::nanoseconds(100 * TimeInteger
.QuadPart
);
201 inline TimePoint
<> toTimePoint(FILETIME Time
) {
202 ULARGE_INTEGER TimeInteger
;
203 TimeInteger
.LowPart
= Time
.dwLowDateTime
;
204 TimeInteger
.HighPart
= Time
.dwHighDateTime
;
206 // Adjust for different epoch
207 TimeInteger
.QuadPart
-= 11644473600ll * 10000000;
209 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
210 return TimePoint
<>(std::chrono::nanoseconds(100 * TimeInteger
.QuadPart
));
213 inline FILETIME
toFILETIME(TimePoint
<> TP
) {
214 ULARGE_INTEGER TimeInteger
;
215 TimeInteger
.QuadPart
= TP
.time_since_epoch().count() / 100;
216 TimeInteger
.QuadPart
+= 11644473600ll * 10000000;
219 Time
.dwLowDateTime
= TimeInteger
.LowPart
;
220 Time
.dwHighDateTime
= TimeInteger
.HighPart
;
225 // Returns command line arguments. Unlike arguments given to main(),
226 // this function guarantees that the returned arguments are encoded in
227 // UTF-8 regardless of the current code page setting.
228 std::error_code
GetCommandLineArguments(SmallVectorImpl
<const char *> &Args
,
229 BumpPtrAllocator
&Alloc
);
230 } // end namespace windows
231 } // end namespace sys
232 } // end namespace llvm.