From 4f02255c205378427f5831463c0c07e45382b2b2 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Wed, 5 Jun 2024 06:02:18 +0200 Subject: [PATCH] libstore: remove static initializers for Store registrations Ref #359. Change-Id: Ia45530ddee25fa9fc399ff10738bb0d8bbc8b221 --- src/libstore/dummy-store.cc | 5 +++- src/libstore/dummy-store.hh | 8 +++++++ src/libstore/globals.cc | 23 +++++++++++++++++++ src/libstore/http-binary-cache-store.cc | 5 +++- src/libstore/http-binary-cache-store.hh | 8 +++++++ src/libstore/legacy-ssh-store.cc | 7 ++++-- src/libstore/legacy-ssh-store.hh | 8 +++++++ src/libstore/local-binary-cache-store.cc | 5 +++- src/libstore/local-binary-cache-store.hh | 8 +++++++ src/libstore/local-store.hh | 3 +++ src/libstore/meson.build | 6 ++++- src/libstore/platform/darwin.cc | 5 ++++ src/libstore/platform/fallback.cc | 4 +++- src/libstore/platform/linux.cc | 4 +++- src/libstore/s3-binary-cache-store.cc | 8 ++++++- src/libstore/s3-binary-cache-store.hh | 2 ++ src/libstore/ssh-store.cc | 6 +++-- .../{ssh-store-config.hh => ssh-store.hh} | 2 ++ src/libstore/store-api.cc | 4 ++-- src/libstore/store-api.hh | 19 ++++----------- src/libstore/uds-remote-store.cc | 4 +++- src/libstore/uds-remote-store.hh | 2 ++ src/nix/main.cc | 2 +- 23 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 src/libstore/dummy-store.hh create mode 100644 src/libstore/http-binary-cache-store.hh create mode 100644 src/libstore/legacy-ssh-store.hh create mode 100644 src/libstore/local-binary-cache-store.hh rename src/libstore/{ssh-store-config.hh => ssh-store.hh} (97%) diff --git a/src/libstore/dummy-store.cc b/src/libstore/dummy-store.cc index e16f87e4b..f14b7ddd1 100644 --- a/src/libstore/dummy-store.cc +++ b/src/libstore/dummy-store.cc @@ -1,3 +1,4 @@ +#include "dummy-store.hh" #include "store-api.hh" namespace nix { @@ -73,6 +74,8 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store { unsupported("getFSAccessor"); } }; -static RegisterStoreImplementation regDummyStore; +void registerDummyStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/dummy-store.hh b/src/libstore/dummy-store.hh new file mode 100644 index 000000000..355c011f8 --- /dev/null +++ b/src/libstore/dummy-store.hh @@ -0,0 +1,8 @@ +#pragma once +///@file + +namespace nix { + +void registerDummyStore(); + +} diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 6cfa3ffac..b534882de 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -33,6 +33,16 @@ #include #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 { @@ -396,6 +406,17 @@ static void preloadNSS() }); } +static void registerStoreImplementations() { + registerDummyStore(); + registerHttpBinaryCacheStore(); + registerLegacySSHStore(); + registerLocalBinaryCacheStore(); + registerLocalStore(); + registerS3BinaryCacheStore(); + registerSSHStore(); + registerUDSRemoteStore(); +} + static bool initLibStoreDone = false; void assertLibStoreInitialized() { @@ -433,6 +454,8 @@ void initLibStore() { unsetenv("TMPDIR"); #endif + registerStoreImplementations(); + initLibStoreDone = true; } diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 7ceea716a..c68faf552 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -1,3 +1,4 @@ +#include "http-binary-cache-store.hh" #include "binary-cache-store.hh" #include "filetransfer.hh" #include "globals.hh" @@ -194,6 +195,8 @@ protected: } }; -static RegisterStoreImplementation regHttpBinaryCacheStore; +void registerHttpBinaryCacheStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/http-binary-cache-store.hh b/src/libstore/http-binary-cache-store.hh new file mode 100644 index 000000000..097c5480b --- /dev/null +++ b/src/libstore/http-binary-cache-store.hh @@ -0,0 +1,8 @@ +#pragma once +///@file + +namespace nix { + +void registerHttpBinaryCacheStore(); + +} diff --git a/src/libstore/legacy-ssh-store.cc b/src/libstore/legacy-ssh-store.cc index 5650282d6..9e2d65a1c 100644 --- a/src/libstore/legacy-ssh-store.cc +++ b/src/libstore/legacy-ssh-store.cc @@ -1,4 +1,4 @@ -#include "ssh-store-config.hh" +#include "legacy-ssh-store.hh" #include "archive.hh" #include "pool.hh" #include "remote-store.hh" @@ -8,6 +8,7 @@ #include "store-api.hh" #include "path-with-outputs.hh" #include "ssh.hh" +#include "ssh-store.hh" #include "derivations.hh" namespace nix { @@ -412,6 +413,8 @@ public: { unsupported("queryRealisation"); } }; -static RegisterStoreImplementation regLegacySSHStore; +void registerLegacySSHStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/legacy-ssh-store.hh b/src/libstore/legacy-ssh-store.hh new file mode 100644 index 000000000..76298d8d9 --- /dev/null +++ b/src/libstore/legacy-ssh-store.hh @@ -0,0 +1,8 @@ +#pragma once +///@file + +namespace nix { + +void registerLegacySSHStore(); + +} diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 99dd0e0f6..2f6a092e1 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -1,3 +1,4 @@ +#include "local-binary-cache-store.hh" #include "binary-cache-store.hh" #include "globals.hh" #include "nar-info-disk-cache.hh" @@ -124,6 +125,8 @@ std::set LocalBinaryCacheStore::uriSchemes() return {"file"}; } -static RegisterStoreImplementation regLocalBinaryCacheStore; +void registerLocalBinaryCacheStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/local-binary-cache-store.hh b/src/libstore/local-binary-cache-store.hh new file mode 100644 index 000000000..727d60cb3 --- /dev/null +++ b/src/libstore/local-binary-cache-store.hh @@ -0,0 +1,8 @@ +#pragma once +///@file + +namespace nix { + +void registerLocalBinaryCacheStore(); + +} diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh index f6b553615..1913aa192 100644 --- a/src/libstore/local-store.hh +++ b/src/libstore/local-store.hh @@ -421,4 +421,7 @@ void canonicaliseTimestampAndPermissions(const Path & path); MakeError(PathInUse, Error); +// Implemented by the relevant platform/ module being used. +void registerLocalStore(); + } diff --git a/src/libstore/meson.build b/src/libstore/meson.build index 5416bd2b5..74f5cd04e 100644 --- a/src/libstore/meson.build +++ b/src/libstore/meson.build @@ -118,12 +118,16 @@ libstore_headers = files( 'derived-path-map.hh', 'derived-path.hh', 'downstream-placeholder.hh', + 'dummy-store.hh', 'filetransfer.hh', 'fs-accessor.hh', 'gc-store.hh', 'globals.hh', + 'http-binary-cache-store.hh', 'indirect-root-store.hh', + 'legacy-ssh-store.hh', 'length-prefixed-protocol-helper.hh', + 'local-binary-cache-store.hh', 'local-fs-store.hh', 'local-store.hh', 'lock.hh', @@ -152,8 +156,8 @@ libstore_headers = files( 'serve-protocol-impl.hh', 'serve-protocol.hh', 'sqlite.hh', - 'ssh-store-config.hh', 'ssh.hh', + 'ssh-store.hh', 'store-api.hh', 'store-cast.hh', 'uds-remote-store.hh', diff --git a/src/libstore/platform/darwin.cc b/src/libstore/platform/darwin.cc index 1f7e9be23..078753bff 100644 --- a/src/libstore/platform/darwin.cc +++ b/src/libstore/platform/darwin.cc @@ -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()); } + +void registerLocalStore() { + StoreImplementations::add(); +} + } diff --git a/src/libstore/platform/fallback.cc b/src/libstore/platform/fallback.cc index 5a01d64c8..0593ec204 100644 --- a/src/libstore/platform/fallback.cc +++ b/src/libstore/platform/fallback.cc @@ -1,5 +1,7 @@ #include "platform/fallback.hh" namespace nix { -static RegisterStoreImplementation regLocalStore; +void registerLocalStore() { + Implementations::add(); +} } diff --git a/src/libstore/platform/linux.cc b/src/libstore/platform/linux.cc index 03b8bc0be..f22fbe58f 100644 --- a/src/libstore/platform/linux.cc +++ b/src/libstore/platform/linux.cc @@ -25,7 +25,9 @@ namespace { constexpr const std::string_view nativeSystem = SYSTEM; } -static RegisterStoreImplementation regLocalStore; +void registerLocalStore() { + StoreImplementations::add(); +} static void readProcLink(const std::string & file, UncheckedRoots & roots) { diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index ffebfda8d..921a2e556 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -526,8 +526,14 @@ struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStoreConfig, public virtual }; -static RegisterStoreImplementation regS3BinaryCacheStore; +void registerS3BinaryCacheStore() { + StoreImplementations::add(); +} } +#else +namespace nix { +void registerS3BinaryCacheStore() {} +} #endif diff --git a/src/libstore/s3-binary-cache-store.hh b/src/libstore/s3-binary-cache-store.hh index c62ea5147..baf21e2f4 100644 --- a/src/libstore/s3-binary-cache-store.hh +++ b/src/libstore/s3-binary-cache-store.hh @@ -29,4 +29,6 @@ public: virtual const Stats & getS3Stats() = 0; }; +void registerS3BinaryCacheStore(); + } diff --git a/src/libstore/ssh-store.cc b/src/libstore/ssh-store.cc index 80d10eb0f..94c0b7237 100644 --- a/src/libstore/ssh-store.cc +++ b/src/libstore/ssh-store.cc @@ -1,4 +1,4 @@ -#include "ssh-store-config.hh" +#include "ssh-store.hh" #include "store-api.hh" #include "local-fs-store.hh" #include "remote-store.hh" @@ -110,6 +110,8 @@ ref SSHStore::openConnection() return conn; } -static RegisterStoreImplementation regSSHStore; +void registerSSHStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/ssh-store-config.hh b/src/libstore/ssh-store.hh similarity index 97% rename from src/libstore/ssh-store-config.hh rename to src/libstore/ssh-store.hh index bf55d20cf..51951f80b 100644 --- a/src/libstore/ssh-store-config.hh +++ b/src/libstore/ssh-store.hh @@ -26,4 +26,6 @@ struct CommonSSHStoreConfig : virtual StoreConfig )"}; }; +void registerSSHStore(); + } diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 6d9fec41b..f921956e8 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1472,7 +1472,7 @@ ref openStore(const std::string & uri_, parsedUri.authority.value_or("") + parsedUri.path ); - for (auto implem : *Implementations::registered) { + for (auto implem : *StoreImplementations::registered) { if (implem.uriSchemes.count(parsedUri.scheme)) { auto store = implem.create(parsedUri.scheme, baseURI, params); if (store) { @@ -1526,6 +1526,6 @@ std::list> getDefaultSubstituters() return stores; } -std::vector * Implementations::registered = 0; +std::vector * StoreImplementations::registered = 0; } diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index efd0e4d9b..2da5cac39 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -50,11 +50,9 @@ namespace nix { * that calls `StoreConfig(params)` (otherwise you're gonna encounter an * `assertion failure` when trying to instantiate it). * - * You can then register the new store using: - * - * ``` - * cpp static RegisterStoreImplementation regStore; - * ``` + * You can then register the new store by defining a registration function + * (using `StoreImplementations::add`) and calling it in + * `registerStoreImplementations` in `globals.cc`. */ MakeError(SubstError, Error); @@ -1004,7 +1002,7 @@ struct StoreFactory std::function ()> getConfig; }; -struct Implementations +struct StoreImplementations { static std::vector * registered; @@ -1027,15 +1025,6 @@ struct Implementations } }; -template -struct RegisterStoreImplementation -{ - RegisterStoreImplementation() - { - Implementations::add(); - } -}; - /** * Display a set of paths in human-readable form (i.e., between quotes diff --git a/src/libstore/uds-remote-store.cc b/src/libstore/uds-remote-store.cc index 226cdf717..44dd45e88 100644 --- a/src/libstore/uds-remote-store.cc +++ b/src/libstore/uds-remote-store.cc @@ -88,6 +88,8 @@ void UDSRemoteStore::addIndirectRoot(const Path & path) } -static RegisterStoreImplementation regUDSRemoteStore; +void registerUDSRemoteStore() { + StoreImplementations::add(); +} } diff --git a/src/libstore/uds-remote-store.hh b/src/libstore/uds-remote-store.hh index ff7e9ae3f..8b56e0af0 100644 --- a/src/libstore/uds-remote-store.hh +++ b/src/libstore/uds-remote-store.hh @@ -63,4 +63,6 @@ private: std::optional path; }; +void registerUDSRemoteStore(); + } diff --git a/src/nix/main.cc b/src/nix/main.cc index e84e4f310..05c40db03 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -201,7 +201,7 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs res["args"] = toJSON(); auto stores = nlohmann::json::object(); - for (auto & implem : *Implementations::registered) { + for (auto & implem : *StoreImplementations::registered) { auto storeConfig = implem.getConfig(); auto storeName = storeConfig->name(); auto & j = stores[storeName];