Skip to content

Commit 26af3d5

Browse files
committed
Improve generation of CMake config files
* Update CMake config COMPATIBILITY to match SOVERSION * Use kebab-case instead of camelCase for CMake config file naming. This matches the project naming better. * Generate find_dependency() commands based on library dependencies at build time. This intentionally ignores the command line tools' dependencies. * Let CMake config file require CXX because isocodes_lookup, mcc_lookup, and (when using Boost) iso8859 require it.
1 parent d041c99 commit 26af3d5

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

CMakeLists.txt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
##############################################################################
2-
# Copyright (c) 2021, 2022, 2023 Leon Lynch
2+
# Copyright (c) 2021-2024 Leon Lynch
33
#
44
# This file is licensed under the terms of the LGPL v2.1 license.
55
# See LICENSE file.
@@ -96,30 +96,35 @@ install(FILES
9696

9797
# Generate and install basic CMake config files
9898
include(CMakePackageConfigHelpers) # Provides CMake config generator macros
99+
# NOTE: src subdirectory provides EMV_UTILS_PACKAGE_DEPENDENCIES
100+
foreach(pkg IN LISTS EMV_UTILS_PACKAGE_DEPENDENCIES)
101+
# Build dependency string for use in CMake config file
102+
string(APPEND EMV_UTILS_CONFIG_PACKAGE_DEPENDENCIES "find_dependency(${pkg})\n")
103+
endforeach()
99104
set(EMV_UTILS_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} CACHE STRING "Installation location for emv-utils CMake config files")
100105
message(STATUS "Using CMake config install location \"${EMV_UTILS_INSTALL_CMAKEDIR}\"")
101-
configure_package_config_file(cmake/emvUtilsConfig.cmake.in
102-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfig.cmake"
106+
configure_package_config_file(cmake/emv-utils-config.cmake.in
107+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config.cmake"
103108
INSTALL_DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
104109
)
105110
write_basic_package_version_file(
106-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfigVersion.cmake"
107-
COMPATIBILITY AnyNewerVersion
111+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-version.cmake"
112+
COMPATIBILITY SameMinorVersion
108113
)
109114
install(FILES
110-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfig.cmake"
111-
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfigVersion.cmake"
115+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config.cmake"
116+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-version.cmake"
112117
DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
113118
COMPONENT emv_development
114119
)
115120
install(EXPORT emvUtilsTargets
116-
FILE emvUtilsTargets.cmake
121+
FILE emv-utils-config-targets.cmake
117122
DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
118123
NAMESPACE emv::
119124
COMPONENT emv_development
120125
)
121126
export(EXPORT emvUtilsTargets
122-
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsTargets.cmake"
127+
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-targets.cmake"
123128
NAMESPACE emv::
124129
)
125130

cmake/emv-utils-config.cmake.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@PACKAGE_INIT@
2+
3+
include(CMakeFindDependencyMacro)
4+
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
5+
if(NOT "CXX" IN_LIST languages)
6+
# Various components of emv-utils, like isocodes_lookup and mcc_lookup,
7+
# require CXX
8+
message(FATAL_ERROR "emv-utils requires CXX")
9+
endif()
10+
@EMV_UTILS_CONFIG_PACKAGE_DEPENDENCIES@
11+
12+
check_required_components(emv-utils)
13+
14+
include("${CMAKE_CURRENT_LIST_DIR}/emv-utils-config-targets.cmake")
15+
16+
include(FindPackageHandleStandardArgs)
17+
set(emv-utils_CONFIG ${CMAKE_CURRENT_LIST_FILE})
18+
find_package_handle_standard_args(emv-utils CONFIG_MODE)

cmake/emvUtilsConfig.cmake.in

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ else()
6565
# Try custom Findjson-c.cmake module
6666
find_package(json-c REQUIRED)
6767
endif()
68+
# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent scope to
69+
# facilitate the generation of CMake package configuration files.
70+
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "json-c")
71+
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
6872

6973
include(GNUInstallDirs) # Provides CMAKE_INSTALL_* variables and good defaults for install()
7074

@@ -196,12 +200,26 @@ if(ISO8859_IMPL STREQUAL "boost")
196200
message(STATUS "Using Boost.Locale for iso8859 implementation")
197201
target_sources(iso8859 PRIVATE iso8859_boost.cpp)
198202
target_link_libraries(iso8859 PRIVATE Boost::locale)
203+
204+
# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent scope to
205+
# facilitate the generation of CMake package configuration files.
206+
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "Boost COMPONENTS locale")
207+
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
199208
endif()
200209
if(ISO8859_IMPL STREQUAL "iconv")
201210
find_package(Iconv REQUIRED)
211+
202212
message(STATUS "Using iconv for iso8859 implementation")
203213
target_sources(iso8859 PRIVATE iso8859_iconv.c)
204-
target_link_libraries(iso8859 PRIVATE Iconv::Iconv)
214+
if(Iconv_LIBRARIES)
215+
target_link_libraries(iso8859 PRIVATE Iconv::Iconv)
216+
217+
# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent
218+
# scope to facilitate the generation of CMake package configuration
219+
# files.
220+
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "Iconv")
221+
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
222+
endif()
205223
endif()
206224
if(ISO8859_IMPL STREQUAL "simple")
207225
message(STATUS "Using simple iso8859 implementation")

0 commit comments

Comments
 (0)