From 7e4608a3f8112451cd5597577a0ac73744c8980e Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Sep 2025 01:54:27 -0400 Subject: [PATCH] More `extern "C"` for FFI This allows us to catch the header and file getting out of sync, because we are not doing overloading by mistake. --- src/libexpr-c/nix_api_expr.cc | 4 ++++ src/libexpr-c/nix_api_expr_internal.h | 4 ++++ src/libexpr-c/nix_api_external.cc | 8 ++++++++ src/libexpr-c/nix_api_value.cc | 4 ++++ src/libfetchers-c/nix_api_fetchers.cc | 4 ++++ src/libflake-c/nix_api_flake.cc | 4 ++++ src/libmain-c/nix_api_main.cc | 4 ++++ src/libstore-c/nix_api_store.cc | 4 ++++ src/libstore-c/nix_api_store_internal.h | 4 ++++ src/libutil-c/nix_api_util.cc | 4 ++++ src/libutil-c/nix_api_util_internal.h | 4 ++++ 11 files changed, 48 insertions(+) diff --git a/src/libexpr-c/nix_api_expr.cc b/src/libexpr-c/nix_api_expr.cc index 02e901de9..a028202ae 100644 --- a/src/libexpr-c/nix_api_expr.cc +++ b/src/libexpr-c/nix_api_expr.cc @@ -40,6 +40,8 @@ static T * unsafe_new_with_self(F && init) return new (p) T(init(static_cast(p))); } +extern "C" { + nix_err nix_libexpr_init(nix_c_context * context) { if (context) @@ -287,3 +289,5 @@ void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * o GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0); #endif } + +} // extern "C" diff --git a/src/libexpr-c/nix_api_expr_internal.h b/src/libexpr-c/nix_api_expr_internal.h index a26595cec..3aa1d9932 100644 --- a/src/libexpr-c/nix_api_expr_internal.h +++ b/src/libexpr-c/nix_api_expr_internal.h @@ -8,6 +8,8 @@ #include "nix_api_value.h" #include "nix/expr/search-path.hh" +extern "C" { + struct nix_eval_state_builder { nix::ref store; @@ -61,4 +63,6 @@ struct nix_realised_string std::vector storePaths; }; +} // extern "C" + #endif // NIX_API_EXPR_INTERNAL_H diff --git a/src/libexpr-c/nix_api_external.cc b/src/libexpr-c/nix_api_external.cc index ecb67cfb4..ff2950448 100644 --- a/src/libexpr-c/nix_api_external.cc +++ b/src/libexpr-c/nix_api_external.cc @@ -14,6 +14,8 @@ #include +extern "C" { + void nix_set_string_return(nix_string_return * str, const char * c) { str->str = c; @@ -40,6 +42,8 @@ nix_err nix_external_add_string_context(nix_c_context * context, nix_string_cont NIXC_CATCH_ERRS } +} // extern "C" + class NixCExternalValue : public nix::ExternalValueBase { NixCExternalValueDesc & desc; @@ -170,6 +174,8 @@ public: virtual ~NixCExternalValue() override {}; }; +extern "C" { + ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v) { if (context) @@ -198,3 +204,5 @@ void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b } NIXC_CATCH_ERRS_NULL } + +} // extern "C" diff --git a/src/libexpr-c/nix_api_value.cc b/src/libexpr-c/nix_api_value.cc index fb90e2872..0f6595e49 100644 --- a/src/libexpr-c/nix_api_value.cc +++ b/src/libexpr-c/nix_api_value.cc @@ -111,6 +111,8 @@ static void nix_c_primop_wrapper( v = vTmp; } +extern "C" { + PrimOp * nix_alloc_primop( nix_c_context * context, PrimOpFun fun, @@ -651,3 +653,5 @@ const StorePath * nix_realised_string_get_store_path(nix_realised_string * s, si { return &s->storePaths[i]; } + +} // extern "C" diff --git a/src/libfetchers-c/nix_api_fetchers.cc b/src/libfetchers-c/nix_api_fetchers.cc index 4e8037a5e..7fefedb0c 100644 --- a/src/libfetchers-c/nix_api_fetchers.cc +++ b/src/libfetchers-c/nix_api_fetchers.cc @@ -2,6 +2,8 @@ #include "nix_api_fetchers_internal.hh" #include "nix_api_util_internal.h" +extern "C" { + nix_fetchers_settings * nix_fetchers_settings_new(nix_c_context * context) { try { @@ -17,3 +19,5 @@ void nix_fetchers_settings_free(nix_fetchers_settings * settings) { delete settings; } + +} // extern "C" diff --git a/src/libflake-c/nix_api_flake.cc b/src/libflake-c/nix_api_flake.cc index ad8f0bf4e..2de0e667e 100644 --- a/src/libflake-c/nix_api_flake.cc +++ b/src/libflake-c/nix_api_flake.cc @@ -10,6 +10,8 @@ #include "nix/flake/flake.hh" +extern "C" { + nix_flake_settings * nix_flake_settings_new(nix_c_context * context) { nix_clear_err(context); @@ -203,3 +205,5 @@ nix_value * nix_locked_flake_get_output_attrs( } NIXC_CATCH_ERRS_NULL } + +} // extern "C" diff --git a/src/libmain-c/nix_api_main.cc b/src/libmain-c/nix_api_main.cc index eacb80455..2d4f588a8 100644 --- a/src/libmain-c/nix_api_main.cc +++ b/src/libmain-c/nix_api_main.cc @@ -5,6 +5,8 @@ #include "nix/main/plugin.hh" +extern "C" { + nix_err nix_init_plugins(nix_c_context * context) { if (context) @@ -14,3 +16,5 @@ nix_err nix_init_plugins(nix_c_context * context) } NIXC_CATCH_ERRS } + +} // extern "C" diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 4f91f5332..1026c2227 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -10,6 +10,8 @@ #include "nix/store/globals.hh" +extern "C" { + nix_err nix_libstore_init(nix_c_context * context) { if (context) @@ -180,3 +182,5 @@ nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store } NIXC_CATCH_ERRS } + +} // extern "C" diff --git a/src/libstore-c/nix_api_store_internal.h b/src/libstore-c/nix_api_store_internal.h index b0194bfd3..cbe04b2c7 100644 --- a/src/libstore-c/nix_api_store_internal.h +++ b/src/libstore-c/nix_api_store_internal.h @@ -2,6 +2,8 @@ #define NIX_API_STORE_INTERNAL_H #include "nix/store/store-api.hh" +extern "C" { + struct Store { nix::ref ptr; @@ -12,4 +14,6 @@ struct StorePath nix::StorePath path; }; +} // extern "C" + #endif diff --git a/src/libutil-c/nix_api_util.cc b/src/libutil-c/nix_api_util.cc index 2254f18fa..a43e7103b 100644 --- a/src/libutil-c/nix_api_util.cc +++ b/src/libutil-c/nix_api_util.cc @@ -9,6 +9,8 @@ #include "nix_api_util_config.h" +extern "C" { + nix_c_context * nix_c_context_create() { return new nix_c_context(); @@ -156,3 +158,5 @@ nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callb callback(str.c_str(), str.size(), user_data); return NIX_OK; } + +} // extern "C" diff --git a/src/libutil-c/nix_api_util_internal.h b/src/libutil-c/nix_api_util_internal.h index 664cd6e23..92bb9c1d2 100644 --- a/src/libutil-c/nix_api_util_internal.h +++ b/src/libutil-c/nix_api_util_internal.h @@ -7,6 +7,8 @@ #include "nix/util/error.hh" #include "nix_api_util.h" +extern "C" { + struct nix_c_context { nix_err last_err_code = NIX_OK; @@ -47,4 +49,6 @@ nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callb } #define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr) +} // extern "C" + #endif // NIX_API_UTIL_INTERNAL_H