Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[microTVM][Zephyr][projectAPI] Minimize project build commands #12209

Merged
merged 6 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions apps/microtvm/zephyr/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

BOARDS = API_SERVER_DIR / "boards.json"

CMAKELIST_FILENAME = "CMakeLists.txt"

# Used to check Zephyr version installed on the host.
# We only check two levels of the version.
ZEPHYR_VERSION = 2.7
Expand Down Expand Up @@ -299,7 +301,7 @@ def _get_nrf_device_args(options):
),
server.ProjectOption(
"zephyr_board",
required=["generate_project", "flash", "open_transport"],
required=["generate_project"],
choices=list(BOARD_PROPERTIES),
type="str",
help="Name of the Zephyr board to build for.",
Expand Down Expand Up @@ -468,7 +470,7 @@ def _generate_cmake_args(self, mlf_extracted_path, options) -> str:
if options.get("west_cmd"):
cmake_args += f"set(WEST {options['west_cmd']})\n"

if self._is_qemu(options):
if self._is_qemu(options["zephyr_board"]):
# Some boards support more than one emulator, so ensure QEMU is set.
cmake_args += f"set(EMU_PLATFORM qemu)\n"

Expand Down Expand Up @@ -510,7 +512,7 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
os.makedirs(extract_path)
tf.extractall(path=extract_path)

if self._is_qemu(options):
if self._is_qemu(options["zephyr_board"]):
shutil.copytree(API_SERVER_DIR / "qemu-hack", project_dir / "qemu-hack")

# Populate CRT.
Expand All @@ -525,8 +527,8 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
shutil.copy2(src_path, dst_path)

# Populate Makefile.
with open(project_dir / "CMakeLists.txt", "w") as cmake_f:
with open(API_SERVER_DIR / "CMakeLists.txt.template", "r") as cmake_template_f:
with open(project_dir / CMAKELIST_FILENAME, "w") as cmake_f:
with open(API_SERVER_DIR / f"{CMAKELIST_FILENAME}.template", "r") as cmake_template_f:
for line in cmake_template_f:
if self.API_SERVER_CRT_LIBS_TOKEN in line:
crt_libs = self.CRT_LIBS_BY_PROJECT_TYPE[options["project_type"]]
Expand Down Expand Up @@ -576,22 +578,33 @@ def build(self, options):
_KNOWN_QEMU_ZEPHYR_BOARDS = ("mps2_an521", "mps3_an547")

@classmethod
def _is_qemu(cls, options):
return (
"qemu" in options["zephyr_board"]
or options["zephyr_board"] in cls._KNOWN_QEMU_ZEPHYR_BOARDS
)
def _is_qemu(cls, board: str) -> bool:
return "qemu" in board or board in cls._KNOWN_QEMU_ZEPHYR_BOARDS

@classmethod
def _has_fpu(cls, zephyr_board):
fpu_boards = [name for name, board in BOARD_PROPERTIES.items() if board["fpu"]]
return zephyr_board in fpu_boards

@classmethod
def _find_board_from_cmake_file(cls) -> str:
with open(API_SERVER_DIR / CMAKELIST_FILENAME) as cmake_f:
for line in cmake_f:
if line.startswith("set(BOARD"):
zephyr_board = line.strip("\n").strip("set(BOARD ").strip(")")
break

if not zephyr_board:
mehrdadh marked this conversation as resolved.
Show resolved Hide resolved
raise RuntimeError(
f"Zephyr Board is not found in the {API_SERVER_DIR / CMAKELIST_FILENAME}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this message be changed to "No Zephyr board defined in the {API_SERVER_DIR / CMAKELIST_FILENAME}" ? Or "set in the ..." ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

)
return zephyr_board

def flash(self, options):
if self._is_qemu(options):
return # NOTE: qemu requires no flash step--it is launched from open_transport.
zephyr_board = self._find_board_from_cmake_file()

zephyr_board = options["zephyr_board"]
if self._is_qemu(zephyr_board):
return # NOTE: qemu requires no flash step--it is launched from open_transport.

# The nRF5340DK requires an additional `nrfjprog --recover` before each flash cycle.
# This is because readback protection is enabled by default when this device is flashed.
Expand All @@ -606,7 +619,9 @@ def flash(self, options):
check_call(["make", "flash"], cwd=API_SERVER_DIR / "build")

def open_transport(self, options):
if self._is_qemu(options):
zephyr_board = self._find_board_from_cmake_file()

if self._is_qemu(zephyr_board):
transport = ZephyrQemuTransport(options)
else:
transport = ZephyrSerialTransport(options)
Expand Down
4 changes: 1 addition & 3 deletions gallery/how_to/work_with_microtvm/micro_tvmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ tvmc micro build \
# bash
tvmc micro flash \
project \
zephyr \
--project-option zephyr_board=qemu_x86
zephyr
# bash

############################################################
Expand All @@ -181,7 +180,6 @@ tvmc micro flash \
tvmc run \
--device micro \
project \
--project-option zephyr_board=qemu_x86 \
--fill-mode ones \
--print-top 4
# bash
Expand Down
6 changes: 1 addition & 5 deletions tests/micro/common/test_tvmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ def test_tvmc_model_run(platform, board, output_dir):
cmd_result = _run_tvmc(["micro", "build", project_dir, platform])
assert cmd_result == 0, "tvmc micro failed in step: build"

cmd_result = _run_tvmc(
["micro", "flash", project_dir, platform, "--project-option", f"{platform}_board={board}"]
)
cmd_result = _run_tvmc(["micro", "flash", project_dir, platform])
assert cmd_result == 0, "tvmc micro failed in step: flash"

cmd_result = _run_tvmc(
Expand All @@ -186,8 +184,6 @@ def test_tvmc_model_run(platform, board, output_dir):
"--device",
"micro",
project_dir,
"--project-option",
f"{platform}_board={board}",
"--fill-mode",
"random",
]
Expand Down