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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <memory>
#include <cassert>
#include "vcpkg.h"
#include "vcpkg_Commands.h"
#include "metrics.h"
#include <Shlobj.h>
#include "vcpkg_Files.h"
#include "vcpkg_System.h"
#include "vcpkg_Input.h"
using namespace vcpkg;
bool g_debugging = false;
void invalid_command(const std::string& cmd)
{
System::println(System::color::error, "invalid command: %s", cmd);
print_usage();
exit(EXIT_FAILURE);
}
static void inner(const vcpkg_cmd_arguments& args)
{
TrackProperty("command", args.command);
if (args.command.empty())
{
print_usage();
exit(EXIT_FAILURE);
}
if (auto command_function = find_command(args.command, get_available_commands_type_c()))
{
return command_function(args);
}
fs::path vcpkg_root_dir;
if (args.vcpkg_root_dir != nullptr)
{
vcpkg_root_dir = fs::absolute(Strings::utf8_to_utf16(*args.vcpkg_root_dir));
}
else
{
auto vcpkg_root_dir_env = System::wdupenv_str(L"VCPKG_ROOT");
if (!vcpkg_root_dir_env.empty())
{
vcpkg_root_dir = fs::absolute(vcpkg_root_dir_env);
}
else
{
vcpkg_root_dir = Files::find_file_recursively_up(fs::absolute(System::get_exe_path_of_current_process()), ".vcpkg-root");
}
}
Checks::check_exit(!vcpkg_root_dir.empty(), "Error: Could not detect vcpkg-root.");
const expected<vcpkg_paths> expected_paths = vcpkg_paths::create(vcpkg_root_dir);
Checks::check_exit(!expected_paths.error_code(), "Error: Invalid vcpkg root directory %s: %s", vcpkg_root_dir.string(), expected_paths.error_code().message());
const vcpkg_paths paths = expected_paths.get_or_throw();
int exit_code = _wchdir(paths.root.c_str());
Checks::check_exit(exit_code == 0, "Changing the working dir failed");
if (auto command_function = find_command(args.command, get_available_commands_type_b()))
{
return command_function(args, paths);
}
triplet default_target_triplet;
if (args.target_triplet != nullptr)
{
default_target_triplet = triplet::from_canonical_name(*args.target_triplet);
}
else
{
const auto vcpkg_default_triplet_env = System::wdupenv_str(L"VCPKG_DEFAULT_TRIPLET");
if (!vcpkg_default_triplet_env.empty())
{
default_target_triplet = triplet::from_canonical_name(Strings::utf16_to_utf8(vcpkg_default_triplet_env));
}
else
{
default_target_triplet = triplet::X86_WINDOWS;
}
}
Input::check_triplet(default_target_triplet, paths);
if (auto command_function = find_command(args.command, get_available_commands_type_a()))
{
return command_function(args, paths, default_target_triplet);
}
return invalid_command(args.command);
}
static void loadConfig()
{
fs::path localappdata;
{
// Config path in AppDataLocal
wchar_t* localappdatapath = nullptr;
if (S_OK != SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localappdatapath))
__fastfail(1);
localappdata = localappdatapath;
CoTaskMemFree(localappdatapath);
}
try
{
std::string config_contents = Files::get_contents(localappdata / "vcpkg" / "config").get_or_throw();
std::unordered_map<std::string, std::string> keys;
auto pghs = parse_paragraphs(config_contents);
if (pghs.size() > 0)
keys = pghs[0];
for (size_t x = 1; x < pghs.size(); ++x)
{
for (auto&& p : pghs[x])
keys.insert(p);
}
auto user_id = keys["User-Id"];
auto user_time = keys["User-Since"];
Checks::check_throw(!user_id.empty() && !user_time.empty(), ""); // Use as goto to the catch statement
SetUserInformation(user_id, user_time);
return;
}
catch (...)
{
}
// config file not found, could not be read, or invalid
std::string user_id, user_time;
InitUserInformation(user_id, user_time);
SetUserInformation(user_id, user_time);
try
{
std::error_code ec;
fs::create_directory(localappdata / "vcpkg", ec);
std::ofstream(localappdata / "vcpkg" / "config", std::ios_base::out | std::ios_base::trunc)
<< "User-Id: " << user_id << "\n"
<< "User-Since: " << user_time << "\n";
}
catch (...)
{
}
}
static System::Stopwatch2 g_timer;
static std::string trim_path_from_command_line(const std::string& full_command_line)
{
Checks::check_exit(full_command_line.size() > 0, "Internal failure - cannot have empty command line");
if (full_command_line[0] == '"')
{
auto it = std::find(full_command_line.cbegin() + 1, full_command_line.cend(), '"');
if (it != full_command_line.cend()) // Skip over the quote
++it;
while (it != full_command_line.cend() && *it == ' ') // Skip over a space
++it;
return std::string(it, full_command_line.cend());
}
auto it = std::find(full_command_line.cbegin(), full_command_line.cend(), ' ');
while (it != full_command_line.cend() && *it == ' ')
++it;
return std::string(it, full_command_line.cend());
}
int wmain(const int argc, const wchar_t* const* const argv)
{
if (argc == 0)
std::abort();
std::cout.sync_with_stdio(false);
std::cout.imbue(std::locale::classic());
g_timer.start();
atexit([]()
{
g_timer.stop();
TrackMetric("elapsed_us", g_timer.microseconds());
Flush();
});
TrackProperty("version", version());
const std::string trimmed_command_line = trim_path_from_command_line(Strings::utf16_to_utf8(GetCommandLineW()));
TrackProperty("cmdline", trimmed_command_line);
loadConfig();
const vcpkg_cmd_arguments args = vcpkg_cmd_arguments::create_from_command_line(argc, argv);
if (args.printmetrics != opt_bool::unspecified)
SetPrintMetrics(args.printmetrics == opt_bool::enabled);
if (args.sendmetrics != opt_bool::unspecified)
SetSendMetrics(args.sendmetrics == opt_bool::enabled);
if (args.debug != opt_bool::unspecified)
{
g_debugging = (args.debug == opt_bool::enabled);
}
if (g_debugging)
{
inner(args);
exit(EXIT_FAILURE);
}
std::string exc_msg;
try
{
inner(args);
exit(EXIT_FAILURE);
}
catch (std::exception& e)
{
exc_msg = e.what();
}
catch (...)
{
exc_msg = "unknown error(...)";
}
TrackProperty("error", exc_msg);
std::cerr
<< "vcpkg.exe has crashed.\n"
<< "Please send an email to:\n"
<< " vcpkg@microsoft.com\n"
<< "containing a brief summary of what you were trying to do and the following data blob:\n"
<< "\n"
<< "Version=" << version() << "\n"
<< "EXCEPTION='" << exc_msg << "'\n"
<< "CMD=\n";
for (int x = 0; x < argc; ++x)
std::cerr << argv[x] << "|\n";
std::cerr
<< "\n";
}
|