CMake: How to add custom generic compilation rule?

cmake, protocol-buffers

Solution

It looks like CMake's `FindProtobuf` module provides this functionality via the function `PROTOBUF_GENERATE_CPP`.

You can pass multiple .proto files in the one call, e.g.

file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})

Note that even though the CMakeLists.txt file which calls `find_package(Protobuf)` could be the top-level one, the CMakeLists.txt file(s) which invoke the function would need to be in the same directory as the .proto files.

Problem

I need to compile protocol buffer .proto files to .pb.cc,.pb.h files. There is a program for this conversion. ``` protoc test.proto --cpp_out . ``` How can I add such a generic rule in cmake? I can do this with add_custom_command. But I have to this for every .proto file. Is there a better way to do this?

Original source