blob: 62e20f267aa1eb6cc399ba8d8de2cc9d88027896 [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
NAKAMURA Takumi613655f2011-08-20 06:35:31 +000029// Require at least Windows XP(5.1) API.
30#define _WIN32_WINNT 0x0501
NAKAMURA Takumifd6cb642011-08-23 03:49:11 +000031#define _WIN32_IE 0x0600 // MinGW at it again.
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"
35#include "llvm/ADT/StringRef.h"
Paul Robinsonc38deee2014-11-24 18:05:29 +000036#include "llvm/ADT/Twine.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000037#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000038#include "llvm/Support/Compiler.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000039#include <system_error>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000040#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000041#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000042#include <cassert>
43#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000044#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000045
Reid Spencer879ed5a2006-08-23 07:30:48 +000046inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000047 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000048 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000049 char *buffer = NULL;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000050 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
51 FORMAT_MESSAGE_FROM_SYSTEM,
52 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
53 if (R)
54 *ErrMsg = prefix + buffer;
55 else
56 *ErrMsg = prefix + "Unknown error";
57
Reid Spencer42bcf6e2006-08-21 06:02:44 +000058 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000059 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000060}
Jeff Cohena531d042007-03-05 05:22:08 +000061
Michael J. Spencer513f1b62011-12-12 06:03:33 +000062template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000063class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000064 typedef typename HandleTraits::handle_type handle_type;
65 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000066
Michael J. Spencer513f1b62011-12-12 06:03:33 +000067 ScopedHandle(const ScopedHandle &other); // = delete;
68 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000069public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000070 ScopedHandle()
71 : Handle(HandleTraits::GetInvalid()) {}
72
73 explicit ScopedHandle(handle_type h)
74 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000075
76 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000077 if (HandleTraits::IsValid(Handle))
78 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000079 }
80
Michael J. Spencer513f1b62011-12-12 06:03:33 +000081 handle_type take() {
82 handle_type t = Handle;
83 Handle = HandleTraits::GetInvalid();
84 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000085 }
86
Michael J. Spencer513f1b62011-12-12 06:03:33 +000087 ScopedHandle &operator=(handle_type h) {
88 if (HandleTraits::IsValid(Handle))
89 HandleTraits::Close(Handle);
90 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000091 return *this;
92 }
93
Michael J. Spencer39c46212010-12-06 04:28:13 +000094 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000095 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000096 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000097 }
98
Michael J. Spencer513f1b62011-12-12 06:03:33 +000099 operator handle_type() const {
100 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000101 }
Michael J. Spencer39c46212010-12-06 04:28:13 +0000102};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000103
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000104struct CommonHandleTraits {
105 typedef HANDLE handle_type;
106
107 static handle_type GetInvalid() {
108 return INVALID_HANDLE_VALUE;
109 }
110
111 static void Close(handle_type h) {
112 ::CloseHandle(h);
113 }
114
115 static bool IsValid(handle_type h) {
116 return h != GetInvalid();
117 }
118};
119
120struct JobHandleTraits : CommonHandleTraits {
121 static handle_type GetInvalid() {
122 return NULL;
123 }
124};
125
126struct CryptContextTraits : CommonHandleTraits {
127 typedef HCRYPTPROV handle_type;
128
129 static handle_type GetInvalid() {
130 return 0;
131 }
132
133 static void Close(handle_type h) {
134 ::CryptReleaseContext(h, 0);
135 }
136
137 static bool IsValid(handle_type h) {
138 return h != GetInvalid();
139 }
140};
141
142struct FindHandleTraits : CommonHandleTraits {
143 static void Close(handle_type h) {
144 ::FindClose(h);
145 }
146};
147
148struct FileHandleTraits : CommonHandleTraits {};
149
150typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
151typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
152typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
153typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
154typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000155
156namespace llvm {
157template <class T>
158class SmallVectorImpl;
159
160template <class T>
161typename SmallVectorImpl<T>::const_pointer
162c_str(SmallVectorImpl<T> &str) {
163 str.push_back(0);
164 str.pop_back();
165 return str.data();
166}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000167
168namespace sys {
Paul Robinsonc38deee2014-11-24 18:05:29 +0000169namespace path {
170std::error_code widenPath(const Twine &Path8,
171 SmallVectorImpl<wchar_t> &Path16);
172} // end namespace path
173
Rui Ueyama471d0c52013-09-10 19:45:51 +0000174namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000175std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
176std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
177 SmallVectorImpl<char> &utf8);
Rafael Espindola9c359662014-09-03 20:02:00 +0000178/// Convert from UTF16 to the current code page used in the system
179std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
180 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000181} // end namespace windows
182} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000183} // end namespace llvm.
Yaron Keren3f02c142015-01-21 16:20:38 +0000184
185#endif