Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0219-msft committed Apr 14, 2023
1 parent e31f0b9 commit 2e57679
Show file tree
Hide file tree
Showing 19 changed files with 496 additions and 500 deletions.
20 changes: 14 additions & 6 deletions include/vcpkg/archives.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include <vcpkg/base/fwd/expected.h>
#include <vcpkg/base/fwd/files.h>
#include <vcpkg/base/fwd/message_sinks.h>
#include <vcpkg/base/fwd/span.h>
#include <vcpkg/base/fwd/system.process.h>

#include <vcpkg/fwd/tools.h>
#include <vcpkg/fwd/vcpkgpaths.h>

#include <vcpkg/base/path.h>

#include <vcpkg/tools.h>

namespace vcpkg
{
// Extract `archive` to `to_path` using `tar_tool`.
Expand All @@ -32,14 +33,21 @@ namespace vcpkg
struct ZipTool
{
#if defined _WIN32
ZipTool(const ToolCache& tools, MessageSink& status_sink);
ZipTool(RemoveFilesystem& fs, const ToolCache& tools, MessageSink& status_sink);

private:
RemoveFilesystem* fs;
Path seven_zip;
#else
ZipTool(RemoveFilesystem& fs);

private:
RemoveFilesystem* fs;
#endif

public:
// Compress the source directory into the destination file.
ExpectedL<Unit> compress_directory_to_zip(RemoveFilesystem& fs,
const Path& source,
const Path& destination) const;
ExpectedL<Unit> compress_directory_to_zip(const Path& source, const Path& destination) const;

Command decompress_zip_archive_cmd(const Path& dst, const Path& archive_path) const;
};
Expand Down
12 changes: 3 additions & 9 deletions include/vcpkg/base/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,9 @@ namespace vcpkg
template<class... Args>
T& emplace(Args&&... args)
{
if (m_is_present)
{
m_t = T(static_cast<Args&&>(args)...);
}
else
{
new (&m_t) T(static_cast<Args&&>(args)...);
m_is_present = true;
}
if (m_is_present) destroy();
new (&m_t) T(static_cast<Args&&>(args)...);
m_is_present = true;
return m_t;
}

Expand Down
62 changes: 34 additions & 28 deletions include/vcpkg/binarycaching.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vcpkg/base/expected.h>
#include <vcpkg/base/path.h>

#include <vcpkg/archives.h>
#include <vcpkg/packagespec.h>

#include <iterator>
Expand All @@ -23,26 +24,26 @@ namespace vcpkg
{
struct CacheStatus
{
bool should_attempt_precheck(const IBinaryProvider* sender) const noexcept;
bool should_attempt_restore(const IBinaryProvider* sender) const noexcept;
bool should_attempt_precheck(const IReadBinaryProvider* sender) const noexcept;
bool should_attempt_restore(const IReadBinaryProvider* sender) const noexcept;

bool is_unavailable(const IBinaryProvider* sender) const noexcept;
const IBinaryProvider* get_available_provider() const noexcept;
bool is_unavailable(const IReadBinaryProvider* sender) const noexcept;
const IReadBinaryProvider* get_available_provider() const noexcept;
bool is_restored() const noexcept;

void mark_unavailable(const IBinaryProvider* sender);
void mark_available(const IBinaryProvider* sender) noexcept;
void mark_unavailable(const IReadBinaryProvider* sender);
void mark_available(const IReadBinaryProvider* sender) noexcept;
void mark_restored() noexcept;

private:
CacheStatusState m_status = CacheStatusState::unknown;

// The set of providers who know they do not have the associated cache entry.
// Flat vector set because N is tiny.
std::vector<const IBinaryProvider*> m_known_unavailable_providers;
std::vector<const IReadBinaryProvider*> m_known_unavailable_providers;

// The provider who affirmatively has the associated cache entry.
const IBinaryProvider* m_available_provider = nullptr; // meaningful iff m_status == available
const IReadBinaryProvider* m_available_provider = nullptr; // meaningful iff m_status == available
};

struct BinaryPackageReadInfo
Expand All @@ -51,36 +52,35 @@ namespace vcpkg
std::string package_abi;
PackageSpec spec;
std::string raw_version;
Path package_dir;
};

struct BinaryPackageWriteInfo : BinaryPackageReadInfo
{
Path package_dir;
using BinaryPackageReadInfo::BinaryPackageReadInfo;

// Filled if BinaryCache has a provider that returns true for needs_nuspec_data()
Optional<std::string> nuspec;
// Filled if BinaryCache has a provider that returns true for needs_zip_file()
// Note: this can be empty if an error occurred while compressing.
Optional<Path> zip_path;
};

struct IBinaryPushProvider
struct IWriteBinaryProvider
{
virtual ~IBinaryPushProvider() = default;

virtual void prefetch_tools(const ToolCache& cache, MessageSink& sink) = 0;
virtual ~IWriteBinaryProvider() = default;

/// Called upon a successful build of `action` to store those contents in the binary cache.
/// Prerequisite: action has a package_abi()
/// returns the number of successful uploads
virtual size_t push_success(const BinaryPackageWriteInfo& request, MessageSink& msg_sink) = 0;

virtual bool needs_nuspec_data() const = 0;
virtual bool needs_zip_file() const = 0;
};

struct IBinaryProvider
struct IReadBinaryProvider
{
virtual ~IBinaryProvider() = default;
virtual ~IReadBinaryProvider() = default;

/// Attempts to restore the package referenced by `action` into the packages directory.
///
Expand Down Expand Up @@ -116,6 +116,13 @@ namespace vcpkg
std::string instantiate_variables(const BinaryPackageReadInfo& info) const;
};

struct NuGetRepoInfo
{
std::string repo;
std::string branch;
std::string commit;
};

struct BinaryConfigParserState
{
bool nuget_interactive = false;
Expand Down Expand Up @@ -153,6 +160,7 @@ namespace vcpkg
// These are filled in later by reading from args and environment
std::string nuget_prefix;
bool use_nuget_cache = false;
NuGetRepoInfo nuget_repo_info;

void clear();
};
Expand All @@ -162,24 +170,24 @@ namespace vcpkg

struct BinaryProviders
{
std::vector<std::unique_ptr<IBinaryProvider>> read;
std::vector<std::unique_ptr<IBinaryPushProvider>> push;
std::vector<std::unique_ptr<IReadBinaryProvider>> read;
std::vector<std::unique_ptr<IWriteBinaryProvider>> write;
std::string nuget_prefix;
NuGetRepoInfo nuget_repo;
};

ExpectedL<BinaryProviders> make_binary_providers(const VcpkgCmdArguments& args, const VcpkgPaths& paths);

struct BinaryCache
{
BinaryCache() = default;
BinaryCache(RemoveFilesystem& fs);
BinaryCache(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const LineInfo& info);
BinaryCache(BinaryProviders&& providers, RemoveFilesystem& fs, const ToolCache& tools);
~BinaryCache();

/// Attempts to restore the package referenced by `action` into the packages directory.
RestoreResult try_restore(const InstallPlanAction& action);

/// Called upon a successful build of `action` to store those contents in the binary cache.
void push_success(const InstallPlanAction& action, Path package_dir);
void push_success(const InstallPlanAction& action);

/// Gives the IBinaryProvider an opportunity to batch any downloading or server communication for
/// executing `actions`.
Expand All @@ -191,15 +199,13 @@ namespace vcpkg
std::vector<CacheAvailability> precheck(View<InstallPlanAction> actions);

private:
Optional<RemoveFilesystem&> m_fs;
Optional<const ToolCache&> m_tools;
BinaryProviders m_config;

RemoveFilesystem& m_fs;
Optional<ZipTool> m_zip_tool;
std::unordered_map<std::string, CacheStatus> m_status;
std::vector<std::unique_ptr<IBinaryProvider>> m_read_providers;
std::vector<std::unique_ptr<IBinaryPushProvider>> m_push_providers;
bool m_needs_nuspec_data = false;
std::string m_nuget_prefix;
bool m_needs_zip_file = false;
Path zip_tool;
};

ExpectedL<DownloadManagerConfig> parse_download_configuration(const Optional<std::string>& arg);
Expand Down
21 changes: 5 additions & 16 deletions include/vcpkg/binarycaching.private.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

#include <vcpkg/base/fwd/files.h>

#include <vcpkg/fwd/binarycaching.h>
#include <vcpkg/fwd/dependencies.h>
#include <vcpkg/fwd/packagespec.h>
#include <vcpkg/fwd/vcpkgpaths.h>

#include <vcpkg/base/strings.h>

#include <vcpkg/binarycaching.h>

namespace vcpkg
{
// Turns:
Expand All @@ -30,23 +31,11 @@ namespace vcpkg
std::string nupkg_filename() const { return Strings::concat(id, '.', version, ".nupkg"); }
};

NugetReference make_nugetref(const BinaryPackageReadInfo& info, StringView prefix);
NugetReference make_nugetref(const InstallPlanAction& action, StringView prefix);

namespace details
{
struct NuGetRepoInfo
{
std::string repo;
std::string branch;
std::string commit;
};

NuGetRepoInfo get_nuget_repo_info_from_env();
}

NuGetRepoInfo get_nuget_repo_info_from_env();
std::string generate_nuspec(const Path& package_dir,
const InstallPlanAction& action,
const NugetReference& ref,
details::NuGetRepoInfo rinfo = details::get_nuget_repo_info_from_env());
StringView id_prefix,
NuGetRepoInfo repo_info);
}
1 change: 0 additions & 1 deletion include/vcpkg/commands.setinstalled.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace vcpkg::Commands::SetInstalled
void perform_and_exit_ex(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
const PathsPortFileProvider& provider,
BinaryCache& binary_cache,
const CMakeVars::CMakeVarProvider& cmake_vars,
ActionPlan action_plan,
DryRun dry_run,
Expand Down
4 changes: 2 additions & 2 deletions include/vcpkg/dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace vcpkg
const RequestType& request_type,
Triplet host_triplet,
std::map<std::string, std::vector<FeatureSpec>>&& dependencies,
Path&& package_dir);
std::vector<LocalizedString>&& build_failure_messages);

