Skip to content

Commit

Permalink
Add heuristic message. (#504)
Browse files Browse the repository at this point in the history
* Add heuristic message.

As requested in microsoft/vcpkg#20190 (comment)

* Avoid need for clang-format off by keeping things in the LocalizedString domain.

* fmt more things
  • Loading branch information
BillyONeal authored Apr 14, 2022
1 parent de2fa05 commit cf4232d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
8 changes: 4 additions & 4 deletions include/vcpkg/base/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ namespace vcpkg::Util
}
}

template<class Range, class Comp = std::less<typename Range::value_type>>
template<class Range, class Comp = std::less<>>
void sort(Range& cont, Comp comp = Comp())
{
using std::begin;
Expand All @@ -266,12 +266,12 @@ namespace vcpkg::Util
return std::any_of(rng.begin(), rng.end(), std::move(pred));
}

template<class Range>
Range&& sort_unique_erase(Range&& cont)
template<class Range, class Comp = std::less<>>
Range&& sort_unique_erase(Range&& cont, Comp comp = Comp())
{
using std::begin;
using std::end;
std::sort(begin(cont), end(cont));
std::sort(begin(cont), end(cont), comp);
cont.erase(std::unique(begin(cont), end(cont)), end(cont));

return std::forward<Range>(cont);
Expand Down
1 change: 1 addition & 0 deletions locales/messages.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"BuildingPackageFailed": "building {spec} failed with: {build_result}",
"BuildingPackageFailedDueToMissingDeps": "due to the following missing dependencies:",
"CMakeTargetsUsage": "{package_name} provides CMake targets:",
"CMakeTargetsUsageHueristicMessage": "# this is heuristically generated, and may not be correct",
"ChecksFailedCheck": "vcpkg has crashed; no additional details are available.",
"ChecksUnreachableCode": "unreachable code was reached",
"ChecksUpdateVcpkg": "updating vcpkg by rerunning bootstrap-vcpkg may resolve this failure.",
Expand Down
2 changes: 2 additions & 0 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"_BuildingPackageFailedDueToMissingDeps.comment": "Printed after BuildingPackageFailed, and followed by a list of dependencies that were missing.\n",
"CMakeTargetsUsage": "{package_name} provides CMake targets:",
"_CMakeTargetsUsage.comment": "'targets' are a CMake and Makefile concept\nexample of {package_name} is 'zlib'.\n",
"CMakeTargetsUsageHueristicMessage": "# this is heuristically generated, and may not be correct",
"_CMakeTargetsUsageHueristicMessage.comment": "Displayed after CMakeTargetsUsage; the # must be kept at the beginning so that the message remains a comment.\n",
"ChecksFailedCheck": "vcpkg has crashed; no additional details are available.",
"ChecksUnreachableCode": "unreachable code was reached",
"ChecksUpdateVcpkg": "updating vcpkg by rerunning bootstrap-vcpkg may resolve this failure.",
Expand Down
40 changes: 20 additions & 20 deletions src/vcpkg/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ namespace
(msg::package_name),
"'header' refers to C/C++ .h files",
"{package_name} is header-only and can be used from CMake via:");
DECLARE_AND_REGISTER_MESSAGE(
CMakeTargetsUsageHueristicMessage,
(),
"Displayed after CMakeTargetsUsage; the # must be kept at the beginning so that the message remains a comment.",
"# this is heuristically generated, and may not be correct");
DECLARE_AND_REGISTER_MESSAGE(CMakeTargetsUsage,
(msg::package_name),
"'targets' are a CMake and Makefile concept",
Expand Down Expand Up @@ -814,43 +819,38 @@ namespace vcpkg::Install
}
else
{
auto msg = msg::format(msgCMakeTargetsUsage, msg::package_name = bpgh.spec.name()).extract_data();
auto msg = msg::format(msgCMakeTargetsUsage, msg::package_name = bpgh.spec.name()).appendnl();
msg.append_indent().append(msgCMakeTargetsUsageHueristicMessage).appendnl();

for (auto&& library_target_pair : library_targets)
{
auto config_it = config_files.find(library_target_pair.first);
if (config_it != config_files.end())
Strings::append(msg, " find_package(", config_it->second, " CONFIG REQUIRED)\n");
else
Strings::append(msg, " find_package(", library_target_pair.first, " CONFIG REQUIRED)\n");
msg.append_indent();
msg.append_fmt_raw("find_package({} CONFIG REQUIRED)",
config_it == config_files.end() ? library_target_pair.first : config_it->second);
msg.appendnl();

auto& targets = library_target_pair.second;
Util::sort(targets, [](const std::string& l, const std::string& r) {
Util::sort_unique_erase(targets, [](const std::string& l, const std::string& r) {
if (l.size() < r.size()) return true;
if (l.size() > r.size()) return false;
return l < r;
});
targets.erase(std::unique(targets.begin(), targets.end()), targets.end());

if (targets.size() > 4)
{
auto omitted = targets.size() - 4;
library_target_pair.second.erase(targets.begin() + 4, targets.end());
msg.append(LocalizedString()
.append_indent()
.append_raw("# ")
.append(msgCmakeTargetsExcluded, msg::count = omitted)
.extract_data());
msg.append_indent().append_raw("# ").append(msgCmakeTargetsExcluded, msg::count = omitted);
}
msg.append(LocalizedString()
.append_indent()
.append_raw(fmt::format("target_link_libraries(main PRIVATE {})",
Strings::join(" ", targets)))
.appendnl()
.appendnl()
.extract_data());

msg.append_indent()
.append_fmt_raw("target_link_libraries(main PRIVATE {})", Strings::join(" ", targets))
.appendnl()
.appendnl();
}
ret.message = std::move(msg);

ret.message = msg.extract_data();
}
ret.cmake_targets_map = std::move(library_targets);
}
Expand Down

0 comments on commit cf4232d

Please sign in to comment.