cmake_minimum_required(VERSION 3.21)
project(qtmaildir VERSION 0.27.0 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)

# A dev build counts its own rebuilds so one binary of an unreleased X.Y.Z can
# be told from another (item 167). OFF for a release: the version a release
# prints must be a clean X.Y.Z, which the release procedure checks and the
# SlackBuild builds from a tarball that carries no counter at all.
option(QTMAILDIR_BUILD_NUMBER "Number each build of a dev tree" 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)

# buildnumber.h is generated separately, and by a BUILD step rather than by
# configure_file: this whole block runs once per cmake run, so a counter
# written here would not move when the user rebuilt after a fix, which is the
# defect item 167 describes. The header always exists so version.h can include
# it unconditionally; in a release build it is empty and
# QTMAILDIR_VERSION_DISPLAY collapses to the plain version.
set(QTMAILDIR_BUILD_NUMBER_HEADER
    ${CMAKE_CURRENT_BINARY_DIR}/generated/qtmaildir/buildnumber.h)

if(QTMAILDIR_BUILD_NUMBER)
    # Once now, so the header exists before the first compile of a fresh build
    # directory, and then again on every build.
    execute_process(COMMAND ${CMAKE_COMMAND}
        -DCOUNTER_FILE=${CMAKE_CURRENT_BINARY_DIR}/build-number
        -DHEADER_FILE=${QTMAILDIR_BUILD_NUMBER_HEADER}
        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/BuildNumber.cmake)

    add_custom_target(qtmaildir_buildnumber ALL
        COMMAND ${CMAKE_COMMAND}
            -DCOUNTER_FILE=${CMAKE_CURRENT_BINARY_DIR}/build-number
            -DHEADER_FILE=${QTMAILDIR_BUILD_NUMBER_HEADER}
            -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/BuildNumber.cmake
        BYPRODUCTS ${QTMAILDIR_BUILD_NUMBER_HEADER}
        COMMENT "Numbering this build"
        VERBATIM)
else()
    file(WRITE ${QTMAILDIR_BUILD_NUMBER_HEADER}
        "// Release build: no counter, so QTMAILDIR_VERSION_DISPLAY is the
// plain version. Generated by the top-level CMakeLists.txt.
#pragma once
")
endif()

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