diff options
Diffstat (limited to 'toolsrc/src/vcpkg.cpp')
| -rw-r--r-- | toolsrc/src/vcpkg.cpp | 345 |
1 files changed, 208 insertions, 137 deletions
diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index f705858cc..3e313c702 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/src/vcpkg.cpp @@ -1,178 +1,249 @@ -#include "vcpkg.h" -#include <regex> +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> + +#include <iostream> +#include <fstream> +#include <memory> +#include <cassert> +#include "vcpkg_Commands.h" +#include "metrics.h" +#include <Shlobj.h> #include "vcpkg_Files.h" -#include "vcpkglib_helpers.h" +#include "vcpkg_System.h" +#include "vcpkg_Input.h" +#include "Paragraphs.h" +#include "vcpkg_info.h" +#include "vcpkg_Strings.h" -namespace +using namespace vcpkg; + +bool g_debugging = false; + +void invalid_command(const std::string& cmd) { - using namespace vcpkg; + System::println(System::color::error, "invalid command: %s", cmd); + Commands::Help::print_usage(); + exit(EXIT_FAILURE); +} - struct Parser +static void inner(const vcpkg_cmd_arguments& args) +{ + TrackProperty("command", args.command); + if (args.command.empty()) { - Parser(const char* c, const char* e) : cur(c), end(e) - { - } + Commands::Help::print_usage(); + exit(EXIT_FAILURE); + } - private: - const char* cur; - const char* const end; + if (auto command_function = Commands::find(args.command, Commands::get_available_commands_type_c())) + { + return command_function(args); + } - void peek(char& ch) const - { - if (cur == end) - ch = 0; - else - ch = *cur; - } + 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"); - void next(char& ch) + if (!vcpkg_root_dir_env.empty()) { - if (cur == end) - ch = 0; - else - { - ++cur; - peek(ch); - } + vcpkg_root_dir = fs::absolute(vcpkg_root_dir_env); } - - void skip_spaces(char& ch) + else { - while (ch == ' ' || ch == '\t') - next(ch); + vcpkg_root_dir = Files::find_file_recursively_up(fs::absolute(System::get_exe_path_of_current_process()), ".vcpkg-root"); } + } - static bool is_alphanum(char ch) - { - return (ch >= 'A' && ch <= 'Z') - || (ch >= 'a' && ch <= 'z') - || (ch >= '0' && ch <= '9'); - } + Checks::check_exit(!vcpkg_root_dir.empty(), "Error: Could not detect vcpkg-root."); - static bool is_lineend(char ch) - { - return ch == '\r' || ch == '\n' || ch == 0; - } + 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"); - void get_fieldvalue(char& ch, std::string& fieldvalue) + if (auto command_function = Commands::find(args.command, Commands::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()) { - fieldvalue.clear(); - - auto beginning_of_line = cur; - do - { - // scan to end of current line (it is part of the field value) - while (!is_lineend(ch)) - next(ch); - - fieldvalue.append(beginning_of_line, cur); - - if (ch == '\r') - next(ch); - if (ch == '\n') - next(ch); - - if (is_alphanum(ch)) - { - // Line begins a new field. - return; - } - - beginning_of_line = cur; - - // Line may continue the current field with data or terminate the paragraph, - // depending on first nonspace character. - skip_spaces(ch); - - if (is_lineend(ch)) - { - // Line was whitespace or empty. - // This terminates the field and the paragraph. - // We leave the blank line's whitespace consumed, because it doesn't matter. - return; - } - - // First nonspace is not a newline. This continues the current field value. - // We forcibly convert all newlines into single '\n' for ease of text handling later on. - fieldvalue.push_back('\n'); - } - while (true); + default_target_triplet = triplet::from_canonical_name(Strings::utf16_to_utf8(vcpkg_default_triplet_env)); } - - void get_fieldname(char& ch, std::string& fieldname) + else { - auto begin_fieldname = cur; - while (is_alphanum(ch) || ch == '-') - next(ch); - Checks::check_throw(ch == ':', "Expected ':'"); - fieldname = std::string(begin_fieldname, cur); - - // skip ': ' - next(ch); - skip_spaces(ch); + default_target_triplet = triplet::X86_WINDOWS; } + } - void get_paragraph(char& ch, std::unordered_map<std::string, std::string>& fields) - { - fields.clear(); - std::string fieldname; - std::string fieldvalue; - do - { - get_fieldname(ch, fieldname); + Input::check_triplet(default_target_triplet, paths); - auto it = fields.find(fieldname); - Checks::check_throw(it == fields.end(), "Duplicate field"); + if (auto command_function = Commands::find(args.command, Commands::get_available_commands_type_a())) + { + return command_function(args, paths, default_target_triplet); + } - get_fieldvalue(ch, fieldvalue); + return invalid_command(args.command); +} - fields.emplace(fieldname, fieldvalue); - } - while (!is_lineend(ch)); - } +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::read_contents(localappdata / "vcpkg" / "config").get_or_throw(); + + std::unordered_map<std::string, std::string> keys; + auto pghs = Paragraphs::parse_paragraphs(config_contents); + if (pghs.size() > 0) + keys = pghs[0]; - public: - std::vector<std::unordered_map<std::string, std::string>> get_paragraphs() + for (size_t x = 1; x < pghs.size(); ++x) { - std::vector<std::unordered_map<std::string, std::string>> paragraphs; + for (auto&& p : pghs[x]) + keys.insert(p); + } - char ch; - peek(ch); + 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 - while (ch != 0) - { - if (ch == '\n' || ch == '\r' || ch == ' ' || ch == '\t') - { - next(ch); - continue; - } + SetUserInformation(user_id, user_time); + return; + } + catch (...) + { + } - paragraphs.emplace_back(); - get_paragraph(ch, paragraphs.back()); - } + // 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 (...) + { + } +} - return paragraphs; - } - }; +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()); } -namespace vcpkg +int wmain(const int argc, const wchar_t* const* const argv) { - std::string shorten_description(const std::string& desc) + 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", Info::version()); + + const std::string trimmed_command_line = trim_path_from_command_line(Strings::utf16_to_utf8(GetCommandLineW())); + TrackProperty("cmdline", trimmed_command_line); + loadConfig(); + TrackProperty("sqmuser", GetSQMUser()); + + const vcpkg_cmd_arguments args = vcpkg_cmd_arguments::create_from_command_line(argc, argv); + + if (args.printmetrics != opt_bool_t::UNSPECIFIED) + SetPrintMetrics(args.printmetrics == opt_bool_t::ENABLED); + if (args.sendmetrics != opt_bool_t::UNSPECIFIED) + SetSendMetrics(args.sendmetrics == opt_bool_t::ENABLED); + + if (args.debug != opt_bool_t::UNSPECIFIED) { - auto simple_desc = std::regex_replace(desc.substr(0, 49), std::regex("\\n( |\\t)?"), ""); - if (desc.size() > 49) - simple_desc.append("..."); - return simple_desc; + g_debugging = (args.debug == opt_bool_t::ENABLED); } - std::vector<std::unordered_map<std::string, std::string>> get_paragraphs(const fs::path& control_path) + if (g_debugging) { - return parse_paragraphs(Files::get_contents(control_path).get_or_throw()); + inner(args); + exit(EXIT_FAILURE); } - std::vector<std::unordered_map<std::string, std::string>> parse_paragraphs(const std::string& str) + std::string exc_msg; + try + { + inner(args); + exit(EXIT_FAILURE); + } + catch (std::exception& e) + { + exc_msg = e.what(); + } + catch (...) { - return Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs(); + exc_msg = "unknown error(...)"; } + TrackProperty("error", exc_msg); + std::cerr + << "vcpkg.exe has crashed.\n" + << "Please send an email to:\n" + << " " << Info::email() << "\n" + << "containing a brief summary of what you were trying to do and the following data blob:\n" + << "\n" + << "Version=" << Info::version() << "\n" + << "EXCEPTION='" << exc_msg << "'\n" + << "CMD=\n"; + for (int x = 0; x < argc; ++x) + std::cerr << Strings::utf16_to_utf8(argv[x]) << "|\n"; + std::cerr + << "\n"; } |
