Skip to content

Commit

Permalink
Merge pull request #10955 from NixOS/meson-nix-util-c
Browse files Browse the repository at this point in the history
Build nix-util-c with meson + unit tests
  • Loading branch information
Ericson2314 authored Jun 25, 2024
2 parents ccb679e + ae3304b commit 65d7c80
Show file tree
Hide file tree
Showing 16 changed files with 768 additions and 1 deletion.
69 changes: 68 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
"stdenv"
];

/**
`flatMapAttrs attrs f` applies `f` to each attribute in `attrs` and
merges the results into a single attribute set.
This can be nested to form a build matrix where all the attributes
generated by the innermost `f` are returned as is.
(Provided that the names are unique.)
See https://nixos.org/manual/nixpkgs/stable/index.html#function-library-lib.attrsets.concatMapAttrs
*/
flatMapAttrs = attrs: f: lib.concatMapAttrs f attrs;

forAllSystems = lib.genAttrs systems;

forAllCrossSystems = lib.genAttrs crossSystems;
Expand Down Expand Up @@ -160,6 +172,19 @@
};
});

# TODO: define everything here instead of top level?
nix-components = {
inherit (final)
nix-util
nix-util-test-support
nix-util-test
nix-util-c
nix-store
nix-fetchers
nix-perl-bindings
;
};

nix-util = final.callPackage ./src/libutil/package.nix {
inherit
fileset
Expand All @@ -169,6 +194,30 @@
;
};

nix-util-test-support = final.callPackage ./tests/unit/libutil-support/package.nix {
inherit
fileset
stdenv
versionSuffix
;
};

nix-util-test = final.callPackage ./tests/unit/libutil/package.nix {
inherit
fileset
stdenv
versionSuffix
;
};

nix-util-c = final.callPackage ./src/libutil-c/package.nix {
inherit
fileset
stdenv
versionSuffix
;
};

nix-store = final.callPackage ./src/libstore/package.nix {
inherit
fileset
Expand Down Expand Up @@ -281,7 +330,25 @@
# the old build system is gone and we are back to one build
# system, we should reenable this.
#perlBindings = self.hydraJobs.perlBindings.${system};
} // devFlake.checks.${system} or {}
}
# Add "passthru" tests
// flatMapAttrs ({
"" = nixpkgsFor.${system}.native;
} // lib.optionalAttrs (! nixpkgsFor.${system}.native.stdenv.hostPlatform.isDarwin) {
# TODO: enable static builds for darwin, blocked on:
# https://github.com/NixOS/nixpkgs/issues/320448
"static-" = nixpkgsFor.${system}.static;
})
(nixpkgsPrefix: nixpkgs:
flatMapAttrs nixpkgs.nix-components
(pkgName: pkg:
flatMapAttrs pkg.tests or {}
(testName: test: {
"${nixpkgsPrefix}${pkgName}-${testName}" = test;
})
)
)
// devFlake.checks.${system} or {}
);

