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