Skip to content

Provide namespaced CMake targets for shared and static libraries - #1742

Open
Tyagiquamar wants to merge 2 commits into
open-source-parsers:masterfrom
Tyagiquamar:fix/cmake-namespaced-targets
Open

Tyagiquamar wants to merge 2 commits into
open-source-parsers:masterfrom
Tyagiquamar:fix/cmake-namespaced-targets

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

find_package(jsoncpp) previously exposed a single namespaced target, JsonCpp::JsonCpp, implemented in jsoncpp-namespaced-targets.cmake as an INTERFACE IMPORTED wrapper that silently preferred jsoncpp_static whenever both libraries were installed. Clients could not choose the shared or static library through the namespaced interface, and the effective linkage of JsonCpp::JsonCpp changed 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::JsonCppShared when available, otherwise JsonCpp::JsonCppStatic
  • JsonCpp::jsoncpp -> JsonCpp::JsonCpp, matching the spelling used in The behavior of find_package(JsonCpp) can't be controlled by client code #1740

Because the same jsoncpp-namespaced-targets.cmake file is installed by both the CMake and Meson builds, both package generators benefit.

Root Cause

Reported in #1740: with only jsoncpp_lib installed the namespaced target links the shared library, with only jsoncpp_static it 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.cmake now defines JsonCpp::JsonCppShared and JsonCpp::JsonCppStatic as thin INTERFACE IMPORTED targets (the same mechanism already used, since ALIAS targets cannot be created for imported targets). JsonCpp::JsonCpp links 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::jsoncpp is 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 calls find_package(jsoncpp) and asserts:

  • every expected namespaced target exists (JsonCpp::JsonCpp, JsonCpp::jsoncpp, plus JsonCpp::JsonCppShared/JsonCpp::JsonCppStatic according to BUILD_SHARED_LIBS/BUILD_STATIC_LIBS)
  • each namespaced target's INTERFACE_LINK_LIBRARIES points at the intended library target
  • JsonCpp::JsonCpp prefers the shared library when both are installed
  • the consumer executable links and runs correctly

The 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:

  • default (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 new jsoncpp_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 4

The 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.

@coveralls

coveralls commented Dec 2, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 47064457846

Coverage remained the same at 88.415%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2752
Covered Lines: 2634
Line Coverage: 95.71%
Relevant Branches: 2781
Covered Branches: 2258
Branch Coverage: 81.19%
Branches in Coverage %: Yes
Coverage Strength: 23642.52 hits per line

💛 - Coveralls

@greptile-apps

greptile-apps Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Confidence Score: 3/5RetriggerView in Greptile

[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

  1. P1 Visual Studio tests fail ▶
  2. P1 Windows runtime settings are lost ▶

Summary

Adds explicit shared and static namespaced targets and makes JsonCpp::JsonCpp prefer the shared library.

  • JsonCpp packages let consumers choose shared or static libraries directly.

Reviews (2) · Last reviewed commit: "test(cmake): handle older CMake, multi-c..." · Reviewed by Greptile

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Visual Studio tests fail

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.

Comment on lines +30 to +33
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}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants