Skip to content

Commit e92a2d7

Browse files
committed
fix(cache): align dependency resolution, package export, and CI with net/json modules
2 parents c0b5f08 + ee635be commit e92a2d7

File tree

3 files changed

+162
-41
lines changed

3 files changed

+162
-41
lines changed

.github/workflows/cache-strict-ci.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ jobs:
6868
sudo apt-get update -y
6969
sudo apt-get install -y $DEPS
7070
71-
- name: Fetch sibling net
71+
- name: Fetch sibling dependencies
7272
run: |
73-
rm -rf ../net
73+
rm -rf ../net ../json
7474
git clone --depth 1 --branch "${VIX_GIT_BRANCH}" https://github.com/vixcpp/net.git ../net
75+
git clone --depth 1 --branch "${VIX_GIT_BRANCH}" https://github.com/vixcpp/json.git ../json
7576
test -f ../net/CMakeLists.txt || (echo "::error::../net/CMakeLists.txt is missing"; exit 1)
77+
test -f ../json/CMakeLists.txt || (echo "::error::../json/CMakeLists.txt is missing"; exit 1)
7678
7779
- name: Select compiler
7880
run: |
@@ -90,8 +92,7 @@ jobs:
9092
-DCMAKE_BUILD_TYPE=Debug \
9193
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
9294
-DVIX_CACHE_BUILD_TESTS=ON \
93-
-DVIX_CACHE_FETCH_NET=${{ matrix.fetch_net }} \
94-
-DVIX_CACHE_FETCH_NLOHMANN=${{ matrix.fetch_json }}
95+
-DVIX_CACHE_FETCH_NET=${{ matrix.fetch_net }}
9596
9697
- name: Build
9798
run: |

CMakeLists.txt

Lines changed: 147 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cmake_minimum_required(VERSION 3.20)
3030
project(vix_cache VERSION 0.1.0 LANGUAGES CXX)
3131

3232
include(GNUInstallDirs)
33+
include(CMakePackageConfigHelpers)
3334

