Skip to content

Commit 16cbdca

Browse files
committed
fix(cache): use vix::json instead of nlohmann_json to fix export set
1 parent a7f0302 commit 16cbdca

File tree

1 file changed

+45
-88
lines changed

1 file changed

+45
-88
lines changed

CMakeLists.txt

Lines changed: 45 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# that can be found in the License file.
88
#
99
# ====================================================================
10-
# Vix.cpp Cache Module
10+
# Vix.cpp - Cache Module
1111
# ====================================================================
1212
# Purpose:
1313
# HTTP caching engine for Vix. Provides pluggable cache stores,
@@ -20,6 +20,7 @@
2020
#
2121
# Public Dependencies:
2222
# - vix::net
23+
# - vix::json
2324
#
2425
# Installation/Export:
2526
# Installs into the umbrella export-set `VixTargets`.
@@ -48,11 +49,12 @@ endif()
4849
# Sources discovery
4950
file(GLOB_RECURSE CACHE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
5051

52+
# --------------------------------------------------------------------
5153
# Net dependency
54+
# --------------------------------------------------------------------
5255
option(VIX_CACHE_FETCH_NET "Auto-fetch vix::net if missing" ON)
5356

5457
if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
55-
# Umbrella owns dependency graph: never add_subdirectory/FetchContent here.
5658
if (NOT TARGET vix::net)
5759
message(FATAL_ERROR "[cache] Umbrella build: vix::net must be provided by umbrella before cache.")
5860
endif()
@@ -70,63 +72,44 @@ else()
7072
)
7173
FetchContent_MakeAvailable(vix_net)
7274
else()
73-
message(FATAL_ERROR "vix::net not found. Enable VIX_CACHE_FETCH_NET=ON or provide the target before cache.")
75+
message(FATAL_ERROR
76+
"vix::net not found. Enable VIX_CACHE_FETCH_NET=ON or provide the target before cache.")
7477
endif()
7578
endif()
7679
endif()
7780

7881
set(VIX_NET_TARGET vix::net)
7982

8083
# --------------------------------------------------------------------
81-
# JSON dependency (required by src/FileStore.cpp -> <nlohmann/json.hpp>)
84+
# JSON dependency
8285
# Policy:
83-
# - Umbrella: must provide nlohmann_json::nlohmann_json already (or via system).
84-
# - Standalone: try CONFIG package, optionally FetchContent if allowed.
86+
# - Umbrella: vix::json must already be provided by umbrella.
87+
# - Standalone: try sibling ../json first.
88+
# Notes:
89+
# - Cache must not export-link nlohmann_json directly.
90+
# - The json module owns the backend provider selection.
8591
# --------------------------------------------------------------------
86-
option(VIX_CACHE_FETCH_NLOHMANN "Fetch nlohmann/json if missing (standalone only)" ON)
87-
8892
if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
89-
set(VIX_CACHE_FETCH_NLOHMANN OFF CACHE BOOL "Fetch nlohmann/json if missing (standalone only)" FORCE)
90-
endif()
91-
92-
set(VIX_NLOHMANN_TARGET "")
93-
set(_VIX_CACHE_INSTALL_FALLBACK OFF)
94-
95-
# First try CONFIG package
96-
find_package(nlohmann_json CONFIG QUIET)
97-
if (TARGET nlohmann_json::nlohmann_json)
98-
set(VIX_NLOHMANN_TARGET nlohmann_json::nlohmann_json)
99-
endif()
100-
101-
# Standalone fallback: FetchContent
102-
if (NOT VIX_NLOHMANN_TARGET)
103-
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD) AND VIX_CACHE_FETCH_NLOHMANN)
104-
include(FetchContent)
105-
message(STATUS "[cache] Fetching nlohmann/json via FetchContent")
106-
FetchContent_Declare(nlohmann_json
107-
GIT_REPOSITORY https://github.com/nlohmann/json.git
108-
GIT_TAG v3.11.3
109-
GIT_SHALLOW TRUE
110-
)
111-
FetchContent_MakeAvailable(nlohmann_json)
112-
113-
# Fallback is header-only: do not export-link it
114-
set(_VIX_CACHE_INSTALL_FALLBACK ON)
93+
if (NOT TARGET vix::json)
94+
message(FATAL_ERROR "[cache] Umbrella build: vix::json must be provided by umbrella before cache.")
95+
endif()
96+
else()
97+
if (NOT TARGET vix::json)
98+
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../json/CMakeLists.txt")
99+
message(STATUS "[cache] Adding json from umbrella: ../json")
100+
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../json" "json")
101+
else()
102+
message(FATAL_ERROR
103+
"vix::json not found. Provide the target before cache or make sure ../json is available.")
104+
endif()
115105
endif()
116106
endif()
117107

118-
if (NOT VIX_NLOHMANN_TARGET AND NOT _VIX_CACHE_INSTALL_FALLBACK)
119-
message(FATAL_ERROR
120-
"[cache] Missing dependency: nlohmann_json.\n"
121-
"Reason: src/FileStore.cpp includes <nlohmann/json.hpp>.\n"
122-
"Fix:\n"
123-
" - Umbrella: ensure nlohmann_json is available before adding cache.\n"
124-
" - Standalone: install nlohmann_json or set VIX_CACHE_FETCH_NLOHMANN=ON.\n"
125-
)
126-
endif()
127-
108+
set(VIX_JSON_TARGET vix::json)
128109

