diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2022-03-16 13:33:38 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2022-03-17 00:08:06 +0000 |
commit | 73d2fc626440800e4e92d93b3bd88f0362fa50a2 (patch) | |
tree | 7b3b1fd77e46b045a6a4ab11d859c44bd1ab930f /zephyr/zmake/zmake/project.py | |
parent | 5081c7ce7527c977b8eca37fb4a561863d91b75b (diff) | |
download | chrome-ec-73d2fc626440800e4e92d93b3bd88f0362fa50a2.tar.gz |
zephyr: zmake: Add a syntax to make simple variants of a build
Add a BUILD.py syntax to copy another project and make simple
modifications.
BUG=b:224998797
BRANCH=none
TEST=provided unit test passes
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Change-Id: I87f0fe6b0e02c3ac34bec6de09d38e620fc293e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3530957
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
Diffstat (limited to 'zephyr/zmake/zmake/project.py')
-rw-r--r-- | zephyr/zmake/zmake/project.py | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/zephyr/zmake/zmake/project.py b/zephyr/zmake/zmake/project.py index b2c36cbb33..b2232bb263 100644 --- a/zephyr/zmake/zmake/project.py +++ b/zephyr/zmake/zmake/project.py @@ -6,7 +6,7 @@ import dataclasses import logging import pathlib -from typing import Dict, List +from typing import Callable, Dict, List import zmake.build_config as build_config import zmake.configlib as configlib @@ -154,6 +154,41 @@ class Project: ) +@dataclasses.dataclass +class ProjectRegistrationHandler: + """Return value of register_project. + + This is intended to be used to create simple variants of a project + like so:: + + brd = register_project(project_name="brd", ...) + brd_changed = brd.variant(project_name="brd-changed", ...) + brd_changed_again = brd_changed.variant(project_name="brd-changed-again", ...) + """ + + base_config: ProjectConfig + register_func: Callable[[], "ProjectRegistrationHandler"] + + def variant(self, **kwargs) -> "ProjectRegistrationHandler": + """Register a new variant based on the base config. + + Args: + kwargs: Any project config changes. Note lists will be + concatenated. + + Returns: + Another ProjectRegistrationHandler. + """ + new_config = dataclasses.asdict(self.base_config) + for key, value in kwargs.items(): + if isinstance(value, list): + new_config[key] = [*new_config[key], *value] + else: + new_config[key] = value + + return self.register_func(**new_config) + + def load_config_file(path) -> List[Project]: """Load a BUILD.py config file and create associated projects. @@ -165,8 +200,13 @@ def load_config_file(path) -> List[Project]: """ projects: List[Project] = [] - def register_project(**kwargs): - projects.append(Project(ProjectConfig(**kwargs))) + def register_project(**kwargs) -> ProjectRegistrationHandler: + config = ProjectConfig(**kwargs) + projects.append(Project(config)) + return ProjectRegistrationHandler( + base_config=config, + register_func=register_project, + ) # The Python environment passed to the config file. config_globals = { |