std::string displayname() const;
const std::string& public_abi() const;
Expand All @@ -80,8 +80,8 @@ namespace vcpkg
InternalFeatureSet feature_list;
Triplet host_triplet;

Optional<AbiInfo> abi_info;
// only valid with source_control_file_and_location
Optional<AbiInfo> abi_info;
Optional<Path> package_dir;
};

Expand Down
3 changes: 2 additions & 1 deletion include/vcpkg/fwd/binarycaching.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ namespace vcpkg
};

struct CacheStatus;
struct IBinaryProvider;
struct IReadBinaryProvider;
struct IWriteBinaryProvider;
struct BinaryCache;
struct BinaryConfigParserState;
struct BinaryPackageReadInfo;
Expand Down
1 change: 0 additions & 1 deletion include/vcpkg/install.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ namespace vcpkg
const KeepGoing keep_going,
const VcpkgPaths& paths,
StatusParagraphs& status_db,
BinaryCache& binary_cache,
const IBuildLogsRecorder& build_logs_recorder,
const CMakeVars::CMakeVarProvider& var_provider);

Expand Down
10 changes: 10 additions & 0 deletions include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ namespace vcpkg
Optional<std::string> nuget_id_prefix;
constexpr static StringLiteral VCPKG_USE_NUGET_CACHE_ENV = "VCPKG_USE_NUGET_CACHE";
Optional<bool> use_nuget_cache;
constexpr static StringLiteral VCPKG_NUGET_REPOSITORY_ENV = "VCPKG_NUGET_REPOSITORY";
Optional<std::string> vcpkg_nuget_repository;
constexpr static StringLiteral GITHUB_REPOSITORY_ENV = "GITHUB_REPOSITORY";
Optional<std::string> github_repository;
constexpr static StringLiteral GITHUB_SERVER_URL_ENV = "GITHUB_SERVER_URL";
Optional<std::string> github_server_url;
constexpr static StringLiteral GITHUB_REF_ENV = "GITHUB_REF";
Optional<std::string> github_ref;
constexpr static StringLiteral GITHUB_SHA_ENV = "GITHUB_SHA";
Optional<std::string> github_sha;

