daemon: Use inline functions and variables instead of extern

Avoid separate declarations and definitions for so-called 'global' objects.

* nix/libstore/derivations.{cc, hh} (drvHashes): Use inline instead of separate
declaration and definition.
* nix/libstore/globals.{cc, hh} (settings, nixVersion): Same.
* nix/libstore/local-store.hh (drvsLogDir, deduplicationMinSize): Same.
* nix/libstore/optimise-store.cc (deduplicationMinSize): Same.
* nix/libstore/store-api.{cc, hh} (store): Same.
* nix/libutil/archive.{cc, hh} (defaultPathFilter): Same.
* nix/libutil/hash.{cc, hh} (base32Chars): Same and modify header files.
* nix/libutil/util.{cc, hh} (logType, verbosity, _writeToStderr,
_isInterrupted): Same.
* nix/local.mk: Modified according to the rename of shared.hh.
* nix/nix-daemon/guix-daemon.cc (blockInt, argvSaved, run): Same and
modify header files.
* nix/nix-daemon/nix-daemon.cc: Modify header files.
* nix/nix-daemon/shared.hh: Renamed to nix-daemon.hh

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Congcong Kuo 2025-10-20 11:39:00 +08:00 committed by Ludovic Courtès
parent 6fe5e7f707
commit d1a9fb85a4
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
19 changed files with 30 additions and 72 deletions

View file

@ -3367,9 +3367,6 @@ void DerivationGoal::registerOutputs()
}
string drvsLogDir = "drvs";
Path DerivationGoal::openLogFile()
{
logSize = 0;

View file

@ -211,9 +211,6 @@ bool isFixedOutputDrv(const Derivation & drv)
}
DrvHashes drvHashes;
/* Returns the hash of a derivation modulo fixed-output
subderivations. A fixed-output derivation is a derivation with one
output (`out') for which an expected hash and hash algorithm are

View file

@ -77,7 +77,7 @@ Hash hashDerivationModulo(StoreAPI & store, Derivation drv);
/* Memoisation of hashDerivationModulo(). */
typedef std::map<Path, Hash> DrvHashes;
extern DrvHashes drvHashes;
inline DrvHashes drvHashes;
/* Split a string specifying a derivation and a set of outputs
(/nix/store/hash-foo!out1,out2,...) into the derivation path and

View file

@ -17,9 +17,6 @@ namespace nix {
#define DEFAULT_SOCKET_PATH "/daemon-socket/socket"
Settings settings;
Settings::Settings()
{
keepFailed = false;
@ -205,7 +202,4 @@ Settings::SettingsMap Settings::getOverrides()
}
const string nixVersion = PACKAGE_VERSION;
}

View file

@ -227,10 +227,10 @@ private:
// FIXME: don't use a global variable.
extern Settings settings;
inline Settings settings;
extern const string nixVersion;
inline const string nixVersion {PACKAGE_VERSION};
}

View file

@ -19,7 +19,7 @@ namespace nix {
const int nixSchemaVersion = 7;
extern string drvsLogDir;
inline std::string drvsLogDir {"drvs"};
struct Derivation;
@ -293,6 +293,8 @@ void canonicaliseTimestampAndPermissions(const Path & path);
MakeError(PathInUse, Error);
/* Size below which a file is not considered for deduplication. */
extern const size_t deduplicationMinSize;
/* Any file smaller than this is not considered for deduplication.
Keep in sync with (guix store deduplication). */
inline const size_t deduplicationMinSize {8192};
}

View file

