[CMake] using cmake with google's protocol buffers

Eric Noulard eric.noulard at gmail.com
Thu Sep 18 04:25:53 EDT 2008


2008/9/18 Neil Girdhar <mistersheik at gmail.com>:
>
> Hi, I'm trying to add a special cmake rule to build google's protocol
> buffers, and I was hoping someone could help me do it.  I've broken down the
> details of the protocol compiler here, so all that's missing is a cmake
> expert.

Did you look at the recently posted CMake module:
http://www.cmake.org/pipermail/cmake/2008-September/023963.html

I've added some suggested "basic" commands afterwards
but I really suggest to try the preceding module and may be improve it or
fix it, if it does not suit your needs and give feedback to the original author.

> I have some ".proto" files that live in the same directories as the source.
> I want to specify a rule that calls this:
>
> protoc DIR/SOMEFILE.proto  --cpp_out=SOMEDIR
>
> SOMEDIR should probably be DIR/CMakeFiles/DIR.dir

What would you do that?
I suggest that SOMEDIR should be
${CMAKE_CURRENT_BINARY_DIR}
which the directory in the build tree which the sibbling
of the corresponding source tree directory (${CMAKE_CURRENT_SOURCE_DIR})

> this will generate
> SOMEDIR/SOMEFILE.pb.h
> SOMEDIR/SOMEFILE.pb.cc

Then something like this in DIR/CMakeLists.txt

ADD_CUSTOM_COMMAND(
         OUTPUT SOMEFILE.pb.h SOMEFILE.pb.cc
         COMMAND protoc SOMEFILE.proto --cpp_out=${CMAKE_CURRENT_BINARY_DIR}
         DEPENDS
         SOMEFILE.proto
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})



> I want to include SOMEFILE.pb.h in my source files and
> I want them to be found.
> I want to compile and link SOMEFILE.pb.cc automatically

Then those file are compiled in a LIBRARY or EXECUTABLE
ADD_LIBRARY(mylib
                        SOMEFILE.pb.h SOMEFILE.pb.cc)

then do not forget to tell CMake that those files are to be generated:
set_source_files_properties(SOMEFILE.pb.h SOMEFILE.pb.cc
                                         PROPERTIES
                                         GENERATED TRUE)
>
> what is the easiest set of rules that I can use to extend cmake to do this?



-- 
Erk


More information about the CMake mailing list