MACRO(GENPROTO) FILE(GLOB PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.proto) FOREACH(proto ${PROTO_FILES}) FILE(TO_NATIVE_PATH ${proto} proto_native) string(REPLACE ".proto" ".pb.cc" PbCC ${proto_native}) string(REPLACE ".proto" ".pb.h" Pbh ${proto_native}) string(REPLACE "${PROJECT_SOURCE_DIR}/" "${PROJECT_BINARY_DIR}/" PbCC ${PbCC}) string(REPLACE "${PROJECT_SOURCE_DIR}/" "${PROJECT_BINARY_DIR}/" Pbh ${Pbh}) if ((not EXISTS ${PbCC}) OR (${proto_native} IS_NEWER_THAN ${PbCC})) # message("proto: " ${proto_native} " -> " ${PbCC}) EXECUTE_PROCESS(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROJECT_SOURCE_DIR} --cpp_out=${PROJECT_BINARY_DIR} ${proto_native} RESULT_VARIABLE res_var ) if (res_var STREQUAL "1") message(FATAL_ERROR "Generate proto error!!! file: ${proto_native}") endif() execute_process(COMMAND cp ${Pbh} ${CMAKE_CURRENT_SOURCE_DIR} ERROR_VARIABLE res_var) if(res_var) message(FATAL_ERROR ${res_var}) endif() endif() ENDFOREACH(proto) ENDMACRO()
时间: 2024-04-27 12:24:22 浏览: 166
这是一个CMake脚本中的宏定义,用于生成Protocol Buffer的C++代码。具体来说,该宏会遍历当前目录下的所有`.proto`文件,使用`PROTOBUF_PROTOC_EXECUTABLE`指定的编译器生成对应的`.pb.cc`和`.pb.h`文件,并将这些文件拷贝到源代码目录中。其中,`PROTOBUF_PROTOC_EXECUTABLE`是一个环境变量,用于指定Protocol Buffer的编译器路径。该脚本的主要功能是简化生成Protocol Buffer代码的过程,方便用户在CMake项目中使用。
阅读全文