How to build nanopb proto sources into a zephyr_library

I'm currently porting a project that was originally developed on NRF5 SDK to Zephyr RTOS.

We used protobuf in varoius places.  We also have multiple different conditional builds for different target markets that enable or disable certain features so code needs to be built conditoinally based on Kconfig settings.

I'm now trying to port our protobuf based features which we used for portability between our firmware and the mobile apps.  I see that nanopb has already been integrated into zephyr as a module.  There are also examples of using nanopb directly in the CMakelist.txt from your application build folder.

The problem I'm having is adding protobuf support in a library instead of directly in the app/src.  

My directory layout:

zephyr

repo/app/CMakelists.txt    -- app build 

repo/lib/data_exchange/file_formats/CMakelists.txt    -- library folder

repo/lib/data_exchange/file_formats/file_format.proto 

The goal is to run protoc on file_format.proto and add the generated source files to a zephyr_library named file_formats.

the file zephyr/modules/nanopb/nanopb.cmake has this function:

# Generate source and header files from provided .proto files and
# add these as sources to the specified target.
function(zephyr_nanopb_sources target)
# Turn off the default nanopb behavior
set(NANOPB_GENERATE_CPP_STANDALONE OFF)

nanopb_generate_cpp(proto_srcs proto_hdrs RELPATH ${CMAKE_CURRENT_SOURCE_DIR} ${ARGN})

target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_sources(${target} PRIVATE ${proto_srcs} ${proto_hdrs})

# Create unique target name for generated header list
string(MD5 unique_chars "${proto_hdrs}")
set(gen_target_name ${target}_proto_${unique_chars})

add_custom_target(${gen_target_name} DEPENDS ${proto_hdrs})
add_dependencies(nanopb_generated_headers ${gen_target_name})
endfunction()

I'm trying to create my own function that would be add the source files into a zephyr_library:

zephyr_library()
zephyr_library_nanopb_source(file_format.proto)

I tried doing this:

function(zephyr_library_nanopb_sources target)
  # add protoc dependencies to generate c and add c to library
  # Turn off the default nanopb behavior
  set(NANOPB_GENERATE_CPP_STANDALONE OFF)

  nanopb_generate_cpp(proto_srcs proto_hdrs RELPATH ${CMAKE_CURRENT_SOURCE_DIR} ${ARGN})

  #zephyr_include_directories(${target} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
  #zephyr_library_sources(${target} PRIVATE ${proto_srcs} ${proto_hdrs})
  #zephyr_include_directories(${CMAKE_CURRENT_BINARY_DIR})
  zephyr_library_sources(${proto_srcs})

  # Create unique target name for generated header list
  string(MD5 unique_chars "${proto_hdrs}")
  set(gen_target_name ${target}_proto_${unique_chars})

  add_custom_target(${gen_target_name} DEPENDS ${proto_hdrs})
  add_dependencies(nanopb_generated_headers ${gen_target_name})
endfunction()

but it doesn't work:

zephyr_library_sources Function invoked with incorrect arguments for
function named: zephyr_library_sources

  • Hello,

    I don't have that much experience with Protobuf, but wouldn't it be sufficient if you could conditionally select the *.proto file during compilation?  Below is a demo sample I created  based on the nanopb project that selects the *.proto file based on  the Kconfig configuration (CONFIG_SAMPLE_PB_PROD_A or CONFIG_SAMPLE_PB_PROD_B).

    Project

    nanopb_332011.zip 

    Build command

    $ west build -b <target board> -- -DCONFIG_SAMPLE_PB_PROD_A

    Best regards,

    Vidar

Related