This is an automated email from the ASF dual-hosted git repository.
mehrdadh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new 347d79cbcf [microTVM][CRT]Move Makefile to CMake to be cross-platform
compatible (#14013)
347d79cbcf is described below
commit 347d79cbcf4999e8dbdf227679e933dd6efb0fc2
Author: Mehrdad Hessar <[email protected]>
AuthorDate: Tue Feb 21 10:45:42 2023 -0800
[microTVM][CRT]Move Makefile to CMake to be cross-platform compatible
(#14013)
This PR converts CRT Makefile to a CMakelist file to be cross platform
compatible.
---
cmake/modules/Micro.cmake | 2 +-
src/runtime/crt/host/CMakeLists.txt.template | 59 +++++++++++++++++++
src/runtime/crt/host/Makefile.template | 87 ----------------------------
src/runtime/crt/host/microtvm_api_server.py | 58 +++++++++----------
tests/lint/check_file_type.py | 2 +-
5 files changed, 88 insertions(+), 120 deletions(-)
diff --git a/cmake/modules/Micro.cmake b/cmake/modules/Micro.cmake
index da82cc9f45..d887486d2e 100644
--- a/cmake/modules/Micro.cmake
+++ b/cmake/modules/Micro.cmake
@@ -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()
diff --git a/src/runtime/crt/host/CMakeLists.txt.template
b/src/runtime/crt/host/CMakeLists.txt.template
new file mode 100644
index 0000000000..be0bce8551
--- /dev/null
+++ b/src/runtime/crt/host/CMakeLists.txt.template
@@ -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)
diff --git a/src/runtime/crt/host/Makefile.template
b/src/runtime/crt/host/Makefile.template
deleted file mode 100644
index 526b17deb7..0000000000
--- a/src/runtime/crt/host/Makefile.template
+++ /dev/null
@@ -1,87 +0,0 @@
-# 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.
-
-INCLUDES ?= -isystem crt/include -Icrt_config
-TVM_WORKSPACE_SIZE_BYTES := <TVM_WORKSPACE_SIZE_BYTES>
-CFLAGS ?= -Werror -Wall
-CXXFLAGS ?= -Werror -Wall -std=c++11 -DTVM_HOST_USE_GRAPH_EXECUTOR_MODULE
-DTVM_WORKSPACE_SIZE_BYTES=$(TVM_WORKSPACE_SIZE_BYTES)
-LDFLAGS ?= -Werror -Wall
-
-# Codegen produces spurious lines like: int32_t arg2_code =
((int32_t*)arg_type_ids)[(2)];
-MODEL_CFLAGS ?= -Wno-error=unused-variable -Wno-error=missing-braces
-Wno-error=unused-const-variable -Wno-unused-variable
-
-AR ?= ${PREFIX}ar
-CC ?= ${PREFIX}gcc
-CXX ?= ${PREFIX}g++
-RANLIB ?= ${PREFIX}ranlib
-
-ifeq (${VERBOSE}, 1)
-QUIET ?=
-else
-QUIET ?= @
-endif
-
-PWD = $(shell pwd)
-BUILD_DIR = build
-
-CRT_LIB_NAMES = \
- microtvm_rpc_server microtvm_rpc_common \
- aot_executor_module aot_executor \
- graph_executor_module graph_executor \
- common memory
-
-CRT_LIBS = $(patsubst %, $(BUILD_DIR)/crt/lib%.a, $(CRT_LIB_NAMES))
-
-CRT_INCLUDES = $(glob crt/include/**)
-
-$(BUILD_DIR)/crt/lib%.a: $(glob crt/src/runtime/%/*.c)
- ${QUIET}cd crt && $(MAKE) -s \
- BUILD_DIR=../$(BUILD_DIR)/crt \
- CRT_CONFIG=$(PWD)/crt_config/crt_config.h \
- EXTRA_CFLAGS="$(CFLAGS)" \
- EXTRA_CXXFLAGS="$(CXXFLAGS)" \
- EXTRA_LDFLAGS="$(EXTRA_LDFLAGS)" \
- $(patsubst $(BUILD_DIR)/crt/lib%.a,%,$@)
-
-crt: $(CRT_LIBS)
-.PHONY: crt
-
-# Compile codegen files
-$(BUILD_DIR)/model/codegen/host/%.o: model/codegen/host/%.c
- ${QUIET}mkdir -p $(dir $@)
- ${QUIET}$(CC) $(INCLUDES) $(CFLAGS) $(MODEL_CFLAGS) -c -o "$@" "$<"
-
-MODEL_LIBS = \
- $(patsubst model/codegen/host/src/%.c,
$(BUILD_DIR)/model/codegen/host/src/%.o, $(wildcard
model/codegen/host/src/*.c)) \
- $(wildcard model/codegen/host/lib/*.o)
-
-# Compile src/ files
-build/%.o: src/%.cc
- ${QUIET}mkdir -p $(dir $@)
- ${QUIET}$(CXX) $(INCLUDES) $(CXXFLAGS) -c -o "$@" "$<"
-
-SRCS = $(wildcard src/*.cc)
-OBJS = $(patsubst src/%.cc,build/%.o,$(SRCS))
-
-build/main: ${OBJS} ${MODEL_LIBS} ${CRT_LIBS}
- ${QUIET}mkdir -p $(dir $@)
- ${QUIET}$(CXX) $(LDFLAGS) -o "$@" $^
-
-all: build/main
-.PHONY = all
-
-.DEFAULT_GOAL = all
diff --git a/src/runtime/crt/host/microtvm_api_server.py
b/src/runtime/crt/host/microtvm_api_server.py
index 9887cb6e04..af6947a18b 100644
--- a/src/runtime/crt/host/microtvm_api_server.py
+++ b/src/runtime/crt/host/microtvm_api_server.py
@@ -40,11 +40,13 @@ IS_TEMPLATE = not os.path.exists(os.path.join(PROJECT_DIR,
MODEL_LIBRARY_FORMAT_
# 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):
@@ -79,29 +81,25 @@ class Handler(server.ProjectAPIHandler):
# 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.
@@ -134,11 +132,12 @@ class Handler(server.ProjectAPIHandler):
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
@@ -162,13 +161,10 @@ class Handler(server.ProjectAPIHandler):
)
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.
diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py
index f5f83f26cd..56f812c867 100644
--- a/tests/lint/check_file_type.py
+++ b/tests/lint/check_file_type.py
@@ -157,7 +157,7 @@ ALLOW_SPECIFIC_FILE = {
"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",