From ab1eb0e2c7873e2cc09843f72c7cfe1e77d75041 Mon Sep 17 00:00:00 2001 From: Michael Conrad Date: Mon, 4 Dec 2023 13:25:04 -0500 Subject: [PATCH 1/2] 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 --- include/CMakeLists.txt | 2 +- include/fluent-bit-minimal.h | 33 +++++++++++++++++++++++++++++++++ include/fluent-bit/flb_lib.h | 28 ++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 include/fluent-bit-minimal.h 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..38d50b38dfa 100644 --- a/include/fluent-bit/flb_lib.h +++ b/include/fluent-bit/flb_lib.h @@ -20,8 +20,32 @@ #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; + struct flb_cf; + + /* 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 From 5d0e4a0d2d44965fb963cbde6d36c12a4c1227a5 Mon Sep 17 00:00:00 2001 From: Michael Conrad Date: Mon, 4 Dec 2023 14:45:19 -0500 Subject: [PATCH 2/2] flb_lib: update examples and at least one test to use fluent-bit-minimal.h To verify that fluent-bit-minimal.h works, I updated one of the existing unit tests to use it. Many more unit tests could be switched, or a new unit test entirely could be added, but this seemed like the simplest solution for testing. I also updated the examples to use the new header, since this is what third party users will be looking at first. Signed-off-by: Michael Conrad --- examples/hello_world/hello_world.c | 4 +++- examples/out_lib/out_lib.c | 4 +++- tests/runtime/in_event_test.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/hello_world/hello_world.c b/examples/hello_world/hello_world.c index f4925bf674e..9b49d96e06e 100644 --- a/examples/hello_world/hello_world.c +++ b/examples/hello_world/hello_world.c @@ -17,7 +17,9 @@ * limitations under the License. */ -#include +#include +#include +#include int main() { diff --git a/examples/out_lib/out_lib.c b/examples/out_lib/out_lib.c index ada207b0c4f..fab39e18cde 100644 --- a/examples/out_lib/out_lib.c +++ b/examples/out_lib/out_lib.c @@ -17,7 +17,9 @@ * limitations under the License. */ -#include +#include +#include +#include #include int my_stdout_json(void *record, size_t size, void *data) diff --git a/tests/runtime/in_event_test.c b/tests/runtime/in_event_test.c index 1169d8fd821..773723c8c02 100644 --- a/tests/runtime/in_event_test.c +++ b/tests/runtime/in_event_test.c @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -#include +#include #include "flb_tests_runtime.h" void flb_test_input_event()