#MIT License
#
#Copyright (c) 2017 Mindaugas Vinkelis
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

cmake_minimum_required(VERSION 3.25)
project(bitsery_tests
        LANGUAGES CXX)

find_package(GTest 1.10 REQUIRED)

if (NOT TARGET Bitsery::bitsery)
    message(FATAL_ERROR "Bitsery::bitsery alias not set. Please generate CMake from bitsery root directory.")
endif()

file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

foreach (TestFile ${TestSourceFiles})
    get_filename_component(TestName ${TestFile} NAME_WE)
    set(TestName bitsery.test.${TestName})
    add_executable(${TestName} ${TestFile})
    target_link_libraries(${TestName} PRIVATE GTest::Main Bitsery::bitsery)
    if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        target_compile_options(${TestName} PRIVATE -Wextra -Wconversion -Wno-missing-braces -Wpedantic -Weffc++ -Werror)
    endif()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        target_compile_options(${TestName} PRIVATE -Wno-c++14-extensions)
    endif()
    gtest_discover_tests(${TestName})

#    add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
endforeach()

#======================= setup development environment ====================

# get all header files for IDE  (in my case Clion) and create dummy project that consumes theses files
get_directory_property(ParentDir PARENT_DIRECTORY)
if (ParentDir)
    # only include when working from root project (Bitsery)
    file(GLOB_RECURSE HeadersForIDE ${ParentDir}/include/bitsery/*.h)
    # create dummy target IDE
    file(WRITE ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp "//generated by CMake to create dummy target with all includes for IDE.")
    add_library(bitsery.dummy_for_ide ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp)
    # add headers so IDE correctly show them
    target_sources(bitsery.dummy_for_ide PRIVATE ${HeadersForIDE} serialization_test_utils.h)
    target_link_libraries(bitsery.dummy_for_ide PRIVATE GTest::Main Bitsery::bitsery)

    # creates a "check_includes" target to verify if all headers has required includes
    # to simplify things a little bit, it only works with modern compiler (C++17)
    # as some bitsery extensions require C++17 compliant compiler.
    if("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
        add_library(check_includes OBJECT)
        target_compile_features(check_includes PRIVATE cxx_std_17)
        file(WRITE ${CMAKE_BINARY_DIR}/check_includes.in "
// generated by CMake to verify header includes.
// we need exactly 201703L, because some compilers with experimental C++17 support
// provides bigger number than 201402L (C++14) but doesn't actually has enough
// functionality to build these files
#if __cplusplus >= 201703L
    #include \"@HeaderFile@\"
#elif defined(_MSC_VER)
#pragma message(\"/Zc:__cplusplus option is required to enable check_includes\")
#else
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message (\"`__cplusplus` macro value should be 201703L or greater, actual value is: \" XSTR(__cplusplus))
#endif
")

        file(GLOB_RECURSE HeaderFiles "${ParentDir}/include/bitsery/*.h")
        foreach (HeaderFile ${HeaderFiles})
            SET(CHK_TARGET_NAME "chk_inc_${HeaderFile}")
            STRING(REPLACE "${ParentDir}/include/bitsery/" "" CHK_TARGET_NAME ${CHK_TARGET_NAME})
            STRING(REGEX REPLACE "/" "_" CHK_TARGET_NAME ${CHK_TARGET_NAME})
            STRING(REGEX REPLACE "\\\\" "_" CHK_TARGET_NAME ${CHK_TARGET_NAME})
            configure_file(${CMAKE_BINARY_DIR}/check_includes.in "${CHK_TARGET_NAME}.cpp")
            target_sources(check_includes PRIVATE "${CHK_TARGET_NAME}.cpp")
        endforeach ()
    else()
        message(WARNING "`check_includes` target will be disabled, as it require compiler with C++17 support.")
    endif()
endif()

