Skip to content

Commit 6c8d873

Browse files
committed
fix(cmake): improve utils dependency resolution and standalone package export
2 parents 8743c5b + 1a37131 commit 6c8d873

1 file changed

Lines changed: 73 additions & 20 deletions

File tree

CMakeLists.txt

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ execute_process(
5656
OUTPUT_STRIP_TRAILING_WHITESPACE
5757
ERROR_QUIET
5858
)
59+
5960
if(NOT VIX_GIT_HASH)
6061
set(VIX_GIT_HASH "unknown")
6162
endif()
@@ -71,14 +72,18 @@ if(VIX_HEADER_ONLY)
7172
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
7273
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
7374
)
75+
7476
target_compile_definitions(vix_utils INTERFACE
7577
VIX_GIT_HASH="${VIX_GIT_HASH}"
7678
VIX_BUILD_DATE="${VIX_BUILD_DATE}"
7779
)
7880
else()
7981
file(GLOB_RECURSE UTILS_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
82+
8083
if(NOT UTILS_SOURCES)
81-
message(FATAL_ERROR "No sources found under src/. If you want header-only, set VIX_HEADER_ONLY=ON")
84+
message(FATAL_ERROR
85+
"No sources found under src/. If you want header-only, set VIX_HEADER_ONLY=ON"
86+
)
8287
endif()
8388

8489
add_library(vix_utils STATIC ${UTILS_SOURCES})
@@ -96,33 +101,67 @@ else()
96101
)
97102
endif()
98103

99-
# -------------------------
104+
# --------------------------------------------------------------------
100105
# Dependencies: spdlog + fmt
106+
# Resolution policy:
107+
# 1) Reuse existing targets if already available
108+
# 2) Try installed packages via find_package(...)
109+
# 3) Fail with a clear message if nothing is found
110+
#
101111
# Prefer header-only targets to avoid runtime shared-library ABI issues
102112
# in released Linux binaries.
103-
# -------------------------
104-
find_package(spdlog CONFIG REQUIRED)
105-
find_package(fmt CONFIG REQUIRED)
106-
113+
# --------------------------------------------------------------------
107114
set(_VIX_SPDLOG_TARGET "")
108115
set(_VIX_FMT_TARGET "")
109116

110-
# Prefer header-only spdlog to avoid libspdlog.so runtime dependency
117+
# Reuse already available spdlog targets first
111118
if (TARGET spdlog::spdlog_header_only)
112119
set(_VIX_SPDLOG_TARGET spdlog::spdlog_header_only)
113120
elseif (TARGET spdlog::spdlog)
114121
set(_VIX_SPDLOG_TARGET spdlog::spdlog)
115122
else()
116-
message(FATAL_ERROR "[utils] spdlog target not found.")
123+
find_package(spdlog CONFIG QUIET)
124+
125+
if (TARGET spdlog::spdlog_header_only)
126+
set(_VIX_SPDLOG_TARGET spdlog::spdlog_header_only)
127+
elseif (TARGET spdlog::spdlog)
128+
set(_VIX_SPDLOG_TARGET spdlog::spdlog)
129+
endif()
117130
endif()
118131

119-
# Prefer header-only fmt to avoid libfmt.so runtime dependency
132+
# Reuse already available fmt targets first
120133
if (TARGET fmt::fmt-header-only)
121134
set(_VIX_FMT_TARGET fmt::fmt-header-only)
122135
elseif (TARGET fmt::fmt)
123136
set(_VIX_FMT_TARGET fmt::fmt)
124137
else()
125-
message(FATAL_ERROR "[utils] fmt target not found.")
138+
find_package(fmt CONFIG QUIET)
139+
140+
if (TARGET fmt::fmt-header-only)
141+
set(_VIX_FMT_TARGET fmt::fmt-header-only)
142+
elseif (TARGET fmt::fmt)
143+
set(_VIX_FMT_TARGET fmt::fmt)
144+
endif()
145+
endif()
146+
147+
if (NOT _VIX_SPDLOG_TARGET)
148+
message(FATAL_ERROR
149+
"[utils] spdlog not found.\n"
150+
"Expected one of:\n"
151+
" - existing target: spdlog::spdlog_header_only or spdlog::spdlog\n"
152+
" - installed package resolvable via find_package(spdlog CONFIG)\n"
153+
"Make sure spdlog is installed and visible through CMAKE_PREFIX_PATH or spdlog_DIR."
154+
)
155+
endif()
156+
157+
if (NOT _VIX_FMT_TARGET)
158+
message(FATAL_ERROR
159+
"[utils] fmt not found.\n"
160+
"Expected one of:\n"
161+
" - existing target: fmt::fmt-header-only or fmt::fmt\n"
162+
" - installed package resolvable via find_package(fmt CONFIG)\n"
163+
"Make sure fmt is installed and visible through CMAKE_PREFIX_PATH or fmt_DIR."
164+
)
126165
endif()
127166

