From b6407955712b51837cc3dc07ee5e6143761027c6 Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Sat, 27 Feb 2021 18:25:11 +0100 Subject: [PATCH 1/6] - exported targets go to separate generated file and package config file generated from template to use automatic package resolving and resolution logic CMake provides helpers to generate config file. Generated config file has usefull macro check_required_components() to set necessary variables like PackageName_FOUND if requirements has been satisfied. An absence of dedicated config file confuses user project as necessary variables are not set. --- CMakeLists.txt | 6 ++++-- jsoncppConfig.cmake.in | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 jsoncppConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ad82f9..23aaeee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,11 +177,13 @@ if(JSONCPP_WITH_CMAKE_PACKAGE) include(CMakePackageConfigHelpers) install(EXPORT jsoncpp DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp - FILE jsoncppConfig.cmake) + FILE jsoncpp-targets.cmake) + configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) + write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) endif() diff --git a/jsoncppConfig.cmake.in b/jsoncppConfig.cmake.in new file mode 100644 index 0000000..e3fa4b9 --- /dev/null +++ b/jsoncppConfig.cmake.in @@ -0,0 +1,10 @@ +cmake_policy(PUSH) +cmake_policy(VERSION 3.0) + +@PACKAGE_INIT@ + +include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-targets.cmake" ) + +check_required_components(JsonCpp) + +cmake_policy(POP) \ No newline at end of file From 62f3e034755c43759da819781bc23882a2ea3e01 Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Sat, 27 Feb 2021 22:35:57 +0100 Subject: [PATCH 2/6] - declare namespaced export target to simplify the library usage When the static libary is available use it as exported alias, otherwise use shared library. Cmake takes care about import library when Windows platform DLL is used --- jsoncppConfig.cmake.in | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jsoncppConfig.cmake.in b/jsoncppConfig.cmake.in index e3fa4b9..9caa006 100644 --- a/jsoncppConfig.cmake.in +++ b/jsoncppConfig.cmake.in @@ -5,6 +5,12 @@ cmake_policy(VERSION 3.0) include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-targets.cmake" ) +if(TARGET jsoncpp_static) + add_library(JsonCpp::JsonCpp ALIAS jsoncpp_static) +elseif(TARGET jsoncpp_lib) + add_library(JsonCpp::JsonCpp ALIAS jsoncpp_lib) +endif() + check_required_components(JsonCpp) cmake_policy(POP) \ No newline at end of file From cee42e0bd7ddd58e4a1fd7dd3137610e580964b3 Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Sun, 28 Feb 2021 00:25:48 +0100 Subject: [PATCH 3/6] - empty line at end of file --- jsoncppConfig.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsoncppConfig.cmake.in b/jsoncppConfig.cmake.in index 9caa006..789687b 100644 --- a/jsoncppConfig.cmake.in +++ b/jsoncppConfig.cmake.in @@ -13,4 +13,4 @@ endif() check_required_components(JsonCpp) -cmake_policy(POP) \ No newline at end of file +cmake_policy(POP) From a3914b792f86d60ab2cc42f50f2d9e329bcf3eb1 Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Sat, 6 Mar 2021 13:49:53 +0100 Subject: [PATCH 4/6] - narrowed lines to be aligned with overall file line width --- CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23aaeee..6093905 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -178,12 +178,14 @@ if(JSONCPP_WITH_CMAKE_PACKAGE) install(EXPORT jsoncpp DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp FILE jsoncpp-targets.cmake) - configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) + configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) endif() From 2af4a4c6c83fc519b190cf2d9797fd8dc3b31e1d Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Fri, 2 Apr 2021 22:30:43 +0200 Subject: [PATCH 5/6] - workaround for CMake < 3.18 ALIAS target limitation to not point to non-GLOBAL IMPORTED target --- jsoncppConfig.cmake.in | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jsoncppConfig.cmake.in b/jsoncppConfig.cmake.in index 789687b..c1cc6ca 100644 --- a/jsoncppConfig.cmake.in +++ b/jsoncppConfig.cmake.in @@ -6,9 +6,11 @@ cmake_policy(VERSION 3.0) include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-targets.cmake" ) if(TARGET jsoncpp_static) - add_library(JsonCpp::JsonCpp ALIAS jsoncpp_static) + add_library(JsonCpp::JsonCpp INTERFACE IMPORTED ) + set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static") elseif(TARGET jsoncpp_lib) - add_library(JsonCpp::JsonCpp ALIAS jsoncpp_lib) + add_library(JsonCpp::JsonCpp INTERFACE IMPORTED ) + set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib") endif() check_required_components(JsonCpp) From 993e4e2828993b017e6e3f5fff99697eead4d134 Mon Sep 17 00:00:00 2001 From: Sergey Rachev Date: Wed, 14 Apr 2021 20:50:43 +0200 Subject: [PATCH 6/6] - isolated namespace targets into separate file --- CMakeLists.txt | 1 + jsoncpp-namespaced-targets.cmake | 7 +++++++ jsoncppConfig.cmake.in | 9 +-------- 3 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 jsoncpp-namespaced-targets.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6093905..48b0350 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,6 +186,7 @@ if(JSONCPP_WITH_CMAKE_PACKAGE) COMPATIBILITY SameMajorVersion) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake + ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp-namespaced-targets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) endif() diff --git a/jsoncpp-namespaced-targets.cmake b/jsoncpp-namespaced-targets.cmake new file mode 100644 index 0000000..ac1504e --- /dev/null +++ b/jsoncpp-namespaced-targets.cmake @@ -0,0 +1,7 @@ +if (TARGET jsoncpp_static) + add_library(JsonCpp::JsonCpp INTERFACE IMPORTED) + set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static") +elseif (TARGET jsoncpp_lib) + add_library(JsonCpp::JsonCpp INTERFACE IMPORTED) + set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib") +endif () \ No newline at end of file diff --git a/jsoncppConfig.cmake.in b/jsoncppConfig.cmake.in index c1cc6ca..76570bc 100644 --- a/jsoncppConfig.cmake.in +++ b/jsoncppConfig.cmake.in @@ -4,14 +4,7 @@ cmake_policy(VERSION 3.0) @PACKAGE_INIT@ include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-targets.cmake" ) - -if(TARGET jsoncpp_static) - add_library(JsonCpp::JsonCpp INTERFACE IMPORTED ) - set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static") -elseif(TARGET jsoncpp_lib) - add_library(JsonCpp::JsonCpp INTERFACE IMPORTED ) - set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib") -endif() +include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-namespaced-targets.cmake" ) check_required_components(JsonCpp)