Skip to content

Commit

Permalink
move cmake args to generate project
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdadh committed Jul 28, 2022
1 parent ae3e2a8 commit f974ff9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack")

set(QEMU_PIPE "\${QEMU_PIPE}") # QEMU_PIPE is set by the calling TVM instance.

set(ENABLE_CMSIS <ENABLE_CMSIS>)
<CMAKE_ARGS>

find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(microtvm_autogenerated_project)
Expand Down
55 changes: 30 additions & 25 deletions apps/microtvm/zephyr/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ def _get_nrf_device_args(options):
),
server.ProjectOption(
"verbose",
optional=["build"],
optional=["generate_project"],
type="bool",
help="Run build with verbose output.",
),
server.ProjectOption(
"west_cmd",
optional=["build"],
optional=["generate_project"],
default=WEST_CMD,
type="str",
help=(
Expand All @@ -292,14 +292,14 @@ def _get_nrf_device_args(options):
server.ProjectOption(
"zephyr_base",
required=(["generate_project", "open_transport"] if not ZEPHYR_BASE else None),
optional=(["generate_project", "open_transport", "build"] if ZEPHYR_BASE else ["build"]),
optional=(["generate_project", "open_transport"] if ZEPHYR_BASE else ["build"]),
default=ZEPHYR_BASE,
type="str",
help="Path to the zephyr base directory.",
),
server.ProjectOption(
"zephyr_board",
required=["generate_project", "build", "flash", "open_transport"],
required=["generate_project", "flash", "open_transport"],
choices=list(BOARD_PROPERTIES),
type="str",
help="Name of the Zephyr board to build for.",
Expand Down Expand Up @@ -419,7 +419,7 @@ def _create_prj_conf(self, project_dir, options):
f.write("\n")

API_SERVER_CRT_LIBS_TOKEN = "<API_SERVER_CRT_LIBS>"
ENABLE_CMSIS_TOKEN = "<ENABLE_CMSIS>"
CMAKE_ARGS = "<CMAKE_ARGS>"

CRT_LIBS_BY_PROJECT_TYPE = {
"host_driven": "microtvm_rpc_server microtvm_rpc_common aot_executor_module aot_executor common",
Expand Down Expand Up @@ -457,6 +457,28 @@ def _cmsis_required(self, project_path: Union[str, pathlib.Path]) -> bool:
return True
return False

def _generate_cmake_args(self, mlf_extracted_path, options) -> str:
cmake_args = "\n# cmake args\n"
if options.get("verbose"):
cmake_args += "set(CMAKE_VERBOSE_MAKEFILE TRUE)\n"

if options.get("zephyr_base"):
cmake_args += f"set(ZEPHYR_BASE {options['zephyr_base']})\n"

if options.get("west_cmd"):
cmake_args += f"set(WEST {options['west_cmd']})\n"

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

cmake_args += f"set(BOARD {options['zephyr_board']})\n"

enable_cmsis = self._cmsis_required(mlf_extracted_path)
cmake_args += f"set(ENABLE_CMSIS {str(enable_cmsis).upper()})\n"

return cmake_args

def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options):
# Check Zephyr version
version = self._get_platform_version(get_zephyr_base(options))
Expand Down Expand Up @@ -510,9 +532,8 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
crt_libs = self.CRT_LIBS_BY_PROJECT_TYPE[options["project_type"]]
line = line.replace("<API_SERVER_CRT_LIBS>", crt_libs)

if self.ENABLE_CMSIS_TOKEN in line:
enable_cmsis = self._cmsis_required(extract_path)
line = line.replace(self.ENABLE_CMSIS_TOKEN, str(enable_cmsis).upper())
if self.CMAKE_ARGS in line:
line = self._generate_cmake_args(extract_path, options)

cmake_f.write(line)

Expand Down Expand Up @@ -542,23 +563,7 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
def build(self, options):
BUILD_DIR.mkdir()

cmake_args = ["cmake", ".."]
if options.get("verbose"):
cmake_args.append("-DCMAKE_VERBOSE_MAKEFILE:BOOL=TRUE")

if options.get("zephyr_base"):
cmake_args.append(f"-DZEPHYR_BASE:STRING={options['zephyr_base']}")

if options.get("west_cmd"):
cmake_args.append(f"-DWEST={options['west_cmd']}")

if self._is_qemu(options):
# Some boards support more than one emulator, so ensure QEMU is set.
cmake_args.append(f"-DEMU_PLATFORM=qemu")

cmake_args.append(f"-DBOARD:STRING={options['zephyr_board']}")

check_call(cmake_args, cwd=BUILD_DIR)
check_call(["cmake", ".."], cwd=BUILD_DIR)

args = ["make", "-j2"]
if options.get("verbose"):
Expand Down
10 changes: 5 additions & 5 deletions gallery/how_to/work_with_microtvm/micro_tvmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ tvmc compile magic_wand.tflite \
#
# To generate a Zephyr project we use TVM Micro subcommand ``create``. We pass the MLF format and the path
# for the project to ``create`` subcommand along with project options. Project options for each
# platform (Zephyr/Arduino) are defined in their Project API server file. To generate Zephyr project, run:
# platform (Zephyr/Arduino) are defined in their Project API server file. To build
# Zephyr project for a different Zephyr board, change ``zephyr_board`` project option.
# To generate Zephyr project, run:
#
# bash
tvmc micro create \
Expand Down Expand Up @@ -151,11 +153,9 @@ tvmc micro create \
# bash
tvmc micro build \
project \
zephyr \
--project-option zephyr_board=qemu_x86
zephyr
# bash
# This will build the project in ``project`` directory and generates binary files under ``project/build``. To build
# Zephyr project for a different Zephyr board, change ``zephyr_board`` project option.
# This will build the project in ``project`` directory and generates binary files under ``project/build``.
#
# Next, we flash the Zephyr binary file to Zephyr device. For ``qemu_x86`` Zephyr board this step does not
# actually perform any action since QEMU will be used, however you need this step for physical hardware.
Expand Down
8 changes: 2 additions & 6 deletions tests/micro/common/test_tvmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def test_tvmc_model_build_only(platform, board, output_dir):
cmd_result = _run_tvmc(create_project_cmd)
assert cmd_result == 0, "tvmc micro failed in step: create-project"

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

Expand Down Expand Up @@ -174,9 +172,7 @@ def test_tvmc_model_run(platform, board, output_dir):
cmd_result = _run_tvmc(create_project_cmd)
assert cmd_result == 0, "tvmc micro failed in step: create-project"

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

cmd_result = _run_tvmc(
Expand Down

0 comments on commit f974ff9

Please sign in to comment.