[vcpkg] Split vcpkg::Commands::Fetch into backend and frontend
MODIFY
include/vcpkg/commands.h
include/vcpkg/commands.h
@@ -139,8 +139,6 @@ namespace vcpkg::Commands
namespace Fetch
{
- fs::path get_tool_path(const VcpkgPaths& paths, const std::string& tool);
- std::string get_tool_version(const VcpkgPaths& paths, const std::string& tool);
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}

ADD
include/vcpkg/tools.h
+#pragma once
+
+#include <vcpkg/base/files.h>
+
+#include <string>
+#include <utility>
+
+namespace vcpkg
+{
+ struct VcpkgPaths;
+
+ struct ToolCache
+ {
+ virtual ~ToolCache() {}
+
+ virtual const fs::path& get_tool_path(const VcpkgPaths& paths, const std::string& tool) const = 0;
+ virtual const std::string& get_tool_version(const VcpkgPaths& paths, const std::string& tool) const = 0;
+ };
+
+ std::unique_ptr<ToolCache> get_tool_cache();
+}

MODIFY
include/vcpkg/vcpkgpaths.h
include/vcpkg/vcpkgpaths.h
#include <vcpkg/binaryparagraph.h>
#include <vcpkg/packagespec.h>
+#include <vcpkg/tools.h>
#include <vcpkg/base/cache.h>
#include <vcpkg/base/expected.h>
@@ -78,6 +79,7 @@ namespace vcpkg
fs::path ports_cmake;
const fs::path& get_tool_exe(const std::string& tool) const;
+ const std::string& get_tool_version(const std::string& tool) const;
/// <summary>Retrieve a toolset matching a VS version</summary>
/// <remarks>
@@ -89,10 +91,11 @@ namespace vcpkg
private:
Lazy<std::vector<std::string>> available_triplets;
- Cache<std::string, fs::path> tool_paths;
Lazy<std::vector<Toolset>> toolsets;
Lazy<std::vector<Toolset>> toolsets_vs2013;
fs::path default_vs_path;
+
+ mutable std::unique_ptr<ToolCache> m_tool_cache;
};
}

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -104,7 +104,7 @@ static void inner(const VcpkgCmdArguments& args)
"Error: Invalid vcpkg root directory %s: %s",
vcpkg_root_dir.string(),
expected_paths.error().message());
- const VcpkgPaths paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
+ const VcpkgPaths& paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
#if defined(_WIN32)
const int exit_code = _wchdir(paths.root.c_str());

MODIFY
src/vcpkg/base/archives.cpp
src/vcpkg/base/archives.cpp
@@ -21,7 +21,7 @@ namespace vcpkg::Archives
static bool recursion_limiter_sevenzip_old = false;
Checks::check_exit(VCPKG_LINE_INFO, !recursion_limiter_sevenzip_old);
recursion_limiter_sevenzip_old = true;
- const auto nuget_exe = Commands::Fetch::get_tool_path(paths, Tools::NUGET);
+ const auto nuget_exe = paths.get_tool_exe(Tools::NUGET);
const std::string stem = archive.stem().u8string();
// assuming format of [name].[version in the form d.d.d]
@@ -56,7 +56,7 @@ namespace vcpkg::Archives
static bool recursion_limiter_sevenzip = false;
Checks::check_exit(VCPKG_LINE_INFO, !recursion_limiter_sevenzip);
recursion_limiter_sevenzip = true;
- const auto seven_zip = Commands::Fetch::get_tool_path(paths, Tools::SEVEN_ZIP);
+ const auto seven_zip = paths.get_tool_exe(Tools::SEVEN_ZIP);
const auto code_and_output = System::cmd_execute_and_capture_output(Strings::format(
R"("%s" x "%s" -o"%s" -y)", seven_zip.u8string(), archive.u8string(), to_path_partial.u8string()));
Checks::check_exit(VCPKG_LINE_INFO,
@@ -103,5 +103,4 @@ namespace vcpkg::Archives
to_path.u8string(),
ec.message());
}
-
}

MODIFY
src/vcpkg/build.cpp
src/vcpkg/build.cpp
@@ -462,7 +462,7 @@ namespace vcpkg::Build
std::vector<AbiEntry> abi_tag_entries;
- abi_tag_entries.emplace_back(AbiEntry{"cmake", Commands::Fetch::get_tool_version(paths, Tools::CMAKE)});
+ abi_tag_entries.emplace_back(AbiEntry{"cmake", paths.get_tool_version(Tools::CMAKE)});
abi_tag_entries.insert(abi_tag_entries.end(), dependency_abis.begin(), dependency_abis.end());

