Skip to content

Commit

Permalink
[microTVM][CRT]Move Makefile to CMake to be cross-platform compatible (
Browse files Browse the repository at this point in the history
…#14013)

This PR converts CRT Makefile to a CMakelist file to be cross platform compatible.
  • Loading branch information
mehrdadh authored Feb 21, 2023
1 parent 5ec33bb commit 347d79c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 120 deletions.
2 changes: 1 addition & 1 deletion cmake/modules/Micro.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if(USE_MICRO)
APPEND
PLATFORM_FILE_COPY_JOBS
"src/runtime/crt/host microtvm_api_server.py -> crt"
"src/runtime/crt/host Makefile.template -> crt"
"src/runtime/crt/host CMakeLists.txt.template -> crt"
"src/runtime/crt/host **.cc -> crt/src"
)
else()
Expand Down
59 changes: 59 additions & 0 deletions src/runtime/crt/host/CMakeLists.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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.

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.18)
set(CMAKE_CXX_STANDARD 11)

project(crt_autogenerated_project C CXX)
add_executable(main)

set(CRT_LIB_BASE crt/src/runtime/crt)
set(CRT_LIBS microtvm_rpc_server
microtvm_rpc_common
aot_executor_module
aot_executor
graph_executor_module
graph_executor
common
memory
)

# Build CRT libraries
foreach(crt_lib_name ${CRT_LIBS})
add_library(${crt_lib_name})
file(GLOB_RECURSE crt_lib_srcs ${CRT_LIB_BASE}/${crt_lib_name}/*.c ${CRT_LIB_BASE}/${crt_lib_name}/*.cc)
target_sources(${crt_lib_name} PRIVATE ${crt_lib_srcs})
target_include_directories(${crt_lib_name} PRIVATE crt_config crt/include)
target_compile_definitions(${crt_lib_name} PRIVATE -DTVM_HOST_USE_GRAPH_EXECUTOR_MODULE)
target_link_libraries(main PRIVATE ${crt_lib_name})
endforeach(crt_lib_name ${CRT_LIBS})

# Build model files
add_library(tvm_model)
file(GLOB_RECURSE tvm_model_srcs model/codegen/host/src/*.c model/codegen/host/lib/*.o)
target_sources(tvm_model PRIVATE ${tvm_model_srcs})
target_include_directories(tvm_model PRIVATE ${CMAKE_SOURCE_DIR}/include crt_config crt/include)
target_compile_options(tvm_model PRIVATE -Wno-error=unused-variable -Wno-error=missing-braces -Wno-error=unused-const-variable -Wno-unused-variable)
set_target_properties(tvm_model PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(main PRIVATE tvm_model)

file(GLOB_RECURSE app_srcs src/**.cc)
target_sources(main PRIVATE ${app_srcs} ${cmsis_lib_srcs})
target_compile_definitions(main PRIVATE -DTVM_HOST_USE_GRAPH_EXECUTOR_MODULE)
target_include_directories(main PRIVATE crt_config include ${CMAKE_SOURCE_DIR}/include crt/include)
87 changes: 0 additions & 87 deletions src/runtime/crt/host/Makefile.template

This file was deleted.

58 changes: 27 additions & 31 deletions src/runtime/crt/host/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
# Used this size to pass most CRT tests in TVM.
WORKSPACE_SIZE_BYTES = 2 * 1024 * 1024

MAKEFILE_FILENAME = "Makefile"
CMAKEFILE_FILENAME = "CMakeLists.txt"

# The build target given to make
BUILD_TARGET = "build/main"

class Handler(server.ProjectAPIHandler):

class Handler(server.ProjectAPIHandler):
BUILD_TARGET = "build/main"

def __init__(self):
Expand Down Expand Up @@ -79,29 +81,25 @@ def server_info_query(self, tvm_version):
# These files and directories will be recursively copied into generated projects from the CRT.
CRT_COPY_ITEMS = ("include", "Makefile", "src")

# The build target given to make
BUILD_TARGET = "build/main"

def _populate_makefile(
def _populate_cmake(
self,
makefile_template_path: pathlib.Path,
makefile_path: pathlib.Path,
cmakefile_template_path: pathlib.Path,
cmakefile_path: pathlib.Path,
memory_size: int,
verbose: bool,
):
"""Generate Makefile from template."""
flags = {
"TVM_WORKSPACE_SIZE_BYTES": str(memory_size),
}
"""Generate CMakeList file from template."""

regex = re.compile(r"([A-Z_]+) := (<[A-Z_]+>)")
with open(makefile_path, "w") as makefile_f:
with open(makefile_template_path, "r") as makefile_template_f:
for line in makefile_template_f:
m = regex.match(line)
if m:
var, token = m.groups()
line = line.replace(token, flags[var])
makefile_f.write(line)
with open(cmakefile_path, "w") as cmakefile_f:
with open(cmakefile_template_path, "r") as cmakefile_template_f:
for line in cmakefile_template_f:
cmakefile_f.write(line)
cmakefile_f.write(
f"target_compile_definitions(main PUBLIC -DTVM_WORKSPACE_SIZE_BYTES={memory_size})\n"
)
if verbose:
cmakefile_f.write(f"set(CMAKE_VERBOSE_MAKEFILE TRUE)\n")

def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options):
# Make project directory.
Expand Down Expand Up @@ -134,11 +132,12 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
else:
shutil.copy2(src_path, dst_path)

# Populate Makefile
self._populate_makefile(
current_dir / f"{MAKEFILE_FILENAME}.template",
project_dir / MAKEFILE_FILENAME,
# Populate CMake file
self._populate_cmake(
current_dir / f"{CMAKEFILE_FILENAME}.template",
project_dir / CMAKEFILE_FILENAME,
options.get("workspace_size_bytes", WORKSPACE_SIZE_BYTES),
options.get("verbose"),
)

# Populate crt-config.h
Expand All @@ -162,13 +161,10 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
)

def build(self, options):
args = ["make"]
if options.get("verbose"):
args.append("VERBOSE=1")

args.append(self.BUILD_TARGET)

subprocess.check_call(args, cwd=PROJECT_DIR)
build_dir = PROJECT_DIR / "build"
build_dir.mkdir()
subprocess.check_call(["cmake", ".."], cwd=build_dir)
subprocess.check_call(["make"], cwd=build_dir)

def flash(self, options):
pass # Flashing does nothing on host.
Expand Down
2 changes: 1 addition & 1 deletion tests/lint/check_file_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"apps/microtvm/arduino/template_project/Makefile.template",
# microTVM CRT
"src/runtime/crt/crt_config.h.template",
"src/runtime/crt/host/Makefile.template",
"src/runtime/crt/host/CMakeLists.txt.template",
# microTVM Virtual Machines
"apps/microtvm/poetry.lock",
"apps/microtvm/reference-vm/Vagrantfile",
Expand Down

0 comments on commit 347d79c

Please sign in to comment.