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/Compiler.h"
43 #include <system_error>
50 /// Determines if the program is running on Windows 8 or newer. This
51 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
52 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
53 /// yet have VersionHelpers.h, so we have our own helper.
54 inline bool RunningWindows8OrGreater() {
55 // Windows 8 is version 6.2, service pack 0.
56 OSVERSIONINFOEXW osvi = {};
57 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
58 osvi.dwMajorVersion = 6;
59 osvi.dwMinorVersion = 2;
60 osvi.wServicePackMajor = 0;
63 Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
64 Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
65 Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
67 return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
72 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
76 DWORD LastError = GetLastError();
77 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
78 FORMAT_MESSAGE_FROM_SYSTEM |
79 FORMAT_MESSAGE_MAX_WIDTH_MASK,
80 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
82 *ErrMsg = prefix + ": " + buffer;
84 *ErrMsg = prefix + ": Unknown error";
85 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
91 template <typename HandleTraits>
93 typedef typename HandleTraits::handle_type handle_type;
96 ScopedHandle(const ScopedHandle &other); // = delete;
97 void operator=(const ScopedHandle &other); // = delete;
100 : Handle(HandleTraits::GetInvalid()) {}
102 explicit ScopedHandle(handle_type h)
106 if (HandleTraits::IsValid(Handle))
107 HandleTraits::Close(Handle);
111 handle_type t = Handle;
112 Handle = HandleTraits::GetInvalid();
116 ScopedHandle &operator=(handle_type h) {
117 if (HandleTraits::IsValid(Handle))
118 HandleTraits::Close(Handle);
123 // True if Handle is valid.
124 explicit operator bool() const {
125 return HandleTraits::IsValid(Handle) ? true : false;
128 operator handle_type() const {
133 struct CommonHandleTraits {
134 typedef HANDLE handle_type;
136 static handle_type GetInvalid() {
137 return INVALID_HANDLE_VALUE;
140 static void Close(handle_type h) {
144 static bool IsValid(handle_type h) {
145 return h != GetInvalid();
149 struct JobHandleTraits : CommonHandleTraits {
150 static handle_type GetInvalid() {
155 struct CryptContextTraits : CommonHandleTraits {
156 typedef HCRYPTPROV handle_type;
158 static handle_type GetInvalid() {
162 static void Close(handle_type h) {
163 ::CryptReleaseContext(h, 0);
166 static bool IsValid(handle_type h) {
167 return h != GetInvalid();
171 struct FindHandleTraits : CommonHandleTraits {
172 static void Close(handle_type h) {
177 struct FileHandleTraits : CommonHandleTraits {};
179 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
180 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
181 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
182 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
183 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
187 class SmallVectorImpl;
190 typename SmallVectorImpl<T>::const_pointer
191 c_str(SmallVectorImpl<T> &str) {
199 std::error_code widenPath(const Twine &Path8,
200 SmallVectorImpl<wchar_t> &Path16);
201 } // end namespace path
204 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
205 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
206 SmallVectorImpl<char> &utf8);
207 /// Convert from UTF16 to the current code page used in the system
208 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
209 SmallVectorImpl<char> &utf8);
210 } // end namespace windows
211 } // end namespace sys
212 } // end namespace llvm.