gromero commented on a change in pull request #9229: URL: https://github.com/apache/tvm/pull/9229#discussion_r735742140
########## File path: python/tvm/driver/tvmc/micro.py ########## @@ -0,0 +1,278 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import os +import shutil +from pathlib import Path +from collections import defaultdict +import argparse + +import tvm.micro.project as project +from tvm.micro.project_api.server import ServerError +from .main import register_parser +from .common import ( + TVMCException, + get_project_options, + get_options, + check_options, + check_options_choices, +) +from .fmtopt import format_option + + +TVM_HOME = os.getenv("TVM_HOME") + + +ZEPHYR_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/zephyr/template_project" +ARDUINO_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/arduino/template_project" + + +TEMPLATES = { + "zephyr": ZEPHYR_TEMPLATE_DIR, + "arduino": ARDUINO_TEMPLATE_DIR, +} + + +@register_parser +def add_micro_parser(subparsers, main_parser): + micro = subparsers.add_parser("micro", help="select micro context.") + micro.set_defaults(func=drive_micro) + + micro_parser = micro.add_subparsers(title="subcommands") + # Selecting a subcommand under 'micro' is mandatory + micro_parser.required = True + micro_parser.dest = "subcommand" # options available to select + + # 'create_project' subcommand + create_project_parser = micro_parser.add_parser( + "create-project", help="create a project template of a given type or given a template dir." + ) + create_project_parser.set_defaults(subcommand_handler=create_project_handler) + create_project_parser.add_argument( + "PROJECT_DIR", + help="Project dir where the new project based on the template dir will be created.", + ) + create_project_parser.add_argument("MLF", help="MLF .tar archive.") + create_project_parser.add_argument( + "-f", + "--force", + action="store_true", + help="force project creating even if the specified PROJECT_DIR already exists.", + ) + + # 'build' subcommand + build_parser = micro_parser.add_parser( + "build", + help="build a project dir, generally creating an image to be flashed, e.g. zephyr.elf.", + ) + build_parser.set_defaults(subcommand_handler=build_handler) + build_parser.add_argument("PROJECT_DIR", help="Project dir to build.") + build_parser.add_argument("-f", "--force", action="store_true", help="Force rebuild.") + + # 'flash' subcommand + flash_parser = micro_parser.add_parser( + "flash", help="flash the built image on a given micro target." + ) + flash_parser.set_defaults(subcommand_handler=flash_handler) + flash_parser.add_argument("PROJECT_DIR", help="Project dir with a image built.") + + # TODO(gromero): list and select serial when multiple devices exist + # It's not clear yet if the device list should be determined by TVMC or returned by the Project API + + # For each platform add arguments detected automatically using Project API info query. + + # Create subparsers for the platforms under 'create-project', 'build', and 'flash' subcommands first. + help_msg = "you must selected a platform from the list. You can pass '-h' for a selected platform to list its options." + create_project_platforms_parser = create_project_parser.add_subparsers( + title="platforms", help=help_msg, dest="platform" + ) + build_platforms_parser = build_parser.add_subparsers( + title="platforms", help=help_msg, dest="platform" + ) + flash_platforms_parser = flash_parser.add_subparsers( + title="platforms", help=help_msg, dest="platform" + ) + + # Map used to map a API method to proper CLI parsers and handlers. + # API method parser handler smbcmd domain + subcmds = { + "generate_project": [create_project_platforms_parser, create_project_handler], + "build": [build_platforms_parser, build_handler], + "flash": [flash_platforms_parser, flash_handler], + } + + # Helper to add a platform to a subcmd parser. + def _add_parser(parser, platform): + platform_name = platform[0].upper() + platform[1:] + " platform" + o = parser.add_parser(platform, add_help=False, help=f"select {platform_name}.") + o.set_defaults(platform=platform) + return o + + parser_by_subcmd = {} + for subcmd, o in subcmds.items(): + parser_by_platform = {} + o[0].required = True # Selecting a platform/template is mandatory + for platform in TEMPLATES: + new_parser = _add_parser(o[0], platform) + parser_by_platform[platform] = new_parser + + new_parser = o[0].add_parser("template", add_help=False, help="select an adhoc template.") + new_parser.add_argument( + "-d", metavar="TEMPLATE_DIR", required=True, help="project API template directory." + ) + new_parser.set_defaults(platform="template") + parser_by_platform["template"] = new_parser + + parser_by_subcmd[subcmd] = parser_by_platform + + known_args, unkown_args = main_parser.parse_known_args() + + try: + subcmd = known_args.subcommand + platform = known_args.platform + except AttributeError: + # No subcommand or platform, so not need to augment the parser. + return + + # Augment parser with project options + + if platform == "template": + # adhoc template + template_dir = known_args.d + else: + # default template + template_dir = TEMPLATES[platform] + + template = project.TemplateProject.from_directory(template_dir) + template_info = template.info() + + options_by_method = get_project_options(template_info) + + # TODO(gromero): refactor to remove this map. + subcmd_to_method = { + "create-project": "generate_project", + "build": "build", + "flash": "flash", + } + + method = subcmd_to_method[subcmd] + parser_by_subcmd_n_platform = parser_by_subcmd[method][platform] + _, handler = subcmds[method] + + parser_by_subcmd_n_platform.formatter_class = ( + argparse.RawTextHelpFormatter + ) # Set raw help text so help_text format works + parser_by_subcmd_n_platform.set_defaults( + subcommand_handler=handler, + valid_options=options_by_method[method], + template_dir=template_dir, + ) + + parser_by_subcmd_n_platform.add_argument( + "--list-options", + action="help", + help="show all options/values for selected platforms/template.", + ) + + required = any([opt["required"] for opt in options_by_method[method]]) + nargs = "+" if required else "*" + + help_text_by_option = [opt["help_text"] for opt in options_by_method[method]] + help_text = "\n\n".join(help_text_by_option) + "\n\n" + + # TODO(gromero): Experiment with required=required below + parser_by_subcmd_n_platform.add_argument( + "-o", required=True, metavar="OPTION=VALUE", nargs=nargs, help=help_text Review comment: Sure, it's better as you suggested. Done in https://github.com/apache/tvm/pull/9229/commits/16635d2e7399cfc03dc58ee0a429c02891d9b239 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
