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