From 59904b13b2d9869b2e2a5fb1427fa09fccfb2b62 Mon Sep 17 00:00:00 2001
From: Thomas Devoogdt <thomas@devoogdt.com>
Date: Sat, 9 Nov 2024 19:52:39 +0100
Subject: [PATCH 1/4] build: use the system provided msgpack if found

Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
---
 CMakeLists.txt         | 16 +++++++++++-----
 cmake/headers.cmake    |  1 -
 cmake/msgpack.cmake    | 10 ++++++++++
 plugins/CMakeLists.txt |  2 +-
 src/CMakeLists.txt     |  2 +-
 5 files changed, 23 insertions(+), 8 deletions(-)
 create mode 100644 cmake/msgpack.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 41904bffd0f..0a14cd7b98e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -215,6 +215,7 @@ option(FLB_PREFER_SYSTEM_LIB_CARES        "Prefer the libcares system library"
 option(FLB_PREFER_SYSTEM_LIB_JEMALLOC     "Prefer the libjemalloc system library"     ${FLB_PREFER_SYSTEM_LIBS})
 option(FLB_PREFER_SYSTEM_LIB_KAFKA        "Prefer the libkafka system library"        ${FLB_PREFER_SYSTEM_LIBS})
 option(FLB_PREFER_SYSTEM_LIB_LUAJIT       "Prefer the libluajit system library"       ${FLB_PREFER_SYSTEM_LIBS})
+option(FLB_PREFER_SYSTEM_LIB_MSGPACK      "Prefer the libmsgpack system library"      ${FLB_PREFER_SYSTEM_LIBS})
 option(FLB_PREFER_SYSTEM_LIB_NGHTTP2      "Prefer the libnghttp2 system library"      ${FLB_PREFER_SYSTEM_LIBS})
 
 # Enable all features
@@ -472,11 +473,16 @@ FLB_OPTION(FLUENT_PROTO_PROFILES ON)
 add_subdirectory(${FLB_PATH_LIB_FLUENT_OTEL} EXCLUDE_FROM_ALL)
 
 # MsgPack options
-option(MSGPACK_ENABLE_CXX             OFF)
-option(MSGPACK_ENABLE_SHARED          OFF)
-option(MSGPACK_BUILD_TESTS            OFF)
-option(MSGPACK_BUILD_EXAMPLES         OFF)
-add_subdirectory(${FLB_PATH_LIB_MSGPACK} EXCLUDE_FROM_ALL)
+if(FLB_PREFER_SYSTEM_LIB_MSGPACK)
+  find_package(PkgConfig)
+  pkg_check_modules(MSGPACK msgpack>=4.0.0)
+endif()
+if(MSGPACK_FOUND)
+  include_directories(${MSGPACK_INCLUDE_DIRS})
+  link_directories(${MSGPACK_LIBRARY_DIRS})
+else()
+  include(cmake/msgpack.cmake)
+endif()
 
 # MPack
 add_definitions(-DMPACK_EXTENSIONS=1)
diff --git a/cmake/headers.cmake b/cmake/headers.cmake
index 9da112cd30b..93a92db2f6e 100755
--- a/cmake/headers.cmake
+++ b/cmake/headers.cmake
@@ -16,7 +16,6 @@ include_directories(
 
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_CO}
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_RBTREE}
-  ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MSGPACK}/include
 
   # Chunk I/O generate headers also in the binary path
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_CHUNKIO}/include
diff --git a/cmake/msgpack.cmake b/cmake/msgpack.cmake
new file mode 100644
index 00000000000..6211e31f302
--- /dev/null
+++ b/cmake/msgpack.cmake
@@ -0,0 +1,10 @@
+# msgpack cmake
+option(MSGPACK_ENABLE_CXX             OFF)
+option(MSGPACK_ENABLE_SHARED          OFF)
+option(MSGPACK_BUILD_TESTS            OFF)
+option(MSGPACK_BUILD_EXAMPLES         OFF)
+include_directories(
+  ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MSGPACK}/include
+)
+add_subdirectory(${FLB_PATH_LIB_MSGPACK} EXCLUDE_FROM_ALL)
+set(MSGPACK_LIBRARIES "msgpack-c-static")
diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt
index deb24958038..a700b0f5c43 100644
--- a/plugins/CMakeLists.txt
+++ b/plugins/CMakeLists.txt
@@ -182,7 +182,7 @@ endmacro()
 macro(FLB_PLUGIN name src deps)
   add_library(flb-plugin-${name} STATIC ${src})
   add_sanitizers(flb-plugin-${name})
