From da34f9d48ef02fa0193fa6f56f5429d4e1b42a86 Mon Sep 17 00:00:00 2001 From: gsk <171930433@qq.com> Date: Sun, 24 Mar 2024 20:51:58 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BD=BF=E7=94=A8protobuf=5Fgenerate=20?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=AE=8C=E6=AF=95=20cpp-class=20=E5=92=8C=20?= =?UTF-8?q?python=202.=20python=E7=94=9F=E6=88=90=E7=9A=84=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=9C=89=E4=B8=AA=E4=B8=AD=E5=88=92=E7=BA=BFbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 +-- src/example/CMakeLists.txt | 66 ++++++++++++++++-------- src/example/base.proto | 2 +- src/example/message/chasis.proto | 6 +-- src/example/message/gnss.proto | 6 +-- src/example/message/image.proto | 2 +- src/example/message/imu.proto | 6 +-- src/example/message/perception.proto | 6 +-- src/example/message/pointcloud.proto | 6 +-- src/example/message/state.proto | 6 +-- src/example/message/zframe.proto | 4 +- src/example/tutorial.cpp | 76 ++++++++++++++-------------- src/protoc-plugin/CMakeLists.txt | 30 +++++------ 13 files changed, 122 insertions(+), 102 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea00c6173..be711e3d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,14 +13,14 @@ project(yaLanTingLibs # set(SYSROOT_DIR /opt/zhito/sysroot) -set(CMAKE_PREFIX_PATH "/home/gsk/pro/ubuntu2204_docker") +set(CMAKE_PREFIX_PATH "/home/gsk/pro/CyberRT/install") -find_package(Protobuf REQUIRED) +find_package(protobuf REQUIRED) -add_subdirectory(src/protoc-plugin) +# add_subdirectory(src/protoc-plugin) option(BUILD_EXAMPLES OFF) if(BUILD_EXAMPLES) - add_subdirectory(src/example) endif() +add_subdirectory(src/example) diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 968884007..8242b089b 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -5,47 +5,73 @@ set(ENV{struct_pb_options} "namespace=zhito.inner_struct,converter_namespace=zhi # 设置编译器选项 add_compile_options(-Wno-attributes) +set(protobuf_MODULE_COMPATIBLE ON) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/examples) # protobuf_generate_struct_pb 函数 set(struct_pb_plugin_DIRS ${CMAKE_SOURCE_DIR}/src/protoc-plugin) # struct_pb_plugin_DIRS 将指定 proto_to_struct.proto,struct_pb.cmake等目录 -set(Protobuf_IMPORT_DIRS ${struct_pb_plugin_DIRS} ${CMAKE_CURRENT_LIST_DIR}) # 设置protoc --proto_path参数 +# set(Protobuf_IMPORT_DIRS ${struct_pb_plugin_DIRS} ${CMAKE_CURRENT_LIST_DIR}) # 设置protoc --proto_path参数 +set(Protobuf_IMPORT_DIRS ${CMAKE_SOURCE_DIR}) # 设置protoc --proto_path参数 include(${struct_pb_plugin_DIRS}/cmake/struct_pb.cmake) -find_package(PCL REQUIRED COMPONENTS common io) -find_package(OpenCV REQUIRED COMPONENTS core) +# find_package(PCL REQUIRED COMPONENTS common io) +# find_package(OpenCV REQUIRED COMPONENTS core) + +file(GLOB_RECURSE PROTO_FILES "*.proto") +# set(PROTO_FILES +# "/home/gsk/pro/yalantinglibs/src/example/base.proto" +# "/home/gsk/pro/yalantinglibs/src/example/message/zframe.proto" +# "/home/gsk/pro/yalantinglibs/src/example/message/imu.proto" ) -file(GLOB PROTO_FILES "*.proto") -# generate .pb.cc .pb.h -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS - ${struct_pb_plugin_DIRS}/proto_to_struct.proto - ${PROTO_FILES} +protobuf_generate( + LANGUAGE cpp + OUT_VAR PROTO_CLASS_SRCS + IMPORT_DIRS ${CMAKE_SOURCE_DIR} + PROTOS ${struct_pb_plugin_DIRS}/proto_to_struct.proto ${PROTO_FILES} + PROTOC_OUT_DIR ${CMAKE_BINARY_DIR} ) -protobuf_generate_python(PROTO_PYTHON_SRCS - ${struct_pb_plugin_DIRS}/proto_to_struct.proto - ${PROTO_FILES} + +protobuf_generate( + LANGUAGE python + OUT_VAR PROTO_PYTHON_SRCS + IMPORT_DIRS ${CMAKE_SOURCE_DIR} + PROTOS ${struct_pb_plugin_DIRS}/proto_to_struct.proto ${PROTO_FILES} + PROTOC_OUT_DIR ${CMAKE_BINARY_DIR} ) -protobuf_generate_struct_pb(PROTO_STRUCT_SRCS PROTO_STRUCT_HDRS - ${PROTO_FILES} -OPTION $ENV{struct_pb_options} +list(FILTER PROTO_FILES EXCLUDE REGEX "proto_to_struct\\.proto$") + + +protobuf_generate_modified( + LANGUAGE struct_pb + OUT_VAR PROTO_STRUCT_SRCS + IMPORT_DIRS ${CMAKE_SOURCE_DIR} + PROTOS ${PROTO_FILES} + PROTOC_OUT_DIR ${CMAKE_BINARY_DIR} ) +# protobuf_generate_struct_pb(PROTO_STRUCT_SRCS PROTO_STRUCT_HDRS +# ${PROTO_FILES} +# OPTION $ENV{struct_pb_options} +# ) + add_library(proto_common OBJECT - ${PROTO_SRCS} - ${PROTO_STRUCT_SRCS} + ${PROTO_CLASS_SRCS} + # ${PROTO_STRUCT_SRCS} ${PROTO_PYTHON_SRCS} ) target_link_libraries(proto_common protobuf::libprotobuf ) target_include_directories(proto_common PUBLIC - ${CMAKE_CURRENT_LIST_DIR} # base.h - ${CMAKE_BINARY_DIR}/src/protoc-plugin # proto_to_struct.h + ${CMAKE_BINARY_DIR} # base.h + ${CMAKE_CURRENT_LIST_DIR}/ # 找到iguana + ) @@ -53,7 +79,7 @@ add_executable(struct_pb_tutorial tutorial.cpp ) target_link_libraries(struct_pb_tutorial proto_common - $ENV{proto_message} + # $ENV{proto_message} ) target_include_directories(struct_pb_tutorial PUBLIC @@ -62,4 +88,4 @@ target_include_directories(struct_pb_tutorial PUBLIC ) -add_subdirectory(message) +# add_subdirectory(message) diff --git a/src/example/base.proto b/src/example/base.proto index e654c9c8d..8c202be7f 100644 --- a/src/example/base.proto +++ b/src/example/base.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package zhito.inner_class; -import "proto_to_struct.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; option(write_eigen_helper) = true; message Vector4f diff --git a/src/example/message/chasis.proto b/src/example/message/chasis.proto index 4d679d373..2446c1af5 100644 --- a/src/example/message/chasis.proto +++ b/src/example/message/chasis.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -import "base.proto"; -import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +import "src/example/base.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; message ZChassis { ZFrame header = 1 [(inherits_from) = true]; diff --git a/src/example/message/gnss.proto b/src/example/message/gnss.proto index cd17cd0af..f3870c1f9 100644 --- a/src/example/message/gnss.proto +++ b/src/example/message/gnss.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -import "base.proto"; -import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +import "src/example/base.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; message ZGnss { ZFrame header = 1 [(inherits_from) = true];; diff --git a/src/example/message/image.proto b/src/example/message/image.proto index adead8242..e2d1024e3 100644 --- a/src/example/message/image.proto +++ b/src/example/message/image.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; +import "src/example/message/zframe.proto"; message ZImage { diff --git a/src/example/message/imu.proto b/src/example/message/imu.proto index 0f230aef1..7b3c26c20 100644 --- a/src/example/message/imu.proto +++ b/src/example/message/imu.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -import "base.proto"; -import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +import "src/example/base.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; message ZImu { ZFrame header = 1 [(inherits_from) = true]; diff --git a/src/example/message/perception.proto b/src/example/message/perception.proto index a0bc382de..7878c949b 100644 --- a/src/example/message/perception.proto +++ b/src/example/message/perception.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -import "base.proto"; -import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +import "src/example/base.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; message ZPercBase { int32 track_id = 1; diff --git a/src/example/message/pointcloud.proto b/src/example/message/pointcloud.proto index 7e319d7a9..48ea2d4d2 100644 --- a/src/example/message/pointcloud.proto +++ b/src/example/message/pointcloud.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -// import "base.proto"; -// import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +// import "src/example/base.proto"; +// import "src/protoc-plugin/proto_to_struct.proto"; message ZPointCloudXYZIT { ZFrame header = 1 ; diff --git a/src/example/message/state.proto b/src/example/message/state.proto index 308d1916f..42f5f809c 100644 --- a/src/example/message/state.proto +++ b/src/example/message/state.proto @@ -1,9 +1,9 @@ syntax = "proto3"; package zhito.inner_class; -import "zframe.proto"; -import "base.proto"; -import "proto_to_struct.proto"; +import "src/example/message/zframe.proto"; +import "src/example/base.proto"; +import "src/protoc-plugin/proto_to_struct.proto"; message ZState{ ZFrame header = 1 [(inherits_from) = true]; Vector3d pos = 2;//位置: X Y Z diff --git a/src/example/message/zframe.proto b/src/example/message/zframe.proto index 88368d9b0..1267147a0 100644 --- a/src/example/message/zframe.proto +++ b/src/example/message/zframe.proto @@ -1,8 +1,8 @@ syntax = "proto3"; package zhito.inner_class; -// import "base.proto"; -// import "proto_to_struct.proto"; +// import "src/example/base.proto"; +// import "src/protoc-plugin/proto_to_struct.proto"; enum ZFrameType{ None = 0; VEHICLE_MODEL = 1; // 车辆运动约束虚拟帧 diff --git a/src/example/tutorial.cpp b/src/example/tutorial.cpp index 5b5e6aeb7..e32a1a4af 100644 --- a/src/example/tutorial.cpp +++ b/src/example/tutorial.cpp @@ -4,59 +4,59 @@ #include // #include "addressbook.struct_pb.h" -#include "src/example/message/imu.struct_pb.h" -#include "src/example/message/zimage.hpp" -#include "src/example/message/zpointcloud.hpp" +// #include "src/example/message/imu.struct_pb.h" +// #include "src/example/message/zimage.hpp" +// #include "src/example/message/zpointcloud.hpp" -void PointcloudDemo() { - using namespace zhito; - pcl::PointXYZIT p2{1, 2}; +// void PointcloudDemo() { +// using namespace zhito; +// pcl::PointXYZIT p2{1, 2}; - inner_struct::spZPointCloudXYZIT pc = std::make_shared(); - *(std::shared_ptr)pc = - inner_struct::ZFrame{1, 2, 3, inner_struct::ZFrameType::PointCloud, "/zhito/pointcloud"}; - for (int i = 0; i < 1e5; ++i) { - p2.x += (rand() % 10000 * 0.0001); - pc->push_back(p2); - } - inner_class::ZPointCloudXYZIT pc2 = converter::StructToClass(pc); +// inner_struct::spZPointCloudXYZIT pc = std::make_shared(); +// *(std::shared_ptr)pc = +// inner_struct::ZFrame{1, 2, 3, inner_struct::ZFrameType::PointCloud, "/zhito/pointcloud"}; +// for (int i = 0; i < 1e5; ++i) { +// p2.x += (rand() % 10000 * 0.0001); +// pc->push_back(p2); +// } +// inner_class::ZPointCloudXYZIT pc2 = converter::StructToClass(pc); - inner_struct::spZPointCloudXYZIT pc3 = converter::ClassToStruct(pc2); +// inner_struct::spZPointCloudXYZIT pc3 = converter::ClassToStruct(pc2); - // pcl::io::savePCDFileBinaryCompressed("1.pcd", *pc3); +// // pcl::io::savePCDFileBinaryCompressed("1.pcd", *pc3); - std::cout << *pc3 << std::endl; -} +// std::cout << *pc3 << std::endl; +// } -void ImuDemo() { - using namespace zhito; +// void ImuDemo() { +// using namespace zhito; - inner_struct::ZImu imu1{1, 2, 3, inner_struct::ZFrameType::IMU, "channel_name", {4, 5, 6}, {7, 8, 9}, 10}; - inner_class::ZImu imu2 = converter::StructToClass(imu1); - inner_struct::ZImu imu3 = converter::ClassToStruct(imu2); +// inner_struct::ZImu imu1{1, 2, 3, inner_struct::ZFrameType::IMU, "channel_name", {4, 5, 6}, {7, 8, 9}, 10}; +// inner_class::ZImu imu2 = converter::StructToClass(imu1); +// inner_struct::ZImu imu3 = converter::ClassToStruct(imu2); - std::cout << imu1 << std::endl; - std::cout << imu2.ShortDebugString() << std::endl; - std::cout << imu3 << std::endl; -} +// std::cout << imu1 << std::endl; +// std::cout << imu2.ShortDebugString() << std::endl; +// std::cout << imu3 << std::endl; +// } -void ImageDemo() { - using namespace zhito; +// void ImageDemo() { +// using namespace zhito; - inner_struct::ZImage image1{1, 2, 3, inner_struct::ZFrameType::CvImage, "/zhito/image"}; - (cv::Mat&)image1 = cv::Mat::eye(4, 4, CV_64F); +// inner_struct::ZImage image1{1, 2, 3, inner_struct::ZFrameType::CvImage, "/zhito/image"}; +// (cv::Mat&)image1 = cv::Mat::eye(4, 4, CV_64F); - inner_class::ZImage image2 = converter::StructToClass(image1); - inner_struct::ZImage image3 = converter::ClassToStruct(image2); +// inner_class::ZImage image2 = converter::StructToClass(image1); +// inner_struct::ZImage image3 = converter::ClassToStruct(image2); - std::cout << image1 << std::endl; - std::cout << image2.ShortDebugString() << std::endl; - std::cout << image3 << std::endl; -} +// std::cout << image1 << std::endl; +// std::cout << image2.ShortDebugString() << std::endl; +// std::cout << image3 << std::endl; +// } int main() { // ImuDemo(); - PointcloudDemo(); + // PointcloudDemo(); // ImageDemo(); std::cout << "Done!!!" << std::endl; diff --git a/src/protoc-plugin/CMakeLists.txt b/src/protoc-plugin/CMakeLists.txt index 5dd5c8a24..672a81a2a 100644 --- a/src/protoc-plugin/CMakeLists.txt +++ b/src/protoc-plugin/CMakeLists.txt @@ -7,24 +7,18 @@ project(protoc-gen-struct_pb) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/bin) -find_package(Protobuf QUIET) -if (Protobuf_FOUND) +find_package(Protobuf REQUIRED) +file(GLOB SRCS "*.cpp" "*.cc" "*.h") +protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto_to_struct.proto) - file(GLOB SRCS "*.cpp" "*.cc" "*.h") - protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto_to_struct.proto) +add_executable(protoc-gen-struct_pb ${SRCS} ${PROTO_SRCS} ${PROTO_HDRS}) +target_include_directories(protoc-gen-struct_pb PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(protoc-gen-struct_pb PUBLIC protobuf::libprotoc protobuf::libprotobuf) +install( + TARGETS protoc-gen-struct_pb + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin +) - add_executable(protoc-gen-struct_pb ${SRCS} ${PROTO_SRCS} ${PROTO_HDRS}) - target_include_directories(protoc-gen-struct_pb PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) - target_link_libraries(protoc-gen-struct_pb PUBLIC protobuf::libprotoc protobuf::libprotobuf) - install( - TARGETS protoc-gen-struct_pb - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - RUNTIME DESTINATION bin - ) -else () - message(INFO "struct_pb is skipped. To build struct_pb protoc plugin, you must install libprotoc first\n" - "see https://alibaba.github.io/yalantinglibs/en/struct_pb/struct_pb_generating_your_struct.html" - ) -endif ()