From 2b0a994b5832439cf704147c5b485b9a4c7fbc24 Mon Sep 17 00:00:00 2001 From: Charles Chan Date: Fri, 22 Dec 2023 11:14:36 +0800 Subject: [PATCH] Use CMakePackageConfigHelpers to generate config.cmake --- CMakeLists.txt | 72 ++++++++++--------- .../{cJSONConfig.cmake.in => Config.cmake.in} | 15 ++-- library_config/cJSONConfigVersion.cmake.in | 11 --- 3 files changed, 48 insertions(+), 50 deletions(-) rename library_config/{cJSONConfig.cmake.in => Config.cmake.in} (57%) delete mode 100644 library_config/cJSONConfigVersion.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index f23ec631..fe10d6fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,25 +146,25 @@ endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) -install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") -install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") +install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") +install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(TARGETS "${CJSON_LIB}" - EXPORT "${CJSON_LIB}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" + EXPORT "${CJSON_LIB}-targets" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) if (BUILD_SHARED_AND_STATIC_LIBS) install(TARGETS "${CJSON_LIB}-static" - EXPORT "${CJSON_LIB}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" + EXPORT "${CJSON_LIB}-targets" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) endif() if(ENABLE_TARGET_EXPORT) # export library information for CMake projects - install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") + install(EXPORT "${CJSON_LIB}-targets" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") endif() if(ENABLE_CJSON_VERSION_SO) @@ -198,24 +198,24 @@ if(ENABLE_CJSON_UTILS) "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) install(TARGETS "${CJSON_UTILS_LIB}" - EXPORT "${CJSON_UTILS_LIB}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" + EXPORT "${CJSON_UTILS_LIB}-targets" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) if (BUILD_SHARED_AND_STATIC_LIBS) - install(TARGETS "${CJSON_UTILS_LIB}-static" - EXPORT "${CJSON_UTILS_LIB}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" + install(TARGETS "${CJSON_UTILS_LIB}-static" + EXPORT "${CJSON_UTILS_LIB}-targets" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) endif() - install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") - install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") + install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cjson") + install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") if(ENABLE_TARGET_EXPORT) - # export library information for CMake projects - install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") + # export library information for CMake projects + install(EXPORT "${CJSON_UTILS_LIB}-targets" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") endif() if(ENABLE_CJSON_VERSION_SO) @@ -227,18 +227,22 @@ if(ENABLE_CJSON_UTILS) endif() # create the other package config files -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in" - ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in" - ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) - if(ENABLE_TARGET_EXPORT) - # Install package config files - install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake + include(CMakePackageConfigHelpers) + write_basic_package_version_file( ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake - DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") + VERSION ${NCHL_VERSION} + COMPATIBILITY AnyNewerVersion) + + configure_package_config_file( + "${PROJECT_SOURCE_DIR}/library_config/Config.cmake.in" + "${PROJECT_BINARY_DIR}/cJSONConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON" + ) + install(FILES + ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake + ${PROJECT_BINARY_DIR}/cJSONConfig.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cJSON") endif() option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) diff --git a/library_config/cJSONConfig.cmake.in b/library_config/Config.cmake.in similarity index 57% rename from library_config/cJSONConfig.cmake.in rename to library_config/Config.cmake.in index 909f7a9a..2784b7ab 100644 --- a/library_config/cJSONConfig.cmake.in +++ b/library_config/Config.cmake.in @@ -1,9 +1,12 @@ +@PACKAGE_INIT@ +include(CMakeFindDependencyMacro) + # Whether the utils lib was build. set(CJSON_UTILS_FOUND @ENABLE_CJSON_UTILS@) # The include directories used by cJSON -set(CJSON_INCLUDE_DIRS "@CMAKE_INSTALL_FULL_INCLUDEDIR@") -set(CJSON_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@") +set(CJSON_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") +set(CJSON_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) @@ -11,19 +14,21 @@ get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) set(CJSON_LIBRARY "@CJSON_LIB@") if(@ENABLE_TARGET_EXPORT@) # Include the target - include("${_dir}/cjson.cmake") + include("${CMAKE_CURRENT_LIST_DIR}/cjson-targets.cmake") endif() if(CJSON_UTILS_FOUND) # The cJSON utils library - set(CJSON_UTILS_LIBRARY @CJSON_UTILS_LIB@) + set(CJSON_UTILS_LIBRARY "@CJSON_UTILS_LIB@") # All cJSON libraries set(CJSON_LIBRARIES "@CJSON_UTILS_LIB@" "@CJSON_LIB@") if(@ENABLE_TARGET_EXPORT@) # Include the target - include("${_dir}/cjson_utils.cmake") + include("${CMAKE_CURRENT_LIST_DIR}/cjson_utils-targets.cmake") endif() else() # All cJSON libraries set(CJSON_LIBRARIES "@CJSON_LIB@") endif() + +check_required_components("@CMAKE_PROJECT_NAME@") diff --git a/library_config/cJSONConfigVersion.cmake.in b/library_config/cJSONConfigVersion.cmake.in deleted file mode 100644 index 22ffec0f..00000000 --- a/library_config/cJSONConfigVersion.cmake.in +++ /dev/null @@ -1,11 +0,0 @@ -set(PACKAGE_VERSION "@PROJECT_VERSION@") - -# Check whether the requested PACKAGE_FIND_VERSION is compatible -if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") - set(PACKAGE_VERSION_COMPATIBLE FALSE) -else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") - set(PACKAGE_VERSION_EXACT TRUE) - endif() -endif()