cmake_minimum_required(VERSION 3.18)

project(pentobi CXX)
set(PENTOBI_VERSION 21.0)
set(PENTOBI_RELEASE_DATE 2022-05-17)

include(GNUInstallDirs)

if(UNIX AND NOT ANDROID)
    set(build_thumbnailer_default ON)
else()
    set(build_thumbnailer_default OFF)
endif()
option(PENTOBI_BUILD_GTP "Build GTP interface" OFF)
option(PENTOBI_BUILD_GUI "Build GUI" ON)
option(PENTOBI_BUILD_THUMBNAILER "Build Gnome thumbnailer"
    ${build_thumbnailer_default})
option(PENTOBI_BUILD_KDE_THUMBNAILER "Build KDE thumbnailer" OFF)
option(BUILD_TESTING "Build tests" OFF)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "No build type selected, default to Release")
  set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
  add_compile_options(-ffast-math -Wall -Wextra)
endif()

if(BUILD_TESTING)
  if(PENTOBI_BUILD_KDE_THUMBNAILER)
    configure_file(pentobi_kde_thumbnailer/CTestCustom.cmake
        ${CMAKE_BINARY_DIR} COPYONLY)
  endif()
  enable_testing()
endif()

if(UNIX)
  add_custom_target(dist
    COMMAND git archive --prefix=pentobi-${PENTOBI_VERSION}/ HEAD
    | xz -e > ${CMAKE_BINARY_DIR}/pentobi-${PENTOBI_VERSION}.tar.xz
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()

add_subdirectory(libboardgame_base)
add_subdirectory(libpentobi_base)
if(BUILD_TESTING)
    add_subdirectory(libboardgame_test)
endif()
if(PENTOBI_BUILD_GUI OR PENTOBI_BUILD_GTP)
    find_package(Threads)
    add_subdirectory(libboardgame_mcts)
    add_subdirectory(libpentobi_mcts)
endif()
if(PENTOBI_BUILD_GTP)
    add_subdirectory(libboardgame_gtp)
    add_subdirectory(libpentobi_gtp)
    add_subdirectory(pentobi_gtp)
    if(UNIX)
        add_subdirectory(twogtp)
    else()
        message(STATUS "Not building twogtp, needs POSIX")
    endif()
    add_subdirectory(learn_tool)
endif()
if(PENTOBI_BUILD_GUI OR PENTOBI_BUILD_KDE_THUMBNAILER)
    find_package(QT NAMES Qt6 Qt5 REQUIRED)
    if(ANDROID AND QT_VERSION_MAJOR EQUAL 5)
        set(ANDROID_PACKAGE_SOURCE_DIR
            "${CMAKE_SOURCE_DIR}/pentobi/android-qt5")
        # Call find_package(QT) again, otherwise ANDROID_PACKAGE_SOURCE_DIR has
        # no effect with Qt 5.15.
        find_package(QT NAMES Qt5 REQUIRED)
    endif()
    find_package(Qt${QT_VERSION_MAJOR}Gui 5.15 REQUIRED)
endif()
if(PENTOBI_BUILD_GUI)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    find_package(Gettext 0.19.6 REQUIRED)
    find_program(ITSTOOL itstool REQUIRED)
    find_program(RSVG_CONVERT rsvg-convert REQUIRED)
    if((UNIX AND NOT ANDROID) OR PENTOBI_BUILD_THUMBNAILER)
        find_package(DocBookXSL REQUIRED)
        find_program(XSLTPROC xsltproc REQUIRED)
    endif()
    find_package(Qt${QT_VERSION_MAJOR} 5.15 REQUIRED
        COMPONENTS Concurrent LinguistTools QuickControls2)
    if(ANDROID)
        if (QT_VERSION_MAJOR EQUAL 5)
            find_package(Qt5AndroidExtras 5.15 REQUIRED)
        elseif()
            if (${QT_VERSION} VERSION_LESS 6.2)
                message(FATAL_ERROR "Android app needs Qt 5.15 or Qt >=6.2")
            endif()
        endif()
    endif()
    add_subdirectory(libpentobi_paint)
    add_subdirectory(pentobi)
    if(PENTOBI_BUILD_THUMBNAILER)
        add_subdirectory(libpentobi_thumbnail)
        add_subdirectory(pentobi_thumbnailer)
    endif()
endif()
if(PENTOBI_BUILD_KDE_THUMBNAILER)
    add_subdirectory(libpentobi_kde_thumbnailer)
    add_subdirectory(pentobi_kde_thumbnailer)
endif()
