Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tool cache] Add option to require exact versions for abi relevant tools #234

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/vcpkg/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ namespace vcpkg

struct ToolCache
{
enum class RequireExactVersions
{
YES,
NO,
};

virtual ~ToolCache() { }

virtual const 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();
std::unique_ptr<ToolCache> get_tool_cache(ToolCache::RequireExactVersions abiToolVersionHandling);
}
3 changes: 3 additions & 0 deletions include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ namespace vcpkg
constexpr static StringLiteral CMAKE_SCRIPT_ARG = "x-cmake-args";
std::vector<std::string> cmake_args;

constexpr static StringLiteral EXACT_ABI_TOOLS_VERSIONS_SWITCH = "x-abi-tools-use-exact-versions";
Optional<bool> exact_abi_tools_versions;

constexpr static StringLiteral DEBUG_SWITCH = "debug";
Optional<bool> debug = nullopt;
constexpr static StringLiteral SEND_METRICS_SWITCH = "sendmetrics";
Expand Down
43 changes: 31 additions & 12 deletions src/vcpkg/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ namespace vcpkg
virtual ExpectedS<std::string> get_version(const VcpkgPaths& paths, const Path& exe_path) const = 0;
};

template<typename Func>
static Optional<PathAndVersion> find_first_with_sufficient_version(const VcpkgPaths& paths,
const ToolProvider& tool_provider,
const std::vector<Path>& candidates,
const std::array<int, 3>& expected_version)
Func&& accept_version)
{
const auto& fs = paths.get_filesystem();
for (auto&& candidate : candidates)
Expand All @@ -167,12 +168,7 @@ namespace vcpkg
const auto parsed_version = parse_version_string(*version);
if (!parsed_version) continue;
auto& actual_version = *parsed_version.get();
const auto version_acceptable =
actual_version[0] > expected_version[0] ||
(actual_version[0] == expected_version[0] && actual_version[1] > expected_version[1]) ||
(actual_version[0] == expected_version[0] && actual_version[1] == expected_version[1] &&
actual_version[2] >= expected_version[2]);
if (!version_acceptable) continue;
if (!accept_version(actual_version)) continue;

return PathAndVersion{candidate, *version};
}
Expand Down Expand Up @@ -237,7 +233,7 @@ namespace vcpkg
return {std::move(downloaded_path), std::move(downloaded_version)};
}

