cmake_minimum_required(VERSION 3.23)

project(bitserial
    VERSION 1.0.0
    DESCRIPTION "Big-endian bit-field serialization with optimized fixed-width byte-array bulk paths"
    LANGUAGES CXX
)

include(CTest)
include(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the build type." FORCE)
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release RelWithDebInfo MinSizeRel)
endif()

if(MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.29)
    message(FATAL_ERROR "bitserial requires C++20 support: MSVC 19.29 or newer is required.")
endif()

option(BITSERIAL_BUILD_TESTS "Build bitserial tests" ${PROJECT_IS_TOP_LEVEL})
option(BITSERIAL_BUILD_BENCHMARKS "Build bitserial benchmarks" ${PROJECT_IS_TOP_LEVEL})

set(BITSERIAL_IS_X86 FALSE)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64|i[3-6]86)$")
    set(BITSERIAL_IS_X86 TRUE)
endif()

function(bitserial_target_warnings target)
    target_compile_options(${target} PRIVATE
        $<$<CXX_COMPILER_ID:MSVC>:/W4>
        $<$<CXX_COMPILER_ID:MSVC>:/Zc:preprocessor>
        $<$<CXX_COMPILER_ID:MSVC>:/utf-8>
        $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic>
    )
endfunction()

function(bitserial_target_avx2 target)
    if(BITSERIAL_IS_X86)
        target_compile_options(${target} PRIVATE
            $<$<CXX_COMPILER_ID:MSVC>:/arch:AVX2>
            $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-O3 -mavx2 -mbmi2>
        )
    endif()
endfunction()

function(bitserial_target_cxx20 target scope)
    if(cxx_std_20 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
        target_compile_features(${target} ${scope} cxx_std_20)
    else()
        target_compile_options(${target} ${scope}
            $<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/std:c++20>
            $<$<COMPILE_LANG_AND_ID:CXX,GNU,Clang,AppleClang>:-std=c++20>
        )
    endif()
endfunction()

add_library(bitserial INTERFACE)
add_library(bitserial::bitserial ALIAS bitserial)
bitserial_target_cxx20(bitserial INTERFACE)
target_include_directories(bitserial INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if(BITSERIAL_BUILD_TESTS OR BITSERIAL_BUILD_BENCHMARKS)
    set(CATCH_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/catch2")
    if(NOT EXISTS "${CATCH_DIR}/catch_amalgamated.hpp" OR
       NOT EXISTS "${CATCH_DIR}/catch_amalgamated.cpp")
        message(FATAL_ERROR "Catch2 amalgamated sources are missing from ${CATCH_DIR}")
    endif()

    add_library(bitserial_catch2 STATIC "${CATCH_DIR}/catch_amalgamated.cpp")
    target_include_directories(bitserial_catch2 PUBLIC "${CATCH_DIR}")
    bitserial_target_cxx20(bitserial_catch2 PUBLIC)
endif()

if(BITSERIAL_BUILD_TESTS)
    add_executable(bitserial_tests tests/bitserial_tests.cpp)
    target_link_libraries(bitserial_tests PRIVATE bitserial::bitserial bitserial_catch2)
    bitserial_target_warnings(bitserial_tests)
    add_test(NAME bitserial_tests COMMAND bitserial_tests)
endif()

if(BITSERIAL_BUILD_BENCHMARKS)
    add_executable(bitserial_benchmarks benchmarks/baq_bulk_benchmarks.cpp)
    target_link_libraries(bitserial_benchmarks PRIVATE bitserial::bitserial bitserial_catch2)
    bitserial_target_warnings(bitserial_benchmarks)
    bitserial_target_avx2(bitserial_benchmarks)
    target_link_options(bitserial_benchmarks PRIVATE
        $<$<CXX_COMPILER_ID:MSVC>:/STACK:33554432>
    )

    add_custom_target(run_bitserial_benchmarks
        COMMAND $<TARGET_FILE:bitserial_benchmarks> "[!benchmark]"
        DEPENDS bitserial_benchmarks
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Running Catch2 bitserial benchmarks"
        VERBATIM
    )
endif()

install(
    DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/bitserial.hpp
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
    TARGETS bitserial
    EXPORT bitserialTargets
)
install(
    EXPORT bitserialTargets
    NAMESPACE bitserial::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bitserial
)