MODIFY
src/vcpkg/commands.cpp
src/vcpkg/commands.cpp
@@ -57,3 +57,24 @@ namespace vcpkg::Commands
return t;
}
}
+
+namespace vcpkg::Commands::Fetch
+{
+ const CommandStructure COMMAND_STRUCTURE = {
+ Strings::format("The argument should be tool name\n%s", Help::create_example_string("fetch cmake")),
+ 1,
+ 1,
+ {},
+ nullptr,
+ };
+
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
+ {
+ Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
+
+ const std::string tool = args.command_arguments[0];
+ const fs::path tool_path = paths.get_tool_exe(tool);
+ System::println(tool_path.u8string());
+ Checks::exit_success(VCPKG_LINE_INFO);
+ }
+}

RENAME
src/vcpkg/commands.fetch.cpp
src/vcpkg/tools.cpp
#include "pch.h"
+#include <vcpkg/tools.h>
+#include <vcpkg/vcpkgpaths.h>
+
#include <vcpkg/base/archives.h>
-#include <vcpkg/base/checks.h>
#include <vcpkg/base/downloads.h>
+#include <vcpkg/base/files.h>
+#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringrange.h>
-#include <vcpkg/base/strings.h>
-#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
-#include <vcpkg/commands.h>
-#include <vcpkg/help.h>
-namespace vcpkg::Commands::Fetch
+namespace vcpkg
{
struct ToolData
{
@@ -478,19 +478,25 @@ git version 2.17.1.windows.2
}
}
- fs::path get_tool_path(const VcpkgPaths& paths, const std::string& tool)
+ struct ToolCacheImpl final : ToolCache
+ {
+ vcpkg::Cache<std::string, fs::path> path_only_cache;
+ vcpkg::Cache<std::string, PathAndVersion> path_version_cache;
+
+ virtual const fs::path& get_tool_path(const VcpkgPaths& paths, const std::string& tool) const override
{
+ return path_only_cache.get_lazy(tool, [&]() {
// First deal with specially handled tools.
- // For these we may look in locations like Program Files, the PATH etc as well as the auto-downloaded location.
+ // For these we may look in locations like Program Files, the PATH etc as well as the auto-downloaded
+ // location.
if (tool == Tools::SEVEN_ZIP) return get_7za_path(paths);
- if (tool == Tools::CMAKE) return CMake::get_path(paths).path;
- if (tool == Tools::GIT) return Git::get_path(paths).path;
- if (tool == Tools::NINJA) return Ninja::get_path(paths).path;
- if (tool == Tools::NUGET) return Nuget::get_path(paths).path;
- if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths).path;
+ if (tool == Tools::CMAKE || tool == Tools::GIT || tool == Tools::NINJA || tool == Tools::NUGET ||
+ tool == Tools::IFW_INSTALLER_BASE)
+ return get_tool_pathversion(paths, tool).path;
if (tool == Tools::IFW_BINARYCREATOR)
return IfwInstallerBase::get_path(paths).path.parent_path() / "binarycreator.exe";
- if (tool == Tools::IFW_REPOGEN) return IfwInstallerBase::get_path(paths).path.parent_path() / "repogen.exe";
+ if (tool == Tools::IFW_REPOGEN)
+ return IfwInstallerBase::get_path(paths).path.parent_path() / "repogen.exe";
// For other tools, we simply always auto-download them.
const ToolData tool_data = parse_tool_data_from_xml(paths, tool);
@@ -499,34 +505,27 @@ git version 2.17.1.windows.2
return tool_data.exe_path;
}
return fetch_tool(paths, tool, tool_data);
+ });
}
- std::string get_tool_version(const VcpkgPaths& paths, const std::string& tool)
+ const PathAndVersion& get_tool_pathversion(const VcpkgPaths& paths, const std::string& tool) const
{
- if (tool == Tools::CMAKE) return CMake::get_path(paths).version;
- if (tool == Tools::GIT) return Git::get_path(paths).version;
- if (tool == Tools::NINJA) return Ninja::get_path(paths).version;
- if (tool == Tools::NUGET) return Nuget::get_path(paths).version;
- if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths).version;
+ return path_version_cache.get_lazy(tool, [&]() {
+ if (tool == Tools::CMAKE) return CMake::get_path(paths);
+ if (tool == Tools::GIT) return Git::get_path(paths);
+ if (tool == Tools::NINJA) return Ninja::get_path(paths);
+ if (tool == Tools::NUGET) return Nuget::get_path(paths);
+ if (tool == Tools::IFW_INSTALLER_BASE) return IfwInstallerBase::get_path(paths);
Checks::exit_with_message(VCPKG_LINE_INFO, "Finding version for %s is not implemented yet.", tool);
+ });
}
- const CommandStructure COMMAND_STRUCTURE = {
- Strings::format("The argument should be tool name\n%s", Help::create_example_string("fetch cmake")),
- 1,
- 1,
- {},
- nullptr,
- };
-
- void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
+ virtual const std::string& get_tool_version(const VcpkgPaths& paths, const std::string& tool) const override
{
- Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
-
- const std::string tool = args.command_arguments[0];
- const fs::path tool_path = get_tool_path(paths, tool);
- System::println(tool_path.u8string());
- Checks::exit_success(VCPKG_LINE_INFO);
+ return get_tool_pathversion(paths, tool).version;
}
+ };
+
+ std::unique_ptr<ToolCache> get_tool_cache() { return std::make_unique<ToolCacheImpl>(); }
}

