Skip to content

Commit

Permalink
Improve generation of CMake config files
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
leonlynch committed Jan 6, 2024
1 parent d041c99 commit 26af3d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
23 changes: 14 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##############################################################################
# Copyright (c) 2021, 2022, 2023 Leon Lynch
# Copyright (c) 2021-2024 Leon Lynch
#
# This file is licensed under the terms of the LGPL v2.1 license.
# See LICENSE file.
Expand Down Expand Up @@ -96,30 +96,35 @@ install(FILES

# Generate and install basic CMake config files
include(CMakePackageConfigHelpers) # Provides CMake config generator macros
# NOTE: src subdirectory provides EMV_UTILS_PACKAGE_DEPENDENCIES
foreach(pkg IN LISTS EMV_UTILS_PACKAGE_DEPENDENCIES)
# Build dependency string for use in CMake config file
string(APPEND EMV_UTILS_CONFIG_PACKAGE_DEPENDENCIES "find_dependency(${pkg})\n")
endforeach()
set(EMV_UTILS_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} CACHE STRING "Installation location for emv-utils CMake config files")
message(STATUS "Using CMake config install location \"${EMV_UTILS_INSTALL_CMAKEDIR}\"")
configure_package_config_file(cmake/emvUtilsConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfig.cmake"
configure_package_config_file(cmake/emv-utils-config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config.cmake"
INSTALL_DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-version.cmake"
COMPATIBILITY SameMinorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-version.cmake"
DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
COMPONENT emv_development
)
install(EXPORT emvUtilsTargets
FILE emvUtilsTargets.cmake
FILE emv-utils-config-targets.cmake
DESTINATION "${EMV_UTILS_INSTALL_CMAKEDIR}"
NAMESPACE emv::
COMPONENT emv_development
)
export(EXPORT emvUtilsTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/emvUtilsTargets.cmake"
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/emv-utils-config-targets.cmake"
NAMESPACE emv::
)

Expand Down
18 changes: 18 additions & 0 deletions cmake/emv-utils-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if(NOT "CXX" IN_LIST languages)
# Various components of emv-utils, like isocodes_lookup and mcc_lookup,
# require CXX
message(FATAL_ERROR "emv-utils requires CXX")
endif()
@EMV_UTILS_CONFIG_PACKAGE_DEPENDENCIES@

check_required_components(emv-utils)

include("${CMAKE_CURRENT_LIST_DIR}/emv-utils-config-targets.cmake")

include(FindPackageHandleStandardArgs)
set(emv-utils_CONFIG ${CMAKE_CURRENT_LIST_FILE})
find_package_handle_standard_args(emv-utils CONFIG_MODE)
11 changes: 0 additions & 11 deletions cmake/emvUtilsConfig.cmake.in

This file was deleted.

20 changes: 19 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ else()
# Try custom Findjson-c.cmake module
find_package(json-c REQUIRED)
endif()
# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent scope to
# facilitate the generation of CMake package configuration files.
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "json-c")
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)

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

Expand Down Expand Up @@ -196,12 +200,26 @@ if(ISO8859_IMPL STREQUAL "boost")
message(STATUS "Using Boost.Locale for iso8859 implementation")
target_sources(iso8859 PRIVATE iso8859_boost.cpp)
target_link_libraries(iso8859 PRIVATE Boost::locale)

# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent scope to
# facilitate the generation of CMake package configuration files.
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "Boost COMPONENTS locale")
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
endif()
if(ISO8859_IMPL STREQUAL "iconv")
find_package(Iconv REQUIRED)

message(STATUS "Using iconv for iso8859 implementation")
target_sources(iso8859 PRIVATE iso8859_iconv.c)
target_link_libraries(iso8859 PRIVATE Iconv::Iconv)
if(Iconv_LIBRARIES)
target_link_libraries(iso8859 PRIVATE Iconv::Iconv)

# The EMV_UTILS_PACKAGE_DEPENDENCIES variable is set for the parent
# scope to facilitate the generation of CMake package configuration
# files.
list(APPEND EMV_UTILS_PACKAGE_DEPENDENCIES "Iconv")
set(EMV_UTILS_PACKAGE_DEPENDENCIES ${EMV_UTILS_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
endif()
endif()
if(ISO8859_IMPL STREQUAL "simple")
message(STATUS "Using simple iso8859 implementation")
Expand Down

0 comments on commit 26af3d5

Please sign in to comment.