5e5163b3493254e7b198aa65a1690d5a9cd36006
[oota-llvm.git] / lib / Support / Windows / WindowsSupport.h
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 #define NOGDI
34 #define NOMINMAX
35
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/Compiler.h"
42 #include <system_error>
43 #include <windows.h>
44 #include <wincrypt.h>
45 #include <cassert>
46 #include <string>
47 #include <vector>
48
49 #ifndef __CYGWIN__
50 #include <VersionHelpers.h>
51 #else
52 // Cygwin does not have the IsWindows8OrGreater() API.
53 inline bool IsWindows8OrGreater() {
54   OSVERSIONINFO osvi = {};
55   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
56   if (!::GetVersionEx(&osvi))
57     return false;
58   return (osvi.dwMajorVersion > 6 ||
59           (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 2));
60 }
61 #endif // __CYGWIN__
62
63 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
64   if (!ErrMsg)
65     return true;
66   char *buffer = NULL;
67   DWORD LastError = GetLastError();
68   DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
69                           FORMAT_MESSAGE_FROM_SYSTEM |
70                           FORMAT_MESSAGE_MAX_WIDTH_MASK,
71                           NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
72   if (R)
73     *ErrMsg = prefix + ": " + buffer;
74   else
75     *ErrMsg = prefix + ": Unknown error";
76   *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
77
78   LocalFree(buffer);
79   return R != 0;
80 }
81
82 template <typename HandleTraits>
83 class ScopedHandle {
84   typedef typename HandleTraits::handle_type handle_type;
85   handle_type Handle;
86
87   ScopedHandle(const ScopedHandle &other); // = delete;
88   void operator=(const ScopedHandle &other); // = delete;
89 public:
90   ScopedHandle()
91     : Handle(HandleTraits::GetInvalid()) {}
92
93   explicit ScopedHandle(handle_type h)
94     : Handle(h) {}
95
96   ~ScopedHandle() {
97     if (HandleTraits::IsValid(Handle))
98       HandleTraits::Close(Handle);
99   }
100
101   handle_type take() {
102     handle_type t = Handle;
103     Handle = HandleTraits::GetInvalid();
104     return t;
105   }
106
107   ScopedHandle &operator=(handle_type h) {
108     if (HandleTraits::IsValid(Handle))
109       HandleTraits::Close(Handle);
110     Handle = h;
111     return *this;
112   }
113
114   // True if Handle is valid.
115   explicit operator bool() const {
116     return HandleTraits::IsValid(Handle) ? true : false;
117   }
118
119   operator handle_type() const {
120     return Handle;
121   }
122 };
123
124 struct CommonHandleTraits {
125   typedef HANDLE handle_type;
126
127   static handle_type GetInvalid() {
128     return INVALID_HANDLE_VALUE;
129   }
130
131   static void Close(handle_type h) {
132     ::CloseHandle(h);
133   }
134
135   static bool IsValid(handle_type h) {
136     return h != GetInvalid();
137   }
138 };
139
140 struct JobHandleTraits : CommonHandleTraits {
141   static handle_type GetInvalid() {
142     return NULL;
143   }
144 };
145
146 struct CryptContextTraits : CommonHandleTraits {
147   typedef HCRYPTPROV handle_type;
148
149   static handle_type GetInvalid() {
150     return 0;
151   }
152
153   static void Close(handle_type h) {
154     ::CryptReleaseContext(h, 0);
155   }
156
157   static bool IsValid(handle_type h) {
158     return h != GetInvalid();
159   }
160 };
161
162 struct FindHandleTraits : CommonHandleTraits {
163   static void Close(handle_type h) {
164     ::FindClose(h);
165   }
166 };
167
168 struct FileHandleTraits : CommonHandleTraits {};
169
170 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
171 typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
172 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
173 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
174 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
175
176 namespace llvm {
177 template <class T>
178 class SmallVectorImpl;
179
180 template <class T>
181 typename SmallVectorImpl<T>::const_pointer
182 c_str(SmallVectorImpl<T> &str) {
183   str.push_back(0);
184   str.pop_back();
185   return str.data();
186 }
187
188 namespace sys {
189 namespace path {
190 std::error_code widenPath(const Twine &Path8,
191                           SmallVectorImpl<wchar_t> &Path16);
192 } // end namespace path
193
194 namespace windows {
195 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
196 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
197                             SmallVectorImpl<char> &utf8);
198 /// Convert from UTF16 to the current code page used in the system
199 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
200                              SmallVectorImpl<char> &utf8);
201 } // end namespace windows
202 } // end namespace sys
203 } // end namespace llvm.
204
205 #endif