CMake: conditionally generate protobuf `* pb files. {H | cpp} `when changing * .proto files

I am part of a project that uses protobufs to communicate between a Python based client and a C ++ based server. We also use CMake.

With CMake, I’m looking for a way to conditionally call the protoc program only when changing *.proto files. My current directory structure (seems to be part of the problem) has a directory for *.proto files and separate directories for the generated *.pb.{h|cc} files *.pb.{h|cc} and *_pb2.py :

  build/ Messages/proto/ <--- .proto files are here Messages/cpp/ <--- would like the auto generated c++ files here Messages/py/ <--- would like the auto generated Python files here Server/Main.cpp Client/Main.py CMakeLists.txt 

The (root) CMakeLists.txt file (below) launches the protoc program when the build/cmake .. command is executed:

 project(AAA) MESSAGE("Protobuf autogeneration STARTED") file(GLOB proto_packages "${AAA_SOURCE_DIR}/Messages/proto/*.proto") execute_process(COMMAND protoc -I=${AAA_SOURCE_DIR}/Messages/proto --cpp_out=${AAA_SOURCE_DIR}/Messages/cpp/ --python_out=${AAA_SOURCE_DIR}/Messages/py/ ${proto_packages}) MESSAGE("Protobuf autogeneration COMPLETED") cmake_minimum_required(VERSION 2.8) find_package(Boost) find_package(Protobuf REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) include_directories(${PROTOBUF_INCLUDE_DIR}) add_subdirectory(Messages/proto) add_subdirectory(Messages/cpp) add_subdirectory(Server) 

Messages/proto/CMakeLists.txt file (which, I am sure, has no effect):

 file(GLOB proto_packages "${AAA_SOURCE_DIR}/Messages/proto/*.proto") execute_process(COMMAND protoc -I=${AAA_SOURCE_DIR}/Messages/proto --cpp_out=../cpp --python_out=../py ${proto_packages} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 

Ideally , the protoc program should run as part of the build/make , and only (re) generate *.pb.{h|cc} and *_pb2.py when changing the *.proto file.

Some files in the Server/ directory have the #include <Messages/cpp/Xxxx.pb.h> .

For bonus points, I would prefer that the *.pb.{h|cc} and *_pb2.py be generated in the corresponding directories ( Messages/cpp/ and Messages/py/ respectively). However, if someone can help with the dependent part of the problem, I am glad that the *.pb.{h|cc} and *_pb2.py files coexist with the *.proto files.

Thanks in advance for any suggestions!

+8
c ++ python cmake protocol-buffers
source share
1 answer

This is untested, but you need to add a custom command to the custom target.

You will need to create some variables for the output string, but not knowing how to turn * .proto into * .pb files. {h | cc} and * _pb2.py, I can not help it. See the proto_packages_cpp and proto_packages_python variables for where they should go.

 file(GLOB proto_packages "${AAA_SOURCE_DIR}/Messages/proto/*.proto") add_custom_command( COMMAND protoc -I=${AAA_SOURCE_DIR}/Messages/proto --cpp_out=../cpp --python_out=../py ${proto_packages} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${proto_packages} OUTPUT ${proto_packages_cpp} ${proto_packages_python} ) add_custom_target(protobuf_autogeneration_target ALL DEPENDS ${proto_packages} ) 
+3
source share

All Articles