
#  protobuf_generate
#  --------------------------
#
#   Add custom commands to process ``.proto`` files to C++ using protoc and
#   GRPC plugin:
#
#     protobuf_generate(<target> <*.proto files>)
#
#   ``ARGN``
#     ``.proto`` files
#
macro(m_protobuf_generate _target)
    message(STATUS "inside protobuf_generate_grpc_cpp")
    if(NOT TARGET ${_target})
        message(SEND_ERROR "protobuf_generate requires target as first argument")
        return()
    endif()
    if(NOT ${ARGC} GREATER 1)
        message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files as arguments")
        return()
    endif()
    if(NOT _generated_headers)
        set(_generated_headers)
    endif()
    # set(_protobuf_include_path -I . )
    foreach(FIL ${ARGN})
        get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
        message(STATUS "protobuf_generate_grpc_cpp: processing ${ABS_FIL}")
        get_filename_component(FIL_WE ${FIL} NAME_WE)
        file(RELATIVE_PATH REL_FIL ${CMAKE_CURRENT_SOURCE_DIR}/proto ${ABS_FIL})
        get_filename_component(REL_DIR ${REL_FIL} DIRECTORY)
        set(RELFIL_WE "${REL_DIR}/${FIL_WE}")

        set(_GEN_HEADERS
            ${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h
        )
        list(APPEND _generated_headers "${_GEN_HEADERS}") 
        set(_GEN_SOURCES
            ${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc
        )
        #add generated sources to the sources of _target
        target_sources(${_target} PRIVATE ${_GEN_SOURCES})
        set_source_files_properties(${_GEN_SOURCES} ${_GEN_HEADERS} PROPERTIES GENERATED TRUE)

        add_custom_command(
                OUTPUT ${_GEN_SOURCES} ${_GEN_HEADERS} 
                COMMAND ${deps_prefix}/bin/protoc
                ARGS --cpp_out=${_gRPC_PROTO_GENS_DIR}
                -I .
                ${REL_FIL}
                DEPENDS ${FIL}
                WORKING_DIRECTORY ${_PROTO_IMPORT_DIR}
                COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
                VERBATIM
        )
        message(STATUS "protoc will generate ${_GEN_SOURCES} and ${_GEN_HEADERS}")
        #since some of the headers generated by this command are also included in hand-written sources make this command run before the _target
        get_property(_sources TARGET ${_target} PROPERTY SOURCES)
        set_source_files_properties(${_sources} PROPERTIES OBJECT_DEPENDS "${_GEN_HEADERS}")

        # foreach(_source ${_sources})
        #     message(STATUS "setting files ${_source} to depend on ${_GEN_HEADERS}")
        #     set_source_files_properties(${_source} PROPERTIES OBJECT_DEPENDS "${_GEN_HEADERS}")
        # endforeach()
        
        # #since some of the headers generated by this command also included in hand-written sources make this command run before the _target
        # set(_custom_target_name "touch-protoc-${FIL_WE}.proto")
        # add_custom_target(${_custom_target_name} touch ${_custom_target_name} DEPENDS ${_GEN_SRCS_CC})
        # add_dependencies(${_target} ${_custom_target_name})
    endforeach()
endmacro()
