From e2449937634e58e3ca43dd12626ca1ebc2945c5b Mon Sep 17 00:00:00 2001
From: "Austin S. Hemmelgarn" <austin@netdata.cloud>
Date: Wed, 26 Jun 2024 06:59:11 -0400
Subject: [PATCH] Enforce proper include ordering for vendored libraries.
 (#18008)

* Enforce proper include ordering for vendored libraries.

* Fix support for older CMake versions.
---
 packaging/cmake/Modules/NetdataJSONC.cmake    | 7 ++++++-
 packaging/cmake/Modules/NetdataProtobuf.cmake | 9 +++++++--
 packaging/cmake/Modules/NetdataYAML.cmake     | 7 ++++++-
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/packaging/cmake/Modules/NetdataJSONC.cmake b/packaging/cmake/Modules/NetdataJSONC.cmake
index c9514d1cab..9bbb424e1a 100644
--- a/packaging/cmake/Modules/NetdataJSONC.cmake
+++ b/packaging/cmake/Modules/NetdataJSONC.cmake
@@ -63,6 +63,7 @@ macro(netdata_detect_jsonc)
         endif()
 
         if(NOT JSONC_FOUND)
+                set(ENABLE_BUNDLED_JSONC True PARENT_SCOPE)
                 netdata_bundle_jsonc()
                 set(NETDATA_JSONC_LDFLAGS json-c)
                 set(NETDATA_JSONC_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/include)
@@ -96,7 +97,11 @@ endmacro()
 # The specified target must already exist, and the netdata_detect_json-c
 # macro must have already been run at least once for this to work correctly.
 function(netdata_add_jsonc_to_target _target)
-        target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
+        if(ENABLE_BUNDLED_JSONC)
+                target_include_directories(${_target} BEFORE PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
+        else()
+                target_include_directories(${_target} PUBLIC ${NETDATA_JSONC_INCLUDE_DIRS})
+        endif()
         target_compile_options(${_target} PUBLIC ${NETDATA_JSONC_CFLAGS_OTHER})
         target_link_libraries(${_target} PUBLIC ${NETDATA_JSONC_LDFLAGS})
         add_dependencies(${_target} json-c-compat-link)
diff --git a/packaging/cmake/Modules/NetdataProtobuf.cmake b/packaging/cmake/Modules/NetdataProtobuf.cmake
index e6859e9ddb..c142d65664 100644
--- a/packaging/cmake/Modules/NetdataProtobuf.cmake
+++ b/packaging/cmake/Modules/NetdataProtobuf.cmake
@@ -54,7 +54,7 @@ function(netdata_bundle_protobuf)
         FetchContent_MakeAvailable_NoInstall(protobuf)
         message(STATUS "Finished preparing bundled Protobuf.")
 
-        set(BUNDLED_PROTOBUF True PARENT_SCOPE)
+        set(ENABLE_BUNDLED_PROTOBUF True PARENT_SCOPE)
 endfunction()
 
 # Handle detection of Protobuf
@@ -168,7 +168,12 @@ endfunction()
 
 # Add protobuf to a specified target.
 function(netdata_add_protobuf _target)
+        if(ENABLE_BUNDLED_PROTOBUF)
+            target_include_directories(${_target} BEFORE PRIVATE ${PROTOBUF_INCLUDE_DIRS})
+        else()
+            target_include_directories(${_target} PRIVATE ${PROTOBUF_INCLUDE_DIRS})
+        endif()
+
         target_compile_options(${_target} PRIVATE ${PROTOBUF_CFLAGS_OTHER})
-        target_include_directories(${_target} PRIVATE ${PROTOBUF_INCLUDE_DIRS})
         target_link_libraries(${_target} PRIVATE ${PROTOBUF_LIBRARIES})
 endfunction()
diff --git a/packaging/cmake/Modules/NetdataYAML.cmake b/packaging/cmake/Modules/NetdataYAML.cmake
index 1264cb2deb..f2f9b404ee 100644
--- a/packaging/cmake/Modules/NetdataYAML.cmake
+++ b/packaging/cmake/Modules/NetdataYAML.cmake
@@ -45,6 +45,7 @@ macro(netdata_detect_libyaml)
 
         if(ENABLE_BUNDLED_LIBYAML OR NOT YAML_FOUND)
                 netdata_bundle_libyaml()
+                set(ENABLE_BUNDLED_LIBYAML True PARENT_SCOPE)
                 set(NETDATA_YAML_LDFLAGS yaml)
                 get_target_property(NETDATA_YAML_INCLUDE_DIRS yaml INTERFACE_INCLUDE_DIRECTORIES)
                 get_target_property(NETDATA_YAML_CFLAGS_OTHER yaml INTERFACE_COMPILE_DEFINITIONS)
@@ -60,7 +61,11 @@ endmacro()
 # The specified target must already exist, and the netdata_detect_libyaml
 # macro must have already been run at least once for this to work correctly.
 function(netdata_add_libyaml_to_target _target)
-        target_include_directories(${_target} PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
+        if(ENABLE_BUNDLED_LIBYAML)
+                target_include_directories(${_target} BEFORE PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
+        else()
+                target_include_directories(${_target} PUBLIC ${NETDATA_YAML_INCLUDE_DIRS})
+        endif()
         target_compile_options(${_target} PUBLIC ${NETDATA_YAML_CFLAGS_OTHER})
         target_link_libraries(${_target} PUBLIC ${NETDATA_YAML_LDFLAGS})
 endfunction()