Ver código fonte

Add nlohmann_json FetchContent fallback for cross-distro compatibility

Use 3-tier discovery: find_package -> pkg_check_modules -> FetchContent
download of v3.11.3 header-only version when not installed on the system.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Void User 5 meses atrás
pai
commit
817bd32c27
1 arquivos alterados com 24 adições e 2 exclusões
  1. 24 2
      CMakeLists.txt

+ 24 - 2
CMakeLists.txt

@@ -35,8 +35,30 @@ if(NOT gRPC_FOUND)
 endif()
 find_package(nlohmann_json 3.2.0 QUIET)
 if(NOT nlohmann_json_FOUND)
-    find_package(PkgConfig REQUIRED)
-    pkg_check_modules(NLOHMANN_JSON REQUIRED nlohmann_json)
+    find_package(PkgConfig QUIET)
+    if(PKG_CONFIG_FOUND)
+        pkg_check_modules(NLOHMANN_JSON QUIET nlohmann_json)
+    endif()
+    if(NOT nlohmann_json_FOUND AND NOT NLOHMANN_JSON_FOUND)
+        # Not available on system - parent project or FetchContent provides it
+        if(NOT TARGET nlohmann_json::nlohmann_json)
+            include(FetchContent)
+            FetchContent_Declare(
+                nlohmann_json
+                GIT_REPOSITORY https://github.com/nlohmann/json.git
+                GIT_TAG        v3.11.3
+                GIT_SHALLOW    TRUE
+            )
+            set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
+            set(JSON_Install OFF CACHE BOOL "" FORCE)
+            FetchContent_MakeAvailable(nlohmann_json)
+        endif()
+    elseif(NLOHMANN_JSON_FOUND AND NOT TARGET nlohmann_json::nlohmann_json)
+        add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED)
+        set_target_properties(nlohmann_json::nlohmann_json PROPERTIES
+            INTERFACE_INCLUDE_DIRECTORIES "${NLOHMANN_JSON_INCLUDE_DIRS}"
+        )
+    endif()
 endif()
 
 # Find spdlog