Skip to content

Commit

Permalink
proto复杂目录编译完成
Browse files Browse the repository at this point in the history
  • Loading branch information
171930433 committed Mar 24, 2024
1 parent da34f9d commit 74add54
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 68 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ project(yaLanTingLibs


# set(SYSROOT_DIR /opt/zhito/sysroot)
set(CMAKE_PREFIX_PATH "/home/gsk/pro/CyberRT/install")
# set(CMAKE_PREFIX_PATH "/home/gsk/pro/CyberRT/install")
set(CMAKE_PREFIX_PATH "/home/gsk/pro/ubuntu2204_docker/install")

find_package(protobuf REQUIRED)

Expand Down
35 changes: 15 additions & 20 deletions src/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ 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 ${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")
Expand All @@ -43,49 +39,48 @@ protobuf_generate(
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR}
)

list(FILTER PROTO_FILES EXCLUDE REGEX "proto_to_struct\\.proto$")
list(FILTER PROTO_FILES EXCLUDE REGEX "proto_to_struct\\.proto$|image\\.proto$|pointcloud\\.proto$")


protobuf_generate_modified(
LANGUAGE struct_pb
# PLUGIN "struct_pb"
PLUGIN "/home/gsk/pro/yalantinglibs/build/output/bin/protoc-gen-struct_pb"
OUT_VAR PROTO_STRUCT_SRCS
IMPORT_DIRS ${CMAKE_SOURCE_DIR}
PROTOS ${PROTO_FILES}
PROTOC_OUT_DIR ${CMAKE_BINARY_DIR}
PROTOC_OPTION "namespace=zhito.inner_struct,converter_namespace=zhito.converter"
)

# protobuf_generate_struct_pb(PROTO_STRUCT_SRCS PROTO_STRUCT_HDRS
# ${PROTO_FILES}
# OPTION $ENV{struct_pb_options}
# )


add_library(proto_common OBJECT
message/zpointcloud.cpp message/zimage.cpp
${PROTO_CLASS_SRCS}
# ${PROTO_STRUCT_SRCS}
${PROTO_STRUCT_SRCS}
${PROTO_PYTHON_SRCS}
)

target_link_libraries(proto_common protobuf::libprotobuf )
target_link_libraries(proto_common protobuf::libprotobuf
# 第三方库
pcl_common pcl_io # pcl
opencv_imgcodecs opencv_core # opencv
)

target_include_directories(proto_common PUBLIC
${CMAKE_BINARY_DIR} # base.h
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}
${CMAKE_CURRENT_LIST_DIR}/ # 找到iguana

)


add_executable(struct_pb_tutorial tutorial.cpp )

target_link_libraries(struct_pb_tutorial
proto_common
# $ENV{proto_message}
)

target_include_directories(struct_pb_tutorial PUBLIC
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}
)


# add_subdirectory(message)
2 changes: 0 additions & 2 deletions src/example/message/zimage.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <pcl/point_cloud.h>

#include "src/example/message/zframe.struct_pb.h"
#include "src/example/message/image.pb.h"

Expand Down
85 changes: 45 additions & 40 deletions src/example/tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,65 @@
#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;

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);

// void ImuDemo() {
// using namespace zhito;
std::cout << imu1 << std::endl;
std::cout << imu2.ShortDebugString() << std::endl;
std::cout << imu3 << std::endl;
}

void ImageDemo() {
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);
std::cout << "123\n";

// std::cout << imu1 << std::endl;
// std::cout << imu2.ShortDebugString() << std::endl;
// std::cout << imu3 << std::endl;
// }
inner_struct::ZImage image1{1, 2, 3, inner_struct::ZFrameType::CvImage, "/zhito/image"};
cv::Mat& mat1 = image1;
mat1 = cv::Mat::eye(4, 4, CV_64F);

// void ImageDemo() {
// using namespace zhito;
std::cout << image1 << std::endl;

// 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 << image2.ShortDebugString() << std::endl;
std::cout << image3 << std::endl;
}

int main() {
// ImuDemo();
// PointcloudDemo();
// ImageDemo();
ImuDemo();
PointcloudDemo();
ImageDemo();

std::cout << "Done!!!" << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion src/protoc-plugin/FileGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void FileGenerator::generate_dependency_includes(
for (int i = 0; i < file_->dependency_count(); ++i) {
auto dep = file_->dependency(i);
std::string basename = strip_proto(dep->name());
if(basename == "proto_to_struct") {continue;}
if(basename.find("proto_to_struct") != std::string::npos) {continue;}
std::string header_name = basename + ".struct_pb.h";
format("#include \"$1$\"\n", header_name);
//
Expand Down
12 changes: 8 additions & 4 deletions src/protoc-plugin/cmake/struct_pb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function(protobuf_generate_modified)
endif()
endforeach()
else()
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
set(_protobuf_include_path -I ${CMAKE_SOURCE_DIR})
endif()

foreach(DIR ${protobuf_generate_IMPORT_DIRS})
Expand All @@ -98,7 +98,8 @@ function(protobuf_generate_modified)
get_filename_component(_abs_file ${_proto} ABSOLUTE)
get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
get_filename_component(_basename ${_proto} NAME_WE)
file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
# file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
file(RELATIVE_PATH _rel_dir ${CMAKE_SOURCE_DIR} ${_abs_dir})

set(_possible_rel_dir)
if (NOT protobuf_generate_APPEND_PATH)
Expand All @@ -118,14 +119,17 @@ function(protobuf_generate_modified)
list(APPEND _generated_srcs_all ${_generated_srcs})


# message(status _out!!!!!!!!${protobuf_generate_LANGUAGE}_out})
# message(_generated_srcs=${_generated_srcs})
# message(status _opt!!!!!!!!${_opt}${protobuf_generate_PROTOC_OUT_DIR})


# message(ARGS = --${protobuf_generate_LANGUAGE}_out ${_opt}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file})

add_custom_command(
OUTPUT ${_generated_srcs}
#!!!!!!!!!!!!!!!!!!!! 设置环境变量并执行
COMMAND ${CMAKE_COMMAND} -E env "PATH=$ENV{PATH}:${CMAKE_BINARY_DIR}/output/bin/" protoc
# COMMAND ${CMAKE_COMMAND} -E env "PATH=$ENV{PATH}:${CMAKE_BINARY_DIR}/output/bin/" protoc
COMMAND protobuf::protoc
ARGS --${protobuf_generate_LANGUAGE}_out ${_opt}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
DEPENDS ${_abs_file} protobuf::protoc
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
Expand Down

0 comments on commit 74add54

Please sign in to comment.