cmake_minimum_required(VERSION 3.16)
project(CppContainers VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_library(containers INTERFACE)
target_include_directories(containers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(containers INTERFACE cxx_std_17)

option(CONTAINERS_ENABLE_SANITIZERS "Enable AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
function(containers_configure target)
    if(MSVC)
        target_compile_options(${target} PRIVATE /W4 /utf-8)
    else()
        target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)
        if(CONTAINERS_ENABLE_SANITIZERS AND CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
            target_compile_options(${target} PRIVATE -fsanitize=address,undefined -fno-omit-frame-pointer)
            target_link_options(${target} PRIVATE -fsanitize=address,undefined)
        endif()
    endif()
endfunction()

add_executable(containers_demo src/main.cpp)
target_link_libraries(containers_demo PRIVATE containers)
containers_configure(containers_demo)

include(CTest)
if(BUILD_TESTING)
    # 优先使用本机 GoogleTest，否则解压随项目提供的官方源码包。
    find_package(GTest 1.15 CONFIG QUIET)
    if(NOT GTest_FOUND)
        include(FetchContent)
        if(POLICY CMP0135)
            cmake_policy(SET CMP0135 NEW)
        endif()
        set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
        set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
        set(GTEST_ARCHIVE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest-1.15.2.tar.gz")
        if(NOT EXISTS "${GTEST_ARCHIVE}")
            set(GTEST_ARCHIVE "https://codeload.github.com/google/googletest/tar.gz/refs/tags/v1.15.2")
        endif()
        FetchContent_Declare(googletest
            URL "${GTEST_ARCHIVE}"
            URL_HASH SHA256=7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926
        )
        FetchContent_MakeAvailable(googletest)
    endif()
    add_executable(containers_tests tests/linear_tests.cpp tests/tree_tests.cpp)
    target_link_libraries(containers_tests PRIVATE containers GTest::gtest_main)
    containers_configure(containers_tests)
    include(GoogleTest)
    gtest_discover_tests(containers_tests)
endif()
