#
# Copyright 2011 Centreon
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# For more information : contact@centreon.com
#

# Global options.
set(MODULE_DIR "${PROJECT_SOURCE_DIR}/grpc")
set(INC_DIR "${MODULE_DIR}/inc/com/centreon/broker/grpc")
set(SRC_DIR "${MODULE_DIR}/src")
set(TEST_DIR "${MODULE_DIR}/test")
include_directories(${MODULE_DIR}/inc ${SRC_DIR})

# Sources.
set(SOURCES
    "${SRC_DIR}/acceptor.cc"
    "${SRC_DIR}/connector.cc"
    "${SRC_DIR}/factory.cc"
    "${SRC_DIR}/main.cc"
    "${SRC_DIR}/stream.cc"
    "${SRC_DIR}/grpc_stream.grpc.pb.cc"
    "${SRC_DIR}/grpc_stream.pb.cc")

# GRPC module.
set(GRPC "50-grpc")
set(GRPC
    ${GRPC}
    PARENT_SCOPE)
add_library(${GRPC} SHARED ${SOURCES} )
set_target_properties(${GRPC} PROPERTIES PREFIX "")
target_link_libraries(${GRPC} CONAN_PKG::spdlog)

target_precompile_headers(${GRPC} PRIVATE precomp_inc/precomp.hh)
# include_directories($(Boost_INCLUDE_DIRS))

add_custom_command(
  OUTPUT "${SRC_DIR}/grpc_stream.pb.cc" "${SRC_DIR}/grpc_stream.pb.h"
  DEPENDS ${MODULE_DIR}/grpc_stream.proto
  COMMENT "Generating interface files of grpc_stream.proto"
  COMMAND ${Protobuf_PROTOC_EXECUTABLE} ARGS --cpp_out=${SRC_DIR}
          --proto_path=${MODULE_DIR} grpc_stream.proto
  VERBATIM)

add_custom_command(
  OUTPUT "${SRC_DIR}/grpc_stream.grpc.pb.cc" "${SRC_DIR}/grpc_stream.grpc.pb.h"
  DEPENDS ${MODULE_DIR}/grpc_stream.proto
  COMMENT "Generating interface files grpc.pb of grpc_stream.proto"
  COMMAND
    ${Protobuf_PROTOC_EXECUTABLE} ARGS --grpc_out=${SRC_DIR}
    --plugin=protoc-gen-grpc=${CONAN_BIN_DIRS_GRPC}/grpc_cpp_plugin
    --proto_path=${MODULE_DIR} grpc_stream.proto
  VERBATIM)

# Testing.
if(WITH_TESTING)
  set(TESTS_SOURCES
      ${TESTS_SOURCES} ${TEST_DIR}/acceptor.cc
      ${TEST_DIR}/stream_test.cc ${TEST_DIR}/factory_test.cc
      PARENT_SCOPE)
  set(TESTS_LIBRARIES
      ${TESTS_LIBRARIES} ${GRPC}
      PARENT_SCOPE)
endif(WITH_TESTING)

# Install rule.
install(TARGETS "${GRPC}" LIBRARY DESTINATION "${PREFIX_MODULES}")
