find_program(PROTOC protoc) function(proto_generate_cpp PROTO_FILE PB_OUT HDR_VAR SRC_VAR) get_filename_component(PROTO_FILE_NAME ${PROTO_FILE} NAME_WE) get_filename_component(PROTO_FILE_PATH ${PROTO_FILE} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) get_filename_component(PROTO_FILE_DIR ${PROTO_FILE_PATH} DIRECTORY) set(PROTO_HDR "${PB_OUT}/${PROTO_FILE_NAME}.pb.h") set(PROTO_SRC "${PB_OUT}/${PROTO_FILE_NAME}.pb.cc") set(${HDR_VAR} ${${HDR_VAR}} ${PROTO_HDR} PARENT_SCOPE) set(${SRC_VAR} ${${SRC_VAR}} ${PROTO_SRC} PARENT_SCOPE) if(NOT CMAKE_SCRIPT_MODE_FILE) add_custom_command(OUTPUT ${PROTO_HDR} ${PROTO_SRC} COMMAND ${PROTOC} --cpp_out=${PB_OUT} --proto_path ${PROTO_FILE_DIR} ${PROTO_FILE_PATH} DEPENDS ${PROTO_FILE_PATH} COMMENT "Generating nanopb sources for ${PROTO_FILE}") set_property(SOURCE ${PROTO_HDR} ${PROTO_SRC} PROPERTY GENERATED TRUE) set_property(DIRECTORY . APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${PROTO_HDR} ${PROTO_SRC}) endif() endfunction()