-  target_link_libraries(flb-plugin-${name} fluent-bit-static msgpack-c-static ${deps})
+  target_link_libraries(flb-plugin-${name} fluent-bit-static ${MSGPACK_LIBRARIES} ${deps})
 endmacro()
 
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8a790166680..d9e2dba9333 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -383,7 +383,7 @@ set(FLB_DEPS
   ctraces-static
   mk_core
   jsmn
-  msgpack-c-static
+  ${MSGPACK_LIBRARIES}
   mpack-static
   chunkio-static
   miniz

From 2c27a438dc92228086449bfc8afa737d69dd6fd0 Mon Sep 17 00:00:00 2001
From: Thomas Devoogdt <thomas@devoogdt.com>
Date: Sun, 10 Nov 2024 12:44:47 +0100
Subject: [PATCH 2/4] build: use the system provided sqlite if found

Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
---
 CMakeLists.txt      | 12 +++++++++++-
 cmake/headers.cmake |  1 -
 cmake/sqlite.cmake  |  6 ++++++
 src/CMakeLists.txt  |  2 +-
 4 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 cmake/sqlite.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0a14cd7b98e..48a07df7f14 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -217,6 +217,7 @@ option(FLB_PREFER_SYSTEM_LIB_KAFKA        "Prefer the libkafka system library"
 option(FLB_PREFER_SYSTEM_LIB_LUAJIT       "Prefer the libluajit system library"       ${FLB_PREFER_SYSTEM_LIBS})
 option(FLB_PREFER_SYSTEM_LIB_MSGPACK      "Prefer the libmsgpack system library"      ${FLB_PREFER_SYSTEM_LIBS})
 option(FLB_PREFER_SYSTEM_LIB_NGHTTP2      "Prefer the libnghttp2 system library"      ${FLB_PREFER_SYSTEM_LIBS})
+option(FLB_PREFER_SYSTEM_LIB_SQLITE       "Prefer the libsqlite3 system library"      ${FLB_PREFER_SYSTEM_LIBS})
 
 # Enable all features
 if(FLB_ALL)
@@ -689,8 +690,17 @@ if (FLB_SIGNV4)
 endif()
 
 if(FLB_SQLDB)
+  if(FLB_PREFER_SYSTEM_LIB_SQLITE)
+    find_package(PkgConfig)
+    pkg_check_modules(SQLITE sqlite3>=3.0.0)
+  endif()
+  if(SQLITE_FOUND)
+    include_directories(${SQLITE_INCLUDE_DIRS})
+    link_directories(${SQLITE_LIBRARY_DIRS})
+  else()
+    include(cmake/sqlite.cmake)
+  endif()
   FLB_DEFINITION(FLB_HAVE_SQLDB)
-  add_subdirectory(${FLB_PATH_LIB_SQLITE})
 endif()
 
 if(FLB_TRACE)
diff --git a/cmake/headers.cmake b/cmake/headers.cmake
index 93a92db2f6e..bd74463a5b7 100755
--- a/cmake/headers.cmake
+++ b/cmake/headers.cmake
@@ -24,7 +24,6 @@ include_directories(
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MONKEY}/include
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MONKEY}/include/monkey
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MBEDTLS}/include
-  ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_SQLITE}
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MPACK}/src
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_MINIZ}/
   ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_ONIGMO}
