Merge "libstore: remove static initializers for Store registrations" into main

This commit is contained in:
rebecca “wiggles” turner 2024-08-28 16:43:22 +00:00 committed by Gerrit Code Review
commit 422550fd68
23 changed files with 118 additions and 30 deletions

View file

@ -1,3 +1,4 @@
#include "dummy-store.hh"
#include "store-api.hh" #include "store-api.hh"
namespace nix { namespace nix {
@ -73,6 +74,8 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
{ unsupported("getFSAccessor"); } { unsupported("getFSAccessor"); }
}; };
static RegisterStoreImplementation<DummyStore, DummyStoreConfig> regDummyStore; void registerDummyStore() {
StoreImplementations::add<DummyStore, DummyStoreConfig>();
}
} }

View file

@ -0,0 +1,8 @@
#pragma once
///@file
namespace nix {
void registerDummyStore();
}

View file

@ -33,6 +33,16 @@
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
// All built-in store implementations.
#include "dummy-store.hh"
#include "http-binary-cache-store.hh"
#include "legacy-ssh-store.hh"
#include "local-binary-cache-store.hh"
#include "local-store.hh"
#include "s3-binary-cache-store.hh"
#include "ssh-store.hh"
#include "uds-remote-store.hh"
namespace nix { namespace nix {
@ -396,6 +406,17 @@ static void preloadNSS()
}); });
} }
static void registerStoreImplementations() {
registerDummyStore();
registerHttpBinaryCacheStore();
registerLegacySSHStore();
registerLocalBinaryCacheStore();
registerLocalStore();
registerS3BinaryCacheStore();
registerSSHStore();
registerUDSRemoteStore();
}
static bool initLibStoreDone = false; static bool initLibStoreDone = false;
void assertLibStoreInitialized() { void assertLibStoreInitialized() {
@ -433,6 +454,8 @@ void initLibStore() {
unsetenv("TMPDIR"); unsetenv("TMPDIR");
#endif #endif
registerStoreImplementations();
initLibStoreDone = true; initLibStoreDone = true;
} }

View file

@ -1,3 +1,4 @@
#include "http-binary-cache-store.hh"
#include "binary-cache-store.hh" #include "binary-cache-store.hh"
#include "filetransfer.hh" #include "filetransfer.hh"
#include "globals.hh" #include "globals.hh"
@ -194,6 +195,8 @@ protected:
} }
}; };
static RegisterStoreImplementation<HttpBinaryCacheStore, HttpBinaryCacheStoreConfig> regHttpBinaryCacheStore; void registerHttpBinaryCacheStore() {
StoreImplementations::add<HttpBinaryCacheStore, HttpBinaryCacheStoreConfig>();
}
} }

View file

@ -0,0 +1,8 @@
#pragma once
///@file
namespace nix {
void registerHttpBinaryCacheStore();
}

View file

@ -1,4 +1,4 @@
#include "ssh-store-config.hh" #include "legacy-ssh-store.hh"
#include "archive.hh" #include "archive.hh"
#include "pool.hh" #include "pool.hh"
#include "remote-store.hh" #include "remote-store.hh"
@ -8,6 +8,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "path-with-outputs.hh" #include "path-with-outputs.hh"
#include "ssh.hh" #include "ssh.hh"
#include "ssh-store.hh"
#include "derivations.hh" #include "derivations.hh"
namespace nix { namespace nix {
@ -412,6 +413,8 @@ public:
{ unsupported("queryRealisation"); } { unsupported("queryRealisation"); }
}; };
static RegisterStoreImplementation<LegacySSHStore, LegacySSHStoreConfig> regLegacySSHStore; void registerLegacySSHStore() {
StoreImplementations::add<LegacySSHStore, LegacySSHStoreConfig>();
}
} }

View file

@ -0,0 +1,8 @@
#pragma once
///@file
namespace nix {
void registerLegacySSHStore();
}

View file

