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/Allocator.h"
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/VersionTuple.h"
48 #include <system_error>
51 // Must be included after windows.h
56 /// Determines if the program is running on Windows 8 or newer. This
57 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
58 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
59 /// yet have VersionHelpers.h, so we have our own helper.
60 bool RunningWindows8OrGreater();
62 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
63 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
64 /// GetVersionEx is deprecated, but this API exposes the build number which can
65 /// be useful for working around certain kernel bugs.
66 llvm::VersionTuple GetWindowsOSVersion();
68 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
70 // Include GetLastError() in a fatal error message.
71 LLVM_ATTRIBUTE_NORETURN inline void ReportLastErrorFatal(const char *Msg) {
73 MakeErrMsg(&ErrMsg, Msg);
74 llvm::report_fatal_error(ErrMsg);
77 template <typename HandleTraits>
79 typedef typename HandleTraits::handle_type handle_type;
82 ScopedHandle(const ScopedHandle &other) = delete;
83 void operator=(const ScopedHandle &other) = delete;
86 : Handle(HandleTraits::GetInvalid()) {}
88 explicit ScopedHandle(handle_type h)
92 if (HandleTraits::IsValid(Handle))
93 HandleTraits::Close(Handle);
97 handle_type t = Handle;
98 Handle = HandleTraits::GetInvalid();
102 ScopedHandle &operator=(handle_type h) {
103 if (HandleTraits::IsValid(Handle))
104 HandleTraits::Close(Handle);
109 // True if Handle is valid.
110 explicit operator bool() const {
111 return HandleTraits::IsValid(Handle) ? true : false;
114 operator handle_type() const {
119 struct CommonHandleTraits {
120 typedef HANDLE handle_type;
122 static handle_type GetInvalid() {
123 return INVALID_HANDLE_VALUE;
126 static void Close(handle_type h) {
130 static bool IsValid(handle_type h) {
131 return h != GetInvalid();
135 struct JobHandleTraits : CommonHandleTraits {
136 static handle_type GetInvalid() {
141 struct CryptContextTraits : CommonHandleTraits {
142 typedef HCRYPTPROV handle_type;
144 static handle_type GetInvalid() {
148 static void Close(handle_type h) {
149 ::CryptReleaseContext(h, 0);
152 static bool IsValid(handle_type h) {
153 return h != GetInvalid();
157 struct RegTraits : CommonHandleTraits {
158 typedef HKEY handle_type;
160 static handle_type GetInvalid() {
164 static void Close(handle_type h) {
168 static bool IsValid(handle_type h) {
169 return h != GetInvalid();
173 struct FindHandleTraits : CommonHandleTraits {
174 static void Close(handle_type h) {
179 struct FileHandleTraits : CommonHandleTraits {};
181 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
182 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
183 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
184 typedef ScopedHandle<RegTraits> ScopedRegHandle;
185 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
186 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
189 class SmallVectorImpl;
192 typename SmallVectorImpl<T>::const_pointer
193 c_str(SmallVectorImpl<T> &str) {
201 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
202 ULARGE_INTEGER TimeInteger;
203 TimeInteger.LowPart = Time.dwLowDateTime;
204 TimeInteger.HighPart = Time.dwHighDateTime;
206 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
207 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
210 inline TimePoint<> toTimePoint(FILETIME Time) {
211 ULARGE_INTEGER TimeInteger;
212 TimeInteger.LowPart = Time.dwLowDateTime;
213 TimeInteger.HighPart = Time.dwHighDateTime;
215 // Adjust for different epoch
216 TimeInteger.QuadPart -= 11644473600ll * 10000000;
218 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
219 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
222 inline FILETIME toFILETIME(TimePoint<> TP) {
223 ULARGE_INTEGER TimeInteger;
224 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
225 TimeInteger.QuadPart += 11644473600ll * 10000000;
228 Time.dwLowDateTime = TimeInteger.LowPart;
229 Time.dwHighDateTime = TimeInteger.HighPart;
234 // Returns command line arguments. Unlike arguments given to main(),
235 // this function guarantees that the returned arguments are encoded in
236 // UTF-8 regardless of the current code page setting.
237 std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
238 BumpPtrAllocator &Alloc);
239 } // end namespace windows
240 } // end namespace sys
241 } // end namespace llvm.