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!
c ++ python cmake protocol-buffers
Scottymac
source share