@ -1,3 +1,4 @@
#include "local-binary-cache-store.hh"
#include "binary-cache-store.hh" #include "binary-cache-store.hh"
#include "globals.hh" #include "globals.hh"
#include "nar-info-disk-cache.hh" #include "nar-info-disk-cache.hh"
@ -124,6 +125,8 @@ std::set<std::string> LocalBinaryCacheStore::uriSchemes()
return {"file"}; return {"file"};
} }
static RegisterStoreImplementation<LocalBinaryCacheStore, LocalBinaryCacheStoreConfig> regLocalBinaryCacheStore; void registerLocalBinaryCacheStore() {
StoreImplementations::add<LocalBinaryCacheStore, LocalBinaryCacheStoreConfig>();
}
} }

View file

@ -0,0 +1,8 @@
#pragma once
///@file
namespace nix {
void registerLocalBinaryCacheStore();
}

View file

@ -421,4 +421,7 @@ void canonicaliseTimestampAndPermissions(const Path & path);
MakeError(PathInUse, Error); MakeError(PathInUse, Error);
// Implemented by the relevant platform/ module being used.
void registerLocalStore();
} }

View file

@ -118,12 +118,16 @@ libstore_headers = files(
'derived-path-map.hh', 'derived-path-map.hh',
'derived-path.hh', 'derived-path.hh',
'downstream-placeholder.hh', 'downstream-placeholder.hh',
'dummy-store.hh',
'filetransfer.hh', 'filetransfer.hh',
'fs-accessor.hh', 'fs-accessor.hh',
'gc-store.hh', 'gc-store.hh',
'globals.hh', 'globals.hh',
'http-binary-cache-store.hh',
'indirect-root-store.hh', 'indirect-root-store.hh',
'legacy-ssh-store.hh',
'length-prefixed-protocol-helper.hh', 'length-prefixed-protocol-helper.hh',
'local-binary-cache-store.hh',
'local-fs-store.hh', 'local-fs-store.hh',
'local-store.hh', 'local-store.hh',
'lock.hh', 'lock.hh',
@ -152,8 +156,8 @@ libstore_headers = files(
'serve-protocol-impl.hh', 'serve-protocol-impl.hh',
'serve-protocol.hh', 'serve-protocol.hh',
'sqlite.hh', 'sqlite.hh',
'ssh-store-config.hh',
'ssh.hh', 'ssh.hh',
'ssh-store.hh',
'store-api.hh', 'store-api.hh',
'store-cast.hh', 'store-cast.hh',
'uds-remote-store.hh', 'uds-remote-store.hh',

View file

@ -261,4 +261,9 @@ void DarwinLocalDerivationGoal::execBuilder(std::string builder, Strings args, S
posix_spawn(nullptr, builder.c_str(), nullptr, &attrp, stringsToCharPtrs(args).data(), stringsToCharPtrs(envStrs).data()); posix_spawn(nullptr, builder.c_str(), nullptr, &attrp, stringsToCharPtrs(args).data(), stringsToCharPtrs(envStrs).data());
} }
void registerLocalStore() {
StoreImplementations::add<DarwinLocalStore, LocalStoreConfig>();
}
} }

View file

@ -1,5 +1,7 @@
#include "platform/fallback.hh" #include "platform/fallback.hh"
namespace nix { namespace nix {
static RegisterStoreImplementation<FallbackLocalStore, LocalStoreConfig> regLocalStore; void registerLocalStore() {
Implementations::add<FallbackLocalStore, LocalStoreConfig>();
}
} }

View file

@ -25,7 +25,9 @@ namespace {
constexpr const std::string_view nativeSystem = SYSTEM; constexpr const std::string_view nativeSystem = SYSTEM;
} }
static RegisterStoreImplementation<LinuxLocalStore, LocalStoreConfig> regLocalStore; void registerLocalStore() {
StoreImplementations::add<LinuxLocalStore, LocalStoreConfig>();
}
static void readProcLink(const std::string & file, UncheckedRoots & roots) static void readProcLink(const std::string & file, UncheckedRoots & roots)
{ {

View file

@ -526,8 +526,14 @@ struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStoreConfig, public virtual
}; };
static RegisterStoreImplementation<S3BinaryCacheStoreImpl, S3BinaryCacheStoreConfig> regS3BinaryCacheStore; void registerS3BinaryCacheStore() {
StoreImplementations::add<S3BinaryCacheStoreImpl, S3BinaryCacheStoreConfig>();
}
} }
#else
namespace nix {
void registerS3BinaryCacheStore() {}
}
#endif #endif

View file

