# Copyright (C) 2020  CZ.NIC, z. s. p. o.
#
# This file is part of FRED.
#
# FRED is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FRED is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FRED.  If not, see <https://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.5)

# macros

macro(set_default __VARIABLE_NAME __VARIABLE_VALUE)
    if(NOT ${__VARIABLE_NAME})
        set(${__VARIABLE_NAME} ${__VARIABLE_VALUE} ${ARGN})
    endif()
endmacro()

# project details

include("cmake/package.cmake")

package(libdiagnostics
    FULL_VERSION "1.2.0")
set(PACKAGE_BUGREPORT    "https://gitlab.office.nic.cz/fred/libdiagnostics/")

project(LibDiagnostics
    VERSION ${PACKAGE_VERSION}
    LANGUAGES CXX)

# build options

option(LIBDIAGNOSTICS_BUILD_TESTS "Build libdiagnostics tests" OFF)

# compile options

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Specifies the build type on single-configuration generators.")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
option(THREADS_PREFER_PTHREAD_FLAG "The use of the -pthread compiler and linker flag is preferred" TRUE)
set(SPDLOG_ACTIVE_LEVEL "SPDLOG_LEVEL_TRACE" CACHE STRING "This will turn on/off logging statements at compile time.")
set(FMT_STRING_ALIAS "1" CACHE STRING "Constructs a compile-time format string.")
set(FMT_HEADER_ONLY "1" CACHE STRING "Use fmt library in the header-only manner.")
option(CMAKE_EXPORT_COMPILE_COMMANDS "If enabled, generates a compile_commands.json file containing the exact compiler calls." ON)

# package dependencies

include("cmake/package_dependencies.cmake")

add_package_dependencies(FROM_FILE "dependencies.txt")

# dependencies

find_package(Threads REQUIRED)

# configuration files

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
configure_file(diagnostics.pc.in diagnostics.pc @ONLY)

#build

add_library(fred-diagnostics-services-iface STATIC
    ${FRED_DIAGNOSTICS_PROTO_SRCS}
    ${FRED_DIAGNOSTICS_GRPC_SRCS})

add_library(FredDiagnosticsServicesIface::library ALIAS fred-diagnostics-services-iface)

foreach(target_name fred-diagnostics-services-iface)
    message(STATUS "setting grpc properties on: ${target_name} at ${CMAKE_CURRENT_BINARY_DIR}")
    set_target_properties(${target_name} PROPERTIES
        CXX_STANDARD 14
        CXX_STANDARD_REQUIRED YES
        CXX_EXTENSIONS NO
        ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
    target_compile_options(${target_name}
        PRIVATE
            $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra -Wno-unused-parameter -ggdb -grecord-gcc-switches>)
    target_include_directories(${target_name}
        PUBLIC
            ${CMAKE_CURRENT_BINARY_DIR})
    target_link_libraries(${target_name}
        PUBLIC
            protobuf::libprotobuf
            gRPC::grpc++)
endforeach()

add_library(diagnostics STATIC
    src/diagnostics.cc
    src/proto_wrap.cc)

add_library(LibDiagnostics::library ALIAS diagnostics)

target_link_libraries(diagnostics PUBLIC
    Threads::Threads
    FredDiagnosticsServicesIface::library)

foreach(target_name diagnostics)
    message(STATUS "setting common properties on:  ${target_name} at ${CMAKE_CURRENT_BINARY_DIR}")
    set_target_properties(${target_name} PROPERTIES
        CXX_STANDARD 14
        CXX_STANDARD_REQUIRED YES
        CXX_EXTENSIONS NO
        ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
    target_include_directories(${target_name}
        PUBLIC
            ${CMAKE_CURRENT_SOURCE_DIR}/include
        PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR})
    target_include_directories(${target_name} BEFORE
        PRIVATE
            ${CMAKE_CURRENT_BINARY_DIR})
    target_compile_options(${target_name}
        PRIVATE
            $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra -ggdb -grecord-gcc-switches>)
    target_compile_definitions(${target_name}
        PRIVATE
            SPDLOG_ACTIVE_LEVEL=${SPDLOG_ACTIVE_LEVEL}
            FMT_STRING_ALIAS=${FMT_STRING_ALIAS}
            FMT_HEADER_ONLY=${FMT_HEADER_ONLY})
endforeach()

#export(TARGETS fred-diagnostics-services-iface FILE CMakeFiles-api.cmake)

# tests

if(LIBDIAGNOSTICS_BUILD_TESTS)
    add_subdirectory(test)
endif()

# install

get_directory_property(HAS_PARENT "PARENT_DIRECTORY")
if(NOT HAS_PARENT)
    include(GNUInstallDirs)

    install(TARGETS diagnostics
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/diagnostics.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

    add_custom_target(uninstall
        COMMAND xargs -L10 rm -v < ${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt
        COMMAND cat ${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt | xargs -L1 dirname | sort | uniq | xargs -L10 rmdir -p --ignore-fail-on-non-empty)
endif()

# dist

include("cmake/package_dist.cmake")

package_dist(PACKAGE_NAME "${PACKAGE_NAME}"
             PACKAGE_PREFIX "${LIBDIAGNOSTICS_PREFIX}"
             PACKAGE_TARNAME "${PACKAGE_TARNAME}")
