Skip to content

Make jsoncpp optional - #68

Merged
Kicer86 merged 13 commits into
masterfrom
make_jsoncpp_optional
Apr 26, 2026
Merged

Kicer86 merged 13 commits into
masterfrom
make_jsoncpp_optional

Conversation

@Kicer86

@Kicer86 Kicer86 commented Apr 24, 2026

Copy link
Copy Markdown
Owner

No description provided.

Kicer86 added 5 commits April 24, 2026 23:59
…arget

Move src/link_header_pagination_strategy.cpp and src/services/github/request.cpp
into a new CMake library target 'cpp_restapi_json_pagination'. This target is
optional and controlled by the CppRestAPI_JsonPagination CMake option (ON by
default; forced ON when tests or examples are enabled).

The core 'cpp_restapi' library no longer depends on jsoncpp. Users that do not
need JSON-aware pagination merging or the GitHub request/builder helpers can
set -DCppRestAPI_JsonPagination=OFF and drop the jsoncpp build/run-time
dependency completely.

Public headers for the split classes now use a new export macro generated for
the new target (CPP_RESTAPI_JSON_PAGINATION_EXPORT). Tests and GitHub examples
are updated to link the new target in addition to cpp_restapi.

README is updated with a short section describing the new library and its
CMake option.
Move src/services/github/request.cpp out of cpp_restapi_json_pagination into a
new optional library target cpp_restapi_github controlled by the new
CppRestAPI_GitHub CMake option (ON by default). The target depends on
cpp_restapi_json_pagination (for LinkHeaderPaginationStrategy used to merge
paginated responses), so enabling CppRestAPI_GitHub automatically enables
CppRestAPI_JsonPagination.

This lets consumers that only need the generic Link-header pagination
strategy skip pulling in the GitHub helpers, and lets consumers that do not
use GitHub disable that surface independently.

Public github/request.hpp now uses a new CPP_RESTAPI_GITHUB_EXPORT macro.
Tests and GitHub examples are updated to link the new target. README is
updated accordingly.
Link cpp_restapi_json_pagination and cpp_restapi_github as INTERFACE
dependencies of cpp_restapi. Downstream applications now just need
`target_link_libraries(app PRIVATE cpp_restapi)` to transparently pull in
the enabled optional components — no separate entries required for the
pagination strategy or the GitHub helpers.

To avoid a dependency cycle between cpp_restapi and
cpp_restapi_json_pagination (which only needs the HeaderUtils helper from
core), move src/header_utils.{hpp,cpp} into cpp_restapi_json_pagination.
The helper has no other users in core.

Tests and examples are simplified accordingly (no longer listing the
optional targets explicitly). README is updated to describe the new,
simpler usage.
The previous commit moved src/header_utils.{hpp,cpp} into
cpp_restapi_json_pagination purely to avoid a link-time dependency cycle
between cpp_restapi and cpp_restapi_json_pagination. That placement was
awkward: HeaderUtils is a generic Link-header parsing helper, not something
specific to JSON pagination.

Collapse the implementation into the header (single regex-based function).
No .cpp file means no archive symbol to resolve, so nothing links back into
core and the cycle stays broken regardless of where the header lives. The
header returns to being an internal helper in src/ that any future
pagination strategy can include.
The JSON pagination and GitHub helper sections described the
cpp_restapi_json_pagination / cpp_restapi_github CMake targets and how
they are re-exposed through the umbrella cpp_restapi target. That is an
implementation detail of the build; from a consumer perspective there is
only one library to link against. Keep just the CMake options users need
to toggle the features.
@Kicer86
Kicer86 force-pushed the make_jsoncpp_optional branch from b87e4fb to 81f0682 Compare April 24, 2026 21:59
Kicer86 added 8 commits April 25, 2026 15:11
The top-level CMakeLists.txt carried inline definitions for the
cpp_restapi_json_pagination and cpp_restapi_github targets — duplicating
the structural pattern already used by every backend
(src/<name>_backend/CMakeLists.txt + add_subdirectory).

Move each optional component to its own subdirectory:
  src/json_pagination/    (link_header_pagination_strategy.cpp + CMakeLists)
  src/services/github/    (now also owns its own CMakeLists.txt)

The root CMakeLists.txt now just guards each component with its option
and calls add_subdirectory, mirroring the backend handling.

