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