MODIFY
src/vcpkg/vcpkgpaths.cpp
src/vcpkg/vcpkgpaths.cpp
@@ -93,7 +93,13 @@ namespace vcpkg
const fs::path& VcpkgPaths::get_tool_exe(const std::string& tool) const
{
- return this->tool_paths.get_lazy(tool, [&]() { return Commands::Fetch::get_tool_path(*this, tool); });
+ if (!m_tool_cache) m_tool_cache = get_tool_cache();
+ return m_tool_cache->get_tool_path(*this, tool);
+ }
+ const std::string& VcpkgPaths::get_tool_version(const std::string& tool) const
+ {
+ if (!m_tool_cache) m_tool_cache = get_tool_cache();
+ return m_tool_cache->get_tool_version(*this, tool);
}
const Toolset& VcpkgPaths::get_toolset(const Build::PreBuildInfo& prebuildinfo) const

MODIFY
vcpkglib/vcpkglib.vcxproj
vcpkglib/vcpkglib.vcxproj
<ItemGroup>
<ClInclude Include="..\include\pch.h" />
<ClInclude Include="..\include\vcpkg\base\archives.h" />
+ <ClInclude Include="..\include\vcpkg\base\cache.h" />
<ClInclude Include="..\include\vcpkg\base\checks.h" />
<ClInclude Include="..\include\vcpkg\base\chrono.h" />
<ClInclude Include="..\include\vcpkg\base\cofffilereader.h" />
<ClInclude Include="..\include\vcpkg\sourceparagraph.h" />
<ClInclude Include="..\include\vcpkg\statusparagraph.h" />
<ClInclude Include="..\include\vcpkg\statusparagraphs.h" />
+ <ClInclude Include="..\include\vcpkg\tools.h" />
<ClInclude Include="..\include\vcpkg\triplet.h" />
<ClInclude Include="..\include\vcpkg\update.h" />
<ClInclude Include="..\include\vcpkg\userconfig.h" />
<ClCompile Include="..\src\vcpkg\commands.edit.cpp" />
<ClCompile Include="..\src\vcpkg\commands.env.cpp" />
<ClCompile Include="..\src\vcpkg\commands.exportifw.cpp" />
- <ClCompile Include="..\src\vcpkg\commands.fetch.cpp" />
<ClCompile Include="..\src\vcpkg\commands.hash.cpp" />
<ClCompile Include="..\src\vcpkg\commands.import.cpp" />
<ClCompile Include="..\src\vcpkg\commands.integrate.cpp" />
<ClCompile Include="..\src\vcpkg\sourceparagraph.cpp" />
<ClCompile Include="..\src\vcpkg\statusparagraph.cpp" />
<ClCompile Include="..\src\vcpkg\statusparagraphs.cpp" />
+ <ClCompile Include="..\src\vcpkg\tools.cpp" />
<ClCompile Include="..\src\vcpkg\triplet.cpp" />
<ClCompile Include="..\src\vcpkg\update.cpp" />
<ClCompile Include="..\src\vcpkg\userconfig.cpp" />

MODIFY
vcpkglib/vcpkglib.vcxproj.filters
vcpkglib/vcpkglib.vcxproj.filters
<ClCompile Include="..\src\vcpkg\commands.upgrade.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
- <ClCompile Include="..\src\vcpkg\commands.fetch.cpp">
- <Filter>Source Files\vcpkg</Filter>
- </ClCompile>
<ClCompile Include="..\src\vcpkg\base\downloads.cpp">
<Filter>Source Files\vcpkg\base</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg\base\stringrange.cpp">
<Filter>Source Files\vcpkg\base</Filter>
</ClCompile>
+ <ClCompile Include="..\src\vcpkg\tools.cpp">
+ <Filter>Source Files\vcpkg</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\pch.h">
<ClInclude Include="..\include\vcpkg\base\stringrange.h">
<Filter>Header Files\vcpkg\base</Filter>
</ClInclude>
+ <ClInclude Include="..\include\vcpkg\base\cache.h">
+ <Filter>Header Files\vcpkg\base</Filter>
+ </ClInclude>
+ <ClInclude Include="..\include\vcpkg\tools.h">
+ <Filter>Header Files\vcpkg</Filter>
+ </ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file

[vcpkg] Split vcpkg::Commands::Fetch into backend and frontend