@ -29,4 +29,6 @@ public:
virtual const Stats & getS3Stats() = 0; virtual const Stats & getS3Stats() = 0;
}; };
void registerS3BinaryCacheStore();
} }

View file

@ -1,4 +1,4 @@
#include "ssh-store-config.hh" #include "ssh-store.hh"
#include "store-api.hh" #include "store-api.hh"
#include "local-fs-store.hh" #include "local-fs-store.hh"
#include "remote-store.hh" #include "remote-store.hh"
@ -110,6 +110,8 @@ ref<RemoteStore::Connection> SSHStore::openConnection()
return conn; return conn;
} }
static RegisterStoreImplementation<SSHStore, SSHStoreConfig> regSSHStore; void registerSSHStore() {
StoreImplementations::add<SSHStore, SSHStoreConfig>();
}
} }

View file

@ -26,4 +26,6 @@ struct CommonSSHStoreConfig : virtual StoreConfig
)"}; )"};
}; };
void registerSSHStore();
} }

View file

@ -1472,7 +1472,7 @@ ref<Store> openStore(const std::string & uri_,
parsedUri.authority.value_or("") + parsedUri.path parsedUri.authority.value_or("") + parsedUri.path
); );
for (auto implem : *Implementations::registered) { for (auto implem : *StoreImplementations::registered) {
if (implem.uriSchemes.count(parsedUri.scheme)) { if (implem.uriSchemes.count(parsedUri.scheme)) {
auto store = implem.create(parsedUri.scheme, baseURI, params); auto store = implem.create(parsedUri.scheme, baseURI, params);
if (store) { if (store) {
@ -1526,6 +1526,6 @@ std::list<ref<Store>> getDefaultSubstituters()
return stores; return stores;
} }
std::vector<StoreFactory> * Implementations::registered = 0; std::vector<StoreFactory> * StoreImplementations::registered = 0;
} }

View file

@ -50,11 +50,9 @@ namespace nix {
* that calls `StoreConfig(params)` (otherwise you're gonna encounter an * that calls `StoreConfig(params)` (otherwise you're gonna encounter an
* `assertion failure` when trying to instantiate it). * `assertion failure` when trying to instantiate it).
* *
* You can then register the new store using: * You can then register the new store by defining a registration function
* * (using `StoreImplementations::add`) and calling it in
* ``` * `registerStoreImplementations` in `globals.cc`.
* cpp static RegisterStoreImplementation<Foo, FooConfig> regStore;
* ```
*/ */
MakeError(SubstError, Error); MakeError(SubstError, Error);
@ -1004,7 +1002,7 @@ struct StoreFactory
std::function<std::shared_ptr<StoreConfig> ()> getConfig; std::function<std::shared_ptr<StoreConfig> ()> getConfig;
}; };
struct Implementations struct StoreImplementations
{ {
static std::vector<StoreFactory> * registered; static std::vector<StoreFactory> * registered;
@ -1027,15 +1025,6 @@ struct Implementations
} }
}; };
template<typename T, typename TConfig>
struct RegisterStoreImplementation
{
RegisterStoreImplementation()
{
Implementations::add<T, TConfig>();
}
};
/** /**
* Display a set of paths in human-readable form (i.e., between quotes * Display a set of paths in human-readable form (i.e., between quotes

View file

@ -88,6 +88,8 @@ void UDSRemoteStore::addIndirectRoot(const Path & path)
} }
static RegisterStoreImplementation<UDSRemoteStore, UDSRemoteStoreConfig> regUDSRemoteStore; void registerUDSRemoteStore() {
StoreImplementations::add<UDSRemoteStore, UDSRemoteStoreConfig>();
}
} }

View file

@ -63,4 +63,6 @@ private:
std::optional<std::string> path; std::optional<std::string> path;
}; };
void registerUDSRemoteStore();
} }

View file

@ -201,7 +201,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs
res["args"] = toJSON(); res["args"] = toJSON();
auto stores = nlohmann::json::object(); auto stores = nlohmann::json::object();
for (auto & implem : *Implementations::registered) { for (auto & implem : *StoreImplementations::registered) {
auto storeConfig = implem.getConfig(); auto storeConfig = implem.getConfig();
auto storeName = storeConfig->name(); auto storeName = storeConfig->name();
auto & j = stores[storeName]; auto & j = stores[storeName];