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/Compiler.h" 43 #include <system_error> 44 #include <windows.h> 45 #include <wincrypt.h> 46 #include <cassert> 47 #include <string> 48 49 /// Determines if the program is running on Windows 8 or newer. This 50 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended 51 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't 52 /// yet have VersionHelpers.h, so we have our own helper. 53 inline bool RunningWindows8OrGreater() { 54 // Windows 8 is version 6.2, service pack 0. 55 OSVERSIONINFOEXW osvi = {}; 56 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 57 osvi.dwMajorVersion = 6; 58 osvi.dwMinorVersion = 2; 59 osvi.wServicePackMajor = 0; 60 61 DWORDLONG Mask = 0; 62 Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL); 63 Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL); 64 Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); 65 66 return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | 67 VER_SERVICEPACKMAJOR, 68 Mask) != FALSE; 69 } 70 71 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) { 72 if (!ErrMsg) 73 return true; 74 char *buffer = NULL; 75 DWORD LastError = GetLastError(); 76 DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | 77 FORMAT_MESSAGE_FROM_SYSTEM | 78 FORMAT_MESSAGE_MAX_WIDTH_MASK, 79 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL); 80 if (R) 81 *ErrMsg = prefix + ": " + buffer; 82 else 83 *ErrMsg = prefix + ": Unknown error"; 84 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")"; 85 86 LocalFree(buffer); 87 return R != 0; 88 } 89 90 template <typename HandleTraits> 91 class ScopedHandle { 92 typedef typename HandleTraits::handle_type handle_type; 93 handle_type Handle; 94 95 ScopedHandle(const ScopedHandle &other); // = delete; 96 void operator=(const ScopedHandle &other); // = delete; 97 public: 98 ScopedHandle() 99 : Handle(HandleTraits::GetInvalid()) {} 100 101 explicit ScopedHandle(handle_type h) 102 : Handle(h) {} 103 104 ~ScopedHandle() { 105 if (HandleTraits::IsValid(Handle)) 106 HandleTraits::Close(Handle); 107 } 108 109 handle_type take() { 110 handle_type t = Handle; 111 Handle = HandleTraits::GetInvalid(); 112 return t; 113 } 114 115 ScopedHandle &operator=(handle_type h) { 116 if (HandleTraits::IsValid(Handle)) 117 HandleTraits::Close(Handle); 118 Handle = h; 119 return *this; 120 } 121 122 // True if Handle is valid. 123 explicit operator bool() const { 124 return HandleTraits::IsValid(Handle) ? true : false; 125 } 126 127 operator handle_type() const { 128 return Handle; 129 } 130 }; 131 132 struct CommonHandleTraits { 133 typedef HANDLE handle_type; 134 135 static handle_type GetInvalid() { 136 return INVALID_HANDLE_VALUE; 137 } 138 139 static void Close(handle_type h) { 140 ::CloseHandle(h); 141 } 142 143 static bool IsValid(handle_type h) { 144 return h != GetInvalid(); 145 } 146 }; 147 148 struct JobHandleTraits : CommonHandleTraits { 149 static handle_type GetInvalid() { 150 return NULL; 151 } 152 }; 153 154 struct CryptContextTraits : CommonHandleTraits { 155 typedef HCRYPTPROV handle_type; 156 157 static handle_type GetInvalid() { 158 return 0; 159 } 160 161 static void Close(handle_type h) { 162 ::CryptReleaseContext(h, 0); 163 } 164 165 static bool IsValid(handle_type h) { 166 return h != GetInvalid(); 167 } 168 }; 169 170 struct RegTraits : CommonHandleTraits { 171 typedef HKEY handle_type; 172 173 static handle_type GetInvalid() { 174 return NULL; 175 } 176 177 static void Close(handle_type h) { 178 ::RegCloseKey(h); 179 } 180 181 static bool IsValid(handle_type h) { 182 return h != GetInvalid(); 183 } 184 }; 185 186 struct FindHandleTraits : CommonHandleTraits { 187 static void Close(handle_type h) { 188 ::FindClose(h); 189 } 190 }; 191 192 struct FileHandleTraits : CommonHandleTraits {}; 193 194 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle; 195 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle; 196 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext; 197 typedef ScopedHandle<RegTraits> ScopedRegHandle; 198 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle; 199 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle; 200 201 namespace llvm { 202 template <class T> 203 class SmallVectorImpl; 204 205 template <class T> 206 typename SmallVectorImpl<T>::const_pointer 207 c_str(SmallVectorImpl<T> &str) { 208 str.push_back(0); 209 str.pop_back(); 210 return str.data(); 211 } 212 213 namespace sys { 214 namespace path { 215 std::error_code widenPath(const Twine &Path8, 216 SmallVectorImpl<wchar_t> &Path16); 217 } // end namespace path 218 219 namespace windows { 220 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16); 221 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, 222 SmallVectorImpl<char> &utf8); 223 /// Convert from UTF16 to the current code page used in the system 224 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len, 225 SmallVectorImpl<char> &utf8); 226 } // end namespace windows 227 } // end namespace sys 228 } // end namespace llvm. 229 230 #endif 231