129-
# STATIC
110+
# --------------------------------------------------------------------
111+
# Library mode
112+
# --------------------------------------------------------------------
130113
if (CACHE_SOURCES)
131114
message(STATUS "[cache] Building STATIC library with detected sources.")
132115

@@ -141,22 +124,13 @@ if (CACHE_SOURCES)
141124
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
142125
)
143126

144-
if (_VIX_CACHE_INSTALL_FALLBACK)
145-
target_include_directories(vix_cache
146-
PUBLIC
147-
$<BUILD_INTERFACE:${nlohmann_json_SOURCE_DIR}/single_include>
148-
)
149-
endif()
150-
151127
target_link_libraries(vix_cache
152128
PUBLIC
153129
${VIX_NET_TARGET}
130+
PRIVATE
131+
${VIX_JSON_TARGET}
154132
)
155133

156-
if (VIX_NLOHMANN_TARGET)
157-
target_link_libraries(vix_cache PUBLIC ${VIX_NLOHMANN_TARGET})
158-
endif()
159-
160134
if (TARGET vix_warnings)
161135
target_link_libraries(vix_cache PUBLIC vix_warnings)
162136
endif()
@@ -172,14 +146,6 @@ if (CACHE_SOURCES)
172146
EXPORT_NAME cache
173147
)
174148

175-
if (_VIX_CACHE_INSTALL_FALLBACK)
176-
install(
177-
DIRECTORY "${nlohmann_json_SOURCE_DIR}/single_include/nlohmann"
178-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
179-
FILES_MATCHING PATTERN "*.hpp"
180-
)
181-
endif()
182-
183149
install(TARGETS vix_cache
184150
EXPORT VixTargets
185151
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -189,11 +155,13 @@ if (CACHE_SOURCES)
189155
)
190156

191157
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
192-
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
193-
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
158+
install(
159+
DIRECTORY include/
160+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
161+
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
162+
)
194163
endif()
195164

196-
# HEADER-ONLY
197165
else()
198166
message(STATUS "[cache] Building HEADER-ONLY library (no sources).")
199167

@@ -207,20 +175,11 @@ else()
207175
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
208176
)
209177

210-
if (_VIX_CACHE_INSTALL_FALLBACK)
211-
target_include_directories(vix_cache INTERFACE
212-
$<BUILD_INTERFACE:${nlohmann_json_SOURCE_DIR}/single_include>
213-
)
214-
endif()
215-
216178
target_link_libraries(vix_cache INTERFACE
217179
${VIX_NET_TARGET}
180+
${VIX_JSON_TARGET}
218181
)
219182

220-
if (VIX_NLOHMANN_TARGET)
221-
target_link_libraries(vix_cache INTERFACE ${VIX_NLOHMANN_TARGET})
222-
endif()
223-
224183
if (VIX_ENABLE_SANITIZERS AND TARGET vix_sanitizers)
225184
target_link_libraries(vix_cache INTERFACE vix_sanitizers)
226185
endif()
@@ -230,22 +189,18 @@ else()
230189
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
231190
)
232191

233-
if (_VIX_CACHE_INSTALL_FALLBACK)
192+
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
234193
install(
235-
DIRECTORY "${nlohmann_json_SOURCE_DIR}/single_include/nlohmann"
236-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
237-
FILES_MATCHING PATTERN "*.hpp"
194+
DIRECTORY include/
195+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
196+
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
238197
)
239198
endif()
240-
241-
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
242-
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
243-
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
244-
endif()
245-
246199
endif()
247200

201+
# --------------------------------------------------------------------
248202
# Tests
203+
# --------------------------------------------------------------------
249204
option(VIX_CACHE_BUILD_TESTS "Build cache module tests" OFF)
250205

251206
if (VIX_CACHE_BUILD_TESTS)
@@ -254,15 +209,17 @@ if (VIX_CACHE_BUILD_TESTS)
254209
add_subdirectory(tests)
255210
endif()
256211

212+
# --------------------------------------------------------------------
257213
# Summary
214+
# --------------------------------------------------------------------
258215
message(STATUS "------------------------------------------------------")
259216
message(STATUS "vix::cache configured (${PROJECT_VERSION})")
260217
if (CACHE_SOURCES)
261218
message(STATUS "Mode: STATIC / sources found")
262219
else()
263220
message(STATUS "Mode: HEADER-ONLY / no sources")
264221
endif()
265-
message(STATUS "JSON target: ${VIX_NLOHMANN_TARGET}")
222+
message(STATUS "JSON target: ${VIX_JSON_TARGET}")
266223
message(STATUS "Net target: ${VIX_NET_TARGET}")
267224
message(STATUS "Include dir: ${CMAKE_CURRENT_SOURCE_DIR}/include (for <vix/cache/...>)")
268225
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

0 commit comments

Comments
 (0)