# Global options.
project(connectors CXX)

set(CONNECTOR_VERSION "${COLLECT_MAJOR}.${COLLECT_MINOR}.${COLLECT_PATCH}")
add_definitions(-DCENTREON_CONNECTOR_VERSION=\"${CONNECTOR_VERSION}\")

include(${PROJECT_SOURCE_DIR}/cmake/Findperl.cmake)

set(PREFIX_CONNECTORS "${CMAKE_INSTALL_PREFIX}/lib64/centreon-connector/")

# Generate code to dynamically load modules.

# Disable some warnings generated by Embedded Perl.
get_property(
  EMBEDDED_PERL_CXXFLAGS
  SOURCE ${PROJECT_SOURCE_DIR}/perl/src/embedded_perl.cc
  PROPERTY COMPILE_FLAGS)

if(EMBEDDED_PERL_CXXFLAGS)
  string(REGEX REPLACE "-pedantic *" EMBEDDED_PERL_CXXFLAGS
                       "${EMBEDDED_PERL_CXXFLAGS}")
  set_property(SOURCE "${PROJECT_SOURCE_DIR}/perl/src/embedded_perl.cc"
               PROPERTY COMPILE_FLAGS "${EMBEDDED_PERL_CXXFLAGS}")
endif()

# With libasan
option(WITH_ASAN
       "Add the libasan to check memory leaks and other memory issues." OFF)

if(WITH_ASAN)
  set(CMAKE_BUILD_TYPE Debug)
  set(CMAKE_CXX_FLAGS_DEBUG
      "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
  set(CMAKE_LINKER_FLAGS_DEBUG
      "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif()

# We will use pkg-config if available.
include_directories(${PROJECT_SOURCE_DIR}/common/inc)
include_directories(${CMAKE_SOURCE_DIR}/clib/inc)

add_subdirectory(perl)
add_subdirectory(ssh)
link_directories(${CMAKE_SOURCE_DIR}/build/centreon-clib/)

# Testing.
if(WITH_TESTING)
  set_source_files_properties(${PROJECT_SOURCE_DIR}/perl/src/xs_init.cc
                              PROPERTIES GENERATED TRUE)
  include_directories(/usr/lib64/perl5/CORE)

  include_directories(${PROJECT_SOURCE_DIR}/perl/inc)
  include_directories(${PROJECT_SOURCE_DIR}/ssh/inc)
  include_directories(${Libssh2_INCLUDE_DIR})

  add_definitions(-DBUILD_PATH="${CMAKE_BINARY_DIR}")

  add_executable(
    ut_connector
    # Core sources
    ${PROJECT_SOURCE_DIR}/perl/src/embedded_perl.cc
    ${PROJECT_SOURCE_DIR}/common/src/log.cc
    ${PROJECT_SOURCE_DIR}/common/src/result.cc
    ${PROJECT_SOURCE_DIR}/common/src/reporter.cc
    ${PROJECT_SOURCE_DIR}/common/src/parser.cc
    ${PROJECT_SOURCE_DIR}/perl/src/checks/check.cc
    ${PROJECT_SOURCE_DIR}/perl/src/script.cc
    ${PROJECT_SOURCE_DIR}/perl/src/xs_init.cc
    ${PROJECT_SOURCE_DIR}/ssh/src/checks/check.cc
    ${PROJECT_SOURCE_DIR}/ssh/src/orders/options.cc
    ${PROJECT_SOURCE_DIR}/ssh/src/orders/parser.cc
    ${PROJECT_SOURCE_DIR}/ssh/src/sessions/credentials.cc
    ${PROJECT_SOURCE_DIR}/ssh/src/sessions/session.cc
    # Test sources.
    ${PROJECT_SOURCE_DIR}/perl/test/main.cc
    ${PROJECT_SOURCE_DIR}/perl/test/connector.cc
    ${PROJECT_SOURCE_DIR}/perl/test/embedded_perl.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/buffer_handle.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/checks.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/connector.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/fake_listener.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/orders.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/reporter.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/sessions.cc
    ${PROJECT_SOURCE_DIR}/ssh/test/options.cc)
  add_dependencies(ut_connector centreon_connector_perl)
  add_dependencies(ut_connector centreon_clib)
  target_precompile_headers(ut_connector REUSE_FROM centreon_connector_perl)

  target_link_libraries(
    ut_connector PRIVATE
    GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main
    centreon_clib
    ${PERL_LIBRARIES}
    fmt::fmt
    spdlog::spdlog
    ssh2
    absl::any absl::log absl::base absl::bits
    absl::raw_hash_set absl::hash absl::low_level_hash absl::hashtablez_sampler
    )

  set_target_properties(
    ut_connector
    PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests
               RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/tests
               RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/tests
               RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/tests
               RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR}/tests)
endif(WITH_TESTING)
