# taken from the nds client library
# Notes from autotools version mythbuster
# https://autotools.io/libtool/version.html
# Warning
# A common mistake is to assume that the three values passed to -version-info
# map directly into the three numbers at the end of the library name. This is
# not the case, and indeed, current, revision and age are applied differently
# depending on the operating system that one is using.
#
# For Linux, for instance, while the last two values map directly from the
# command-line, the first is calculated by subtracting age from current. On
# the other hand, on modern FreeBSD, only one component is used in the library
# version, which corresponds to current.
#
# The rules of thumb, when dealing with these values are:
#
#    Always increase the revision value.
#    Increase the current value whenever an interface has been added, removed or changed.
#    Increase the age value only if the changes made to the ABI are backward compatible.
#========================================================================
# CALCULATE_LIBTOOL_VERSION
#
# library_name = Variable to set with the swig property value
# current      = Property to be queried
# revision     = SWIG target to query
# age          =
#------------------------------------------------------------------------
function( NDS_LIBTOOL_VERSION_CALCULATION library_name current revision age )
    math( EXPR ${library_name}_VERSION_MAJOR "${current} - ${age}" )
    set( ${library_name}_VERSION_MAJOR ${${library_name}_VERSION_MAJOR}
            PARENT_SCOPE )
    set( ${library_name}_VERSION_MINOR ${age} PARENT_SCOPE )
    set( ${library_name}_VERSION_MICRO ${revision} PARENT_SCOPE )
    set( ${library_name}_VERSION
            ${${library_name}_VERSION_MAJOR}.${age}.${revision}
            PARENT_SCOPE )
    set( ${library_name}_SOVERSION
            ${${library_name}_VERSION_MAJOR}
            PARENT_SCOPE )
endfunction( NDS_LIBTOOL_VERSION_CALCULATION )

configure_file( include/cds-pubsub/version.hh.in
        "${CMAKE_CURRENT_BINARY_DIR}/include/cds-pubsub/version.hh"
        @ONLY )

set( PUB_SUB_SOVERSION_CURRENT 1 )
set( PUB_SUB_SOVERSION_REVISION 2 )
set( PUB_SUB_SOVERSION_AGE 1 )
nds_libtool_version_calculation( PUB_SUB ${PUB_SUB_SOVERSION_CURRENT}
        ${PUB_SUB_SOVERSION_REVISION}
        ${PUB_SUB_SOVERSION_AGE})
message("Version = ${PUB_SUB_VERSION}")
message("SOVersion = ${PUB_SUB_SOVERSION}")

set (PUB_SUB_SRC
        common.cc
        pub.cc
        sub.cc
        pipeline_parser.cc
        )

add_library(pub_sub SHARED
        ${PUB_SUB_SRC}
)
target_include_directories(pub_sub PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/private")
target_include_directories(pub_sub PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
        $<INSTALL_INTERFACE:include>
        )
target_link_libraries(pub_sub PUBLIC
        Boost::boost
        Boost::system
        Threads::Threads
        zstd::zstd
        )

set(PUBLIC_HEADERS include/cds-pubsub/common.hh
        include/cds-pubsub/pub.hh
        include/cds-pubsub/pub_plugin.hh
        include/cds-pubsub/sub.hh
        include/cds-pubsub/sub_plugin.hh
        "${CMAKE_CURRENT_BINARY_DIR}/include/cds-pubsub/version.hh"
)
set_target_properties(pub_sub PROPERTIES
        PUBLIC_HEADER "${PUBLIC_HEADERS}"
        OUTPUT_NAME cds-pubsub
        VERSION ${PUB_SUB_VERSION}
        SOVERSION ${PUB_SUB_SOVERSION}
        )

add_library(pub_sub_asan
        ${PUB_SUB_SRC}
        )
target_compile_options(pub_sub_asan PUBLIC "-fsanitize=address")
target_include_directories(pub_sub_asan PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/private")
target_include_directories(pub_sub_asan PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
        $<INSTALL_INTERFACE:include>
        )
target_link_libraries(pub_sub_asan PRIVATE
        asan
        Boost::boost
        Boost::system
        Threads::Threads
        zstd::zstd
        )
set_target_properties(pub_sub_asan PROPERTIES
        OUTPUT_NAME cds-pubsub-asan
        VERSION ${PUB_SUB_VERSION}
        SOVERSION ${PUB_SUB_SOVERSION}
        )

install(TARGETS pub_sub pub_sub_asan
        EXPORT libcds-pubsubConfig
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        PUBLIC_HEADER DESTINATION include/${PROJECT_SHORT_NAME}
        )

write_basic_package_version_file(
        libcds-pubsubConfigVersion.cmake
        VERSION ${PACKAGE_VERSION}
        COMPATIBILITY AnyNewerVersion
)
install(FILES ${CMAKE_SOURCE_DIR}/cmake/FindZStd.cmake
        DESTINATION share/${CMAKE_PROJECT_NAME}/cmake
)
install(FILES libcds-pubsubConfig.cmake
        DESTINATION share/${CMAKE_PROJECT_NAME}/cmake
)
install(EXPORT libcds-pubsubConfig DESTINATION share/${CMAKE_PROJECT_NAME}/cmake
        NAMESPACE cds::
        FILE libcds-pubsub_generated.cmake
        EXPORT_LINK_INTERFACE_LIBRARIES)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcds-pubsubConfigVersion.cmake
        DESTINATION share/${CMAKE_PROJECT_NAME}/cmake)

configure_file(tests/test_version.cc.in "${CMAKE_CURRENT_BINARY_DIR}/tests/test_version.cc" @ONLY)

set(TEST_SRC
    tests/test_common.cc
    tests/test_full_subscription_internal.cc
    tests/test_main.cc
    tests/test_pub_iface.cc
    tests/test_sub_iface.cc
    tests/test_zlib_common.cc
    tests/test_pipeline_parser.cc
    ${CMAKE_CURRENT_BINARY_DIR}/tests/test_version.cc
    )

add_executable(test_sender tests/test_sender.cc)
target_link_libraries(test_sender PUBLIC pub_sub)

add_executable(test_slow_receiver tests/test_slow_receiver.cc)
target_link_libraries(test_slow_receiver PUBLIC pub_sub)

add_executable(test_pub_sub
        ${TEST_SRC})
target_include_directories(test_pub_sub PUBLIC
        private
        tests)
target_link_libraries(test_pub_sub PUBLIC pub_sub catch2)

add_executable(test_pub_sub_asan
        ${TEST_SRC})
target_include_directories(test_pub_sub_asan PUBLIC
        private
        tests)
target_link_libraries(test_pub_sub_asan PUBLIC pub_sub_asan catch2)

add_test(NAME test_pub_sub COMMAND test_pub_sub
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

add_test(NAME test_pub_sub_asan COMMAND test_pub_sub
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")