summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2021-11-30 12:28:04 -0700
committerCommit Bot <commit-bot@chromium.org>2021-11-30 21:40:04 +0000
commit3119da651b167a0a0bce4ce31c9eefdb9c18d2f8 (patch)
tree3d0b0a56ded7f60998c140504bc4a45e1bec17df
parentb610600e9b9acc23d77ccdbcce448fd2d59ae55d (diff)
downloadchrome-ec-3119da651b167a0a0bce4ce31c9eefdb9c18d2f8.tar.gz
zephyr: zmake: Add a list-projects subcommand
Prior to converting to the BUILD.py format, it was possible to find all projects Zmake supports simply by searching for zmake.yaml files (e.g., with "find -name zmake.yaml"). Now that Zmake has a more complex project config format, and a single BUILD.py file may expose multiple projects, we need to expose on the CLI a way to print out all projects. BUG=b:193815337 BRANCH=none TEST=Provided unit tests pass Signed-off-by: Jack Rosenthal <jrosenth@chromium.org> Change-Id: Ie726f35f9044b3d5baf782e5e22c9405c35ce1f7 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3309360 Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r--zephyr/zmake/tests/test_zmake.py67
-rw-r--r--zephyr/zmake/zmake/__main__.py19
-rw-r--r--zephyr/zmake/zmake/zmake.py16
3 files changed, 102 insertions, 0 deletions
diff --git a/zephyr/zmake/tests/test_zmake.py b/zephyr/zmake/tests/test_zmake.py
index f735d942ed..83862ce65d 100644
--- a/zephyr/zmake/tests/test_zmake.py
+++ b/zephyr/zmake/tests/test_zmake.py
@@ -13,6 +13,7 @@ import unittest
import unittest.mock as mock
from unittest.mock import patch
+import pytest
from testfixtures import LogCapture
import zmake.build_config
@@ -228,5 +229,71 @@ class TestFilters(unittest.TestCase):
assert "devicetree error: 'adc' is marked as required" in list(dt_errs)[0]
+@pytest.mark.parametrize(
+ ["project_names", "format", "search_dir", "expected_output"],
+ [
+ (
+ ["link", "samus"],
+ "{config.project_name}\n",
+ None,
+ "link\nsamus\n",
+ ),
+ (
+ ["link", "samus"],
+ "{config.project_name}\n",
+ pathlib.Path("/foo/bar"),
+ "link\nsamus\n",
+ ),
+ (
+ [],
+ "{config.project_name}\n",
+ None,
+ "",
+ ),
+ (
+ ["link"],
+ "",
+ None,
+ "",
+ ),
+ (
+ ["link"],
+ "{config.zephyr_board}\n",
+ None,
+ "some_board\n",
+ ),
+ (
+ ["link"],
+ "{config.project_name} is_test={config.is_test}\n",
+ None,
+ "link is_test=False\n",
+ ),
+ ],
+)
+def test_list_projects(project_names, format, search_dir, expected_output, capsys):
+ """Test listing projects with default directory."""
+ fake_projects = {
+ name: zmake.project.Project(
+ zmake.project.ProjectConfig(
+ project_name=name,
+ zephyr_board="some_board",
+ supported_toolchains=["coreboot-sdk"],
+ output_packer=zmake.output_packers.RawBinPacker,
+ )
+ )
+ for name in project_names
+ }
+ zmk = zm.Zmake()
+ with mock.patch(
+ "zmake.project.find_projects",
+ autospec=True,
+ return_value=fake_projects,
+ ):
+ zmk.list_projects(format=format, search_dir=search_dir)
+
+ captured = capsys.readouterr()
+ assert captured.out == expected_output
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/zephyr/zmake/zmake/__main__.py b/zephyr/zmake/zmake/__main__.py
index aef897d1d3..558ca23555 100644
--- a/zephyr/zmake/zmake/__main__.py
+++ b/zephyr/zmake/zmake/__main__.py
@@ -220,6 +220,25 @@ def main(argv=None):
help="Exit with code 2 if warnings are detected",
)
+ list_projects = sub.add_parser(
+ "list-projects",
+ help="List projects known to zmake.",
+ )
+ list_projects.add_argument(
+ "--format",
+ default="{config.project_name}\n",
+ help=(
+ "Output format to print projects (str.format(config=project.config) is "
+ "called on this for each project)."
+ ),
+ )
+ list_projects.add_argument(
+ "search_dir",
+ type=pathlib.Path,
+ nargs="?",
+ help="Optional directory to search for BUILD.py files in.",
+ )
+
test = sub.add_parser("test")
test.add_argument(
"build_dir",
diff --git a/zephyr/zmake/zmake/zmake.py b/zephyr/zmake/zmake/zmake.py
index d2559a9270..713c6773e3 100644
--- a/zephyr/zmake/zmake/zmake.py
+++ b/zephyr/zmake/zmake/zmake.py
@@ -810,3 +810,19 @@ class Zmake:
if proc.wait():
raise OSError(get_process_failure_msg(proc))
return 0
+
+ def list_projects(self, format, search_dir):
+ """List project names known to zmake on stdout.
+
+ Args:
+ format: The formatting string to print projects with.
+ search_dir: Directory to start the search for
+ BUILD.py files at.
+ """
+ if not search_dir:
+ search_dir = self.module_paths["ec"] / "zephyr"
+
+ for project in zmake.project.find_projects(search_dir).values():
+ print(format.format(config=project.config), end="")
+
+ return 0