blob: 1d1cedcce29932f1bff6c67f793358aeb39e77a6 [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
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000022// mingw-w64 tends to define it as 0x0502 in its headers.
23#undef _WIN32_WINNT
Aaron Ballman42f66222014-02-03 17:20:26 +000024#undef _WIN32_IE
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000025
NAKAMURA Takumi613655f2011-08-20 06:35:31 +000026// Require at least Windows XP(5.1) API.
27#define _WIN32_WINNT 0x0501
NAKAMURA Takumifd6cb642011-08-23 03:49:11 +000028#define _WIN32_IE 0x0600 // MinGW at it again.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000029#define WIN32_LEAN_AND_MEAN
Jeff Cohen3800a282005-07-13 02:15:18 +000030
Rui Ueyama471d0c52013-09-10 19:45:51 +000031#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringRef.h"
Paul Robinsonc38deee2014-11-24 18:05:29 +000033#include "llvm/ADT/Twine.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000034#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000035#include "llvm/Support/Compiler.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000036#include <system_error>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000037#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000038#include <wincrypt.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000039#include <cassert>
40#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000041#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000042
Reid Spencer879ed5a2006-08-23 07:30:48 +000043inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000044 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000045 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000046 char *buffer = NULL;
Aaron Ballman69c55ed2013-11-18 17:43:22 +000047 DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48 FORMAT_MESSAGE_FROM_SYSTEM,
49 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
50 if (R)
51 *ErrMsg = prefix + buffer;
52 else
53 *ErrMsg = prefix + "Unknown error";
54
Reid Spencer42bcf6e2006-08-21 06:02:44 +000055 LocalFree(buffer);
Aaron Ballman69c55ed2013-11-18 17:43:22 +000056 return R != 0;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000057}
Jeff Cohena531d042007-03-05 05:22:08 +000058
Michael J. Spencer513f1b62011-12-12 06:03:33 +000059template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000060class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000061 typedef typename HandleTraits::handle_type handle_type;
62 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000063
Michael J. Spencer513f1b62011-12-12 06:03:33 +000064 ScopedHandle(const ScopedHandle &other); // = delete;
65 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000066public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000067 ScopedHandle()
68 : Handle(HandleTraits::GetInvalid()) {}
69
70 explicit ScopedHandle(handle_type h)
71 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000072
73 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000074 if (HandleTraits::IsValid(Handle))
75 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000076 }
77
Michael J. Spencer513f1b62011-12-12 06:03:33 +000078 handle_type take() {
79 handle_type t = Handle;
80 Handle = HandleTraits::GetInvalid();
81 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000082 }
83
Michael J. Spencer513f1b62011-12-12 06:03:33 +000084 ScopedHandle &operator=(handle_type h) {
85 if (HandleTraits::IsValid(Handle))
86 HandleTraits::Close(Handle);
87 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000088 return *this;
89 }
90
Michael J. Spencer39c46212010-12-06 04:28:13 +000091 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000092 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000093 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000094 }
95
Michael J. Spencer513f1b62011-12-12 06:03:33 +000096 operator handle_type() const {
97 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000098 }
Michael J. Spencer39c46212010-12-06 04:28:13 +000099};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000100
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000101struct CommonHandleTraits {
102 typedef HANDLE handle_type;
103
104 static handle_type GetInvalid() {
105 return INVALID_HANDLE_VALUE;
106 }
107
108 static void Close(handle_type h) {
109 ::CloseHandle(h);
110 }
111
112 static bool IsValid(handle_type h) {
113 return h != GetInvalid();
114 }
115};
116
117struct JobHandleTraits : CommonHandleTraits {
118 static handle_type GetInvalid() {
119 return NULL;
120 }
121};
122
123struct CryptContextTraits : CommonHandleTraits {
124 typedef HCRYPTPROV handle_type;
125
126 static handle_type GetInvalid() {
127 return 0;
128 }
129
130 static void Close(handle_type h) {
131 ::CryptReleaseContext(h, 0);
132 }
133
134 static bool IsValid(handle_type h) {
135 return h != GetInvalid();
136 }
137};
138
139struct FindHandleTraits : CommonHandleTraits {
140 static void Close(handle_type h) {
141 ::FindClose(h);
142 }
143};
144
145struct FileHandleTraits : CommonHandleTraits {};
146
147typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
148typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
149typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
150typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
151typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000152
153namespace llvm {
154template <class T>
155class SmallVectorImpl;
156
157template <class T>
158typename SmallVectorImpl<T>::const_pointer
159c_str(SmallVectorImpl<T> &str) {
160 str.push_back(0);
161 str.pop_back();
162 return str.data();
163}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000164
165namespace sys {
Paul Robinsonc38deee2014-11-24 18:05:29 +0000166namespace path {
167std::error_code widenPath(const Twine &Path8,
168 SmallVectorImpl<wchar_t> &Path16);
169} // end namespace path
170
Rui Ueyama471d0c52013-09-10 19:45:51 +0000171namespace windows {
Rafael Espindola885719f2014-06-12 17:49:35 +0000172std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
173std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
174 SmallVectorImpl<char> &utf8);
Rafael Espindola9c359662014-09-03 20:02:00 +0000175/// Convert from UTF16 to the current code page used in the system
176std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
177 SmallVectorImpl<char> &utf8);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000178} // end namespace windows
179} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000180} // end namespace llvm.