128167
target_link_libraries(vix_utils
@@ -136,9 +175,10 @@ if (VIX_HEADER_ONLY)
136175
else()
137176
target_compile_definitions(vix_utils PUBLIC SPDLOG_FMT_EXTERNAL=1)
138177
endif()
139-
# -------------------------
178+
179+
# --------------------------------------------------------------------
140180
# Windows: prevent Win32 API usage by spdlog
141-
# -------------------------
181+
# --------------------------------------------------------------------
142182
if (WIN32)
143183
if (VIX_HEADER_ONLY)
144184
target_compile_definitions(vix_utils INTERFACE SPDLOG_NO_WIN32_API=1)
@@ -158,7 +198,9 @@ else()
158198
if (VIX_HEADER_ONLY)
159199
if (NOT MSVC)
160200
target_compile_options(vix_utils INTERFACE
161-
-Wall -Wextra -Wpedantic
201+
-Wall
202+
-Wextra
203+
-Wpedantic
162204
-Wno-array-bounds
163205
-Wno-stringop-overflow
164206
)
@@ -170,7 +212,9 @@ else()
170212
else()
171213
if (NOT MSVC)
172214
target_compile_options(vix_utils PRIVATE
173-
-Wall -Wextra -Wpedantic
215+
-Wall
216+
-Wextra
217+
-Wpedantic
174218
-Wno-array-bounds
175219
-Wno-stringop-overflow
176220
)
@@ -194,14 +238,15 @@ endif()
194238
if(VIX_ENABLE_LTO AND NOT VIX_HEADER_ONLY AND CMAKE_BUILD_TYPE STREQUAL "Release")
195239
include(CheckIPOSupported)
196240
check_ipo_supported(RESULT ipo_ok OUTPUT ipo_msg)
241+
197242
if(ipo_ok)
198243
set_property(TARGET vix_utils PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
199244
else()
200245
message(WARNING "IPO/LTO not supported: ${ipo_msg}")
201246
endif()
202247
endif()
203248

204-
# Install / Export (umbrella)
249+
# Install / Export (umbrella + standalone package)
205250
set_target_properties(vix_utils PROPERTIES
206251
EXPORT_NAME utils
207252
OUTPUT_NAME vix_utils
@@ -225,7 +270,8 @@ else()
225270
endif()
226271

227272
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
228-
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
273+
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
274+
)
229275

230276
configure_package_config_file(
231277
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/vix_utilsConfig.cmake.in"
@@ -254,17 +300,21 @@ install(EXPORT VixTargets
254300
# Examples (opt-in)
255301
if (VIX_UTILS_BUILD_EXAMPLES AND NOT VIX_HEADER_ONLY)
256302
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
257-
add_executable(utils_log_demo examples/log_demo.cpp)
303+
304+
add_executable(utils_log_demo examples/log_demo.cpp)
258305
target_link_libraries(utils_log_demo PRIVATE vix::utils)
259306

260-
add_executable(utils_validation_demo examples/validation_demo.cpp)
307+
add_executable(utils_validation_demo examples/validation_demo.cpp)
261308
target_link_libraries(utils_validation_demo PRIVATE vix::utils)
262309

263-
add_executable(utils_env_time_uuid examples/env_time_uuid.cpp)
310+
add_executable(utils_env_time_uuid examples/env_time_uuid.cpp)
264311
target_link_libraries(utils_env_time_uuid PRIVATE vix::utils)
265312

266313
add_custom_target(utils_examples ALL
267-
DEPENDS utils_log_demo utils_validation_demo utils_env_time_uuid
314+
DEPENDS
315+
utils_log_demo
316+
utils_validation_demo
317+
utils_env_time_uuid
268318
)
269319
endif()
270320

@@ -274,3 +324,6 @@ if(DEFINED UTILS_SOURCES AND NOT VIX_HEADER_ONLY)
274324
else()
275325
message(STATUS "Utils header-only mode or no sources listed")
276326
endif()
327+
328+
message(STATUS "Utils spdlog target: ${_VIX_SPDLOG_TARGET}")
329+
message(STATUS "Utils fmt target: ${_VIX_FMT_TARGET}")

0 commit comments

Comments
 (0)