cmake_minimum_required(VERSION 3.21)
project(qtmaildir VERSION 0.26.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# Off for a packaging build, which has no use for the test binaries and would
# otherwise pull in Qt6::Test and compile the whole suite to produce nothing
# that ships.
option(QTMAILDIR_BUILD_TESTS "Build the test suite" ON)

set(QTMAILDIR_QT_COMPONENTS Widgets Svg WebEngineWidgets)
if(QTMAILDIR_BUILD_TESTS)
    list(APPEND QTMAILDIR_QT_COMPONENTS Test)
endif()
find_package(Qt6 6.5 REQUIRED COMPONENTS ${QTMAILDIR_QT_COMPONENTS})

# Optional, and host tooling rather than a link dependency: lrelease compiles
# the tracked .ts into a .qm. A build without it is English-only rather than
# broken, which is the same outcome as a missing .qm at runtime.
find_package(Qt6 6.5 QUIET COMPONENTS LinguistTools)

# notmuch ships no pkg-config file; locate it by hand.
find_path(NOTMUCH_INCLUDE_DIR notmuch.h)
find_library(NOTMUCH_LIBRARY NAMES notmuch)
if(NOT NOTMUCH_INCLUDE_DIR OR NOT NOTMUCH_LIBRARY)
    message(FATAL_ERROR
        "libnotmuch not found. Need notmuch.h and libnotmuch on the system.")
endif()
message(STATUS "Found notmuch: ${NOTMUCH_LIBRARY}")

find_package(PkgConfig REQUIRED)
pkg_check_modules(GMIME REQUIRED IMPORTED_TARGET gmime-3.0)

# cmark-gfm renders the composer's markdown body into the HTML part.
#
# TWO lookups, not one, and this is the trap: only the CORE library ships a
# pkg-config file. `libcmark-gfm-extensions` has none (verified 2026-08-20 on
# Slackware, cmark-gfm-0.29.0.gfm.13), so it is located by hand exactly as
# notmuch is. The extensions library is not optional here: autolink,
# strikethrough and tasklist all live in it, and without it a bare URL in a
# mail body is not a link.
pkg_check_modules(CMARK_GFM REQUIRED IMPORTED_TARGET libcmark-gfm)
find_library(CMARK_GFM_EXTENSIONS_LIBRARY NAMES cmark-gfm-extensions)
if(NOT CMARK_GFM_EXTENSIONS_LIBRARY)
    message(FATAL_ERROR
        "libcmark-gfm-extensions not found. It ships with cmark-gfm but has "
        "no pkg-config file; it provides autolink, strikethrough and tasklist.")
endif()
message(STATUS "Found cmark-gfm extensions: ${CMARK_GFM_EXTENSIONS_LIBRARY}")

# The version lives only in the project() call above; version.h is generated
# from it so no source file repeats the literal.
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in
    ${CMAKE_CURRENT_BINARY_DIR}/generated/qtmaildir/version.h
    @ONLY)

add_subdirectory(src)
if(QTMAILDIR_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()
