[vcpkg] Add --x-xunit internal command to print installation results in a VSTS friendly format.
MODIFY
include/vcpkg/base/chrono.h
include/vcpkg/base/chrono.h
@@ -7,18 +7,37 @@ namespace vcpkg::Chrono
{
class ElapsedTime
{
- public:
- static ElapsedTime create_started();
+ using duration = std::chrono::high_resolution_clock::time_point::duration;
- constexpr ElapsedTime() : m_start_tick() {}
+ public:
+ constexpr ElapsedTime() : m_duration() {}
+ constexpr ElapsedTime(duration d) : m_duration(d) {}
template<class TimeUnit>
- TimeUnit elapsed() const
+ TimeUnit as() const
+ {
+ return std::chrono::duration_cast<TimeUnit>(m_duration);
+ }
+
+ std::string to_string() const;
+
+ private:
+ std::chrono::high_resolution_clock::time_point::duration m_duration;
+ };
+
+ class ElapsedTimer
+ {
+ public:
+ static ElapsedTimer create_started();
+
+ constexpr ElapsedTimer() : m_start_tick() {}
+
+ ElapsedTime elapsed() const
{
- return std::chrono::duration_cast<TimeUnit>(std::chrono::high_resolution_clock::now() - this->m_start_tick);
+ return ElapsedTime(std::chrono::high_resolution_clock::now() - this->m_start_tick);
}
- double microseconds() const { return elapsed<std::chrono::duration<double, std::micro>>().count(); }
+ double microseconds() const { return elapsed().as<std::chrono::duration<double, std::micro>>().count(); }
std::string to_string() const;

MODIFY
include/vcpkg/globalstate.h
include/vcpkg/globalstate.h
@@ -9,7 +9,7 @@ namespace vcpkg
{
struct GlobalState
{
- static Util::LockGuarded<Chrono::ElapsedTime> timer;
+ static Util::LockGuarded<Chrono::ElapsedTimer> timer;
static std::atomic<bool> debugging;
static std::atomic<bool> feature_packages;

MODIFY
include/vcpkg/install.h
include/vcpkg/install.h
#pragma once
+#include <vcpkg/base/chrono.h>
#include <vcpkg/build.h>
#include <vcpkg/dependencies.h>
#include <vcpkg/vcpkgcmdarguments.h>
@@ -25,7 +26,7 @@ namespace vcpkg::Install
PackageSpec spec;
Build::ExtendedBuildResult build_result;
- std::string timing;
+ vcpkg::Chrono::ElapsedTime timing;
const Dependencies::AnyAction* action;
};
@@ -36,6 +37,7 @@ namespace vcpkg::Install
std::string total_elapsed_time;
void print() const;
+ std::string xunit_results() const;
};
struct InstallDir

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -255,7 +255,7 @@ int main(const int argc, const char* const* const argv)
{
if (argc == 0) std::abort();
- *GlobalState::timer.lock() = Chrono::ElapsedTime::create_started();
+ *GlobalState::timer.lock() = Chrono::ElapsedTimer::create_started();
#if defined(_WIN32)
GlobalState::g_init_console_cp = GetConsoleCP();

MODIFY
src/vcpkg/base/chrono.cpp
src/vcpkg/base/chrono.cpp
@@ -50,12 +50,14 @@ namespace vcpkg::Chrono
return Strings::format("%.4g ns", nanos_as_double);
}
- ElapsedTime ElapsedTime::create_started()
+ ElapsedTimer ElapsedTimer::create_started()
{
- ElapsedTime t;
+ ElapsedTimer t;
t.m_start_tick = std::chrono::high_resolution_clock::now();
return t;
}
- std::string ElapsedTime::to_string() const { return format_time_userfriendly(elapsed<std::chrono::nanoseconds>()); }
+ std::string ElapsedTime::to_string() const { return format_time_userfriendly(as<std::chrono::nanoseconds>()); }
+
+ std::string ElapsedTimer::to_string() const { return elapsed().to_string(); }
}

MODIFY
src/vcpkg/build.cpp
src/vcpkg/build.cpp
@@ -67,7 +67,7 @@ namespace vcpkg::Build::Command
const Build::BuildPackageConfig build_config{
*scf, spec.triplet(), fs::path{port_dir}, build_package_options, features_as_set};
- const auto build_timer = Chrono::ElapsedTime::create_started();
+ const auto build_timer = Chrono::ElapsedTimer::create_started();
const auto result = Build::build_package(paths, build_config, status_db);
System::println("Elapsed time for package %s: %s", spec.to_string(), build_timer.to_string());
@@ -323,7 +323,7 @@ namespace vcpkg::Build
const auto cmd_set_environment = make_build_env_cmd(pre_build_info, toolset);
const std::string command = Strings::format(R"(%s && %s)", cmd_set_environment, cmd_launch_cmake);
- const auto timer = Chrono::ElapsedTime::create_started();
+ const auto timer = Chrono::ElapsedTimer::create_started();
const int return_code = System::cmd_execute_clean(command);
const auto buildtimeus = timer.microseconds();

MODIFY
src/vcpkg/commands.ci.cpp
src/vcpkg/commands.ci.cpp
@@ -63,9 +63,11 @@ namespace vcpkg::Commands::CI
};
static const std::string OPTION_EXCLUDE = "--exclude";
+ static const std::string OPTION_XUNIT = "--x-xunit";
- static const std::array<CommandSetting, 1> CI_SETTINGS = {{
+ static const std::array<CommandSetting, 2> CI_SETTINGS = {{
{OPTION_EXCLUDE, "Comma separated list of ports to skip"},
+ {OPTION_XUNIT, "File to output results in XUnit format (internal)"},
}};
const CommandStructure COMMAND_STRUCTURE = {
@@ -114,6 +116,18 @@ namespace vcpkg::Commands::CI
result.summary.print();
}
+ auto it_xunit = options.settings.find(OPTION_XUNIT);
+ if (it_xunit != options.settings.end())
+ {
+ std::string xunit_doc = "<assemblies><assembly><collection>\n";
+
+ for (auto&& result : results)
+ xunit_doc += result.summary.xunit_results();
+
+ xunit_doc += "</collection></assembly></assemblies>\n";
+ paths.get_filesystem().write_contents(fs::u8path(it_xunit->second), xunit_doc);
+ }
+
Checks::exit_success(VCPKG_LINE_INFO);
}
}

