1#############################################################################
2# $Id: CMake.NCBIptb.grpc.cmake 597692 2019-11-26 14:04:23Z gouriano $
3#############################################################################
4#############################################################################
5##
6##  NCBI CMake wrapper extension
7##  In NCBI CMake wrapper, adds support of GRPC/PROTOBUF code generation
8##    Author: Andrei Gourianov, gouriano@ncbi
9##
10
11
12##############################################################################
13function(NCBI_internal_process_proto_dataspec _variable _access _value)
14    if(NOT "${_access}" STREQUAL "MODIFIED_ACCESS" OR "${_value}" STREQUAL "")
15        return()
16    endif()
17    cmake_parse_arguments(DT "GENERATE" "DATASPEC;RETURN" "REQUIRES" ${_value})
18
19    get_filename_component(_path     ${DT_DATASPEC} DIRECTORY)
20    get_filename_component(_basename ${DT_DATASPEC} NAME_WE)
21    get_filename_component(_ext      ${DT_DATASPEC} EXT)
22    file(RELATIVE_PATH     _relpath  ${NCBI_SRC_ROOT} ${_path})
23
24    set(_specfiles  ${DT_DATASPEC})
25    set(_pb_srcfiles "")
26    set(_pb_incfiles "")
27    if (EXISTS "${NCBI_PROTOC_APP}" AND "PROTOBUF" IN_LIST DT_REQUIRES)
28        set(_pb_srcfiles ${_path}/${_basename}.pb.cc)
29        set(_pb_incfiles ${NCBI_INC_ROOT}/${_relpath}/${_basename}.pb.h)
30    endif()
31    set(_gr_srcfiles "")
32    set(_gr_incfiles "")
33    if (EXISTS "${NCBI_PROTOC_APP}" AND EXISTS "${NCBI_GRPC_PLUGIN}" AND "GRPC" IN_LIST DT_REQUIRES)
34        set(_gr_srcfiles ${_path}/${_basename}.grpc.pb.cc)
35        set(_gr_incfiles ${NCBI_INC_ROOT}/${_relpath}/${_basename}.grpc.pb.h)
36    endif()
37    if(NOT "${DT_RETURN}" STREQUAL "")
38#        set(${DT_RETURN} DATASPEC ${_specfiles} SOURCES ${_pb_srcfiles} ${_gr_srcfiles} HEADERS ${_pb_incfiles} ${_gr_incfiles} INCLUDE ${NCBI_INC_ROOT}/${_relpath} PARENT_SCOPE)
39        set(${DT_RETURN} DATASPEC ${_specfiles} SOURCES ${_pb_srcfiles} ${_gr_srcfiles} HEADERS ${_pb_incfiles} ${_gr_incfiles} PARENT_SCOPE)
40    endif()
41
42    if(NOT DT_GENERATE)
43        return()
44    endif()
45
46    if (EXISTS "${NCBI_PROTOC_APP}" AND "PROTOBUF" IN_LIST DT_REQUIRES)
47        set(_cmd ${NCBI_PROTOC_APP} --cpp_out=. -I. ${_relpath}/${_basename}${_ext})
48        add_custom_command(
49            OUTPUT ${_pb_srcfiles} ${_pb_incfiles}
50            COMMAND ${_cmd} VERBATIM
51            COMMAND ${CMAKE_COMMAND} -E make_directory ${NCBI_INC_ROOT}/${_relpath}
52            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_path}/${_basename}.pb.h ${NCBI_INC_ROOT}/${_relpath} VERBATIM
53            COMMAND ${CMAKE_COMMAND} -E remove -f ${_path}/${_basename}.pb.h VERBATIM
54            WORKING_DIRECTORY ${NCBI_SRC_ROOT}
55            COMMENT "Generate PROTOC C++ classes from ${DT_DATASPEC}"
56            DEPENDS ${DT_DATASPEC}
57            VERBATIM
58        )
59    endif()
60    if (EXISTS "${NCBI_PROTOC_APP}" AND EXISTS "${NCBI_GRPC_PLUGIN}" AND "GRPC" IN_LIST DT_REQUIRES)
61        set(_cmd ${NCBI_PROTOC_APP} --grpc_out=generate_mock_code=true:. --plugin=protoc-gen-grpc=${NCBI_GRPC_PLUGIN} -I. ${_relpath}/${_basename}${_ext})
62        add_custom_command(
63            OUTPUT ${_gr_srcfiles} ${_gr_incfiles}
64            COMMAND ${_cmd} VERBATIM
65            COMMAND ${CMAKE_COMMAND} -E make_directory ${NCBI_INC_ROOT}/${_relpath}
66            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_path}/${_basename}.grpc.pb.h ${NCBI_INC_ROOT}/${_relpath} VERBATIM
67            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_path}/${_basename}_mock.grpc.pb.h ${NCBI_INC_ROOT}/${_relpath} VERBATIM
68            COMMAND ${CMAKE_COMMAND} -E remove -f ${_path}/${_basename}.grpc.pb.h VERBATIM
69            COMMAND ${CMAKE_COMMAND} -E remove -f ${_path}/${_basename}_mock.grpc.pb.h VERBATIM
70            WORKING_DIRECTORY ${NCBI_SRC_ROOT}
71            COMMENT "Generate GRPC C++ classes from ${DT_DATASPEC}"
72            DEPENDS ${DT_DATASPEC}
73            VERBATIM
74        )
75    endif()
76endfunction()
77
78#############################################################################
79NCBI_register_hook(DATASPEC NCBI_internal_process_proto_dataspec ".proto")
80