Skip to content

Commit

Permalink
Fix duplicate light app gen (project-chip#23293)
Browse files Browse the repository at this point in the history
* Special case NXP light app gen. Do not allow duplicate generation instructions

* Ran zap regen

* Restyle
  • Loading branch information
andy31415 authored and adbridge committed Nov 18, 2022
1 parent 203aa12 commit a1204c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
55 changes: 41 additions & 14 deletions scripts/tools/zap_regen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@
import sys
import subprocess
import logging
from dataclasses import dataclass

CHIP_ROOT_DIR = os.path.realpath(
os.path.join(os.path.dirname(__file__), '../..'))


@dataclass(eq=True, frozen=True)
class ZapDistinctOutput:
"""Defines the properties that determine if some output seems unique or
not, for the purposes of detecting codegen overlap.
Not perfect, since separate templates may use the same file names, but
better than nothing.
"""

input_template: str
output_directory: str


class ZAPGenerateTarget:
def __init__(self, zap_config, template=None, output_dir=None):
self.script = './scripts/tools/zap/generate.py'
Expand All @@ -38,6 +52,9 @@ def __init__(self, zap_config, template=None, output_dir=None):
else:
self.output_dir = None

def distinct_output(self):
return ZapDistinctOutput(input_template=self.template, output_directory=self.output_dir)

def log_command(self):
"""Log the command that will get run for this target
"""
Expand Down Expand Up @@ -135,10 +152,17 @@ def getGlobalTemplatesTargets():
logging.info("Found example %s (via %s)" %
(example_name, str(filepath)))

generate_subdir = example_name

# Special casing lighting app because separate folders
if example_name == "lighting-app":
if 'nxp' in str(filepath):
generate_subdir = f"{example_name}/nxp"

# The name zap-generated is to make includes clear by using
# a name like <zap-generated/foo.h>
output_dir = os.path.join(
'zzz_generated', example_name, 'zap-generated')
'zzz_generated', generate_subdir, 'zap-generated')
targets.append(ZAPGenerateTarget(filepath, output_dir=output_dir))

targets.append(ZAPGenerateTarget(
Expand All @@ -162,19 +186,6 @@ def getTestsTemplatesTargets(test_target):
}
}

# Place holder has apps within each build
for filepath in Path('./examples/placeholder').rglob('*.zap'):
example_name = filepath.as_posix()
example_name = example_name[example_name.index(
'apps/') + len('apps/'):]
example_name = example_name[:example_name.index('/')]

templates[example_name] = {
'zap': filepath,
'template': 'examples/placeholder/templates/templates.json',
'output_dir': os.path.join('zzz_generated', 'placeholder', example_name, 'zap-generated')
}

targets = []
for key, target in templates.items():
if test_target == 'all' or test_target == key:
Expand Down Expand Up @@ -223,6 +234,22 @@ def getTargets(type, test_target):
for target in targets:
target.log_command()

# validate that every target as a DISTINCT directory (we had bugs here
# for various examples duplicating zap files)
distinct_outputs = set()
for target in targets:
o = target.distinct_output()

if o in distinct_outputs:
logging.error("Same output %r:" % o)
for t in targets:
if t.distinct_output() == o:
logging.error(" %s" % t.zap_config)

raise Exception("Duplicate/overlapping output directory: %r" % o)

distinct_outputs.add(o)

return targets


Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1204c2

Please sign in to comment.