MODIFY
src/vcpkg/commands.integrate.cpp
src/vcpkg/commands.integrate.cpp
@@ -257,7 +257,7 @@ CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=%s")",
std::error_code ec;
const bool was_deleted = fs.remove(path, ec);
- Checks::check_exit(VCPKG_LINE_INFO, !ec, "Error: Unable to remove user-wide integration: %d", ec.message());
+ Checks::check_exit(VCPKG_LINE_INFO, !ec, "Error: Unable to remove user-wide integration: %s", ec.message());
if (was_deleted)
{

MODIFY
src/vcpkg/commands.portsdiff.cpp
src/vcpkg/commands.portsdiff.cpp
@@ -151,14 +151,14 @@ namespace vcpkg::Commands::PortsDiff
const std::vector<std::string>& added_ports = setp.only_left;
if (!added_ports.empty())
{
- System::println("\nThe following %d ports were added:", added_ports.size());
+ System::println("\nThe following %zd ports were added:", added_ports.size());
do_print_name_and_version(added_ports, current_names_and_versions);
}
const std::vector<std::string>& removed_ports = setp.only_right;
if (!removed_ports.empty())
{
- System::println("\nThe following %d ports were removed:", removed_ports.size());
+ System::println("\nThe following %zd ports were removed:", removed_ports.size());
do_print_name_and_version(removed_ports, previous_names_and_versions);
}
@@ -168,7 +168,7 @@ namespace vcpkg::Commands::PortsDiff
if (!updated_ports.empty())
{
- System::println("\nThe following %d ports were updated:", updated_ports.size());
+ System::println("\nThe following %zd ports were updated:", updated_ports.size());
for (const UpdatedPort& p : updated_ports)
{
System::println(" - %-14s %-16s", p.port, p.version_diff.to_string());

MODIFY
src/vcpkg/globalstate.cpp
src/vcpkg/globalstate.cpp
namespace vcpkg
{
- Util::LockGuarded<Chrono::ElapsedTime> GlobalState::timer;
+ Util::LockGuarded<Chrono::ElapsedTimer> GlobalState::timer;
std::atomic<bool> GlobalState::debugging(false);
std::atomic<bool> GlobalState::feature_packages(false);

MODIFY
src/vcpkg/install.cpp
src/vcpkg/install.cpp
@@ -484,18 +484,18 @@ namespace vcpkg::Install
{
std::vector<SpecSummary> results;
- const auto timer = Chrono::ElapsedTime::create_started();
+ const auto timer = Chrono::ElapsedTimer::create_started();
size_t counter = 0;
const size_t package_count = action_plan.size();
for (const auto& action : action_plan)
{
- const auto build_timer = Chrono::ElapsedTime::create_started();
+ const auto build_timer = Chrono::ElapsedTimer::create_started();
counter++;
const PackageSpec& spec = action.spec();
const std::string display_name = spec.to_string();
- System::println("Starting package %d/%d: %s", counter, package_count, display_name);
+ System::println("Starting package %zd/%zd: %s", counter, package_count, display_name);
results.emplace_back(spec, &action);
@@ -521,8 +521,8 @@ namespace vcpkg::Install
Checks::unreachable(VCPKG_LINE_INFO);
}
- results.back().timing = build_timer.to_string();
- System::println("Elapsed time for package %s: %s", display_name, build_timer.to_string());
+ results.back().timing = build_timer.elapsed();
+ System::println("Elapsed time for package %s: %s", display_name, results.back().timing.to_string());
}
return InstallSummary{std::move(results), timer.to_string()};
@@ -533,6 +533,7 @@ namespace vcpkg::Install
static const std::string OPTION_NO_DOWNLOADS = "--no-downloads";
static const std::string OPTION_RECURSE = "--recurse";
static const std::string OPTION_KEEP_GOING = "--keep-going";
+ static const std::string OPTION_XUNIT = "--x-xunit";
static const std::array<CommandSwitch, 5> INSTALL_SWITCHES = {{
{OPTION_DRY_RUN, "Do not actually build or install"},
@@ -541,7 +542,9 @@ namespace vcpkg::Install
{OPTION_RECURSE, "Allow removal of packages as part of installation"},
{OPTION_KEEP_GOING, "Continue installing packages on failure"},
}};
- static const std::array<std::string, 0> INSTALL_SETTINGS;
+ static const std::array<CommandSetting, 1> INSTALL_SETTINGS = {{
+ {OPTION_XUNIT, "File to output results in XUnit format (Internal use)"},
+ }};
std::vector<std::string> get_all_port_names(const VcpkgPaths& paths)
{
@@ -555,7 +558,7 @@ namespace vcpkg::Install
Help::create_example_string("install zlib zlib:x64-windows curl boost"),
1,
SIZE_MAX,
- {INSTALL_SWITCHES, {}},
+ {INSTALL_SWITCHES, INSTALL_SETTINGS},
&get_all_port_names,
};
@@ -627,7 +630,7 @@ namespace vcpkg::Install
library_target_pair.second.erase(library_target_pair.second.begin() + 4,
library_target_pair.second.end());
System::println(" find_package(%s REQUIRED)\n"
- " # Note: %d targets were omitted\n"
+ " # Note: %zd targets were omitted\n"
" target_link_libraries(main PRIVATE %s)\n",
library_target_pair.first,
omitted,
@@ -735,6 +738,17 @@ namespace vcpkg::Install
summary.print();
}
+ auto it_xunit = options.settings.find(OPTION_XUNIT);
+ if (it_xunit != options.settings.end())
+ {
+ std::string xunit_doc = "<assemblies><assembly><collection>\n";
+
+ xunit_doc += summary.xunit_results();
+
+ xunit_doc += "</collection></assembly></assemblies>\n";
+ paths.get_filesystem().write_contents(fs::u8path(it_xunit->second), xunit_doc);
+ }
+
for (auto&& result : summary.results)
{
if (!result.action) continue;
@@ -751,7 +765,7 @@ namespace vcpkg::Install
}
SpecSummary::SpecSummary(const PackageSpec& spec, const Dependencies::AnyAction* action)
- : spec(spec), build_result{BuildResult::NULLVALUE, nullptr}, timing("0"), action(action)
+ : spec(spec), build_result{BuildResult::NULLVALUE, nullptr}, action(action)
{
}
@@ -770,4 +784,41 @@ namespace vcpkg::Install
}
return nullptr;
}
+
+ std::string InstallSummary::xunit_results() const
+ {
+ std::string xunit_doc;
+ for (auto&& result : results)
+ {
+ std::string inner_block;
+ const char* result_string = "";
+ switch (result.build_result.code)
+ {
+ case BuildResult::POST_BUILD_CHECKS_FAILED:
+ case BuildResult::FILE_CONFLICTS:
+ case BuildResult::BUILD_FAILED:
+ result_string = "Fail";
+ inner_block = Strings::format("<failure><message><![CDATA[%s]]></message></failure>",
+ to_string(result.build_result.code));
+ break;
+ case BuildResult::EXCLUDED:
+ case BuildResult::CASCADED_DUE_TO_MISSING_DEPENDENCIES:
+ result_string = "Skip";
+ inner_block =
+ Strings::format("<reason><![CDATA[%s]]></reason>", to_string(result.build_result.code));
+ break;
+ case BuildResult::SUCCEEDED: result_string = "Pass"; break;
+ default: Checks::exit_fail(VCPKG_LINE_INFO);
+ }
+
+ xunit_doc += Strings::format(R"(<test name="%s" method="%s" time="%lld" result="%s">%s</test>)"
+ "\n",
+ result.spec,
+ result.spec,
+ result.timing.as<std::chrono::seconds>().count(),
+ result_string,
+ inner_block);
+ }
+ return xunit_doc;
+ }
}

MODIFY
src/vcpkg/postbuildlint.cpp
src/vcpkg/postbuildlint.cpp
@@ -479,7 +479,7 @@ namespace vcpkg::PostBuildLint
}
System::println(System::Color::warning,
- "Mismatching number of debug and release binaries. Found %d for debug but %d for release.",
+ "Mismatching number of debug and release binaries. Found %zd for debug but %zd for release.",
debug_count,
release_count);
System::println("Debug binaries");

[vcpkg] Add --x-xunit internal command to print installation results in a VSTS friendly format.