Skip to content

Commit fa4f8bb

Browse files
[nrf noup] Align full zap generation to changes in Matter Upstream
Now, the zap_generate Python script uses common targets to generate Matter Data Model ZAP and Code from zap_regen_all.py, located in the Matter SDK. This approach allows reusing of a common script to generate the whole Data Model and append a newly created cluster. Signed-off-by: Arkadiusz Balys <[email protected]> Restyled by isort
1 parent a3754b2 commit fa4f8bb

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

scripts/west/zap_generate.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44

55
import argparse
66
import os
7+
import shutil
78
import sys
8-
9-
from textwrap import dedent
109
from pathlib import Path
10+
from textwrap import dedent
1111

1212
from west import log
1313
from west.commands import CommandError, WestCommand
14+
from zap_common import DEFAULT_MATTER_PATH, ZapInstaller, existing_dir_path, existing_file_path, find_zap
1415

15-
from zap_common import existing_file_path, existing_dir_path, find_zap, ZapInstaller, DEFAULT_MATTER_PATH
16+
# fmt: off
17+
scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18+
sys.path.append(scripts_dir)
19+
sys.path.append(os.path.join(scripts_dir, 'tools'))
20+
from tools.zap_regen_all import JinjaCodegenTarget, ZAPGenerateTarget, ZapInput
21+
22+
# fmt: on
1623

1724

1825
class ZapGenerate(WestCommand):
@@ -39,6 +46,7 @@ def do_add_parser(self, parser_adder):
3946
default=DEFAULT_MATTER_PATH, help='Path to Matter SDK')
4047
parser.add_argument('-f', '--full', action='store_true', help='Generate full data model files')
4148
parser.add_argument('-k', '--keep-previous', action='store_true', help='Keep previously generated files')
49+
parser.add_argument('-j', '--zcl', action='store_true', help='Generate clusters from zcl.json')
4250
return parser
4351

4452
def build_command(self, zap_file_path, output_path, templates_path=None):
@@ -80,6 +88,10 @@ def do_run(self, args, unknown_args):
8088

8189
if not args.keep_previous:
8290
self.clear_generated_files(output_path)
91+
if args.full:
92+
log.inf(f"Clearing output directory: {output_path}")
93+
shutil.rmtree(output_path)
94+
output_path.mkdir(exist_ok=True)
8395

8496
# Generate source files
8597
self.check_call(self.build_command(zap_file_path, output_path, app_templates_path))
@@ -88,10 +100,56 @@ def do_run(self, args, unknown_args):
88100
self.check_call(self.build_command(zap_file_path, output_path))
89101

90102
if args.full:
91-
output_path = output_path / "app-common/zap-generated"
92-
output_path.mkdir(parents=True, exist_ok=True)
93-
94-
self.check_call(self.build_command(zap_file_path, output_path, templates_path))
103+
# Full build is about generating an apropertiate Matter data model files in a specific directory layout.
104+
# Currently, we must align to the following directory layout:
105+
# sample/
106+
# |_ src/
107+
# |_ default_zap/
108+
# |_ zcl.xml
109+
# |_ sample.zap
110+
# |_ sample.matter
111+
# |_ clusters/
112+
# |_ *All clusters*
113+
# |_ app-common
114+
# |_ zap-generated/
115+
# |_ attributes/
116+
# |_ ids/
117+
#
118+
# Generation of the full data model files consist of three steps:
119+
# 1. ZAPGenerateTarget generates "app-common/zap-generated" directory and a part of "clusters/" directory.
120+
# It generates Attrbutes.h/cpp, Events.h/cpp, Commands.h/cpp files for each cluster.
121+
# 2. JinjaCodegenTarget generates "clusters/" directory.
122+
# It generates "AttributeIds.h/cpp", "EventIds.h", "CommandIds.h" files for each cluster.
123+
# It generates also BUILD.gn files that are used to configure build system.
124+
#
125+
# Currently, we must call JinjaCodegenTarget twice:
126+
# - for all clusters using controller-clusters.matter file to generate all clusters defined in the Matter spec.
127+
# - for the sample's .matter file to generate the new data model files that are not defined in the Matter spec.
128+
#
129+
# To generate the full data model files, we utilizes classes defined in the matter/scripts/tools/zap_regen_all.py file.
130+
# These classes are supposed to be called from the matter root directory, so we must temporarily change the current working directory to the matter root directory.
131+
zcl_file = args.zcl or zap_file_path.parent / "zcl.json"
132+
zap_input = ZapInput.FromPropertiesJson(zcl_file)
133+
template = 'src/app/common/templates/templates.json'
134+
zap_output_dir = output_path / 'app-common' / 'zap-generated'
135+
codegen_output_dir = output_path / 'clusters'
136+
137+
# Temporarily change directory to matter_path so JinjaCodegenTarget and ZAPGenerateTarget can find their scripts
138+
original_cwd = os.getcwd()
139+
os.chdir(args.matter_path)
140+
try:
141+
ZAPGenerateTarget(zap_input, template=template, output_dir=zap_output_dir).generate()
142+
JinjaCodegenTarget(
143+
generator="cpp-sdk",
144+
idl_path="src/controller/data_model/controller-clusters.matter",
145+
output_directory=codegen_output_dir).generate()
146+
JinjaCodegenTarget(
147+
generator="cpp-sdk",
148+
idl_path=zap_file_path.with_suffix(".matter"),
149+
output_directory=codegen_output_dir).generate()
150+
finally:
151+
# Restore original working directory
152+
os.chdir(original_cwd)
95153

96154
log.inf(f"Done. Files generated in {output_path}")
97155

0 commit comments

Comments
 (0)