diff --git a/scripts/build/build/target.py b/scripts/build/build/target.py index 63c278a233cb57..8411ff9432c2c7 100644 --- a/scripts/build/build/target.py +++ b/scripts/build/build/target.py @@ -95,6 +95,27 @@ def Accept(self, full_input: str): return True + def ToDict(self): + """Converts a TargetPart into a dictionary + """ + + result: Dict[str, str] = {} + result['name'] = self.name + + build_arguments: Dict[str, str] = {} + for key, value in self.build_arguments.items(): + build_arguments[key] = str(value) + + result['build_arguments'] = build_arguments + + if self.only_if_re is not None: + result['only_if_re'] = str(self.only_if_re.pattern) + + if self.except_if_re is not None: + result['except_if_re'] = str(self.except_if_re.pattern) + + return result + def _HasVariantPrefix(value: str, prefix: str): """Checks if the given value is or starts with "-". @@ -264,6 +285,44 @@ def HumanString(self): return result + def ToDict(self): + """Outputs a parseable description of the available variants + and modifiers: + + like: + + { + "name": "foo" + "shorthand": "foo-bar-baz[-m1]" + "parts": [ + { + "name": "foo", + "build_arguments": { + "board": "bar" + } + } + { + "name": "baz", + "build_arguments": { + "app": "foo.baz" + } + } + ], + "modifiers": [ + { + "name": "modifier1", + "m1": "True" + } + ] + } + """ + return { + 'name': self.name, + 'shorthand': self.HumanString(), + 'parts': [[part.ToDict() for part in target] for target in self.fixed_targets], + 'modifiers': [part.ToDict() for part in self.modifiers] + } + def AllVariants(self) -> Iterable[str]: """Returns all possible accepted variants by this target. diff --git a/scripts/build/build_examples.py b/scripts/build/build_examples.py index c70a5da7981d49..a52c9aa6f3e131 100755 --- a/scripts/build/build_examples.py +++ b/scripts/build/build_examples.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import logging import os import sys @@ -177,21 +178,29 @@ def cmd_generate(context): @main.command( 'targets', - help=('List the targets that would be generated/built given ' - 'the input arguments')) + help=('Lists the targets that can be used with the build and gen commands')) @click.option( - '--expand', - default=False, - is_flag=True, - help='Expand all possible targets rather than the shorthand string') + '--format', + default='summary', + type=click.Choice(['summary', 'expanded', 'json'], case_sensitive=False), + help=""" + summary - list of shorthand strings summarzing the available targets; + + expanded - list all possible targets rather than the shorthand string; + + json - a JSON representation of the available targets + """) @click.pass_context -def cmd_targets(context, expand): - for target in build.targets.BUILD_TARGETS: - if expand: +def cmd_targets(context, format): + if format == 'expanded': + for target in build.targets.BUILD_TARGETS: build.target.report_rejected_parts = False for s in target.AllVariants(): print(s) - else: + elif format == 'json': + print(json.dumps([target.ToDict() for target in build.targets.BUILD_TARGETS], indent=4)) + else: + for target in build.targets.BUILD_TARGETS: print(target.HumanString())