Skip to content

Cmake config improvements #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ 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()

Expand Down
18 changes: 18 additions & 0 deletions jsoncppConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_policy(PUSH)
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this logic in a file jsoncpp-namespaced-targets.cmake exported with install(EXPORT ...).
Moreover, for consistency with add_subdirectory(), JsonCpp::JsonCpp ALIAS target should be added in CMakeLists.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this logic in a file jsoncpp-namespaced-targets.cmake exported with install(EXPORT ...).

You mean such that lines 6-7 above read like this?

include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-targets.cmake" )
include ( "${CMAKE_CURRENT_LIST_DIR}/jsoncpp-namespaced-targets.cmake" )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's it 😃

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. And about the ALIAS target, where would you do that?

Copy link
Contributor

@SpaceIm SpaceIm Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In /src/lib_json/CMakeLists.txt close to add_library() calls. Unfortunately, it will probably requires some ugly branching because currently jsoncpp can build both shared & static, or shared only, or static only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be glad to add ALIAS to CMakeLists to use the namespaced target in build tree also.
What is the usecase of having both static and shared build at same time? CMake allows to specify the library type during generation with -DBUILD_SHARED_LIBS=Off/On although it relies on CMakeLists.txt to not specify types explicitly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacelm: Could we imagine fixing the include part of your comment in this PR, and leaving the ALIAS part for another PR? So that there could be a discussion about @sergeyrachev's point with which I agree: why not relying on BUILD_SHARED_LIBS instead? That makes it simpler IMO, and if you want both, you can run the build twice 🤔.

Copy link
Contributor

@SpaceIm SpaceIm Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem for me to add ALIAS target in another PR, it would be an improvement.

Then, you can introduce a new breaking change in jsoncpp by removing again the ability to build both static & shared (AFAIK, it was removed for 1.9.0, then introduced again in 1.9.4...). Since I'm against build of both static & shared, I would be happy, but I'm not the maintainer.


check_required_components(JsonCpp)

cmake_policy(POP)