From 25f8f5e7f6b87ff9e44e5180c14f9409bc8af1ec Mon Sep 17 00:00:00 2001 From: Michael Conrad Date: Mon, 4 Dec 2023 13:25:04 -0500 Subject: [PATCH] flb_lib: allow public-API use without dependencies via fluent-bit-minimal.h Currently, fluent-bit.h and flb_lib.h are un-usable by third-parties becuase the fluent-bit build process does not install all of the required header files for fluent-bit's dependencies into the Debian packages. Skipping that problem for the moment, I'm proposing that users of the public API don't actually need or want all the headers required for the interenals of fluent-bit, and there should just be a simple header that declares the public API. This new header sets a preprocessor variable and then includes the normal flb_lib.h, which defines the public API. flb_lib.h sees the preprocessor variable and skips loading any of the dependency headers, instead just declaring the referenced types without defining them. This is an improvement over [an earlier version I proposed][1] where fluent-bit-minimal.h re-defined all the exported symbols of the public API. This new mechanism avoids code duplication and the need to maintain two headers in tandem. [1]: https://github.com/fluent/fluent-bit/issues/7165 Signed-off-by: Michael Conrad --- examples/hello_world/hello_world.c | 2 +- examples/out_lib/out_lib.c | 2 +- include/CMakeLists.txt | 2 +- include/fluent-bit-minimal.h | 33 ++++++++++++++++++++++++++++++ include/fluent-bit/flb_lib.h | 28 +++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 include/fluent-bit-minimal.h diff --git a/examples/hello_world/hello_world.c b/examples/hello_world/hello_world.c index f4925bf674e..71edb8af2b6 100644 --- a/examples/hello_world/hello_world.c +++ b/examples/hello_world/hello_world.c @@ -17,7 +17,7 @@ * limitations under the License. */ -#include +#include int main() { diff --git a/examples/out_lib/out_lib.c b/examples/out_lib/out_lib.c index ada207b0c4f..b651b945a89 100644 --- a/examples/out_lib/out_lib.c +++ b/examples/out_lib/out_lib.c @@ -17,7 +17,7 @@ * limitations under the License. */ -#include +#include #include int my_stdout_json(void *record, size_t size, void *data) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 38f1b0dccd5..075198a724c 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,5 +1,5 @@ # Install fluent-bit headers -install(FILES "fluent-bit.h" +install(FILES "fluent-bit.h" "fluent-bit-minimal.h" DESTINATION ${FLB_INSTALL_INCLUDEDIR} COMPONENT headers PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) diff --git a/include/fluent-bit-minimal.h b/include/fluent-bit-minimal.h new file mode 100644 index 00000000000..463f6c2038a --- /dev/null +++ b/include/fluent-bit-minimal.h @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-2022 The Fluent Bit Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLUENT_BIT_MINIMAL_H +#define FLUENT_BIT_MINIMAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/fluent-bit/flb_lib.h b/include/fluent-bit/flb_lib.h index b5383dace3f..42c4f5f08d8 100644 --- a/include/fluent-bit/flb_lib.h +++ b/include/fluent-bit/flb_lib.h @@ -20,8 +20,31 @@ #ifndef FLB_LIB_H #define FLB_LIB_H -#include -#include +/* Avoid dependencies for library users who just want the public API */ +#ifdef FLUENT_BIT_MINIMAL_H + #include + + struct mk_event_loop; + struct mk_event; + struct flb_config; + + /* Copied from mk_macros.h */ + #ifdef __GNUC__ + #if __GNUC__ >= 4 + #define FLB_EXPORT __attribute__ ((visibility ("default"))) + #else + #define FLB_EXPORT + #endif + #elif defined(_WIN32) + #define FLB_EXPORT __declspec(dllexport) + /* mk_macros doesn't have an 'else'... seems like an oversight */ + #else + #define FLB_EXPORT extern + #endif +#else + #include + #include +#endif /* Lib engine status */ #define FLB_LIB_ERROR -1 @@ -35,6 +58,7 @@ struct flb_lib_ctx { struct mk_event_loop *event_loop; struct mk_event *event_channel; struct flb_config *config; + struct flb_cf; }; /* Used on out_lib to define a callback and further opaque data */