Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to add custom generic compilation rule?

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?

like image 244
Mohammad Moghimi Avatar asked Feb 04 '13 19:02

Mohammad Moghimi


1 Answers

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.

like image 93
Fraser Avatar answered Oct 30 '22 21:10

Fraser