1# Copyright (c) 2023, Basalte bv
2#
3# SPDX-License-Identifier: Apache-2.0
4
5include_guard(GLOBAL)
6
7set(NANOPB_SRC_ROOT_FOLDER ${ZEPHYR_NANOPB_MODULE_DIR})
8list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_NANOPB_MODULE_DIR}/extra)
9
10find_package(Nanopb REQUIRED)
11
12if(NOT PROTOBUF_PROTOC_EXECUTABLE)
13  message(FATAL_ERROR "'protoc' not found, please ensure protoc is installed\
14and in path. See https://docs.zephyrproject.org/latest/samples/modules/nanopb/README.html")
15else()
16  message(STATUS "Found protoc: ${PROTOBUF_PROTOC_EXECUTABLE}")
17endif()
18
19add_custom_target(nanopb_generated_headers)
20
21# Usage:
22#   list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
23#   include(nanopb)
24#
25#   zephyr_nanopb_sources(<target> <proto-files>)
26#
27# Generate source and header files from provided .proto files and
28# add these as sources to the specified target.
29function(zephyr_nanopb_sources target)
30  # Turn off the default nanopb behavior
31  set(NANOPB_GENERATE_CPP_STANDALONE OFF)
32
33  nanopb_generate_cpp(proto_srcs proto_hdrs RELPATH ${CMAKE_CURRENT_SOURCE_DIR} ${ARGN})
34
35  target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
36  target_sources(${target} PRIVATE ${proto_srcs} ${proto_hdrs})
37
38  # Create unique target name for generated header list
39  string(MD5 unique_chars "${proto_hdrs}")
40  set(gen_target_name ${target}_proto_${unique_chars})
41
42  add_custom_target(${gen_target_name} DEPENDS ${proto_hdrs})
43  add_dependencies(nanopb_generated_headers ${gen_target_name})
44endfunction()
45