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