static PathAndVersion get_path(const VcpkgPaths& paths, const ToolProvider& tool)
static PathAndVersion get_path(const VcpkgPaths& paths, const ToolProvider& tool, bool exact_version = false)
{
auto& fs = paths.get_filesystem();

Expand All @@ -260,7 +256,18 @@ namespace vcpkg

tool.add_special_paths(candidate_paths);

const auto maybe_path = find_first_with_sufficient_version(paths, tool, candidate_paths, min_version);
const auto maybe_path = find_first_with_sufficient_version(
paths, tool, candidate_paths, [&min_version, exact_version](const std::array<int, 3>& actual_version) {
if (exact_version)
{
return actual_version[0] == min_version[0] && actual_version[1] == min_version[1] &&
actual_version[2] == min_version[2];
}
return actual_version[0] > min_version[0] ||
(actual_version[0] == min_version[0] && actual_version[1] > min_version[1]) ||
(actual_version[0] == min_version[0] && actual_version[1] == min_version[1] &&
actual_version[2] >= min_version[2]);
});
if (const auto p = maybe_path.get())
{
return *p;
Expand Down Expand Up @@ -555,9 +562,15 @@ gsutil version: 4.58

struct ToolCacheImpl final : ToolCache
{
ToolCache::RequireExactVersions abiToolVersionHandling;
vcpkg::Cache<std::string, Path> path_only_cache;
vcpkg::Cache<std::string, PathAndVersion> path_version_cache;

ToolCacheImpl(ToolCache::RequireExactVersions abiToolVersionHandling)
: abiToolVersionHandling(abiToolVersionHandling)
{
}

virtual const Path& get_tool_path(const VcpkgPaths& paths, const std::string& tool) const override
{
return path_only_cache.get_lazy(tool, [&]() {
Expand Down Expand Up @@ -591,7 +604,8 @@ gsutil version: 4.58
{
return {"cmake", "0"};
}
return get_path(paths, CMakeProvider());
return get_path(
paths, CMakeProvider(), abiToolVersionHandling == ToolCache::RequireExactVersions::YES);
}
if (tool == Tools::GIT)
{
Expand All @@ -615,7 +629,9 @@ gsutil version: 4.58
{
return {"pwsh", "0"};
}
return get_path(paths, PowerShellCoreProvider());
return get_path(paths,
PowerShellCoreProvider(),
abiToolVersionHandling == ToolCache::RequireExactVersions::YES);
}
if (tool == Tools::NUGET) return get_path(paths, NuGetProvider());
if (tool == Tools::IFW_INSTALLER_BASE) return get_path(paths, IfwInstallerBaseProvider());
Expand Down Expand Up @@ -650,5 +666,8 @@ gsutil version: 4.58
}
};

std::unique_ptr<ToolCache> get_tool_cache() { return std::make_unique<ToolCacheImpl>(); }
std::unique_ptr<ToolCache> get_tool_cache(ToolCache::RequireExactVersions abiToolVersionHandling)
{
return std::make_unique<ToolCacheImpl>(abiToolVersionHandling);
}
}
2 changes: 2 additions & 0 deletions src/vcpkg/vcpkgcmdarguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ namespace vcpkg
{WAIT_FOR_LOCK_SWITCH, &VcpkgCmdArguments::wait_for_lock},
{IGNORE_LOCK_FAILURES_SWITCH, &VcpkgCmdArguments::ignore_lock_failures},
{JSON_SWITCH, &VcpkgCmdArguments::json},
{EXACT_ABI_TOOLS_VERSIONS_SWITCH, &VcpkgCmdArguments::exact_abi_tools_versions},
};

Optional<StringView> lookahead;
Expand Down Expand Up @@ -1034,4 +1035,5 @@ namespace vcpkg
constexpr StringLiteral VcpkgCmdArguments::VERSIONS_FEATURE;

constexpr StringLiteral VcpkgCmdArguments::CMAKE_SCRIPT_ARG;
constexpr StringLiteral VcpkgCmdArguments::EXACT_ABI_TOOLS_VERSIONS_SWITCH;
}
11 changes: 8 additions & 3 deletions src/vcpkg/vcpkgpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ namespace vcpkg

struct VcpkgPathsImpl
{
VcpkgPathsImpl(Filesystem& fs, FeatureFlagSettings ff_settings)
VcpkgPathsImpl(Filesystem& fs,
FeatureFlagSettings ff_settings,
ToolCache::RequireExactVersions abiToolsHandling)
: fs_ptr(&fs)
, m_tool_cache(get_tool_cache())
, m_tool_cache(get_tool_cache(abiToolsHandling))
, m_env_cache(ff_settings.compiler_tracking)
, m_ff_settings(ff_settings)
{
Expand Down Expand Up @@ -278,7 +280,10 @@ namespace vcpkg
static Path lockfile_path(const VcpkgPaths& p) { return p.vcpkg_dir / "vcpkg-lock.json"; }

VcpkgPaths::VcpkgPaths(Filesystem& filesystem, const VcpkgCmdArguments& args)
: m_pimpl(std::make_unique<details::VcpkgPathsImpl>(filesystem, args.feature_flag_settings()))
: m_pimpl(std::make_unique<details::VcpkgPathsImpl>(
filesystem,
args.feature_flag_settings(),
Util::Enum::to_enum<ToolCache::RequireExactVersions>(args.exact_abi_tools_versions.value_or(false))))
{
original_cwd = filesystem.current_path(VCPKG_LINE_INFO);
#if defined(_WIN32)
Expand Down