constexpr static StringLiteral CMAKE_SCRIPT_ARG = "cmake-args";
std::vector<std::string> cmake_args;
Expand Down
13 changes: 6 additions & 7 deletions src/vcpkg-test/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

using namespace vcpkg;

struct KnowNothingBinaryProvider : IBinaryProvider, IBinaryPushProvider
struct KnowNothingBinaryProvider : IReadBinaryProvider, IWriteBinaryProvider
{
RestoreResult try_restore(const InstallPlanAction& action) const override
{
Expand Down Expand Up @@ -271,7 +271,7 @@ Build-Depends: bzip
REQUIRE(ref.nupkg_filename() == "zlib2_x64-windows.1.5.0-vcpkgpackageabi.nupkg");

{
auto nuspec = generate_nuspec(pkgPath, ipa, ref, {});
auto nuspec = generate_nuspec(pkgPath, ipa, "", {});
std::string expected = R"(<package>
<metadata>
<id>zlib2_x64-windows</id>
Expand Down Expand Up @@ -299,7 +299,7 @@ Features: a, b
}

{
auto nuspec = generate_nuspec(pkgPath, ipa, ref, {"urlvalue"});
auto nuspec = generate_nuspec(pkgPath, ipa, "", {"urlvalue"});
std::string expected = R"(<package>
<metadata>
<id>zlib2_x64-windows</id>
Expand Down Expand Up @@ -327,7 +327,7 @@ Features: a, b
REQUIRE_EQUAL_TEXT(nuspec, expected);
}
{
auto nuspec = generate_nuspec(pkgPath, ipa, ref, {"urlvalue", "branchvalue", "commitvalue"});
auto nuspec = generate_nuspec(pkgPath, ipa, "", {"urlvalue", "branchvalue", "commitvalue"});
std::string expected = R"(<package>
<metadata>
<id>zlib2_x64-windows</id>
Expand Down Expand Up @@ -359,10 +359,9 @@ Features: a, b
TEST_CASE ("Provider nullptr checks", "[BinaryCache]")
{
// create a binary cache to test
BinaryCache uut;
BinaryProviders providers;
providers.read.emplace_back(std::make_unique<KnowNothingBinaryProvider>());
uut.install_providers(std::move(providers));
BinaryCache uut(std::move(providers), Test::nil_remove_filesystem, Test::nil_tool_cache);

// create an action plan with an action without a package ABI set
auto pghs = Paragraphs::parse_paragraphs(R"(
Expand All @@ -385,7 +384,7 @@ Version: 1.5
InstallPlanAction& ipa_without_abi = install_plan.back();

// test that the binary cache does the right thing. See also CHECKs etc. in KnowNothingBinaryProvider
uut.push_success(Test::nil_remove_filesystem, Test::nil_tool_cache, ipa_without_abi, {}); // should have no effects
uut.push_success(ipa_without_abi); // should have no effects
CHECK(uut.try_restore(ipa_without_abi) == RestoreResult::unavailable);
uut.prefetch(install_plan); // should have no effects
}
Expand Down
Loading

0 comments on commit 2e57679

Please sign in to comment.