Repository navigation
Provide namespaced CMake targets for shared and static libraries #1742
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
Open
Tyagiquamar
wants to merge
2
commits into
open-source-parsers:master
Choose a base branch
from
Tyagiquamar:fix/cmake-namespaced-targets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,28 @@ | ||
| if (TARGET jsoncpp_lib) | ||
| if (NOT TARGET JsonCpp::JsonCppShared) | ||
| add_library(JsonCpp::JsonCppShared INTERFACE IMPORTED) | ||
| set_target_properties(JsonCpp::JsonCppShared PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib") | ||
| endif () | ||
| endif () | ||
|
|
||
| if (TARGET jsoncpp_static) | ||
| if (NOT TARGET JsonCpp::JsonCppStatic) | ||
| add_library(JsonCpp::JsonCppStatic INTERFACE IMPORTED) | ||
| set_target_properties(JsonCpp::JsonCppStatic PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static") | ||
| endif () | ||
| endif () | ||
|
|
||
| if (NOT TARGET JsonCpp::JsonCpp) | ||
| if (TARGET jsoncpp_static) | ||
| if (TARGET JsonCpp::JsonCppShared) | ||
| add_library(JsonCpp::JsonCpp INTERFACE IMPORTED) | ||
| set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_static") | ||
| elseif (TARGET jsoncpp_lib) | ||
| set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCppShared") | ||
| elseif (TARGET JsonCpp::JsonCppStatic) | ||
| add_library(JsonCpp::JsonCpp INTERFACE IMPORTED) | ||
| set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "jsoncpp_lib") | ||
| set_target_properties(JsonCpp::JsonCpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCppStatic") | ||
| endif () | ||
| endif () | ||
|
|
||
| if (NOT TARGET JsonCpp::jsoncpp AND TARGET JsonCpp::JsonCpp) | ||
| add_library(JsonCpp::jsoncpp INTERFACE IMPORTED) | ||
| set_target_properties(JsonCpp::jsoncpp PROPERTIES INTERFACE_LINK_LIBRARIES "JsonCpp::JsonCpp") | ||
| endif () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| cmake_minimum_required(VERSION 3.10) | ||
|
|
||
| # Match the MSVC runtime library used to build jsoncpp when it was compiled with | ||
| # JSONCPP_STATIC_WINDOWS_RUNTIME, otherwise linking fails with LNK2038. | ||
| if(JSONCPP_STATIC_WINDOWS_RUNTIME) | ||
| set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") | ||
| endif() | ||
|
|
||
| project(jsoncpp_package_consumer CXX) | ||
|
|
||
| find_package(jsoncpp REQUIRED) | ||
|
|
||
| set(_expected_targets JsonCpp::JsonCpp JsonCpp::jsoncpp) | ||
| if(EXPECT_SHARED_LIBS) | ||
| list(APPEND _expected_targets JsonCpp::JsonCppShared) | ||
| endif() | ||
| if(EXPECT_STATIC_LIBS) | ||
| list(APPEND _expected_targets JsonCpp::JsonCppStatic) | ||
| endif() | ||
|
|
||
| foreach(_target IN LISTS _expected_targets) | ||
| if(NOT TARGET ${_target}) | ||
| message(FATAL_ERROR "find_package(jsoncpp) did not provide ${_target}") | ||
| endif() | ||
| endforeach() | ||
|
|
||
| if(EXPECT_SHARED_LIBS) | ||
| get_target_property(_shared_link JsonCpp::JsonCppShared INTERFACE_LINK_LIBRARIES) | ||
| if(NOT _shared_link STREQUAL "jsoncpp_lib") | ||
| message(FATAL_ERROR "JsonCpp::JsonCppShared links '${_shared_link}', expected 'jsoncpp_lib'") | ||
| endif() | ||
| set(_expected_default JsonCpp::JsonCppShared) | ||
| else() | ||
| set(_expected_default JsonCpp::JsonCppStatic) | ||
| endif() | ||
|
|
||
| get_target_property(_default_link JsonCpp::JsonCpp INTERFACE_LINK_LIBRARIES) | ||
| if(NOT _default_link STREQUAL "${_expected_default}") | ||
| message(FATAL_ERROR "JsonCpp::JsonCpp links '${_default_link}', expected '${_expected_default}'") | ||
| endif() | ||
|
|
||
| get_target_property(_lowercase_link JsonCpp::jsoncpp INTERFACE_LINK_LIBRARIES) | ||
| if(NOT _lowercase_link STREQUAL "JsonCpp::JsonCpp") | ||
| message(FATAL_ERROR "JsonCpp::jsoncpp links '${_lowercase_link}', expected 'JsonCpp::JsonCpp'") | ||
| endif() | ||
|
|
||
| if(EXPECT_STATIC_LIBS) | ||
| get_target_property(_static_link JsonCpp::JsonCppStatic INTERFACE_LINK_LIBRARIES) | ||
| if(NOT _static_link STREQUAL "jsoncpp_static") | ||
| message(FATAL_ERROR "JsonCpp::JsonCppStatic links '${_static_link}', expected 'jsoncpp_static'") | ||
| endif() | ||
| endif() | ||
|
|
||
| add_executable(jsoncpp_package_consumer main.cpp) | ||
| target_link_libraries(jsoncpp_package_consumer PRIVATE JsonCpp::JsonCpp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #include <json/json.h> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| int main() { | ||
| Json::Value root; | ||
| root["answer"] = 42; | ||
| std::cout << root.toStyledString(); | ||
| return root["answer"].asInt() == 42 ? 0 : 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| cmake_minimum_required(VERSION 3.10) | ||
|
|
||
| foreach(_required_var JSONCPP_BUILD_DIR JSONCPP_SOURCE_DIR) | ||
| if(NOT DEFINED ${_required_var}) | ||
| message(FATAL_ERROR "verify_package_namespaced_targets.cmake requires -D${_required_var}") | ||
| endif() | ||
| endforeach() | ||
|
|
||
| set(PREFIX "${JSONCPP_BUILD_DIR}/package-test-prefix") | ||
| set(CONSUMER_SOURCE "${JSONCPP_BUILD_DIR}/package-test-consumer") | ||
| set(CONSUMER_BUILD "${JSONCPP_BUILD_DIR}/package-test-consumer-build") | ||
|
|
||
| file(REMOVE_RECURSE "${PREFIX}" "${CONSUMER_SOURCE}" "${CONSUMER_BUILD}") | ||
|
|
||
| # Multi-config generators (e.g. Visual Studio) need the configuration that ctest | ||
| # selected via -C, exposed here as CTEST_CONFIGURATION_TYPE. | ||
| set(_build_config "$ENV{CTEST_CONFIGURATION_TYPE}") | ||
|
|
||
| set(_install_args | ||
| --install "${JSONCPP_BUILD_DIR}" | ||
| --prefix "${PREFIX}") | ||
| if(_build_config) | ||
| list(APPEND _install_args --config "${_build_config}") | ||
| endif() | ||
| execute_process( | ||
| COMMAND "${CMAKE_COMMAND}" ${_install_args} | ||
| RESULT_VARIABLE _install_result | ||
| OUTPUT_VARIABLE _install_output | ||
| ERROR_VARIABLE _install_error) | ||
| if(NOT _install_result EQUAL 0) | ||
| message(FATAL_ERROR "cmake --install failed:\n${_install_error}${_install_output}") | ||
| endif() | ||
|
|
||
| set(_consumer_cmake "${JSONCPP_SOURCE_DIR}/test/cmake/consumer/CMakeLists.txt") | ||
| set(_consumer_main "${JSONCPP_SOURCE_DIR}/test/cmake/consumer/main.cpp") | ||
| file(COPY "${_consumer_cmake}" DESTINATION "${CONSUMER_SOURCE}") | ||
| file(COPY "${_consumer_main}" DESTINATION "${CONSUMER_SOURCE}") | ||
|
|
||
| execute_process( | ||
| COMMAND "${CMAKE_COMMAND}" -S "${CONSUMER_SOURCE}" -B "${CONSUMER_BUILD}" | ||
| "-DCMAKE_PREFIX_PATH=${PREFIX}" | ||
| "-DEXPECT_SHARED_LIBS=${EXPECT_SHARED_LIBS}" | ||
| "-DEXPECT_STATIC_LIBS=${EXPECT_STATIC_LIBS}" | ||
| "-DJSONCPP_STATIC_WINDOWS_RUNTIME=${JSONCPP_STATIC_WINDOWS_RUNTIME}" | ||
| RESULT_VARIABLE _configure_result | ||
| OUTPUT_VARIABLE _configure_output | ||
| ERROR_VARIABLE _configure_error) | ||
| if(NOT _configure_result EQUAL 0) | ||
| message(FATAL_ERROR "consumer configure failed:\n${_configure_error}${_configure_output}") | ||
| endif() | ||
|
|
||
| set(_build_args --build "${CONSUMER_BUILD}") | ||
| if(_build_config) | ||
| list(APPEND _build_args --config "${_build_config}") | ||
| endif() | ||
| execute_process( | ||
| COMMAND "${CMAKE_COMMAND}" ${_build_args} | ||
| RESULT_VARIABLE _build_result | ||
| OUTPUT_VARIABLE _build_output | ||
| ERROR_VARIABLE _build_error) | ||
| if(NOT _build_result EQUAL 0) | ||
| message(FATAL_ERROR "consumer build failed:\n${_build_error}${_build_output}") | ||
| endif() | ||
|
|
||
| # Multi-config generators place the executable in a per-config subdirectory. | ||
| set(_exe_search_paths "${CONSUMER_BUILD}") | ||
| if(_build_config) | ||
| list(APPEND _exe_search_paths "${CONSUMER_BUILD}/${_build_config}") | ||
| endif() | ||
| find_program(_consumer_exe NAMES jsoncpp_package_consumer PATHS ${_exe_search_paths} NO_DEFAULT_PATH) | ||
| if(NOT _consumer_exe) | ||
| message(FATAL_ERROR "consumer executable not found in ${CONSUMER_BUILD}") | ||
| endif() | ||
|
|
||
| # On Windows the shared library is installed under <prefix>/bin, which is not on | ||
| # PATH, so the consumer executable cannot load the DLL unless we add it. | ||
| if(WIN32 AND EXPECT_SHARED_LIBS) | ||
| set(ENV{PATH} "${PREFIX}/bin;${ENV{PATH}}") | ||
| endif() | ||
|
|
||
| execute_process( | ||
| COMMAND "${_consumer_exe}" | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| RESULT_VARIABLE _run_result | ||
| OUTPUT_VARIABLE _run_output | ||
| ERROR_VARIABLE _run_error) | ||
| if(NOT _run_result EQUAL 0) | ||
| message(FATAL_ERROR "consumer run failed:\n${_run_error}${_run_output}") | ||
| endif() | ||
|
|
||
| message(STATUS "namespaced target verification output: ${_run_output}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
BUILD_SHARED_LIBS=OFFandJSONCPP_STATIC_WINDOWS_RUNTIME=ON, JsonCpp is built with/MT, but this fresh consumer uses the default/MD. Linking the static library then fails with a runtime mismatch.Pass the runtime choice into the consumer and apply it with the appropriate CMake policy before creating its target.