3
votes

The FindProtobuf module available in cmake offers the command PROTOBUF_GENERATE_CPP that calls protoc from within cmake. This command is normally executed at compilation time (when you run "make"). Since some of my source files include the generated files, the dependency check during "cmake .." fails because the files are not yet generated.

Is it possible to have this command run at configuration time right before the dependency check?

Thanks

1
Looking at the FindProtobuf.cmake file, it looks like PROTOBUF_GENERATE_CPP should make those files when you call the function at configuration time. When it does this it should print Running C++ protocol buffer compiler on ${MATCH_PATH} with root ${PROTOROOT}, generating: ${CPP_FILE} Do you see that when you configure? - SethMMorton
If by configure you mean the execution of the "cmake .." command, no. I see that when I compile the code with make. - Trixl
Take a look at ADD_DEPENDENCIES. Try running PROTOBUF_GENERATE_CPP with DEBUG and look at the information it gives. You might be able to tell your target to be dependent on the output of PROTOBUF_GENERATE_CPP so that it figures out what to do at build time (i.e. when you run make). - SethMMorton
BTW, I was wrong before. It does actually create the files at build time, not at configure time (i.e. running cmake). Sorry about that. - SethMMorton
One more thing. It is possible to make this run at configure time, but it will require you modifying the FindProtobuf.cmake file and including it with your source. - SethMMorton

1 Answers

4
votes

You can mark the files as going to be generated, so that the dependency check will work:

file(GLOB PROTOBUF_FILELIST ${PROTO_INCLUDE_DIR}/*.proto)
foreach( proto_file ${PROTOBUF_FILELIST} )
   get_filename_component(proto_name ${proto_file} NAME_WE)
   get_filename_component(proto_path ${PROTO_INCLUDE_DIR} ABSOLUTE)
   set_source_files_properties("${proto_path}/${proto_name}.pb.cc"
                               "${proto_path}/${proto_name}.pb.h"
                               PROPERTIES GENERATED TRUE)
endforeach()