cmake_minimum_required(VERSION 3.25.1)

include(cmake/SetupVcpkg.cmake)
setup_vcpkg()

include(cmake/SetupHomebrew.cmake)
setup_homebrew()

# CMake options.
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(CMAKE_DISABLE_SOURCE_CHANGES  ON)
set(CMAKE_ERROR_DEPRECATED TRUE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

# VERSION is the next development version, bumped automatically after each release.
# ZEAL_RELEASE_VERSION is the last released version, updated by `just release-prepare`.
# When both match, the build is treated as a release; otherwise it's a dev build.
# The `# x-version` marker on each version line acts as a sed anchor for the bump.
project(Zeal
    VERSION 0.9.1 # x-version
    DESCRIPTION "A simple documentation browser."
    HOMEPAGE_URL "https://zealdocs.org"
    LANGUAGES CXX
)

set(ZEAL_RELEASE_VERSION "0.9.1") # x-version

include(ZealHelpers)

option(ZEAL_USE_CLANG_TIDY "Enable clang-tidy static analysis" OFF)
if(ZEAL_USE_CLANG_TIDY)
    find_program(CLANG_TIDY_PROGRAM clang-tidy)
    if(CLANG_TIDY_PROGRAM)
        message(STATUS "Using clang-tidy: ${CLANG_TIDY_PROGRAM}")
        set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_PROGRAM}")
    else()
        message(FATAL_ERROR "ZEAL_USE_CLANG_TIDY is enabled but clang-tidy was not found.")
    endif()
endif()

option(ZEAL_USE_COMPILER_CACHE "Enable compiler cache (sccache/ccache)" OFF)
if(ZEAL_USE_COMPILER_CACHE)
    find_program(CCACHE_PROGRAM sccache ccache)
    if(CCACHE_PROGRAM)
        message(STATUS "Using compiler cache: ${CCACHE_PROGRAM}")
        set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")

        # sccache/ccache don't support MSVC's /Zi (shared PDB). Use /Z7 (embedded debug info) instead.
        # CMP0141 is NEW (cmake_minimum_required >= 3.25), so use the CMake abstraction.
        # See: https://github.com/mozilla/sccache?tab=readme-ov-file#usage
        if(MSVC)
            set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT
                "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>")
        endif()
    else()
        message(WARNING "ZEAL_USE_COMPILER_CACHE is enabled but no sccache or ccache found.")
    endif()
endif()

# Project information.
set(PROJECT_COMPANY_NAME "Oleg Shparber")
set(PROJECT_COPYRIGHT "© 2013-2026 Oleg Shparber and other contributors")

# Determine version suffix for non-release builds.
if(PROJECT_VERSION STREQUAL ZEAL_RELEASE_VERSION)
    message(NOTICE "Release build: ${PROJECT_VERSION}")
else()
    include(GitVersionInfo)
    if(Zeal_GIT_AT_TAG)
        message(FATAL_ERROR "Tagged commit but PROJECT_VERSION (${PROJECT_VERSION}) != ZEAL_RELEASE_VERSION (${ZEAL_RELEASE_VERSION}).")
    elseif(Zeal_GIT_VERSION_AHEAD)
        set(ZEAL_VERSION_SUFFIX "-dev.${Zeal_GIT_VERSION_AHEAD}")
    else()
        set(ZEAL_VERSION_SUFFIX "-dev")
    endif()
    message(NOTICE "Dev build: ${PROJECT_VERSION}${ZEAL_VERSION_SUFFIX}")
endif()

set(ZEAL_VERSION_FULL "${Zeal_VERSION}${ZEAL_VERSION_SUFFIX}")
message(NOTICE "Calculated Zeal version: ${ZEAL_VERSION_FULL}")

file(WRITE "${CMAKE_BINARY_DIR}/zeal_version" ${ZEAL_VERSION_FULL})

# A custom target to print the full version.
# Usage: cmake --build --preset release --target zeal_version
add_custom_target(zeal_version
    COMMAND ${CMAKE_COMMAND} -E echo "Zeal version: ${ZEAL_VERSION_FULL}"
    VERBATIM
)

# Enable all compiler warnings and treat them as errors.
if(MSVC)
    add_compile_options(/W4)
else()
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

set(CMAKE_COMPILE_WARNING_AS_ERROR ON)

option(ZEAL_FEATURE_UPDATE_CHECK "Automatic update check on startup" ON)
if(ZEAL_FEATURE_UPDATE_CHECK)
    add_compile_definitions(ZEAL_FEATURE_UPDATE_CHECK)
endif()

option(BUILD_TESTING "Build the testing suite" OFF)
if(BUILD_TESTING)
    enable_testing()
endif()

add_subdirectory(assets)
add_subdirectory(src)
