From c703fbd65661a3ce866d9eb524a4e90ed15c6a74 Mon Sep 17 00:00:00 2001 From: Neucrack Date: Mon, 13 Jan 2025 12:09:50 +0800 Subject: [PATCH] optimize compile, add add_requirements func in component.py support --- .../ext_devs/ext_dev/include/maix_axp2101.hpp | 1 - .../ext_dev/src/axp2101/maix_axp2101.cpp | 1 + .../ext_dev_tof100/include/maix_tof100.hpp | 2 +- .../nn/include/maix_nn_face_landmarks.hpp | 4 +- .../vision/port/maixcam/uvc-gadget-server.c | 58 ++++++++------- tools/cmake/build.py | 73 ++++++++++++------- tools/cmake/compile.cmake | 10 +++ 7 files changed, 90 insertions(+), 59 deletions(-) diff --git a/components/ext_devs/ext_dev/include/maix_axp2101.hpp b/components/ext_devs/ext_dev/include/maix_axp2101.hpp index 6506f1ea..43c5f030 100644 --- a/components/ext_devs/ext_dev/include/maix_axp2101.hpp +++ b/components/ext_devs/ext_dev/include/maix_axp2101.hpp @@ -7,7 +7,6 @@ #pragma once #include "maix_basic.hpp" -#include "axp2101.h" namespace maix::ext_dev::axp2101 { diff --git a/components/ext_devs/ext_dev/src/axp2101/maix_axp2101.cpp b/components/ext_devs/ext_dev/src/axp2101/maix_axp2101.cpp index f23b33e8..ec2f51b5 100644 --- a/components/ext_devs/ext_dev/src/axp2101/maix_axp2101.cpp +++ b/components/ext_devs/ext_dev/src/axp2101/maix_axp2101.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "axp2101.h" #define _BV(b) (1ULL << (uint64_t)(b)) diff --git a/components/ext_devs/ext_dev_tof100/include/maix_tof100.hpp b/components/ext_devs/ext_dev_tof100/include/maix_tof100.hpp index 9d321756..5260f3e6 100644 --- a/components/ext_devs/ext_dev_tof100/include/maix_tof100.hpp +++ b/components/ext_devs/ext_dev_tof100/include/maix_tof100.hpp @@ -59,7 +59,7 @@ public: * * @return Matrix containing the distance data, or an empty matrix ([]) if the operation fails. * - * @maixpy maix.ext_dev.Tof100.Tof100.matrix + * @maixpy maix.ext_dev.tof100.Tof100.matrix */ TOFMatrix matrix(); diff --git a/components/nn/include/maix_nn_face_landmarks.hpp b/components/nn/include/maix_nn_face_landmarks.hpp index c7b68aac..388b4f91 100644 --- a/components/nn/include/maix_nn_face_landmarks.hpp +++ b/components/nn/include/maix_nn_face_landmarks.hpp @@ -369,7 +369,7 @@ namespace maix::nn */ void draw_face(image::Image &img, const std::vector &points, int num, const std::vector &points_z=std::vector(), int r_min = 2, int r_max = 4) { - if (points.size() == 0 || points.size() % 2 != 0 || num * 2 > points.size()) + if (points.size() == 0 || points.size() % 2 != 0 || num * 2 > (int)points.size()) { throw std::runtime_error("keypoints size must > 0 and x,y,...,x,y format, num must <= points size"); } @@ -385,7 +385,7 @@ namespace maix::nn } else { - if(num > points_z.size()) + if(num > (int)points_z.size()) { throw std::runtime_error("num must <= points_z size"); } diff --git a/components/vision/port/maixcam/uvc-gadget-server.c b/components/vision/port/maixcam/uvc-gadget-server.c index f510cb29..a9778814 100644 --- a/components/vision/port/maixcam/uvc-gadget-server.c +++ b/components/vision/port/maixcam/uvc-gadget-server.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -234,31 +235,31 @@ static int uvc_video_stream(struct uvc_device *dev, int enable); * UVC generic stuff */ -static int uvc_video_set_format(struct uvc_device *dev) -{ - struct v4l2_format fmt; - int ret; +// static int uvc_video_set_format(struct uvc_device *dev) +// { +// struct v4l2_format fmt; +// int ret; - CLEAR(fmt); +// CLEAR(fmt); - fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; - fmt.fmt.pix.width = dev->width; - fmt.fmt.pix.height = dev->height; - fmt.fmt.pix.pixelformat = dev->fcc; - fmt.fmt.pix.field = V4L2_FIELD_NONE; - if (dev->fcc == V4L2_PIX_FMT_MJPEG) - fmt.fmt.pix.sizeimage = dev->imgsize * 1.5; +// fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; +// fmt.fmt.pix.width = dev->width; +// fmt.fmt.pix.height = dev->height; +// fmt.fmt.pix.pixelformat = dev->fcc; +// fmt.fmt.pix.field = V4L2_FIELD_NONE; +// if (dev->fcc == V4L2_PIX_FMT_MJPEG) +// fmt.fmt.pix.sizeimage = dev->imgsize * 1.5; - ret = ioctl(dev->uvc_fd, VIDIOC_S_FMT, &fmt); - if (ret < 0) { - printf("UVC: Unable to set format %s (%d).\n", strerror(errno), errno); - return ret; - } +// ret = ioctl(dev->uvc_fd, VIDIOC_S_FMT, &fmt); +// if (ret < 0) { +// printf("UVC: Unable to set format %s (%d).\n", strerror(errno), errno); +// return ret; +// } - printf("UVC: Setting format to: %c%c%c%c %ux%u\n", pixfmtstr(dev->fcc), dev->width, dev->height); +// printf("UVC: Setting format to: %c%c%c%c %ux%u\n", pixfmtstr(dev->fcc), dev->width, dev->height); - return 0; -} +// return 0; +// } static int uvc_video_stream(struct uvc_device *dev, int enable) { @@ -418,8 +419,6 @@ static void uvc_video_fill_buffer(struct uvc_device *dev, struct v4l2_buffer *bu static int uvc_video_process(struct uvc_device *dev) { struct v4l2_buffer ubuf; - struct v4l2_buffer vbuf; - unsigned int i; int ret; /* * Return immediately if UVC video output device has not started @@ -631,7 +630,7 @@ err: static int uvc_video_reqbufs_userptr(struct uvc_device *dev, int nbufs) { struct v4l2_requestbuffers rb; - unsigned int i, j, bpl, payload_size; + unsigned int bpl = 0, payload_size = 0; int ret; CLEAR(rb); @@ -673,9 +672,11 @@ static int uvc_video_reqbufs_userptr(struct uvc_device *dev, int nbufs) // important payload_size = dev->width * dev->height * 3 / 2; break; + default: + assert(0); } - for (i = 0; i < rb.count; ++i) { + for (unsigned int i = 0; i < rb.count; ++i) { dev->dummy_buf[i].length = payload_size; dev->dummy_buf[i].start = malloc(payload_size); if (!dev->dummy_buf[i].start) { @@ -1365,7 +1366,7 @@ static void uvc_events_process(struct uvc_device *dev) static void uvc_events_init(struct uvc_device *dev) { struct v4l2_event_subscription sub; - unsigned int payload_size; + unsigned int payload_size = 0; switch (dev->fcc) { case V4L2_PIX_FMT_YUYV: @@ -1374,6 +1375,8 @@ static void uvc_events_init(struct uvc_device *dev) case V4L2_PIX_FMT_MJPEG: payload_size = dev->width * dev->height * 3/2; break; + default: + assert(0); } uvc_fill_streaming_control(dev, &dev->probe, 0, 0); @@ -1461,14 +1464,13 @@ static void usage(const char *argv0) int uvc_main_exist = 0; int uvc_main(int argc, char *argv[]) { - struct uvc_device *udev; + struct uvc_device *udev = NULL; struct timeval tv; - struct v4l2_format fmt; char *uvc_devname = "/dev/video0"; char *mjpeg_image = NULL; fd_set fdsu; - int ret, opt, nfds; + int ret, opt; int bulk_mode = 0; int dummy_data_gen_mode = 0; /* Frame format/resolution related params. */ diff --git a/tools/cmake/build.py b/tools/cmake/build.py index 659b4c87..405644be 100644 --- a/tools/cmake/build.py +++ b/tools/cmake/build.py @@ -21,6 +21,14 @@ from file_downloader import download_extract_files thread_num = cpu_count() +def execute_component_py_func(py_path : str, func : str, *args, **kwargs): + g = {} + with open(py_path, "r", encoding="utf-8") as f: + exec(f.read(), g) + if func not in g: + return False, None + return True, g[func](*args, **kwargs) + def parse_kconfigs(config_path, toolchain_config_path): configs = {} with open(config_path, "r") as f: @@ -61,21 +69,21 @@ def get_components_files(components, valid_components, kconfigs): py_path = os.path.join(components[name], "component.py") if not os.path.exists(py_path): continue - g = {} - with open(py_path, "r", encoding="utf-8") as f: - exec(f.read(), g) - if "add_file_downloads" in g: - try: - files[name] = g["add_file_downloads"](kconfigs) - except Exception as e: - print("\n\n---- ERROR -----") - traceback.print_exc() - print("----------------") - print(f"[ERROR] execute compoent [{name}]'s component.py failed") - sys.exit(1) + found, files[name] = execute_component_py_func(py_path, "add_file_downloads", kconfigs) + if not found: + files[name] = [] return files -def find_valid_components(components): +def get_component_requirements(component_name, component_dir, find_dirs): + requires = [] + component_py = os.path.join(component_dir, "component.py") + if os.path.exists(component_py): + found, requires = execute_component_py_func(component_py, "add_requirements", find_dirs) + if not found: + requires = [] + return requires + +def find_valid_components(components, find_dirs): ''' get project depends(not accurately, just exclude some obvious not depend components) return valid components name @@ -83,17 +91,22 @@ def find_valid_components(components): # get components quire depends = {} for name, dir in components.items(): - cmakelist = os.path.join(dir, "CMakeLists.txt") - with open(cmakelist, "r", encoding="utf-8") as f: - content = f.read() - match = re.findall(r'list\(APPEND ADD_REQUIREMENTS(.*?)\)', content, re.DOTALL|re.MULTILINE) - match = list(set(" ".join(match).split())) - depends[name] = [] - for r in match: - if r in components: - if name == r: - continue - depends[name].append(r) + component_py = os.path.join(dir, "component.py") + found = False + if os.path.exists(component_py): + found, depends_component = execute_component_py_func(component_py, "add_requirements", find_dirs) + if not found: + cmakelist = os.path.join(dir, "CMakeLists.txt") + with open(cmakelist, "r", encoding="utf-8") as f: + content = f.read() + match = re.findall(r'list\(APPEND ADD_REQUIREMENTS(.*?)\)', content, re.DOTALL|re.MULTILINE) + depends_component = list(set(" ".join(match).split())) + depends[name] = [] + for r in depends_component: + if r in components: + if name == r: + continue + depends[name].append(r) # find main depends def get_depend_recursive(name, seen={}): if name in seen: @@ -129,7 +142,7 @@ def get_components_find_dirs(configs): def get_all_components_dl_info(find_dirs, kconfigs): components = get_components_dirs(find_dirs) - valid_components = find_valid_components(components) + valid_components = find_valid_components(components, find_dirs) files_info = get_components_files(components, valid_components, kconfigs) return files_info @@ -306,6 +319,12 @@ if __name__ == "__main__": if " " in dir: raise Exception("Path can not contain space or special characters") components = get_components_dirs(find_dirs) - valid_components = find_valid_components(components) + valid_components = find_valid_components(components, find_dirs) print(";".join(valid_components), end="") - + elif cmd == "get_requirements": + find_dirs = sys.argv[4:] + for dir in find_dirs: + if " " in dir: + raise Exception("Path can not contain space or special characters") + requires = get_component_requirements(sys.argv[2], sys.argv[3], find_dirs) + print(";".join(requires), end="") diff --git a/tools/cmake/compile.cmake b/tools/cmake/compile.cmake index e70f2735..24b28d28 100644 --- a/tools/cmake/compile.cmake +++ b/tools/cmake/compile.cmake @@ -230,6 +230,16 @@ function(register_component) endif() # Add requirements + # get requirements from component.py + set(cmd COMMAND ${python} -u ${SDK_PATH}/tools/cmake/build.py get_requirements ${component_name} ${component_dir} ${SDK_PATH}/components ${SDK_PATH}/components/3rd_party ${SDK_PATH}/components/ext_devs ${MAIXCDK_EXTRA_COMPONENTS_PATH} ${PY_PKG_COMPONENTS_PATH} ${PY_USR_PKG_COMPONENTS_PATH} ${PROJECT_SOURCE_DIR}/../components ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/components) + execute_process(${cmd} RESULT_VARIABLE cmd_res OUTPUT_VARIABLE component_requires) + if(NOT cmd_res EQUAL 0) + message(FATAL_ERROR "Get valid components failed") + endif() + if(component_requires) + message("-- component ${component_name} depend on components from component.py: ${component_requires}") + list(APPEND ADD_REQUIREMENTS ${component_requires}) + endif() target_link_libraries(${component_name} ${include_type} ${ADD_REQUIREMENTS}) set(save_req_cmd COMMAND ${python} -u ${SDK_PATH}/tools/cmake/components_depends.py append