gromero commented on a change in pull request #9229:
URL: https://github.com/apache/tvm/pull/9229#discussion_r735742683



##########
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."

Review comment:
       Yep. Done in 
https://github.com/apache/tvm/pull/9229/commits/e2acda088729eaf540c9dd87608ab36165eec75c




-- 
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]


Reply via email to