Repository navigation
Provide namespaced CMake targets for shared and static libraries - #1742
Tyagiquamar wants to merge 2 commits into
Conversation
Coverage Report for CI Build 47064457846Coverage remained the same at 88.415%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Confidence Score: 3/5
[Medium impact] The change reaches projects that consume JsonCpp through installed CMake packages. The PR still needs fixes for configuration selection and the consumer's Windows runtime setting before merging. Findings
|
| message(FATAL_ERROR "consumer build failed:\n${_build_error}${_build_output}") | ||
| endif() | ||
|
|
||
| find_program(_consumer_exe NAMES jsoncpp_package_consumer PATHS "${CONSUMER_BUILD}" NO_DEFAULT_PATH) |
There was a problem hiding this comment.
With a generator such as Visual Studio, this test ignores the configuration selected by ctest -C. Installation can therefore request files that were not built. Even when installation succeeds, the consumer executable lands under a directory such as Debug, while find_program only searches the build root.
Pass the selected configuration through installation and building, and use the executable's generated path.
| 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}" |
There was a problem hiding this comment.
Windows runtime settings are lost
With BUILD_SHARED_LIBS=OFF and JSONCPP_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.
… runtime in package verification Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com>
Summary
find_package(jsoncpp)previously exposed a single namespaced target,JsonCpp::JsonCpp, implemented injsoncpp-namespaced-targets.cmakeas anINTERFACE IMPORTEDwrapper that silently preferredjsoncpp_staticwhenever both libraries were installed. Clients could not choose the shared or static library through the namespaced interface, and the effective linkage ofJsonCpp::JsonCppchanged depending on which libraries happened to be installed on a given machine.This change provides explicit namespaced targets for each library flavor and makes the default target resolve deterministically:
JsonCpp::JsonCppShared->jsoncpp_lib(only defined when the shared library is installed)JsonCpp::JsonCppStatic->jsoncpp_static(only defined when the static library is installed)JsonCpp::JsonCpp->JsonCpp::JsonCppSharedwhen available, otherwiseJsonCpp::JsonCppStaticJsonCpp::jsoncpp->JsonCpp::JsonCpp, matching the spelling used in The behavior offind_package(JsonCpp)can't be controlled by client code #1740Because the same
jsoncpp-namespaced-targets.cmakefile is installed by both the CMake and Meson builds, both package generators benefit.Root Cause
Reported in #1740: with only
jsoncpp_libinstalled the namespaced target links the shared library, with onlyjsoncpp_staticit links the static one, and with both installed the static library silently takes priority. There was no way for a client project to request the shared library explicitly when both were present.Technical Walkthrough
jsoncpp-namespaced-targets.cmakenow definesJsonCpp::JsonCppSharedandJsonCpp::JsonCppStaticas thinINTERFACE IMPORTEDtargets (the same mechanism already used, sinceALIAStargets cannot be created for imported targets).JsonCpp::JsonCpplinks to the shared flavor when present and falls back to the static one, so its resolution no longer depends on which libraries a machine happens to have installed.JsonCpp::jsoncppis provided as an additional alias for the default target.A new CTest,
jsoncpp_cmake_package_namespaced_targets, installs the package into a scratch prefix inside the build tree, then configures, builds, and runs a small consumer project that callsfind_package(jsoncpp)and asserts:JsonCpp::JsonCpp,JsonCpp::jsoncpp, plusJsonCpp::JsonCppShared/JsonCpp::JsonCppStaticaccording toBUILD_SHARED_LIBS/BUILD_STATIC_LIBS)INTERFACE_LINK_LIBRARIESpoints at the intended library targetJsonCpp::JsonCppprefers the shared library when both are installedThe test is skipped for cross-compiling builds and when the CMake package or install rules are disabled.
Test Plan
Run in Docker (
public.ecr.aws/d3j8x8q7/olympus-base-cpp, GCC 12.2, CMake 4.3.2, Ninja, Python 3.12) for all three library configurations:BUILD_SHARED_LIBS=ON,BUILD_STATIC_LIBS=ON):cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release && cmake --build build && ctest --output-on-failure-> 100% tests passed, 0 failed out of 4 (including the newjsoncpp_cmake_package_namespaced_targets,jsoncpp_readerwriter,jsoncpp_readerwriter_json_checker,jsoncpp_test)-DBUILD_SHARED_LIBS=OFF(static only): same command -> 100% tests passed, 0 failed out of 4-DBUILD_STATIC_LIBS=OFF(shared only): same command -> 100% tests passed, 0 failed out of 4The namespaced-target file logic was additionally verified standalone for the both-libraries and static-only cases by loading it against simulated imported targets and inspecting
INTERFACE_LINK_LIBRARIES.