-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nix-flake-c, nix_flake_init_global, nix_flake_settings_new
- Loading branch information
Showing
14 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../build-utils-meson/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
project('nix-flake-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') | ||
|
||
subdir('build-utils-meson/deps-lists') | ||
|
||
configdata = configuration_data() | ||
|
||
deps_private_maybe_subproject = [ | ||
dependency('nix-util'), | ||
dependency('nix-store'), | ||
dependency('nix-expr'), | ||
dependency('nix-flake'), | ||
] | ||
deps_public_maybe_subproject = [ | ||
dependency('nix-util-c'), | ||
dependency('nix-store-c'), | ||
dependency('nix-expr-c'), | ||
] | ||
subdir('build-utils-meson/subprojects') | ||
|
||
# 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-flake.h', | ||
) | ||
|
||
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. | ||
|
||
# From C++ libraries, only for internals | ||
'-include', 'config-util.hh', | ||
'-include', 'config-store.hh', | ||
'-include', 'config-expr.hh', | ||
# not generated (yet?) | ||
# '-include', 'config-flake.hh', | ||
|
||
# From C libraries, for our public, installed headers too | ||
'-include', 'config-util.h', | ||
'-include', 'config-store.h', | ||
'-include', 'config-expr.h', | ||
'-include', 'config-flake.h', | ||
language : 'cpp', | ||
) | ||
|
||
subdir('build-utils-meson/common') | ||
|
||
sources = files( | ||
'nix_api_flake.cc', | ||
) | ||
|
||
include_dirs = [include_directories('.')] | ||
|
||
headers = [config_h] + files( | ||
'nix_api_flake.h', | ||
) | ||
|
||
# TODO move this header to libexpr, maybe don't use it in tests? | ||
headers += files('nix_api_flake.h') | ||
|
||
subdir('build-utils-meson/export-all-symbols') | ||
subdir('build-utils-meson/windows-version') | ||
|
||
this_library = library( | ||
'nixflakec', | ||
sources, | ||
dependencies : deps_public + deps_private + deps_other, | ||
include_directories : include_dirs, | ||
link_args: linker_export_flags, | ||
prelink : true, # For C++ static initializers | ||
install : true, | ||
) | ||
|
||
install_headers(headers, subdir : 'nix', preserve_path : true) | ||
|
||
libraries_private = [] | ||
|
||
subdir('build-utils-meson/export') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "nix_api_flake.h" | ||
#include "nix_api_flake_internal.hh" | ||
#include "nix_api_util_internal.h" | ||
|
||
#include "flake/flake.hh" | ||
|
||
nix_flake_settings * nix_flake_settings_new(nix_c_context * context) | ||
{ | ||
try { | ||
auto settings = nix::make_ref<nix::flake::Settings>(); | ||
return new nix_flake_settings{settings}; | ||
} | ||
NIXC_CATCH_ERRS_NULL | ||
} | ||
|
||
void nix_flake_settings_free(nix_flake_settings * settings) | ||
{ | ||
delete settings; | ||
} | ||
|
||
nix_err nix_flake_init_global(nix_c_context * context, nix_flake_settings * settings) | ||
{ | ||
static std::shared_ptr<nix::flake::Settings> registeredSettings; | ||
try { | ||
if (registeredSettings) | ||
throw nix::Error("nix_flake_init_global already initialized"); | ||
|
||
registeredSettings = settings->settings; | ||
nix::flake::initLib(*registeredSettings); | ||
} | ||
NIXC_CATCH_ERRS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef NIX_API_FLAKE_H | ||
#define NIX_API_FLAKE_H | ||
/** @defgroup libflake libflake | ||
* @brief Bindings to the Nix Flakes library | ||
* | ||
* @{ | ||
*/ | ||
/** @file | ||
* @brief Main entry for the libflake C bindings | ||
*/ | ||
|
||
#include "nix_api_store.h" | ||
#include "nix_api_util.h" | ||
#include "nix_api_expr.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
// cffi start | ||
|
||
typedef struct nix_flake_settings nix_flake_settings; | ||
|
||
// Function prototypes | ||
/** | ||
* Create a nix_flake_settings initialized with default values. | ||
* @param[out] context Optional, stores error information | ||
* @return A new nix_flake_settings or NULL on failure. | ||
* @see nix_flake_settings_free | ||
*/ | ||
nix_flake_settings * nix_flake_settings_new(nix_c_context * context); | ||
|
||
/** | ||
* @brief Release the resources associated with a nix_flake_settings. | ||
*/ | ||
void nix_flake_settings_free(nix_flake_settings * settings); | ||
|
||
/** | ||
* @brief Register Flakes support process-wide. | ||
*/ | ||
nix_err nix_flake_init_global(nix_c_context * context, nix_flake_settings * settings); | ||
|
||
} // extern "C" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "ref.hh" | ||
#include "flake/settings.hh" | ||
|
||
struct nix_flake_settings | ||
{ | ||
nix::ref<nix::flake::Settings> settings; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ lib | ||
, stdenv | ||
, mkMesonLibrary | ||
|
||
, nix-store-c | ||
, nix-expr-c | ||
, nix-flake | ||
|
||
# Configuration Options | ||
|
||
, version | ||
}: | ||
|
||
let | ||
inherit (lib) fileset; | ||
in | ||
|
||
mkMesonLibrary (finalAttrs: { | ||
pname = "nix-flake-c"; | ||
inherit version; | ||
|
||
workDir = ./.; | ||
fileset = fileset.unions [ | ||
../../build-utils-meson | ||
./build-utils-meson | ||
../../.version | ||
./.version | ||
./meson.build | ||
# ./meson.options | ||
(fileset.fileFilter (file: file.hasExt "cc") ./.) | ||
(fileset.fileFilter (file: file.hasExt "hh") ./.) | ||
(fileset.fileFilter (file: file.hasExt "h") ./.) | ||
]; | ||
|
||
propagatedBuildInputs = [ | ||
nix-expr-c | ||
nix-store-c | ||
nix-flake | ||
]; | ||
|
||
preConfigure = | ||
# "Inline" .version so it's not a symlink, and includes the suffix. | ||
# Do the meson utils, without modification. | ||
'' | ||
chmod u+w ./.version | ||
echo ${version} > ../../.version | ||
''; | ||
|
||
mesonFlags = [ | ||
]; | ||
|
||
env = lib.optionalAttrs (stdenv.isLinux && !(stdenv.hostPlatform.isStatic && stdenv.system == "aarch64-linux")) { | ||
LDFLAGS = "-fuse-ld=gold"; | ||
}; | ||
|
||
meta = { | ||
platforms = lib.platforms.unix ++ lib.platforms.windows; | ||
}; | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "nix_api_store.h" | ||
#include "nix_api_store_internal.h" | ||
#include "nix_api_util.h" | ||
#include "nix_api_util_internal.h" | ||
#include "nix_api_expr.h" | ||
#include "nix_api_value.h" | ||
#include "nix_api_flake.h" | ||
|
||
#include "tests/nix_api_expr.hh" | ||
#include "tests/string_callback.hh" | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
namespace nixC { | ||
|
||
TEST_F(nix_api_store_test, nix_api_init_global_getFlake_exists) | ||
{ | ||
nix_libstore_init(ctx); | ||
assert_ctx_ok(); | ||
nix_libexpr_init(ctx); | ||
assert_ctx_ok(); | ||
|
||
auto settings = nix_flake_settings_new(ctx); | ||
assert_ctx_ok(); | ||
ASSERT_NE(nullptr, settings); | ||
|
||
nix_flake_init_global(ctx, settings); | ||
assert_ctx_ok(); | ||
|
||
nix_eval_state_builder * builder = nix_eval_state_builder_new(ctx, store); | ||
ASSERT_NE(nullptr, builder); | ||
assert_ctx_ok(); | ||
|
||
auto state = nix_eval_state_build(ctx, builder); | ||
assert_ctx_ok(); | ||
ASSERT_NE(nullptr, state); | ||
|
||
nix_eval_state_builder_free(builder); | ||
|
||
auto value = nix_alloc_value(ctx, state); | ||
assert_ctx_ok(); | ||
ASSERT_NE(nullptr, value); | ||
|
||
nix_err err = nix_expr_eval_from_string(ctx, state, "builtins.getFlake", ".", value); | ||
assert_ctx_ok(); | ||
ASSERT_EQ(NIX_OK, err); | ||
ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(ctx, value)); | ||
} | ||
|
||
} // namespace nixC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters