/
system.cpp
217 lines (187 loc) · 7.76 KB
/
system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "pch.h"
#include <vcpkg/base/checks.h>
#include <vcpkg/base/chrono.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <ctime>
using namespace vcpkg::System;
namespace vcpkg
{
Optional<CPUArchitecture> System::to_cpu_architecture(StringView arch)
{
if (Strings::case_insensitive_ascii_equals(arch, "x86")) return CPUArchitecture::X86;
if (Strings::case_insensitive_ascii_equals(arch, "x64")) return CPUArchitecture::X64;
if (Strings::case_insensitive_ascii_equals(arch, "amd64")) return CPUArchitecture::X64;
if (Strings::case_insensitive_ascii_equals(arch, "arm")) return CPUArchitecture::ARM;
if (Strings::case_insensitive_ascii_equals(arch, "arm64")) return CPUArchitecture::ARM64;
return nullopt;
}
ZStringView System::to_zstring_view(CPUArchitecture arch) noexcept
{
switch (arch)
{
case CPUArchitecture::X86: return "x86";
case CPUArchitecture::X64: return "x64";
case CPUArchitecture::ARM: return "arm";
case CPUArchitecture::ARM64: return "arm64";
default: Checks::exit_with_message(VCPKG_LINE_INFO, "unexpected vcpkg::System::CPUArchitecture");
}
}
CPUArchitecture System::get_host_processor()
{
#if defined(_WIN32)
auto w6432 = get_environment_variable("PROCESSOR_ARCHITEW6432");
if (const auto p = w6432.get()) return to_cpu_architecture(*p).value_or_exit(VCPKG_LINE_INFO);
const auto procarch = get_environment_variable("PROCESSOR_ARCHITECTURE").value_or_exit(VCPKG_LINE_INFO);
return to_cpu_architecture(procarch).value_or_exit(VCPKG_LINE_INFO);
#else // ^^^ defined(_WIN32) / !defined(_WIN32) vvv
#if defined(__x86_64__) || defined(_M_X64)
return CPUArchitecture::X64;
#elif defined(__x86__) || defined(_M_X86)
return CPUArchitecture::X86;
#elif defined(__arm__) || defined(_M_ARM)
return CPUArchitecture::ARM;
#elif defined(__aarch64__) || defined(_M_ARM64)
return CPUArchitecture::ARM64;
#else // choose architecture
#error "Unknown host architecture"
#endif // choose architecture
#endif // defined(_WIN32)
}
std::vector<CPUArchitecture> System::get_supported_host_architectures()
{
std::vector<CPUArchitecture> supported_architectures;
supported_architectures.push_back(get_host_processor());
// AMD64 machines support running x86 applications and ARM64 machines support running ARM applications
if (supported_architectures.back() == CPUArchitecture::X64)
{
supported_architectures.push_back(CPUArchitecture::X86);
}
else if (supported_architectures.back() == CPUArchitecture::ARM64)
{
supported_architectures.push_back(CPUArchitecture::ARM);
}
#if defined(_WIN32)
// On ARM32/64 Windows we can rely on x86 emulation
if (supported_architectures.front() == CPUArchitecture::ARM ||
supported_architectures.front() == CPUArchitecture::ARM64)
{
supported_architectures.push_back(CPUArchitecture::X86);
}
#endif // defined(_WIN32)
return supported_architectures;
}
Optional<std::string> System::get_environment_variable(ZStringView varname) noexcept
{
#if defined(_WIN32)
const auto w_varname = Strings::to_utf16(varname);
const auto sz = GetEnvironmentVariableW(w_varname.c_str(), nullptr, 0);
if (sz == 0) return nullopt;
std::wstring ret(sz, L'\0');
Checks::check_exit(VCPKG_LINE_INFO, MAXDWORD >= ret.size());
const auto sz2 = GetEnvironmentVariableW(w_varname.c_str(), ret.data(), static_cast<DWORD>(ret.size()));
Checks::check_exit(VCPKG_LINE_INFO, sz2 + 1 == sz);
ret.pop_back();
return Strings::to_utf8(ret.c_str());
#else // ^^^ defined(_WIN32) / !defined(_WIN32) vvv
auto v = getenv(varname.c_str());
if (!v) return nullopt;
return std::string(v);
#endif // defined(_WIN32)
}
#if defined(_WIN32)
static bool is_string_keytype(const DWORD hkey_type)
{
return hkey_type == REG_SZ || hkey_type == REG_MULTI_SZ || hkey_type == REG_EXPAND_SZ;
}
Optional<std::string> System::get_registry_string(void* base_hkey, StringView sub_key, StringView valuename)
{
HKEY k = nullptr;
const LSTATUS ec =
RegOpenKeyExW(reinterpret_cast<HKEY>(base_hkey), Strings::to_utf16(sub_key).c_str(), 0, KEY_READ, &k);
if (ec != ERROR_SUCCESS) return nullopt;
auto w_valuename = Strings::to_utf16(valuename);
DWORD dw_buffer_size = 0;
DWORD dw_type = 0;
auto rc = RegQueryValueExW(k, w_valuename.c_str(), nullptr, &dw_type, nullptr, &dw_buffer_size);
if (rc != ERROR_SUCCESS || !is_string_keytype(dw_type) || dw_buffer_size == 0 ||
dw_buffer_size % sizeof(wchar_t) != 0)
return nullopt;
std::wstring ret;
ret.resize(dw_buffer_size / sizeof(wchar_t));
rc = RegQueryValueExW(
k, w_valuename.c_str(), nullptr, &dw_type, reinterpret_cast<LPBYTE>(ret.data()), &dw_buffer_size);
if (rc != ERROR_SUCCESS || !is_string_keytype(dw_type) || dw_buffer_size != sizeof(wchar_t) * ret.size())
return nullopt;
ret.pop_back(); // remove extra trailing null byte
return Strings::to_utf8(ret);
}
#else // ^^^ defined(_WIN32) / !defined(_WIN32) vvv
Optional<std::string> System::get_registry_string(void*, StringView, StringView) { return nullopt; }
#endif // defined(_WIN32)
static const Optional<fs::path>& get_program_files()
{
static const auto PROGRAMFILES = []() -> Optional<fs::path> {
auto value = System::get_environment_variable("PROGRAMFILES");
if (auto v = value.get())
{
return *v;
}
return nullopt;
}();
return PROGRAMFILES;
}
const Optional<fs::path>& System::get_program_files_32_bit()
{
static const auto PROGRAMFILES_x86 = []() -> Optional<fs::path> {
auto value = System::get_environment_variable("ProgramFiles(x86)");
if (auto v = value.get())
{
return *v;
}
return get_program_files();
}();
return PROGRAMFILES_x86;
}
const Optional<fs::path>& System::get_program_files_platform_bitness()
{
static const auto ProgramW6432 = []() -> Optional<fs::path> {
auto value = System::get_environment_variable("ProgramW6432");
if (auto v = value.get())
{
return *v;
}
return get_program_files();
}();
return ProgramW6432;
}
int System::get_num_logical_cores() { return std::thread::hardware_concurrency(); }
Optional<CPUArchitecture> System::guess_visual_studio_prompt_target_architecture()
{
// Check for the "vsdevcmd" infrastructure used by Visual Studio 2017 and later
const auto VSCMD_ARG_TGT_ARCH = System::get_environment_variable("VSCMD_ARG_TGT_ARCH");
if (VSCMD_ARG_TGT_ARCH)
{
return to_cpu_architecture(VSCMD_ARG_TGT_ARCH.value_or_exit(VCPKG_LINE_INFO));
}
// Check for the "vcvarsall" infrastructure used by Visual Studio 2015
if (System::get_environment_variable("VCINSTALLDIR"))
{
const auto Platform = System::get_environment_variable("Platform");
if (Platform)
{
return to_cpu_architecture(Platform.value_or_exit(VCPKG_LINE_INFO));
}
else
{
return CPUArchitecture::X86;
}
}
return nullopt;
}
}
namespace vcpkg::Debug
{
std::atomic<bool> g_debugging(false);
}