3435
# Global settings
3536
set(CMAKE_CXX_STANDARD 20)
@@ -51,61 +52,146 @@ file(GLOB_RECURSE CACHE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
5152

5253
# --------------------------------------------------------------------
5354
# Net dependency
55+
# Resolution policy:
56+
# 1) Reuse already available targets
57+
# 2) Try local sibling module
58+
# 3) Try installed packages
59+
# 4) Optional FetchContent as last resort
5460
# --------------------------------------------------------------------
5561
option(VIX_CACHE_FETCH_NET "Auto-fetch vix::net if missing" ON)
5662

57-
if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
58-
if (NOT TARGET vix::net)
59-
message(FATAL_ERROR "[cache] Umbrella build: vix::net must be provided by umbrella before cache.")
60-
endif()
63+
set(VIX_NET_TARGET "")
64+
65+
if (TARGET vix::net)
66+
message(STATUS "[cache] Using existing target vix::net")
67+
set(VIX_NET_TARGET vix::net)
68+
69+
elseif (TARGET vix_net)
70+
message(STATUS "[cache] Using existing target vix_net")
71+
set(VIX_NET_TARGET vix_net)
72+
73+
elseif (TARGET net)
74+
message(STATUS "[cache] Using existing target net")
75+
set(VIX_NET_TARGET net)
76+
6177
else()
62-
if (NOT TARGET vix::net)
63-
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../net/CMakeLists.txt")
64-
message(STATUS "[cache] Adding net from umbrella: ../net")
65-
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../net" "net")
66-
elseif (VIX_CACHE_FETCH_NET)
67-
include(FetchContent)
68-
message(STATUS "[cache] Fetching vix::net via FetchContent")
69-
FetchContent_Declare(vix_net
70-
GIT_REPOSITORY https://github.com/vixcpp/net.git
71-
GIT_TAG v0.1.0
72-
)
73-
FetchContent_MakeAvailable(vix_net)
74-
else()
75-
message(FATAL_ERROR
76-
"vix::net not found. Enable VIX_CACHE_FETCH_NET=ON or provide the target before cache.")
78+
get_filename_component(_VIX_MODULES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
79+
set(_VIX_NET_DIR "${_VIX_MODULES_DIR}/net")
80+
81+
if (EXISTS "${_VIX_NET_DIR}/CMakeLists.txt")
82+
message(STATUS "[cache] Adding local sibling net: ${_VIX_NET_DIR}")
83+
add_subdirectory("${_VIX_NET_DIR}" "${CMAKE_BINARY_DIR}/_vix_net")
84+
endif()
85+
86+
if (TARGET vix::net)
87+
set(VIX_NET_TARGET vix::net)
88+
elseif (TARGET vix_net)
89+
set(VIX_NET_TARGET vix_net)
90+
elseif (TARGET net)
91+
set(VIX_NET_TARGET net)
92+
else()
93+
find_package(vix_net CONFIG QUIET)
94+
find_package(net CONFIG QUIET)
95+
96+
if (TARGET vix::net)
97+
set(VIX_NET_TARGET vix::net)
98+
elseif (TARGET vix_net)
99+
set(VIX_NET_TARGET vix_net)
100+
elseif (TARGET net)
101+
set(VIX_NET_TARGET net)
102+
endif()
103+
endif()
104+
105+
if (NOT VIX_NET_TARGET AND VIX_CACHE_FETCH_NET)
106+
include(FetchContent)
107+
message(STATUS "[cache] Fetching vix::net via FetchContent")
108+
FetchContent_Declare(vix_net
109+
GIT_REPOSITORY https://github.com/vixcpp/net.git
110+
GIT_TAG v0.1.0
111+
)
112+
FetchContent_MakeAvailable(vix_net)
113+
114+
if (TARGET vix::net)
115+
set(VIX_NET_TARGET vix::net)
116+
elseif (TARGET vix_net)
117+
set(VIX_NET_TARGET vix_net)
118+
elseif (TARGET net)
119+
set(VIX_NET_TARGET net)
77120
endif()
78121
endif()
79122
endif()
80123

81-
set(VIX_NET_TARGET vix::net)
124+
if (NOT VIX_NET_TARGET)
125+
message(FATAL_ERROR
126+
"[cache] vix::net not found.\n"
127+
"Expected one of:\n"
128+
" - existing target: vix::net / vix_net / net\n"
129+
" - local sibling module in ../net\n"
130+
" - installed package via find_package(vix_net CONFIG) or find_package(net CONFIG)\n"
131+
)
132+
endif()
82133

83134
# --------------------------------------------------------------------
84135
# JSON dependency
85-
# Policy:
86-
# - Umbrella: vix::json must already be provided by umbrella.
87-
# - Standalone: try sibling ../json first.
136+
# Resolution policy:
137+
# 1) Reuse already available targets
138+
# 2) Try local sibling module
139+
# 3) Try installed packages
88140
# Notes:
89-
# - Cache must not export-link nlohmann_json directly.
90-
# - The json module owns the backend provider selection.
141+
# - Cache depends on the Vix json module, not directly on nlohmann_json.
91142
# --------------------------------------------------------------------
92-
if (DEFINED VIX_UMBRELLA_BUILD AND VIX_UMBRELLA_BUILD)
93-
if (NOT TARGET vix::json)
94-
message(FATAL_ERROR "[cache] Umbrella build: vix::json must be provided by umbrella before cache.")
95-
endif()
143+
set(VIX_JSON_TARGET "")
144+
145+
if (TARGET vix::json)
146+
message(STATUS "[cache] Using existing target vix::json")
147+
set(VIX_JSON_TARGET vix::json)
148+
149+
elseif (TARGET vix_json)
150+
message(STATUS "[cache] Using existing target vix_json")
151+
set(VIX_JSON_TARGET vix_json)
152+
153+
elseif (TARGET json)
154+
message(STATUS "[cache] Using existing target json")
155+
set(VIX_JSON_TARGET json)
156+
96157
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.")
158+
get_filename_component(_VIX_MODULES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
159+
set(_VIX_JSON_DIR "${_VIX_MODULES_DIR}/json")
160+
161+
if (EXISTS "${_VIX_JSON_DIR}/CMakeLists.txt")
162+
message(STATUS "[cache] Adding local sibling json: ${_VIX_JSON_DIR}")
163+
add_subdirectory("${_VIX_JSON_DIR}" "${CMAKE_BINARY_DIR}/_vix_json")
164+
endif()
165+
166+
if (TARGET vix::json)
167+
set(VIX_JSON_TARGET vix::json)
168+
elseif (TARGET vix_json)
169+
set(VIX_JSON_TARGET vix_json)
170+
elseif (TARGET json)
171+
set(VIX_JSON_TARGET json)
172+
else()
173+
find_package(vix_json CONFIG QUIET)
174+
find_package(json CONFIG QUIET)
175+
176+
if (TARGET vix::json)
177+
set(VIX_JSON_TARGET vix::json)
178+
elseif (TARGET vix_json)
179+
set(VIX_JSON_TARGET vix_json)
180+
elseif (TARGET json)
181+
set(VIX_JSON_TARGET json)
104182
endif()
105183
endif()
106184
endif()
107185

108-
set(VIX_JSON_TARGET vix::json)
186+
if (NOT VIX_JSON_TARGET)
187+
message(FATAL_ERROR
188+
"[cache] vix::json not found.\n"
189+
"Expected one of:\n"
190+
" - existing target: vix::json / vix_json / json\n"
191+
" - local sibling module in ../json\n"
192+
" - installed package via find_package(vix_json CONFIG) or find_package(json CONFIG)\n"
193+
)
194+
endif()
109195

110196
# --------------------------------------------------------------------
111197
# Library mode
@@ -198,6 +284,30 @@ else()
198284
endif()
199285
endif()
200286

287+
configure_package_config_file(
288+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/vix_cacheConfig.cmake.in"
289+
"${CMAKE_CURRENT_BINARY_DIR}/vix_cacheConfig.cmake"
290+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_cache"
291+
)
292+
293+
write_basic_package_version_file(
294+
"${CMAKE_CURRENT_BINARY_DIR}/vix_cacheConfigVersion.cmake"
295+
VERSION ${PROJECT_VERSION}
296+
COMPATIBILITY SameMajorVersion
297+
)
298+
299+
install(FILES
300+
"${CMAKE_CURRENT_BINARY_DIR}/vix_cacheConfig.cmake"
301+
"${CMAKE_CURRENT_BINARY_DIR}/vix_cacheConfigVersion.cmake"
302+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_cache"
303+
)
304+
305+
install(EXPORT VixTargets
306+
FILE VixTargets.cmake
307+
NAMESPACE vix::
308+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/vix_cache"
309+
)
310+
201311
# --------------------------------------------------------------------
202312
# Tests
203313
# --------------------------------------------------------------------

cmake/vix_cacheConfig.cmake.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@PACKAGE_INIT@
2+
3+
include(CMakeFindDependencyMacro)
4+
5+
find_dependency(vix_net CONFIG REQUIRED)
6+
find_dependency(vix_json CONFIG REQUIRED)
7+
8+
include("${CMAKE_CURRENT_LIST_DIR}/VixTargets.cmake")
9+
10+
check_required_components(vix_cache)

0 commit comments

Comments
 (0)