Skip to content

Refactor JSON generation in common.cmake into a Python helper script #314

@TheRedSwabian

Description

@TheRedSwabian

Context

The _spl_create_docs_target and _spl_create_reports_target macros in common.cmake (lines ~483–528) manually construct JSON strings using CMake's list(JOIN ...), string(JSON ...), and escaped quotes. This approach is fragile, hard to read, and difficult to maintain — CMake is not designed for structured data manipulation.

Current State

list(JOIN COMPONENTS_INFO "," _components_info_json)
set(_components_info_json "[${_components_info_json}]")
list(JOIN COMPONENTS_SPHINX_INCLUDE_PATTERNS "\",\"" _components_sphinx_include_patterns_json)
set(_components_sphinx_include_patterns_json "[\"${_components_sphinx_include_patterns_json}\"]")
file(WRITE ${_reports_config_json} "{
    \"target\": \"reports\",
    \"include_patterns\": ${_components_sphinx_include_patterns_json},
    \"reports_output_dir\": \"${_rel_reports_output_dir}\",
    \"components_info\": ${_components_info_json}
}")

Additionally, the _spl_create_reports_target macro iterates over COMPONENTS_INFO using string(JSON ... GET ...) to extract per-component fields (name, path, reports_output_dir, long_name, has_docs, source_docs_dir) for generating wrapper index pages and toctree content (lines ~538–570).

Proposed Solution

Create a Python script in src/spl_core/ (e.g., generate_sphinx_config.py) that:

  1. Accepts component info as input (either via CLI args or a simple input file written by CMake)
  2. Generates the config.json for docs/reports targets with proper JSON serialization
  3. Generates the per-component wrapper index files as .md (using myst-parser directive syntax)
  4. Generates the coverage fallback stub

The CMake code would then reduce to:

add_custom_command(
    OUTPUT ${_reports_config_json}
    COMMAND python -m spl_core.generate_sphinx_config
        --target reports
        --output-dir ${_reports_output_dir}
        --project-source-dir ${PROJECT_SOURCE_DIR}
        --components-info-file ${_components_info_file}
    DEPENDS ${_components_info_file}
)

Scope

  • _spl_create_docs_target macro (lines 483–507)
  • _spl_create_reports_target macro (lines 509–600+)
  • All string(JSON ...) and manual JSON string construction
  • Per-component wrapper page generation (toctree content)
  • Convert remaining generated .rst files (wrapper pages) to .md using myst-parser directive syntax

Benefits

  • Proper JSON handling with Python's json module (no escaping issues)
  • Easier to test (unit tests for the Python script)
  • More readable and maintainable
  • Consistent with the existing pattern of Python helpers (e.g., coverage_to_md.py)
  • All generated files use .md — no more mixed .rst/.md for generated content

Acceptance Criteria

  • All JSON config files are generated by the Python script
  • All per-component wrapper index pages are generated as .md (not .rst)
  • CMake only passes parameters and invokes the script
  • Existing integration tests still pass (same output format)
  • Unit tests for the new Python script

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions