blob: 34d961b148d1a71ff90467f2ad85135404fcfd74 [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
Jeff Cohen3800a282005-07-13 02:15:18 +000033
Rui Ueyama471d0c52013-09-10 19:45:51 +000034#include "llvm/ADT/SmallVector.h"
Paul Robinsonaf19bc32015-11-23 17:34:20 +000035#include "llvm/ADT/StringExtras.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000036#include "llvm/ADT/StringRef.h"
Paul Robinsonc38deee2014-11-24 18:05:29 +000037#include "llvm/ADT/Twine.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000038#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000039#include "llvm/Support/Compiler.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000040#include <system_error>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000041#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000042#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000043#include <cassert>
44#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000045#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000046
Reid Spencer879ed5a2006-08-23 07:30:48 +000047inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000048 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000049 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000050 char *buffer = NULL;
Paul Robinsonaf19bc32015-11-23 17:34:20 +000051 DWORD LastError = GetLastError();
Aaron Ballman69c55ed2013-11-18 17:43:22 +000052 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
Paul Robinsonaf19bc32015-11-23 17:34:20 +000053 FORMAT_MESSAGE_FROM_SYSTEM |
54 FORMAT_MESSAGE_MAX_WIDTH_MASK,
55 NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000056 if (R)
Paul Robinsonaf19bc32015-11-23 17:34:20 +000057 *ErrMsg = prefix + ": " + buffer;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000058 else
Paul Robinsonaf19bc32015-11-23 17:34:20 +000059 *ErrMsg = prefix + ": Unknown error";
60 *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
Aaron Ballman69c55ed2013-11-18 17:43:22 +000061
Reid Spencer42bcf6e2006-08-21 06:02:44 +000062 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000063 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000064}
Jeff Cohena531d042007-03-05 05:22:08 +000065
Michael J. Spencer513f1b62011-12-12 06:03:33 +000066template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000067class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000068 typedef typename HandleTraits::handle_type handle_type;
69 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000070
Michael J. Spencer513f1b62011-12-12 06:03:33 +000071 ScopedHandle(const ScopedHandle &other); // = delete;
72 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000073public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000074 ScopedHandle()
75 : Handle(HandleTraits::GetInvalid()) {}
76
77 explicit ScopedHandle(handle_type h)
78 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000079
80 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000081 if (HandleTraits::IsValid(Handle))
82 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000083 }
84
Michael J. Spencer513f1b62011-12-12 06:03:33 +000085 handle_type take() {
86 handle_type t = Handle;
87 Handle = HandleTraits::GetInvalid();
88 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000089 }
90
Michael J. Spencer513f1b62011-12-12 06:03:33 +000091 ScopedHandle &operator=(handle_type h) {
92 if (HandleTraits::IsValid(Handle))
93 HandleTraits::Close(Handle);
94 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000095 return *this;
96 }
97
Michael J. Spencer39c46212010-12-06 04:28:13 +000098 // True if Handle is valid.
Aaron Ballmanb46962f2015-02-15 22:00:20 +000099 explicit operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000100 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +0000101 }
102
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000103 operator handle_type() const {
104 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000105 }
Michael J. Spencer39c46212010-12-06 04:28:13 +0000106};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000107
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000108struct CommonHandleTraits {
109 typedef HANDLE handle_type;
110
111 static handle_type GetInvalid() {
112 return INVALID_HANDLE_VALUE;
113 }
114
115 static void Close(handle_type h) {
116 ::CloseHandle(h);
117 }
118
119 static bool IsValid(handle_type h) {
120 return h != GetInvalid();
121 }
122};
123
124struct JobHandleTraits : CommonHandleTraits {
125 static handle_type GetInvalid() {
126 return NULL;
127 }
128};
129
130struct CryptContextTraits : CommonHandleTraits {
131 typedef HCRYPTPROV handle_type;
132
133 static handle_type GetInvalid() {
134 return 0;
135 }
136
137 static void Close(handle_type h) {
138 ::CryptReleaseContext(h, 0);
139 }
140
141 static bool IsValid(handle_type h) {
142 return h != GetInvalid();
143 }
144};
145
146struct FindHandleTraits : CommonHandleTraits {
147 static void Close(handle_type h) {
148 ::FindClose(h);
149 }
150};
151
152struct FileHandleTraits : CommonHandleTraits {};
153
154typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
155typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
156typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
157typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
158typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000159
160namespace llvm {
161template <class T>
162class SmallVectorImpl;
163
164template <class T>
165typename SmallVectorImpl<T>::const_pointer
166c_str(SmallVectorImpl<T> &str) {
167 str.push_back(0);
168 str.pop_back();
169 return str.data();
170}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000171
172namespace sys {
Paul Robinsonc38deee2014-11-24 18:05:29 +0000173namespace path {
174std::error_code widenPath(const Twine &Path8,
175 SmallVectorImpl<wchar_t> &Path16);
176} // end namespace path
177
Rui Ueyama471d0c52013-09-10 19:45:51 +0000178namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000179std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
180std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
181 SmallVectorImpl<char> &utf8);
Rafael Espindola9c359662014-09-03 20:02:00 +0000182/// Convert from UTF16 to the current code page used in the system
183std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
184 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000185} // end namespace windows
186} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000187} // end namespace llvm.
Yaron Keren3f02c142015-01-21 16:20:38 +0000188
189#endif