generate_export_header is given an explicit EXPORT_FILE_NAME pointing at
${PROJECT_BINARY_DIR} so the generated *_export.h headers stay where
public headers in include/cpp_restapi expect to find them (the default
output directory is CMAKE_CURRENT_BINARY_DIR, which would otherwise move
into the per-subdir build folder).
    The previous layout had cpp_restapi own the core sources/backends and
    the optional cpp_restapi_json_pagination / cpp_restapi_github targets
    PUBLIC-link back to cpp_restapi while cpp_restapi INTERFACE-linked them
    in turn. That worked but described what looked like a dependency cycle
    and exposed three separate library targets to consumers.

    Reorganize the dependency graph as a strict tree and reduce the public
    surface to a single artifact:

      cpp_restapi_core            (OBJECT) -- backends contribute here
      cpp_restapi_json_pagination (OBJECT) -- depends on _core
      cpp_restapi_github          (OBJECT) -- depends on _core, _json_pagination
      cpp_restapi                 (umbrella) -- links all of the above

    cpp_restapi is the only target consumers see and the only artifact
    produced (.so / .a). Its type is left to BUILD_SHARED_LIBS — the user
    decides; we no longer pin it.

    All internal pieces are OBJECT libraries with POSITION_INDEPENDENT_CODE.
    That guarantees there is exactly one place where their object code lands
    (inside cpp_restapi); they cannot be linked twice into a downstream
    build, and there is no chance of multiple consumer shared libraries each
    embedding a private copy of a static helper.

    Internal layout:
      - cpp_restapi_core gets the three core sources and is the sink for
        each backend's target_sources / target_link_libraries, replacing
        cpp_restapi as the backend target name.
      - cpp_restapi_export.h is generated against cpp_restapi_core (with
        BASE_NAME cpp_restapi to keep CPP_RESTAPI_EXPORT) so existing public
        headers do not change.
      - The optional components no longer back-link into cpp_restapi; the
        umbrella PUBLIC-links them, removing the apparent cycle.
      - The umbrella library has a tiny auto-generated stub TU as its only
        direct source; everything else arrives via the linked OBJECT libs.

    Verified with both static (default) and BUILD_SHARED_LIBS=ON builds:
    ctest passes, only libcpp_restapi.{so,a} is produced, ldd on the shared
    library shows just the expected backend/jsoncpp deps.
The top-level CMakeLists.txt was carrying the cpp_restapi_core target
definition, the per-component add_subdirectory calls, and the umbrella
cpp_restapi target — none of which belong at project scope. Move them
into src/CMakeLists.txt so the root file only handles project-wide
concerns: project()/version, options, the no-backend guard, and the
add_subdirectory dispatch to src/, tests/ and examples/.
It was forced to be header-only earlier to avoid an archive-symbol link
cycle between cpp_restapi and cpp_restapi_json_pagination. With the new
umbrella-OBJECT-libraries layout the dependency graph is acyclic
(json_pagination depends on core, never the other way around), so the
.cpp can come back: a single definition of getNextPageUrl lives in
cpp_restapi_core and is reused by anyone that includes header_utils.hpp,
instead of being inlined into every translation unit.
The umbrella library's stub source was generated via file(WRITE) on
every CMake reconfigure, which updated its mtime and forced a relink
of libcpp_restapi each time. Replace it with a tiny checked-in source
file. Same end result, no spurious rebuilds.
Previously the implication CppRestAPI_GitHub => CppRestAPI_JsonPagination
was applied silently with a STATUS message, which meant explicitly
passing -DCppRestAPI_JsonPagination=OFF on the command line was ignored
without any visible feedback. Promote the message to WARNING so the
inconsistent configuration is clearly surfaced.
When CppRestAPI_GitHub or CppRestAPI_JsonPagination is OFF, the
matching public headers (cpp_restapi/github/request.hpp,
cpp_restapi/link_header_pagination_strategy.hpp) used to break the
build with a low-level missing-header error referring to the
generated *_export.h that was never produced.

Add CPP_RESTAPI_HAS_GITHUB and CPP_RESTAPI_HAS_JSON_PAGINATION
feature macros to the configured version.hpp, and #error out from
those headers when the corresponding feature is not present, so the
diagnostic clearly tells the user how to fix the configuration.
The umbrella library needs at least one source file, which previously
existed solely as an empty stub. Replace it with src/version.cpp,
defining cpp_restapi::runtimeVersion() — a small but useful runtime
counterpart to the compile-time CPP_RESTAPI_VERSION constants. It
returns the version string baked into the actually-linked library,
allowing consumers to detect header/library mismatch after an
in-place upgrade.

The function is declared in the configured version.hpp and exported
via CPP_RESTAPI_EXPORT; it lives in the umbrella TU so it's part of
libcpp_restapi itself rather than any sub-component.
@Kicer86
Kicer86 marked this pull request as ready for review April 26, 2026 07:12
@Kicer86
Kicer86 merged commit 27e2fd5 into master Apr 26, 2026
16 checks passed
@Kicer86
Kicer86 deleted the make_jsoncpp_optional branch April 26, 2026 07:12
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.

1 participant