2016-09-19 11:50:08 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2017-04-28 09:55:00 +08:00
|
|
|
#include "Paragraphs.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "metrics.h"
|
2017-04-28 09:55:00 +08:00
|
|
|
#include "vcpkg_Chrono.h"
|
|
|
|
#include "vcpkg_Commands.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_Files.h"
|
2017-08-24 07:17:53 +08:00
|
|
|
#include "vcpkg_GlobalState.h"
|
2016-10-01 07:54:07 +08:00
|
|
|
#include "vcpkg_Input.h"
|
2017-01-25 11:08:51 +08:00
|
|
|
#include "vcpkg_Strings.h"
|
2017-04-28 09:55:00 +08:00
|
|
|
#include "vcpkg_System.h"
|
2017-03-14 07:39:40 +08:00
|
|
|
#include "vcpkglib.h"
|
2017-04-28 09:55:00 +08:00
|
|
|
#include <Shlobj.h>
|
|
|
|
#include <cassert>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
using namespace vcpkg;
|
|
|
|
|
|
|
|
void invalid_command(const std::string& cmd)
|
|
|
|
{
|
2017-04-04 07:31:00 +08:00
|
|
|
System::println(System::Color::error, "invalid command: %s", cmd);
|
2017-01-13 14:03:57 +08:00
|
|
|
Commands::Help::print_usage();
|
2017-03-23 08:46:05 +08:00
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 07:00:17 +08:00
|
|
|
static void inner(const VcpkgCmdArguments& args)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-08-26 07:03:57 +08:00
|
|
|
Metrics::g_metrics.lock()->track_property("command", args.command);
|
2016-09-19 11:50:08 +08:00
|
|
|
if (args.command.empty())
|
|
|
|
{
|
2017-01-13 14:03:57 +08:00
|
|
|
Commands::Help::print_usage();
|
2017-03-23 08:46:05 +08:00
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto command_function = Commands::find(args.command, Commands::get_available_commands_type_c()))
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
return command_function(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path vcpkg_root_dir;
|
|
|
|
if (args.vcpkg_root_dir != nullptr)
|
|
|
|
{
|
2017-05-05 05:54:23 +08:00
|
|
|
vcpkg_root_dir = fs::stdfs::absolute(Strings::to_utf16(*args.vcpkg_root_dir));
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-05 05:58:18 +08:00
|
|
|
const Optional<std::wstring> vcpkg_root_dir_env = System::get_environment_variable(L"VCPKG_ROOT");
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto v = vcpkg_root_dir_env.get())
|
2016-09-20 10:12:46 +08:00
|
|
|
{
|
2017-04-12 06:16:39 +08:00
|
|
|
vcpkg_root_dir = fs::stdfs::absolute(*v);
|
2016-09-20 10:12:46 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
else
|
2016-09-20 10:12:46 +08:00
|
|
|
{
|
2017-04-28 09:55:00 +08:00
|
|
|
vcpkg_root_dir = Files::get_real_filesystem().find_file_recursively_up(
|
|
|
|
fs::stdfs::absolute(System::get_exe_path_of_current_process()), ".vcpkg-root");
|
2016-09-20 10:12:46 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 08:38:04 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, !vcpkg_root_dir.empty(), "Error: Could not detect vcpkg-root.");
|
2016-09-20 10:12:46 +08:00
|
|
|
|
2017-04-04 07:29:11 +08:00
|
|
|
const Expected<VcpkgPaths> expected_paths = VcpkgPaths::create(vcpkg_root_dir);
|
2017-04-28 09:55:00 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO,
|
2017-06-06 06:58:47 +08:00
|
|
|
!expected_paths.error(),
|
2017-04-28 09:55:00 +08:00
|
|
|
"Error: Invalid vcpkg root directory %s: %s",
|
|
|
|
vcpkg_root_dir.string(),
|
2017-06-06 06:58:47 +08:00
|
|
|
expected_paths.error().message());
|
2017-04-04 07:29:11 +08:00
|
|
|
const VcpkgPaths paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
|
2017-08-29 07:59:59 +08:00
|
|
|
const int exit_code = _wchdir(paths.root.c_str());
|
2017-03-14 08:38:04 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, exit_code == 0, "Changing the working dir failed");
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto command_function = Commands::find(args.command, Commands::get_available_commands_type_b()))
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
return command_function(args, paths);
|
|
|
|
}
|
|
|
|
|
2017-04-11 04:00:33 +08:00
|
|
|
Triplet default_triplet;
|
2017-04-11 04:01:43 +08:00
|
|
|
if (args.triplet != nullptr)
|
2016-09-22 18:00:27 +08:00
|
|
|
{
|
2017-04-11 04:01:43 +08:00
|
|
|
default_triplet = Triplet::from_canonical_name(*args.triplet);
|
2016-09-22 18:00:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-28 09:55:00 +08:00
|
|
|
const Optional<std::wstring> vcpkg_default_triplet_env =
|
2017-05-05 05:58:18 +08:00
|
|
|
System::get_environment_variable(L"VCPKG_DEFAULT_TRIPLET");
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto v = vcpkg_default_triplet_env.get())
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-05-05 05:54:23 +08:00
|
|
|
default_triplet = Triplet::from_canonical_name(Strings::to_utf8(*v));
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2016-09-23 15:44:30 +08:00
|
|
|
else
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-04-11 04:00:33 +08:00
|
|
|
default_triplet = Triplet::X86_WINDOWS;
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 04:00:33 +08:00
|
|
|
Input::check_triplet(default_triplet, paths);
|
2016-09-23 15:44:30 +08:00
|
|
|
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto command_function = Commands::find(args.command, Commands::get_available_commands_type_a()))
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-04-11 04:00:33 +08:00
|
|
|
return command_function(args, paths, default_triplet);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return invalid_command(args.command);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadConfig()
|
|
|
|
{
|
|
|
|
fs::path localappdata;
|
|
|
|
{
|
|
|
|
// Config path in AppDataLocal
|
|
|
|
wchar_t* localappdatapath = nullptr;
|
2017-04-28 09:55:00 +08:00
|
|
|
if (S_OK != SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localappdatapath)) __fastfail(1);
|
2016-09-19 11:50:08 +08:00
|
|
|
localappdata = localappdatapath;
|
|
|
|
CoTaskMemFree(localappdatapath);
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-04-09 07:26:26 +08:00
|
|
|
auto maybe_pghs = Paragraphs::get_paragraphs(Files::get_real_filesystem(), localappdata / "vcpkg" / "config");
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto p_pghs = maybe_pghs.get())
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-03-30 00:49:09 +08:00
|
|
|
const auto& pghs = *p_pghs;
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> keys;
|
2017-04-28 09:55:00 +08:00
|
|
|
if (pghs.size() > 0) keys = pghs[0];
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-03-30 00:49:09 +08:00
|
|
|
for (size_t x = 1; x < pghs.size(); ++x)
|
|
|
|
{
|
|
|
|
for (auto&& p : pghs[x])
|
|
|
|
keys.insert(p);
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-03-30 00:49:09 +08:00
|
|
|
auto user_id = keys["User-Id"];
|
|
|
|
auto user_time = keys["User-Since"];
|
2017-04-01 08:00:24 +08:00
|
|
|
if (!user_id.empty() && !user_time.empty())
|
|
|
|
{
|
2017-08-26 07:03:57 +08:00
|
|
|
Metrics::g_metrics.lock()->set_user_information(user_id, user_time);
|
2017-04-01 08:00:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-03-30 00:49:09 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2017-04-28 09:55:00 +08:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
// config file not found, could not be read, or invalid
|
|
|
|
std::string user_id, user_time;
|
2017-08-26 07:03:57 +08:00
|
|
|
{
|
|
|
|
auto locked_metrics = Metrics::g_metrics.lock();
|
|
|
|
locked_metrics->init_user_information(user_id, user_time);
|
|
|
|
locked_metrics->set_user_information(user_id, user_time);
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::error_code ec;
|
2017-04-13 13:48:52 +08:00
|
|
|
auto& fs = Files::get_real_filesystem();
|
|
|
|
fs.create_directory(localappdata / "vcpkg", ec);
|
|
|
|
fs.write_contents(localappdata / "vcpkg" / "config",
|
2017-04-28 09:55:00 +08:00
|
|
|
Strings::format("User-Id: %s\n"
|
|
|
|
"User-Since: %s\n",
|
|
|
|
user_id,
|
|
|
|
user_time));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string trim_path_from_command_line(const std::string& full_command_line)
|
|
|
|
{
|
2017-04-28 09:55:00 +08:00
|
|
|
Checks::check_exit(
|
|
|
|
VCPKG_LINE_INFO, full_command_line.size() > 0, "Internal failure - cannot have empty command line");
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-04-28 09:55:00 +08:00
|
|
|
if (argc == 0) std::abort();
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-08-26 07:55:14 +08:00
|
|
|
GlobalState::g_init_console_cp = GetConsoleCP();
|
|
|
|
GlobalState::g_init_console_output_cp = GetConsoleOutputCP();
|
2017-08-23 05:48:39 +08:00
|
|
|
|
2017-08-26 07:55:14 +08:00
|
|
|
SetConsoleCP(65001);
|
|
|
|
SetConsoleOutputCP(65001);
|
2017-08-24 07:17:53 +08:00
|
|
|
|
2017-08-26 07:55:14 +08:00
|
|
|
*GlobalState::timer.lock() = ElapsedTime::create_started();
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-05-05 05:54:23 +08:00
|
|
|
const std::string trimmed_command_line = trim_path_from_command_line(Strings::to_utf8(GetCommandLineW()));
|
2017-08-26 07:03:57 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
auto locked_metrics = Metrics::g_metrics.lock();
|
|
|
|
locked_metrics->track_property("version", Commands::Version::version());
|
|
|
|
locked_metrics->track_property("cmdline", trimmed_command_line);
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
loadConfig();
|
2017-08-26 07:03:57 +08:00
|
|
|
Metrics::g_metrics.lock()->track_property("sqmuser", Metrics::get_SQM_user());
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-04-04 07:00:17 +08:00
|
|
|
const VcpkgCmdArguments args = VcpkgCmdArguments::create_from_command_line(argc, argv);
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-08-29 07:59:59 +08:00
|
|
|
if (const auto p = args.printmetrics.get()) Metrics::g_metrics.lock()->set_print_metrics(*p);
|
|
|
|
if (const auto p = args.sendmetrics.get()) Metrics::g_metrics.lock()->set_send_metrics(*p);
|
|
|
|
if (const auto p = args.debug.get()) GlobalState::debugging = *p;
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-08-26 07:55:14 +08:00
|
|
|
Checks::register_console_ctrl_handler();
|
2017-08-26 07:03:57 +08:00
|
|
|
|
2017-08-24 07:17:53 +08:00
|
|
|
if (GlobalState::debugging)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
inner(args);
|
2017-03-23 08:46:05 +08:00
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string exc_msg;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
inner(args);
|
2017-03-23 08:46:05 +08:00
|
|
|
Checks::exit_fail(VCPKG_LINE_INFO);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
exc_msg = e.what();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
exc_msg = "unknown error(...)";
|
|
|
|
}
|
2017-08-26 07:03:57 +08:00
|
|
|
Metrics::g_metrics.lock()->track_property("error", exc_msg);
|
2017-03-04 22:10:59 +08:00
|
|
|
|
|
|
|
fflush(stdout);
|
2017-04-28 09:55:00 +08:00
|
|
|
System::print("vcpkg.exe has crashed.\n"
|
|
|
|
"Please send an email to:\n"
|
|
|
|
" %s\n"
|
|
|
|
"containing a brief summary of what you were trying to do and the following data blob:\n"
|
|
|
|
"\n"
|
|
|
|
"Version=%s\n"
|
|
|
|
"EXCEPTION='%s'\n"
|
|
|
|
"CMD=\n",
|
|
|
|
Commands::Contact::email(),
|
|
|
|
Commands::Version::version(),
|
|
|
|
exc_msg);
|
2017-03-04 22:10:59 +08:00
|
|
|
fflush(stdout);
|
2016-09-19 11:50:08 +08:00
|
|
|
for (int x = 0; x < argc; ++x)
|
2017-05-05 05:54:23 +08:00
|
|
|
System::println("%s|", Strings::to_utf8(argv[x]));
|
2017-03-04 22:10:59 +08:00
|
|
|
fflush(stdout);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|