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"
44 #include "llvm/Support/VersionTuple.h"
47 #include <system_error>
50 // Must be included after windows.h
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();
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();
67 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
69 template <typename HandleTraits>
71 typedef typename HandleTraits::handle_type handle_type;
74 ScopedHandle(const ScopedHandle &other) = delete;
75 void operator=(const ScopedHandle &other) = delete;
78 : Handle(HandleTraits::GetInvalid()) {}
80 explicit ScopedHandle(handle_type h)
84 if (HandleTraits::IsValid(Handle))
85 HandleTraits::Close(Handle);
89 handle_type t = Handle;
90 Handle = HandleTraits::GetInvalid();
94 ScopedHandle &operator=(handle_type h) {
95 if (HandleTraits::IsValid(Handle))
96 HandleTraits::Close(Handle);
101 // True if Handle is valid.
102 explicit operator bool() const {
103 return HandleTraits::IsValid(Handle) ? true : false;
106 operator handle_type() const {
111 struct CommonHandleTraits {
112 typedef HANDLE handle_type;
114 static handle_type GetInvalid() {
115 return INVALID_HANDLE_VALUE;
118 static void Close(handle_type h) {
122 static bool IsValid(handle_type h) {
123 return h != GetInvalid();
127 struct JobHandleTraits : CommonHandleTraits {
128 static handle_type GetInvalid() {
133 struct CryptContextTraits : CommonHandleTraits {
134 typedef HCRYPTPROV handle_type;
136 static handle_type GetInvalid() {
140 static void Close(handle_type h) {
141 ::CryptReleaseContext(h, 0);
144 static bool IsValid(handle_type h) {
145 return h != GetInvalid();
149 struct RegTraits : CommonHandleTraits {
150 typedef HKEY handle_type;
152 static handle_type GetInvalid() {
156 static void Close(handle_type h) {
160 static bool IsValid(handle_type h) {
161 return h != GetInvalid();
165 struct FindHandleTraits : CommonHandleTraits {
166 static void Close(handle_type h) {
171 struct FileHandleTraits : CommonHandleTraits {};
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;
181 class SmallVectorImpl;
184 typename SmallVectorImpl<T>::const_pointer
185 c_str(SmallVectorImpl<T> &str) {
193 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
194 ULARGE_INTEGER TimeInteger;
195 TimeInteger.LowPart = Time.dwLowDateTime;
196 TimeInteger.HighPart = Time.dwHighDateTime;
198 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
199 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
202 inline TimePoint<> toTimePoint(FILETIME Time) {
203 ULARGE_INTEGER TimeInteger;
204 TimeInteger.LowPart = Time.dwLowDateTime;
205 TimeInteger.HighPart = Time.dwHighDateTime;
207 // Adjust for different epoch
208 TimeInteger.QuadPart -= 11644473600ll * 10000000;
210 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
211 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
214 inline FILETIME toFILETIME(TimePoint<> TP) {
215 ULARGE_INTEGER TimeInteger;
216 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
217 TimeInteger.QuadPart += 11644473600ll * 10000000;
220 Time.dwLowDateTime = TimeInteger.LowPart;
221 Time.dwHighDateTime = TimeInteger.HighPart;
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.