@ -16,9 +16,6 @@
namespace nix {
/* Any file smaller than this is not considered for deduplication.
Keep in sync with (guix store deduplication). */
const size_t deduplicationMinSize = 8192;
static void makeWritable(const Path & path)
{

View file

@ -253,16 +253,3 @@ template<class T> T readStorePaths(Source & from)
template PathSet readStorePaths(Source & from);
}
#include "local-store.hh"
#include "serialise.hh"
namespace nix {
std::shared_ptr<StoreAPI> store;
}

View file

@ -363,7 +363,7 @@ Paths topoSortPaths(StoreAPI & store, const PathSet & paths);
/* For now, there is a single global store API object, but we'll
purify that in the future. */
extern std::shared_ptr<StoreAPI> store;
inline std::shared_ptr<StoreAPI> store;
/* Display a set of paths in human-readable form (i.e., between quotes

View file

@ -24,8 +24,6 @@ static string archiveVersion1 = "nix-archive-1";
static string caseHackSuffix = "~nix~case~hack~";
PathFilter defaultPathFilter;
static void dumpContents(const Path & path, size_t size,
Sink & sink)

View file

@ -50,7 +50,7 @@ struct PathFilter
virtual bool operator () (const Path & path) { return true; }
};
extern PathFilter defaultPathFilter;
inline PathFilter defaultPathFilter;
void dumpPath(const Path & path, Sink & sink,
PathFilter & filter = defaultPathFilter);

View file

@ -103,10 +103,6 @@ unsigned int hashLength32(const Hash & hash)
}
// omitted: E O U T
const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
string printHash32(const Hash & hash)
{
Hash hash2(hash);

View file

@ -2,14 +2,15 @@
#include <gcrypt.h>
#include "archive.hh"
#include "types.hh"
#include "serialise.hh"
namespace nix {
extern const string base32Chars;
// omitted: E O U T
inline const string base32Chars {"0123456789abcdfghijklmnpqrsvwxyz"};
typedef enum {
htUnknown = 0,
@ -79,8 +80,6 @@ Hash hashFile(HashType ht, const Path & path);
/* Compute the hash of the given path. The hash is defined as
(essentially) hashString(ht, dumpPath(path)). */
struct PathFilter;
extern PathFilter defaultPathFilter;
typedef std::pair<Hash, unsigned long long> HashResult;
HashResult hashPath(HashType ht, const Path & path,
PathFilter & filter = defaultPathFilter);

View file

@ -600,9 +600,6 @@ void createSymlink(const Path & target, const Path & link)
}
LogType logType = ltPretty;
Verbosity verbosity = lvlInfo;
static int nestingLevel = 0;
@ -691,9 +688,6 @@ void writeToStderr(const string & s)
}
void (*_writeToStderr) (const unsigned char * buf, size_t count) = 0;
void readFull(int fd, unsigned char * buf, size_t count)
{
while (count) {
@ -1250,7 +1244,6 @@ void keepOnExec(int fd)
//////////////////////////////////////////////////////////////////////
volatile sig_atomic_t _isInterrupted = 0;
void _interrupted()
{

View file

@ -132,8 +132,8 @@ typedef enum {
ltFlat /* no nesting */
} LogType;
extern LogType logType;
extern Verbosity verbosity; /* suppress msgs > this */
inline LogType logType {ltPretty};
inline Verbosity verbosity {lvlInfo}; /* suppress msgs > this */
class Nest
{
@ -167,7 +167,7 @@ void warnOnce(bool & haveWarned, std::string_view fs);
void writeToStderr(const string & s);
extern void (*_writeToStderr) (const unsigned char * buf, size_t count);
inline void (*_writeToStderr) (const unsigned char * buf, size_t count) = 0;
/* Wrappers arount read()/write() that read/write exactly the
@ -323,7 +323,7 @@ void commonChildInit(Pipe & logPipe);
/* User interruption. */
extern volatile sig_atomic_t _isInterrupted;
inline volatile sig_atomic_t _isInterrupted = 0;
void _interrupted();

View file

@ -113,7 +113,7 @@ guix_daemon_LDADD = \
$(SQLITE3_LIBS) $(LIBGCRYPT_LIBS)
guix_daemon_headers = \
%D%/nix-daemon/shared.hh
%D%/nix-daemon/nix-daemon.hh
if HAVE_LIBBZ2

View file

@ -17,12 +17,12 @@
You should have received a copy of the GNU General Public License
along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "config.h"
#include <types.hh>
#include "shared.hh"
#include <globals.hh>
#include <util.hh>
#include "types.hh"
#include "nix-daemon.hh"
#include "globals.hh"
#include "util.hh"
#include <gcrypt.h>
@ -36,20 +36,13 @@
#include <netdb.h>
#include <strings.h>
#include <exception>
#include <iostream>
#include <format>
#include <libintl.h>
#include <locale.h>
/* Variables used by `nix-daemon.cc'. */
volatile ::sig_atomic_t blockInt;
char **argvSaved;
using namespace nix;
/* Entry point in `nix-daemon.cc'. */
extern void run (const std::vector<int> &);
/* Command-line options. */

View file

@ -1,5 +1,5 @@
#include "config.h"
#include "shared.hh"
#include "nix-daemon.hh"
#include "local-store.hh"
#include "util.hh"
#include "serialise.hh"

View file

@ -20,9 +20,14 @@
#pragma once
#include <vector>
#include <stdlib.h>
#include <signal.h>
extern volatile ::sig_atomic_t blockInt;
/* Variables used by `nix-daemon.cc'. */
inline volatile ::sig_atomic_t blockInt;
inline char **argvSaved;
extern char **argvSaved;
/* Entry point in `nix-daemon.cc'. */
void run (const std::vector<int> &);