cmake_minimum_required(VERSION 3.21)

# The version is stored once, at the repository root, so the Arch package and
# the running application cannot disagree about it.
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/version" FWS_DISKCLEANER_VERSION LIMIT_COUNT 1)
string(STRIP "${FWS_DISKCLEANER_VERSION}" FWS_DISKCLEANER_VERSION)

project(fws-diskcleaner VERSION ${FWS_DISKCLEANER_VERSION} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.4 REQUIRED COMPONENTS Core Gui Widgets)
qt_standard_project_setup()

# Scanning and geometry stay free of widgets so they can be tested without a
# display and reused by a future command-line mode.
add_library(fws-diskcleaner-core STATIC
    src/core/disk-node.cpp
    src/core/disk-node.h
    src/core/disk-scanner.cpp
    src/core/disk-scanner.h
    src/core/format.h
    src/core/treemap-layout.cpp
    src/core/treemap-layout.h
)
# Includes are written from `src/` (`core/...`, `ui/...`) so the layout of the
# tree is visible in every include directive.
target_include_directories(fws-diskcleaner-core PUBLIC src)
target_link_libraries(fws-diskcleaner-core PUBLIC Qt6::Core)

qt_add_executable(fws-diskcleaner
    src/main.cpp
    src/ui/main-window.cpp
    src/ui/main-window.h
    src/ui/treemap-widget.cpp
    src/ui/treemap-widget.h
)
target_link_libraries(fws-diskcleaner PRIVATE fws-diskcleaner-core Qt6::Widgets)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(fws-diskcleaner-core PRIVATE -Wall -Wextra)
    target_compile_options(fws-diskcleaner PRIVATE -Wall -Wextra)
endif()

include(GNUInstallDirs)
install(TARGETS fws-diskcleaner RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES packaging/fws-diskcleaner.desktop
        DESTINATION "${CMAKE_INSTALL_DATADIR}/applications")
install(FILES packaging/fws-diskcleaner.svg
        DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps")

include(CTest)
if(BUILD_TESTING)
    find_package(Qt6 6.4 REQUIRED COMPONENTS Test)

    add_executable(disk-node-test tests/disk-node.test.cpp tests/test-helpers.h)
    target_link_libraries(disk-node-test PRIVATE fws-diskcleaner-core Qt6::Test)
    add_test(NAME disk-node COMMAND disk-node-test)

    add_executable(disk-scanner-test tests/disk-scanner.test.cpp tests/test-helpers.h)
    target_link_libraries(disk-scanner-test PRIVATE fws-diskcleaner-core Qt6::Test)
    add_test(NAME disk-scanner COMMAND disk-scanner-test)

    add_executable(treemap-layout-test tests/treemap-layout.test.cpp)
    target_link_libraries(treemap-layout-test PRIVATE fws-diskcleaner-core Qt6::Test)
    add_test(NAME treemap-layout COMMAND treemap-layout-test)
endif()