packages = forAllSystems (system: {
Expand Down
3 changes: 3 additions & 0 deletions maintainers/hydra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ let
forAllPackages = lib.genAttrs [
"nix"
"nix-util"
"nix-util-c"
"nix-util-test-support"
"nix-util-test"
"nix-store"
"nix-fetchers"
];
Expand Down
7 changes: 7 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ subproject('libfetchers')
subproject('perl')
subproject('internal-api-docs')
subproject('external-api-docs')

# C wrappers
subproject('libutil-c')

# Testing
subproject('libutil-test-support')
subproject('libutil-test')
1 change: 1 addition & 0 deletions src/libutil-c/.version
117 changes: 117 additions & 0 deletions src/libutil-c/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
project('nix-util-c', 'cpp',
version : files('.version'),
default_options : [
'cpp_std=c++2a',
# TODO(Qyriad): increase the warning level
'warning_level=1',
'debug=true',
'optimization=2',
'errorlogs=true', # Please print logs for tests that fail
],
meson_version : '>= 1.1',
license : 'LGPL-2.1-or-later',
)

cxx = meson.get_compiler('cpp')

# See note in ../nix-util/meson.build
deps_private = [ ]

# See note in ../nix-util/meson.build
deps_public = [ ]

# See note in ../nix-util/meson.build
deps_other = [ ]

configdata = configuration_data()

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', 'config-util.h',
# '-include', 'config-store.h',
'-Wno-deprecated-declarations',
'-Wimplicit-fallthrough',
'-Werror=switch',
'-Werror=switch-enum',
'-Wdeprecated-copy',
'-Wignored-qualifiers',
# Enable assertions in libstdc++ by default. Harmless on libc++. Benchmarked
# at ~1% overhead in `nix search`.
#
# FIXME: remove when we get meson 1.4.0 which will default this to on for us:
# https://mesonbuild.com/Release-notes-for-1-4-0.html#ndebug-setting-now-controls-c-stdlib-assertions
'-D_GLIBCXX_ASSERTIONS=1',
language : 'cpp',
)

sources = files(
'nix_api_util.cc',
)

include_dirs = [include_directories('.')]

headers = files(
'nix_api_util.h',
'nix_api_util_internal.h',
)

if host_machine.system() == 'cygwin' or host_machine.system() == 'windows'
# Windows DLLs are stricter about symbol visibility than Unix shared
# objects --- see https://gcc.gnu.org/wiki/Visibility for details.
# This is a temporary sledgehammer to export everything like on Unix,
# and not detail with this yet.
#
# TODO do not do this, and instead do fine-grained export annotations.
linker_export_flags = ['-Wl,--export-all-symbols']
else
linker_export_flags = []
endif

nix_util = dependency('nix-util')
if nix_util.type_name() == 'internal'
# subproject sadly no good for pkg-config module
deps_other += nix_util
else
deps_public += nix_util
endif

# TODO rename, because it will conflict with downstream projects
configdata.set_quoted('PACKAGE_VERSION', meson.project_version())

config_h = configure_file(
configuration : configdata,
output : 'config-util.h',
)

this_library = library(
'nixutilc',
sources,
dependencies : deps_public + deps_private + deps_other,
include_directories : include_dirs,
link_args: linker_export_flags,
install : true,
)

install_headers(headers, subdir : 'nix', preserve_path : true)

libraries_private = []

import('pkgconfig').generate(
this_library,
filebase : meson.project_name(),
name : 'Nix',
description : 'Nix Package Manager',
subdirs : ['nix'],
extra_cflags : ['-std=c++2a'],
requires : deps_public,
requires_private : deps_private,
libraries_private : libraries_private,
)

meson.override_dependency(meson.project_name(), declare_dependency(
include_directories : include_dirs,
link_with : this_library,
compile_args : ['-std=c++2a'],
dependencies : [],
))
1 change: 1 addition & 0 deletions src/libutil-c/meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# vim: filetype=meson
9 changes: 9 additions & 0 deletions src/libutil-c/nix-util-c.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prefix=@prefix@
libdir=@libdir@
includedir=@includedir@

Name: Nix libutil C API
Description: Common functions for the Nix C API, such as error handling
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lnixutil
Cflags: -I${includedir}/nix -std=c++2a
96 changes: 96 additions & 0 deletions src/libutil-c/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{ lib
, stdenv
, releaseTools
, fileset

, meson
, ninja
, pkg-config

, nix-util

# Configuration Options

, versionSuffix ? ""

# Check test coverage of Nix. Probably want to use with at least
# one of `doCheck` or `doInstallCheck` enabled.
, withCoverageChecks ? false
}:

let
version = lib.fileContents ./.version + versionSuffix;

mkDerivation =
if withCoverageChecks
then
# TODO support `finalAttrs` args function in
# `releaseTools.coverageAnalysis`.
argsFun:
releaseTools.coverageAnalysis (let args = argsFun args; in args)
else stdenv.mkDerivation;
in

mkDerivation (finalAttrs: {
pname = "nix-util-c";
inherit version;

src = fileset.toSource {
root = ./.;
fileset = fileset.unions [
./meson.build
./meson.options
(fileset.fileFilter (file: file.hasExt "cc") ./.)
(fileset.fileFilter (file: file.hasExt "hh") ./.)
(fileset.fileFilter (file: file.hasExt "h") ./.)
];
};

outputs = [ "out" "dev" ];

nativeBuildInputs = [
meson
ninja
pkg-config
];

buildInputs = [
nix-util
]
;

propagatedBuildInputs = [
nix-util
];

preConfigure =
# "Inline" .version so it's not a symlink, and includes the suffix
''
echo ${version} > .version
'';

mesonFlags = [
];

env = lib.optionalAttrs (stdenv.isLinux && !(stdenv.hostPlatform.isStatic && stdenv.system == "aarch64-linux")) {
LDFLAGS = "-fuse-ld=gold";
};

enableParallelBuilding = true;

separateDebugInfo = !stdenv.hostPlatform.isStatic;

# TODO Always true after https://github.com/NixOS/nixpkgs/issues/318564
strictDeps = !withCoverageChecks;

hardeningDisable = lib.optional stdenv.hostPlatform.isStatic "pie";

meta = {
platforms = lib.platforms.unix ++ lib.platforms.windows;
};

} // lib.optionalAttrs withCoverageChecks {
lcovFilter = [ "*/boost/*" "*-tab.*" ];

hardeningDisable = [ "fortify" ];
})
1 change: 1 addition & 0 deletions src/libutil-test
1 change: 1 addition & 0 deletions src/libutil-test-support
1 change: 1 addition & 0 deletions tests/unit/libutil-support/.version
Loading

0 comments on commit 65d7c80

Please sign in to comment.