Skip to content

Commit

Permalink
1. 使用protobuf_generate 生成完毕 cpp-class 和 python
Browse files Browse the repository at this point in the history
2. python生成的目录有个中划线bug
  • Loading branch information
171930433 committed Mar 24, 2024
1 parent 3ac4252 commit da34f9d
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 102 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
66 changes: 46 additions & 20 deletions src/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,81 @@ 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

)


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
Expand All @@ -62,4 +88,4 @@ target_include_directories(struct_pb_tutorial PUBLIC
)


add_subdirectory(message)
# add_subdirectory(message)
2 changes: 1 addition & 1 deletion src/example/base.proto
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/chasis.proto
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/gnss.proto
Original file line number Diff line number Diff line change
@@ -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];;
Expand Down
2 changes: 1 addition & 1 deletion src/example/message/image.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax = "proto3";
package zhito.inner_class;

import "zframe.proto";
import "src/example/message/zframe.proto";

message ZImage
{
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/imu.proto
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/perception.proto
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/pointcloud.proto
Original file line number Diff line number Diff line change
@@ -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 ;
Expand Down
6 changes: 3 additions & 3 deletions src/example/message/state.proto
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/example/message/zframe.proto
Original file line number Diff line number Diff line change
@@ -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; // 车辆运动约束虚拟帧
Expand Down
76 changes: 38 additions & 38 deletions src/example/tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@
#include <string>

// #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<inner_struct::ZPointCloudXYZIT>();
*(std::shared_ptr<inner_struct::ZFrame>)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<inner_struct::ZPointCloudXYZIT>();
// *(std::shared_ptr<inner_struct::ZFrame>)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;
Expand Down
30 changes: 12 additions & 18 deletions src/protoc-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

0 comments on commit da34f9d

Please sign in to comment.