diff --git a/cmake/sqlite.cmake b/cmake/sqlite.cmake
new file mode 100644
index 00000000000..76585168d98
--- /dev/null
+++ b/cmake/sqlite.cmake
@@ -0,0 +1,6 @@
+# sqlite cmake
+include_directories(
+  ${FLB_PATH_ROOT_SOURCE}/${FLB_PATH_LIB_SQLITE}
+)
+add_subdirectory(${FLB_PATH_LIB_SQLITE})
+set(SQLITE_LIBRARIES "sqlite3")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d9e2dba9333..6643fc8c82f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -244,7 +244,7 @@ if(FLB_SQLDB)
     )
   set(extra_libs
     ${extra_libs}
-    "sqlite3")
+    ${SQLITE_LIBRARIES})
 endif()
 
 if(FLB_STATIC_CONF)

From 77d67ab9d59e8d76f4cc6a29db4df4cf9957332b Mon Sep 17 00:00:00 2001
From: Thomas Devoogdt <thomas@devoogdt.com>
Date: Sun, 10 Nov 2024 12:46:39 +0100
Subject: [PATCH 3/4] workflows: pr-compile-check.yaml: add libsqlite3-dev
 system library test

Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
---
 .github/workflows/pr-compile-check.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/pr-compile-check.yaml b/.github/workflows/pr-compile-check.yaml
index 42381a8a1d1..13e73505535 100644
--- a/.github/workflows/pr-compile-check.yaml
+++ b/.github/workflows/pr-compile-check.yaml
@@ -48,7 +48,7 @@ jobs:
         run: |
           sudo apt-get update
           sudo apt-get install -y curl gcc-7 g++-7 clang-6.0 libsystemd-dev gcovr libyaml-dev libluajit-5.1-dev \
-            libnghttp2-dev libjemalloc-dev
+            libnghttp2-dev libjemalloc-dev libsqlite3-dev
           sudo ln -s /usr/bin/llvm-symbolizer-6.0 /usr/bin/llvm-symbolizer || true
           mkdir -p /tmp/libbacktrace/build && \
             curl -L https://github.com/ianlancetaylor/libbacktrace/archive/8602fda.tar.gz | \

From a7a4518bffc72d044dc2896653411362537ee7fd Mon Sep 17 00:00:00 2001
From: Thomas Devoogdt <thomas@devoogdt.com>
Date: Fri, 10 Jan 2025 15:17:04 +0100
Subject: [PATCH 4/4] workflows: pr-compile-check.yaml: improve the system
 libraries test

- added a install system libraries step

To make it clear that this is part of the test.

- assert the linked system libs

Check if the system lib is effectively linked as expected.

Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
---
 .github/workflows/pr-compile-check.yaml | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/pr-compile-check.yaml b/.github/workflows/pr-compile-check.yaml
index 13e73505535..f1e95a4e359 100644
--- a/.github/workflows/pr-compile-check.yaml
+++ b/.github/workflows/pr-compile-check.yaml
@@ -47,9 +47,13 @@ jobs:
       - name: Setup environment
         run: |
           sudo apt-get update
-          sudo apt-get install -y curl gcc-7 g++-7 clang-6.0 libsystemd-dev gcovr libyaml-dev libluajit-5.1-dev \
-            libnghttp2-dev libjemalloc-dev libsqlite3-dev
+          sudo apt-get install -y curl gcc-7 g++-7 clang-6.0 libsystemd-dev gcovr libyaml-dev
           sudo ln -s /usr/bin/llvm-symbolizer-6.0 /usr/bin/llvm-symbolizer || true
+
+      - name: Install system libraries for this test
+        run: |
+          sudo apt-get update
+          sudo apt-get install -y libjemalloc-dev libluajit-5.1-dev libnghttp2-dev libsqlite3-dev
           mkdir -p /tmp/libbacktrace/build && \
             curl -L https://github.com/ianlancetaylor/libbacktrace/archive/8602fda.tar.gz | \
             tar --strip-components=1 -xzC /tmp/libbacktrace/ && \
@@ -76,5 +80,11 @@ jobs:
 
       - name: Display dependencies w/ ldd
         run: |
-          ldd ./bin/fluent-bit
+          export ldd_result=$(ldd ./bin/fluent-bit)
+          echo "ldd result:"
+          echo "$ldd_result"
+          echo "$ldd_result" | grep libjemalloc
+          echo "$ldd_result" | grep libluajit
+          echo "$ldd_result" | grep libnghttp2
+          echo "$ldd_result" | grep libsqlite3
         working-directory: build