Compiling protobuf files with project build

szabiaura
Posts: 15
Joined: Wed Jul 08, 2020 5:27 am

Compiling protobuf files with project build

Postby szabiaura » Thu Jul 09, 2020 8:17 am

Hi,

I have a bunch of .proto files that I want to compile with protobuf-c whenever the project is built. I want to make a component from it. I tried adding the following to my CMakeLists.txt:
  1. cmake_minimum_required(VERSION 3.5)
  2.  
  3. set(PROTO_COMPILER "protoc")
  4. set(PROTO_C_COMPILER "protoc-c")
  5. set(C_OUT_PATH "${CMAKE_CURRENT_LIST_DIR}/proto-c")
  6.  
  7. set(PROTO_SRCS "${CMAKE_CURRENT_LIST_DIR}/proto/Device.proto"
  8.                "${CMAKE_CURRENT_LIST_DIR}/proto/Output.proto")
  9.  
  10. set(pb_csrcs
  11.     "proto-c/Device.pb-c.c"
  12.     "proto-c/Output.pb-c.c")
  13.  
  14. if(NOT CMAKE_BUILD_EARLY_EXPANSION)
  15.     add_custom_command(OUTPUT ${pb_csrcs}
  16.          COMMAND ${PROTO_C_COMPILER} --c_out=${C_OUT_PATH} -I . ${PROTO_SRCS}
  17.          DEPENDS ${PROTO_SRCS}
  18.          VERBATIM)
  19. endif()
  20.  
  21. idf_component_register(SRCS "${pb_csrcs}"
  22.                        INCLUDE_DIRS "proto-c"
  23.                        REQUIRES protobuf-c)
  24.  
Bit it doesn't work, build fails with this error:
  1. -- Configuring done
  2. CMake Error at /Users/cc/esp/esp-idf/tools/cmake/component.cmake:468 (add_library):
  3.   Cannot find source file:
  4.  
  5.     /Users/cc/Documents/myproject/components/LEDBridgeProto/proto-c/Device.pb-c.c
  6.  
  7.   Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  8.   .hpp .hxx .in .txx
How is this possible? Shouldn't add_custom_command tell CMake how to build Device.pb-c.c?

Anybody has a good practice for compiling proto files during project build?

Thanks,
--
Szabi

szabiaura
Posts: 15
Joined: Wed Jul 08, 2020 5:27 am

Re: Compiling protobuf files with project build

Postby szabiaura » Fri Jul 10, 2020 5:51 am

Found the problem. Paths in pb_csrcs are relative to the build directory, whereas filenames in the SRCS option for idf_component_register are relative to the source directory. To fix, change lines 10-12 to
  1. set(pb_csrcs
  2.     "${C_OUT_PATH}/Device.pb-c.c"
  3.     "${C_OUT_PATH}/Output.pb-c.c")

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Compiling protobuf files with project build

Postby ESP_Angus » Fri Jul 10, 2020 7:02 am

Hi Szabi,

Glad to hear that you found the solution.

Angus

romaindequidt
Posts: 2
Joined: Wed Apr 01, 2020 4:13 pm

Re: Compiling protobuf files with project build

Postby romaindequidt » Tue Nov 10, 2020 1:53 pm

@szabiaura,
Do you have any examples on github (or anything else) to hava a look at your CMakeLists.txt?

Or, at least, can you copy paste:
- components/LEDBridgeProto/proto/CMakeListst.txt
- components/LEDBridgeProto/CMakeListst.txt

I have a lot of difficulty to trigger the proto file compilation.

Thanks in advance,

Who is online

Users browsing this forum: No registered users and 229 guests