Skip to content

Commit a7f0302

Browse files
committed
fix(cache): make nlohmann_json export-safe and avoid linking fallback target
1 parent 69d48af commit a7f0302

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

CMakeLists.txt

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
9090
endif()
9191

9292
set(VIX_NLOHMANN_TARGET "")
93+
set(_VIX_CACHE_INSTALL_FALLBACK OFF)
9394

9495
# First try CONFIG package
9596
find_package(nlohmann_json CONFIG QUIET)
@@ -105,21 +106,21 @@ if (NOT VIX_NLOHMANN_TARGET)
105106
FetchContent_Declare(nlohmann_json
106107
GIT_REPOSITORY https://github.com/nlohmann/json.git
107108
GIT_TAG v3.11.3
109+
GIT_SHALLOW TRUE
108110
)
109111
FetchContent_MakeAvailable(nlohmann_json)
110112

111-
if (TARGET nlohmann_json::nlohmann_json)
112-
set(VIX_NLOHMANN_TARGET nlohmann_json::nlohmann_json)
113-
endif()
113+
# Fallback is header-only: do not export-link it
114+
set(_VIX_CACHE_INSTALL_FALLBACK ON)
114115
endif()
115116
endif()
116117

117-
if (NOT VIX_NLOHMANN_TARGET)
118+
if (NOT VIX_NLOHMANN_TARGET AND NOT _VIX_CACHE_INSTALL_FALLBACK)
118119
message(FATAL_ERROR
119-
"[cache] Missing dependency: nlohmann_json::nlohmann_json.\n"
120+
"[cache] Missing dependency: nlohmann_json.\n"
120121
"Reason: src/FileStore.cpp includes <nlohmann/json.hpp>.\n"
121122
"Fix:\n"
122-
" - Umbrella: ensure nlohmann_json is available before adding cache (system package or umbrella fallback).\n"
123+
" - Umbrella: ensure nlohmann_json is available before adding cache.\n"
123124
" - Standalone: install nlohmann_json or set VIX_CACHE_FETCH_NLOHMANN=ON.\n"
124125
)
125126
endif()
@@ -140,12 +141,21 @@ if (CACHE_SOURCES)
140141
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
141142
)
142143

143-
target_link_libraries(vix_cache
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+
151+
target_link_libraries(vix_cache
144152
PUBLIC
145153
${VIX_NET_TARGET}
146-
${VIX_NLOHMANN_TARGET}
147154
)
148155

156+
if (VIX_NLOHMANN_TARGET)
157+
target_link_libraries(vix_cache PUBLIC ${VIX_NLOHMANN_TARGET})
158+
endif()
149159

150160
if (TARGET vix_warnings)
151161
target_link_libraries(vix_cache PUBLIC vix_warnings)
@@ -162,6 +172,14 @@ if (CACHE_SOURCES)
162172
EXPORT_NAME cache
163173
)
164174

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+
165183
install(TARGETS vix_cache
166184
EXPORT VixTargets
167185
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -175,7 +193,6 @@ if (CACHE_SOURCES)
175193
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
176194
endif()
177195

178-
179196
# HEADER-ONLY
180197
else()
181198
message(STATUS "[cache] Building HEADER-ONLY library (no sources).")
@@ -190,11 +207,20 @@ else()
190207
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
191208
)
192209

193-
target_link_libraries(vix_cache INTERFACE
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+
216+
target_link_libraries(vix_cache INTERFACE
194217
${VIX_NET_TARGET}
195-
${VIX_NLOHMANN_TARGET}
196218
)
197219

220+
if (VIX_NLOHMANN_TARGET)
221+
target_link_libraries(vix_cache INTERFACE ${VIX_NLOHMANN_TARGET})
222+
endif()
223+
198224
if (VIX_ENABLE_SANITIZERS AND TARGET vix_sanitizers)
199225
target_link_libraries(vix_cache INTERFACE vix_sanitizers)
200226
endif()
@@ -204,6 +230,14 @@ else()
204230
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
205231
)
206232

233+
if (_VIX_CACHE_INSTALL_FALLBACK)
234+
install(
235+
DIRECTORY "${nlohmann_json_SOURCE_DIR}/single_include/nlohmann"
236+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
237+
FILES_MATCHING PATTERN "*.hpp"
238+
)
239+
endif()
240+
207241
if (NOT (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD))
208242
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
209243
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")

0 commit comments

Comments
 (0)