Skip to content

Commit

Permalink
optimize compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jan 13, 2025
1 parent bd99d58 commit b55526c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 59 deletions.
1 change: 0 additions & 1 deletion components/ext_devs/ext_dev/include/maix_axp2101.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#pragma once
#include "maix_basic.hpp"
#include "axp2101.h"

namespace maix::ext_dev::axp2101 {

Expand Down
1 change: 1 addition & 0 deletions components/ext_devs/ext_dev/src/axp2101/maix_axp2101.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mutex>
#include <iostream>
#include <unistd.h>
#include "axp2101.h"

#define _BV(b) (1ULL << (uint64_t)(b))

Expand Down
2 changes: 1 addition & 1 deletion components/ext_devs/ext_dev_tof100/include/maix_tof100.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Tof100 final {
*
* @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();

Expand Down
4 changes: 2 additions & 2 deletions components/nn/include/maix_nn_face_landmarks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ namespace maix::nn
*/
void draw_face(image::Image &img, const std::vector<int> &points, int num, const std::vector<int> &points_z=std::vector<int>(), 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");
}
Expand All @@ -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");
}
Expand Down
58 changes: 30 additions & 28 deletions components/vision/port/maixcam/uvc-gadget-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include <glob.h>

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -631,7 +630,7 @@ static int uvc_video_reqbufs_mmap(struct uvc_device *dev, int nbufs)
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, payload_size;
int ret;

CLEAR(rb);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down
73 changes: 46 additions & 27 deletions tools/cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

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:
Expand Down Expand Up @@ -61,39 +69,44 @@ 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
'''
# 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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -306,6 +319,12 @@ def menuconfig(sdk_path, build_path, configs, gui = True):
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="")
10 changes: 10 additions & 0 deletions tools/cmake/compile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b55526c

Please sign in to comment.