Skip to content

Commit a098402

Browse files
docs: Add automatization for sdk-connectedhomeip links
Automatically read west.yaml and create documentation with the proper SHA of sdk-conenctedhomeip while building sphinx docs. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 905d600 commit a098402

3 files changed

Lines changed: 150 additions & 12 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
Copyright (c) 2026 Nordic Semiconductor ASA
3+
4+
SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
6+
Expose west manifest revisions as Sphinx substitutions for documentation links.
7+
8+
Reads the Matter (sdk-connectedhomeip) revision from west.yml and registers
9+
|sdk-connectedhomeip-revision| for use in links.txt and RST sources.
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import subprocess
15+
from pathlib import Path
16+
from typing import TYPE_CHECKING, Any
17+
18+
import yaml
19+
20+
if TYPE_CHECKING:
21+
from sphinx.application import Sphinx
22+
23+
__version__ = "0.1.0"
24+
25+
MATTER_WEST_PROJECT = "matter"
26+
SDK_CONNECTEDHOMEIP_REVISION = "sdk-connectedhomeip-revision"
27+
28+
29+
def _load_west_manifest(west_manifest: Path) -> dict[str, Any]:
30+
if not west_manifest.is_file():
31+
raise FileNotFoundError(f"west manifest not found: {west_manifest}")
32+
data = yaml.safe_load(west_manifest.read_text(encoding="utf-8"))
33+
if not isinstance(data, dict):
34+
raise ValueError(f"Invalid west manifest: {west_manifest}")
35+
return data
36+
37+
38+
def _matter_project(manifest: dict[str, Any]) -> dict[str, Any]:
39+
projects = manifest.get("manifest", {}).get("projects", [])
40+
if not isinstance(projects, list):
41+
raise ValueError("west manifest has no projects list")
42+
43+
for project in projects:
44+
if isinstance(project, dict) and project.get("name") == MATTER_WEST_PROJECT:
45+
return project
46+
47+
raise ValueError(f"west manifest has no project named {MATTER_WEST_PROJECT!r}")
48+
49+
50+
def _git_short_revision(matter_module: Path, revision: str) -> str | None:
51+
if not matter_module.is_dir():
52+
return None
53+
54+
for ref in (revision, "HEAD"):
55+
proc = subprocess.run(
56+
["git", "-C", str(matter_module), "rev-parse", "--short", ref],
57+
capture_output=True,
58+
text=True,
59+
check=False,
60+
)
61+
if proc.returncode == 0:
62+
short_sha = proc.stdout.strip()
63+
if short_sha:
64+
return short_sha
65+
return None
66+
67+
68+
def connectedhomeip_revision(
69+
west_manifest: Path,
70+
*,
71+
matter_module: Path | None = None,
72+
) -> str:
73+
"""Return the sdk-connectedhomeip revision string for documentation links."""
74+
project = _matter_project(_load_west_manifest(west_manifest))
75+
revision = str(project.get("revision", "")).strip()
76+
if not revision:
77+
raise ValueError(
78+
f"Project {MATTER_WEST_PROJECT!r} in {west_manifest} has no revision"
79+
)
80+
81+
if matter_module is not None:
82+
short_sha = _git_short_revision(matter_module, revision)
83+
if short_sha:
84+
return short_sha
85+
86+
return revision
87+
88+
89+
def load_west_substitutions(
90+
west_manifest: Path,
91+
*,
92+
matter_module: Path | None = None,
93+
) -> dict[str, str]:
94+
"""Build substitution mapping from west.yml for conf.py preprocessing."""
95+
return {
96+
SDK_CONNECTEDHOMEIP_REVISION: connectedhomeip_revision(
97+
west_manifest,
98+
matter_module=matter_module,
99+
),
100+
}
101+
102+
103+
def _config_inited(app, config) -> None:
104+
west_manifest = Path(config.west_manifest_path)
105+
matter_module = Path(config.matter_module_path) if config.matter_module_path else None
106+
substitutions = load_west_substitutions(west_manifest, matter_module=matter_module)
107+
108+
prolog_lines = [
109+
f".. |{name}| replace:: {value}" for name, value in substitutions.items()
110+
]
111+
existing_prolog = config.rst_prolog or ""
112+
if existing_prolog and not existing_prolog.endswith("\n"):
113+
existing_prolog = f"{existing_prolog}\n"
114+
config.rst_prolog = "\n".join(prolog_lines) + "\n" + existing_prolog
115+
116+
117+
def setup(app):
118+
app.add_config_value("west_manifest_path", None, "env", [str])
119+
app.add_config_value("matter_module_path", None, "env", [str])
120+
app.connect("config-inited", _config_inited)
121+
122+
return {
123+
"version": __version__,
124+
"parallel_read_safe": True,
125+
"parallel_write_safe": True,
126+
}

