1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 12:21:48 +02:00

Cleanup config headers

There are two big changes:

1. Public and private config is now separated. Configuration variables
   that are only used internally do not go in a header which is
   installed.

   (Additionally, libutil has a unix-specific private config header,
   which should only be used in unix-specific code. This keeps things a
   bit more organized, in a purely private implementation-internal way.)

2. Secondly, there is no more `-include`. There are very few config
   items that need to be publically exposed, so now it is feasible to
   just make the headers that need them just including the (public)
   configuration header.

And there are also a few more small cleanups on top of those:

- The configuration files have better names.

- The few CPP variables that remain exposed in the public headers are
  now also renamed to always start with `NIX_`. This ensures they should
  not conflict with variables defined elsewhere.

- We now always use `#if` and not `#ifdef`/`#ifndef` for our
  configuration variables, which helps avoid bugs by requiring that
  variables must be defined in all cases.
This commit is contained in:
John Ericson 2025-03-28 13:24:50 -04:00
parent 5a8dedc45c
commit c204e307ac
59 changed files with 333 additions and 385 deletions

View file

@ -1,8 +1,10 @@
#include "nix/types.hh"
#include "util-config-private.hh"
#if HAVE_LIBCPUID
#include <libcpuid/libcpuid.h>
#include <map>
# include <libcpuid/libcpuid.h>
# include <map>
#endif
namespace nix {

View file

@ -27,6 +27,8 @@
#include "nix/strings-inline.hh"
#include "util-config-private.hh"
namespace nix {
namespace fs { using namespace std::filesystem; }
@ -632,62 +634,6 @@ void replaceSymlink(const fs::path & target, const fs::path & link)
}
}
void setWriteTime(
const fs::path & path,
time_t accessedTime,
time_t modificationTime,
std::optional<bool> optIsSymlink)
{
#ifdef _WIN32
// FIXME use `fs::last_write_time`.
//
// Would be nice to use std::filesystem unconditionally, but
// doesn't support access time just modification time.
//
// System clock vs File clock issues also make that annoying.
warn("Changing file times is not yet implemented on Windows, path is %s", path);
#elif HAVE_UTIMENSAT && HAVE_DECL_AT_SYMLINK_NOFOLLOW
struct timespec times[2] = {
{
.tv_sec = accessedTime,
.tv_nsec = 0,
},
{
.tv_sec = modificationTime,
.tv_nsec = 0,
},
};
if (utimensat(AT_FDCWD, path.c_str(), times, AT_SYMLINK_NOFOLLOW) == -1)
throw SysError("changing modification time of %s (using `utimensat`)", path);
#else
struct timeval times[2] = {
{
.tv_sec = accessedTime,
.tv_usec = 0,
},
{
.tv_sec = modificationTime,
.tv_usec = 0,
},
};
#if HAVE_LUTIMES
if (lutimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s", path);
#else
bool isSymlink = optIsSymlink
? *optIsSymlink
: fs::is_symlink(path);
if (!isSymlink) {
if (utimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s (not a symlink)", path);
} else {
throw Error("Cannot change modification time of symlink %s", path);
}
#endif
#endif
}
void setWriteTime(const fs::path & path, const struct stat & st)
{
setWriteTime(path, st.st_atime, st.st_mtime, S_ISLNK(st.st_mode));

View file

@ -10,6 +10,8 @@
# include "nix/windows-error.hh"
#endif
#include "util-config-private.hh"
namespace nix {
void copyRecursive(

View file

@ -2,12 +2,7 @@
include_dirs = [include_directories('..')]
config_h = configure_file(
configuration : configdata,
output : 'config-util.hh',
)
headers = [config_h] + files(
headers = files(
'abstract-setting-to-json.hh',
'ansicolor.hh',
'archive.hh',

View file

@ -23,36 +23,20 @@ deps_public_maybe_subproject = [
subdir('nix-meson-build-support/subprojects')
# Check for each of these functions, and create a define like `#define
# HAVE_LUTIMES 1`. The `#define` is unconditional, 0 for not found and 1
# for found. One therefore uses it with `#if` not `#ifdef`.
# HAVE_POSIX_FALLOCATE 1`. The `#define` is unconditional, 0 for not
# found and 1 for found. One therefore uses it with `#if` not `#ifdef`.
check_funcs = [
'close_range',
# Optionally used for changing the mtime of symlinks.
'lutimes',
# Optionally used for creating pipes on Unix
'pipe2',
# Optionally used to preallocate files to be large enough before
# writing to them.
# WARNING: define also used in libstore
'posix_fallocate',
# Optionally used to get more information about processes failing due
# to a signal on Unix.
'strsignal',
# Optionally used to try to close more file descriptors (e.g. before
# forking) on Unix.
# WARNING: also used in libexpr
'sysconf',
# Optionally used for changing the mtime of files and symlinks.
'utimensat',
[
'posix_fallocate',
'Optionally used to preallocate files to be large enough before writing to them.',
],
]
foreach funcspec : check_funcs
define_name = 'HAVE_' + funcspec.underscorify().to_upper()
define_value = cxx.has_function(funcspec).to_int()
configdata.set(define_name, define_value)
define_name = 'HAVE_' + funcspec[0].underscorify().to_upper()
define_value = cxx.has_function(funcspec[0]).to_int()
configdata.set(define_name, define_value, description: funcspec[1])
endforeach
configdata.set('HAVE_DECL_AT_SYMLINK_NOFOLLOW', cxx.has_header_symbol('fcntl.h', 'AT_SYMLINK_NOFOLLOW').to_int())
subdir('nix-meson-build-support/libatomic')
if host_machine.system() == 'windows'
@ -116,16 +100,14 @@ deps_public += nlohmann_json
cxx = meson.get_compiler('cpp')
add_project_arguments(
# TODO(Qyriad): Yes this is how the autoconf+Make system did it.
# It would be nice for our headers to be idempotent instead.
'-include', 'nix/config-util.hh',
language : 'cpp',
config_priv_h = configure_file(
configuration : configdata,
output : 'util-config-private.hh',
)
subdir('nix-meson-build-support/common')
sources = files(
sources = [config_priv_h] + files(
'archive.cc',
'args.cc',
'canon-path.cc',

View file

@ -7,6 +7,9 @@
#include <unistd.h>
#include <poll.h>
#include "util-config-private.hh"
#include "util-unix-config-private.hh"
namespace nix {
namespace {

View file

@ -1,10 +1,72 @@
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include "nix/file-system.hh"
#include "util-unix-config-private.hh"
namespace nix {
namespace fs {
using namespace std::filesystem;
}
Descriptor openDirectory(const std::filesystem::path & path)
{
return open(path.c_str(), O_RDONLY | O_DIRECTORY);
}
void setWriteTime(const fs::path & path, time_t accessedTime, time_t modificationTime, std::optional<bool> optIsSymlink)
{
// Would be nice to use std::filesystem unconditionally, but
// doesn't support access time just modification time.
//
// System clock vs File clock issues also make that annoying.
#if HAVE_UTIMENSAT && HAVE_DECL_AT_SYMLINK_NOFOLLOW
struct timespec times[2] = {
{
.tv_sec = accessedTime,
.tv_nsec = 0,
},
{
.tv_sec = modificationTime,
.tv_nsec = 0,
},
};
if (utimensat(AT_FDCWD, path.c_str(), times, AT_SYMLINK_NOFOLLOW) == -1)
throw SysError("changing modification time of %s (using `utimensat`)", path);
#else
struct timeval times[2] = {
{
.tv_sec = accessedTime,
.tv_usec = 0,
},
{
.tv_sec = modificationTime,
.tv_usec = 0,
},
};
# if HAVE_LUTIMES
if (lutimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s", path);
# else
bool isSymlink = optIsSymlink ? *optIsSymlink : fs::is_symlink(path);
if (!isSymlink) {
if (utimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s (not a symlink)", path);
} else {
throw Error("Cannot change modification time of symlink %s", path);
}
# endif
#endif
}
}

View file

@ -1,3 +1,53 @@
include_dirs += include_directories('.')
configdata_unix = configuration_data()
configdata_unix.set(
'HAVE_DECL_AT_SYMLINK_NOFOLLOW',
cxx.has_header_symbol('fcntl.h', 'AT_SYMLINK_NOFOLLOW').to_int(),
description : 'Optionally used for changing the files and symlinks.'
)
# Check for each of these functions, and create a define like `#define
# HAVE_CLOSE_RANGE 1`.
check_funcs_unix = [
[
'close_range',
'For closing many file descriptors after forking.',
],
[
'lutimes',
'Optionally used for changing the mtime of symlinks.',
],
[
'pipe2',
'Optionally used for creating pipes on Unix.',
],
[
'strsignal',
'Optionally used to get more information about processes failing due to a signal on Unix.',
],
[
'sysconf',
'Optionally used to try to close more file descriptors (e.g. before forking) on Unix.',
],
[
'utimensat',
'Optionally used for changing the mtime of files and symlinks.',
],
]
foreach funcspec : check_funcs_unix
define_name = 'HAVE_' + funcspec[0].underscorify().to_upper()
define_value = cxx.has_function(funcspec[0]).to_int()
configdata_unix.set(define_name, define_value, description: funcspec[1])
endforeach
config_unix_priv_h = configure_file(
configuration : configdata_unix,
output : 'util-unix-config-private.hh',
)
sources += config_unix_priv_h
sources += files(
'environment-variables.cc',
'file-descriptor.cc',

View file

@ -28,6 +28,9 @@
# include <sys/mman.h>
#endif
#include "util-config-private.hh"
#include "util-unix-config-private.hh"
namespace nix {

View file

@ -3,6 +3,21 @@
#ifdef _WIN32
namespace nix {
namespace fs {
using namespace std::filesystem;
}
void setWriteTime(const fs::path & path, time_t accessedTime, time_t modificationTime, std::optional<bool> optIsSymlink)
{
// FIXME use `fs::last_write_time`.
//
// Would be nice to use std::filesystem unconditionally, but
// doesn't support access time just modification time.
//
// System clock vs File clock issues also make that annoying.
warn("Changing file times is not yet implemented on Windows, path is %s", path);
}
Descriptor openDirectory(const std::filesystem::path & path)
{
return CreateFileW(