blob: cb091f186404dd7e8193b95e04f5b063d55f6058 [file] [log] [blame]
Reid Klecknerd59e2fa2014-02-12 21:26:20 +00001//===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00008//===----------------------------------------------------------------------===//
9//
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000010// 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.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000014//
15//===----------------------------------------------------------------------===//
16
17//===----------------------------------------------------------------------===//
Jeff Cohen3800a282005-07-13 02:15:18 +000018//=== WARNING: Implementation here must contain only generic Win32 code that
19//=== is guaranteed to work on *all* Win32 variants.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000020//===----------------------------------------------------------------------===//
21
Yaron Keren3f02c142015-01-21 16:20:38 +000022#ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23#define LLVM_SUPPORT_WINDOWSSUPPORT_H
24
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000025// mingw-w64 tends to define it as 0x0502 in its headers.
26#undef _WIN32_WINNT
Aaron Ballman42f66222014-02-03 17:20:26 +000027#undef _WIN32_IE
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000028
Pawel Bylica6b129bd2015-10-15 14:50:31 +000029// 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.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000032#define WIN32_LEAN_AND_MEAN
Yunzhong Gaod7009f32016-01-06 01:36:45 +000033#ifndef NOMINMAX
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000034#define NOMINMAX
Yunzhong Gaod7009f32016-01-06 01:36:45 +000035#endif
Jeff Cohen3800a282005-07-13 02:15:18 +000036
Rui Ueyama471d0c52013-09-10 19:45:51 +000037#include "llvm/ADT/SmallVector.h"
Paul Robinsonaf19bc32015-11-23 17:34:20 +000038#include "llvm/ADT/StringExtras.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000039#include "llvm/ADT/StringRef.h"
Paul Robinsonc38deee2014-11-24 18:05:29 +000040#include "llvm/ADT/Twine.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000041#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000042#include "llvm/Support/Compiler.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000043#include <system_error>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000044#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000045#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000046#include <cassert>
47#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000048#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000049
Reid Klecknerffbe12f2016-01-11 20:35:45 +000050/// Determines if the program is running on Windows 8 or newer. This
51/// reimplements the helpers in the Windows 8.1 SDK, which are intended to
52/// supercede raw calls to GetVersionEx, because old Windows SDKs, Cygwin, and
53/// MinGW don't have VersionSupport.h yet.
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000054inline bool IsWindows8OrGreater() {
55 OSVERSIONINFO osvi = {};
56 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
57 if (!::GetVersionEx(&osvi))
58 return false;
59 return (osvi.dwMajorVersion > 6 ||
60 (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 2));
61}
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000062
Reid Spencer879ed5a2006-08-23 07:30:48 +000063inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000064 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000065 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000066 char *buffer = NULL;
Paul Robinsonaf19bc32015-11-23 17:34:20 +000067 DWORD LastError = GetLastError();
Aaron Ballman69c55ed2013-11-18 17:43:22 +000068 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
Paul Robinsonaf19bc32015-11-23 17:34:20 +000069 FORMAT_MESSAGE_FROM_SYSTEM |
70 FORMAT_MESSAGE_MAX_WIDTH_MASK,
71 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000072 if (R)
Paul Robinsonaf19bc32015-11-23 17:34:20 +000073 *ErrMsg = prefix + ": " + buffer;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000074 else
Paul Robinsonaf19bc32015-11-23 17:34:20 +000075 *ErrMsg = prefix + ": Unknown error";
76 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
Aaron Ballman69c55ed2013-11-18 17:43:22 +000077
Reid Spencer42bcf6e2006-08-21 06:02:44 +000078 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000079 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000080}
Jeff Cohena531d042007-03-05 05:22:08 +000081
Michael J. Spencer513f1b62011-12-12 06:03:33 +000082template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000083class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000084 typedef typename HandleTraits::handle_type handle_type;
85 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000086
Michael J. Spencer513f1b62011-12-12 06:03:33 +000087 ScopedHandle(const ScopedHandle &other); // = delete;
88 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000089public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000090 ScopedHandle()
91 : Handle(HandleTraits::GetInvalid()) {}
92
93 explicit ScopedHandle(handle_type h)
94 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000095
96 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000097 if (HandleTraits::IsValid(Handle))
98 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000099 }
100
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000101 handle_type take() {
102 handle_type t = Handle;
103 Handle = HandleTraits::GetInvalid();
104 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000105 }
106
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000107 ScopedHandle &operator=(handle_type h) {
108 if (HandleTraits::IsValid(Handle))
109 HandleTraits::Close(Handle);
110 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000111 return *this;
112 }
113
Michael J. Spencer39c46212010-12-06 04:28:13 +0000114 // True if Handle is valid.
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000115 explicit operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000116 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000117 }
118
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000119 operator handle_type() const {
120 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000121 }
Michael J. Spencer39c46212010-12-06 04:28:13 +0000122};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000123
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000124struct 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
140struct JobHandleTraits : CommonHandleTraits {
141 static handle_type GetInvalid() {
142 return NULL;
143 }
144};
145
146struct 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
162struct FindHandleTraits : CommonHandleTraits {
163 static void Close(handle_type h) {
164 ::FindClose(h);
165 }
166};
167
168struct FileHandleTraits : CommonHandleTraits {};
169
170typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
171typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
172typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
173typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
174typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000175
176namespace llvm {
177template <class T>
178class SmallVectorImpl;
179
180template <class T>
181typename SmallVectorImpl<T>::const_pointer
182c_str(SmallVectorImpl<T> &str) {
183 str.push_back(0);
184 str.pop_back();
185 return str.data();
186}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000187
188namespace sys {
Paul Robinsonc38deee2014-11-24 18:05:29 +0000189namespace path {
190std::error_code widenPath(const Twine &Path8,
191 SmallVectorImpl<wchar_t> &Path16);
192} // end namespace path
193
Rui Ueyama471d0c52013-09-10 19:45:51 +0000194namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000195std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
196std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
197 SmallVectorImpl<char> &utf8);
Rafael Espindola9c359662014-09-03 20:02:00 +0000198/// Convert from UTF16 to the current code page used in the system
199std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
200 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000201} // end namespace windows
202} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000203} // end namespace llvm.
Yaron Keren3f02c142015-01-21 16:20:38 +0000204
205#endif