docs/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
sys.path.insert(0, str(DOC_BASE / '_extensions'))
2929

30+
from west_substitutions import load_west_substitutions
31+
3032
extensions = [
3133
'table_from_rows',
3234
'breathe',
@@ -41,8 +43,12 @@
4143
'memory_layout_viz',
4244
'stack_viz',
4345
'options_from_kconfig',
46+
'west_substitutions',
4447
]
4548

49+
west_manifest_path = str(NCS_MATTER_BASE / 'west.yml')
50+
matter_module_path = str(MATTER_MODULE)
51+
4652
root_doc = 'index'
4753

4854
templates_path = ['_templates']
@@ -90,6 +96,12 @@ def _apply_substitutions(text: str, substitutions: dict[str, str]) -> str:
9096

9197
_shortcuts_path = DOC_BASE / 'shortcuts.txt'
9298
_substitutions = _read_rst_substitutions(_shortcuts_path)
99+
_substitutions.update(
100+
load_west_substitutions(
101+
NCS_MATTER_BASE / 'west.yml',
102+
matter_module=MATTER_MODULE,
103+
)
104+
)
93105
_rst_epilog_links = _apply_substitutions(
94106
(DOC_BASE / 'links.txt').read_text(encoding='utf-8'),
95107
_substitutions,

docs/links.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636

3737
.. ### Matter documentation backtick links (from sdk-nrf links.txt)
3838

39-
.. _Bluetooth LE Arbiter's header file: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/src/platform/Zephyr/BLEAdvertisingArbiter.h
39+
.. _Bluetooth LE Arbiter's header file: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/src/platform/Zephyr/BLEAdvertisingArbiter.h
4040
.. _Bluetooth SIG's Qualification Process: https://www.bluetooth.com/develop-with-bluetooth/qualify/
41-
.. _CHIP Certificate Tool source files: https://github.com/nrfconnect/sdk-connectedhomeip/tree/5e2e5a02d6/src/tools
41+
.. _CHIP Certificate Tool source files: https://github.com/nrfconnect/sdk-connectedhomeip/tree/|sdk-connectedhomeip-revision|/src/tools
4242
.. _CHIP Tool Guide: https://project-chip.github.io/connectedhomeip-doc/development_controllers/chip-tool/chip_tool_guide.html
4343
.. _CONFIG_BT_CTLR_TX_PWR_MINUS: https://nrfconnectdocs.nordicsemi.com/ncs/latest/kconfig/index.html#!CONFIG_BT_CTLR_TX_PWR_MINUS
4444
.. _CONFIG_BT_CTLR_TX_PWR_PLUS: https://nrfconnectdocs.nordicsemi.com/ncs/latest/kconfig/index.html#!CONFIG_BT_CTLR_TX_PWR_PLUS
@@ -56,20 +56,20 @@
5656
.. _Developing Matter 1.0 products with nRF Connect SDK: https://www.youtube.com/watch?v=9Ar13rMxGIk
5757
.. _Distributed Compliance Ledger: https://webui.dcl.csa-iot.org/
5858
.. _Electrical specification for nRF7002: https://docs.nordicsemi.com/bundle/ps_nrf7002/page/chapters/elspec/doc/electrical_specification.html
59-
.. _Factory Data Provider: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/src/platform/nrfconnect/FactoryDataProvider.h
60-
.. _Factory data schema: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/scripts/tools/nrfconnect/nrfconnect_factory_data.schema
59+
.. _Factory Data Provider: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/src/platform/nrfconnect/FactoryDataProvider.h
60+
.. _Factory data schema: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/scripts/tools/nrfconnect/nrfconnect_factory_data.schema
6161
.. _Fprotect: https://nrfconnectdocs.nordicsemi.com/ncs/latest/nrf/libraries/security/bootloader/fprotect.html
6262
.. _GN: https://gn.googlesource.com/gn/
6363
.. _GN website: https://gn.googlesource.com/gn/#getting-a-binary
64-
.. _Generate factory data script: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py
65-
.. _Generate partition script: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/scripts/tools/nrfconnect/nrfconnect_generate_partition.py
64+
.. _Generate factory data script: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py
65+
.. _Generate partition script: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/scripts/tools/nrfconnect/nrfconnect_generate_partition.py
6666
.. _ISO 8601 date format: https://www.iso.org/iso-8601-date-and-time-format.html
6767
.. _JSON Schema: https://json-schema.org/understanding-json-schema/reference
6868
.. _JSON Schema Validator: https://www.jsonschemavalidator.net
6969
.. _Join Bluetooth SIG: https://www.bluetooth.com/develop-with-bluetooth/join/
7070
.. _Join CSA: https://csa-iot.org/become-member/
7171
.. _Join Wi-Fi Alliance: https://www.wi-fi.org/membership
72-
.. _LogModule enumeration: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/src/lib/support/logging/Constants.h
72+
.. _LogModule enumeration: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/src/lib/support/logging/Constants.h
7373
.. _Matter Attestation Form: https://groups.csa-iot.org/wg/members-all/document/folder/2255
7474
.. _Matter Attestation of Security template: https://groups.csa-iot.org/wg/members-all/document/27432
7575
.. _Matter Cluster Editor app: https://docs.nordicsemi.com/bundle/swtools_docs/page/app/pc-nrfconnect-matter-cluster-editor/index.html
@@ -83,12 +83,12 @@
8383
.. _Matter Simple Setup: https://developer.amazon.com/docs/frustration-free-setup/matter-simple-setup-getting-started.html
8484
.. _Matter Simple Setup for Thread Overview: https://developer.amazon.com/docs/frustration-free-setup/matter-simple-setup-for-thread-overview.html
8585
.. _Matter factory data Kconfig options: https://nrfconnectdocs.nordicsemi.com/ncs/latest/kconfig/index.html#!CHIP_FACTORY_DATA
86-
.. _Matter nRF Connect Kconfig: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/config/nrfconnect/chip-module/Kconfig
87-
.. _Matter nRF Connect platform source files: https://github.com/nrfconnect/sdk-connectedhomeip/tree/5e2e5a02d6/src/platform/nrfconnect
88-
.. _Matter nRF Connect scripts: https://github.com/nrfconnect/sdk-connectedhomeip/tree/5e2e5a02d6/scripts/tools/nrfconnect
86+
.. _Matter nRF Connect Kconfig: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/config/nrfconnect/chip-module/Kconfig
87+
.. _Matter nRF Connect platform source files: https://github.com/nrfconnect/sdk-connectedhomeip/tree/|sdk-connectedhomeip-revision|/src/platform/nrfconnect
88+
.. _Matter nRF Connect scripts: https://github.com/nrfconnect/sdk-connectedhomeip/tree/|sdk-connectedhomeip-revision|/scripts/tools/nrfconnect
8989
.. _Nordic Developer Academy: https://academy.nordicsemi.com/
9090
.. _Online Power Profiler for Matter over Thread: https://devzone.nordicsemi.com/power/w/opp/16/online-power-profiler-for-matter-over-thread
91-
.. _OTA Provider for Linux: https://github.com/nrfconnect/sdk-connectedhomeip/blob/5e2e5a02d6/examples/ota-provider-app/linux
91+
.. _OTA Provider for Linux: https://github.com/nrfconnect/sdk-connectedhomeip/blob/|sdk-connectedhomeip-revision|/examples/ota-provider-app/linux
9292
.. _PHP JSON Schema: https://github.com/swaggest/php-json-schema
9393
.. _PICS Tool: https://picstool.csa-iot.org/
9494
.. _Platform Security Architecture (PSA): https://www.psacertified.org/what-is-psa-certified/
@@ -118,7 +118,7 @@
118118
.. _`Matter CIDs for nRF54L10`: https://docs.nordicsemi.com/bundle/comp_matrix_nrf54l10/page/COMP/nrf54l10/nrf54l10_matter_cids.html
119119
.. _`Matter CIDs for nRF5340`: https://docs.nordicsemi.com/bundle/comp_matrix_nrf5340/page/COMP/nrf5340/nrf5340_matter_cids.html
120120
.. _`Matter CIDs for nRF52840`: https://docs.nordicsemi.com/bundle/comp_matrix_nrf52840/page/COMP/nrf52840/nrf52840_matter_cids.html
121-
.. _other controller setups: https://github.com/nrfconnect/sdk-connectedhomeip/tree/5e2e5a02d6/src/controller
121+
.. _other controller setups: https://github.com/nrfconnect/sdk-connectedhomeip/tree/|sdk-connectedhomeip-revision|/src/controller
122122
.. _zcbor: https://github.com/NordicSemiconductor/zcbor
123123

124124
.. ### Matter module/platform doc links (sdk-connectedhomeip)

0 commit comments

Comments
 (0)