Search code examples
c++cmakeprotocol-buffersgrpcprotoc

Build protoc for C++ with CMake


I'm currently working on a C++ project that reference gRPC as a git submodule and I'm using CMake to compile the dependencies and my sources. For that I basically have this in my CMakeLists.txt:

ADD_SUBDIRECTORY(lib/grpc)

Then I run:

make grpc_cpp_plugin
make my_project

Even though I specify cpp_plugin here, when it's time to compile protoc I'm actually compiling for all the languages supported, eg (Java, Csharp, ...) :

/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc.o
/src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc.o
/src/google/protobuf/compiler/java/java_context.cc.o
/src/google/protobuf/compiler/java/java_doc_comment.cc.o

After looking around for some info on how to build protoc only for C++, I found that someone opened an issue on the github protobuf directory (link). However, it doesn't seem to give a clear answer.

Is there a 'clean' way to only compile the c++ dependency here ?


Solution

  • After doing tons of grep in gRPC's CMake files I finally compiled only the c++ version of protoc, protobuf and gRPC. And I did it in 3 steps:

    1. go to grpc/third_party/protobuf/cmake/libprotoc.cmake and remove the lines including csharp, java, ruby, ... (Be careful to keep cpp and the langage agnostic ones)
    2. go to grpc/CMakeLists.txt and you should be able to find this:
    add_library(grpc_plugin_support
      src/compiler/cpp_generator.cc
      src/compiler/csharp_generator.cc
      src/compiler/node_generator.cc
      src/compiler/objective_c_generator.cc
      src/compiler/php_generator.cc
      src/compiler/python_generator.cc
      src/compiler/ruby_generator.cc
    )
    

    so remove what's not needed.

    1. and finally, grpc/third_party/protobuf/src/google/protobuf/compiler/main.cc and remove all the references to the other langage.